-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
413 Arithmetic Slices.swift
40 lines (34 loc) 路 964 Bytes
/
413 Arithmetic Slices.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//
// 413 Arithmetic Slices.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 18/02/2021.
// Copyright 漏 2021 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/arithmetic-slices/
class Solution {
/// Finds the number of arithmetic slices in array `A`.
///
/// - Parameter A: The array.
/// - Returns: The number of arithmetic slices.
///
/// - Complexity:
/// - time: O(n), where n is the length of `A`.
/// - space: O(1), only constant space is used.
func numberOfArithmeticSlices(_ A: [Int]) -> Int {
let n = A.count
guard n > 2 else { return 0 }
var dp = 0
var ans = 0
for i in 2..<n {
if A[i] - A[i - 1] == A[i - 1] - A[i - 2] {
dp += 1
ans += dp
} else {
dp = 0
}
}
return ans
}
}