Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/413.html
413. Arithmetic Slices (Medium)
A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.
For example, these are arithmetic sequence:
1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9
The following sequence is not arithmetic.
1, 1, 2, 5, 7
A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 <= P < Q < N.
A slice (P, Q) of array A is called arithmetic if the sequence:
A[P], A[p + 1], ..., A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q.
The function should return the number of arithmetic slices in the array A.
Example:
A = [1, 2, 3, 4] return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.
Related Topics:
Math, Dynamic Programming
Similar Questions:
Solution 1.
-
class Solution { public int numberOfArithmeticSlices(int[] A) { if (A == null || A.length < 3) return 0; int length = A.length; int prevDifference = A[1] - A[0]; List<int[]> slicesList = new ArrayList<int[]>(); int begin = 0, end = 1; for (int i = 2; i < length; i++) { int curDifference = A[i] - A[i - 1]; if (curDifference == prevDifference) end = i; else { if (end - begin >= 2) slicesList.add(new int[]{begin, end}); begin = i - 1; end = i; } prevDifference = curDifference; } if (end - begin >= 2) slicesList.add(new int[]{begin, end}); int count = 0; for (int[] slice : slicesList) { int intervalLength = slice[1] - slice[0]; int curCount = intervalLength * (intervalLength - 1) / 2; count += curCount; } return count; } } ############ class Solution { public int numberOfArithmeticSlices(int[] nums) { int ans = 0, cnt = 0; int d = 3000; for (int i = 0; i < nums.length - 1; ++i) { if (nums[i + 1] - nums[i] == d) { ++cnt; } else { d = nums[i + 1] - nums[i]; cnt = 0; } ans += cnt; } return ans; } }
-
// OJ: https://leetcode.com/problems/arithmetic-slices/ // Time: O(N) // Space: O(1) class Solution { public: int numberOfArithmeticSlices(vector<int>& A) { int N = A.size(), ans = 0, i = 0; while (i + 2 < N) { int j = i + 1, d = A[j] - A[i]; while (j + 1 < N && A[j + 1] - A[j] == d) ++j; int L = j - i + 1; ans += (L - 1) * (L - 2) / 2; i = j; } return ans; } };
-
class Solution: def numberOfArithmeticSlices(self, nums: List[int]) -> int: ans = cnt = 0 d = 3000 for a, b in pairwise(nums): if b - a == d: cnt += 1 else: d = b - a cnt = 0 ans += cnt return ans ############ class Solution(object): def numberOfArithmeticSlices(self, nums): """ :type nums: List[int] :rtype: int """ ans = 0 if len(nums) > 2: diff = [nums[i] - nums[i - 1] for i in range(1, len(nums))] count = 1 pre = diff[0] for i in range(1, len(diff)): if diff[i] == pre: count += 1 else: ans += count * (count - 1) / 2 count = 1 pre = diff[i] ans += count * (count - 1) / 2 return ans
-
func numberOfArithmeticSlices(nums []int) (ans int) { cnt, d := 0, 3000 for i, b := range nums[1:] { a := nums[i] if b-a == d { cnt++ } else { d = b - a cnt = 0 } ans += cnt } return }
-
function numberOfArithmeticSlices(nums: number[]): number { let ans = 0; let cnt = 0; let d = 3000; for (let i = 0; i < nums.length - 1; ++i) { const a = nums[i]; const b = nums[i + 1]; if (b - a == d) { ++cnt; } else { d = b - a; cnt = 0; } ans += cnt; } return ans; }