LeetCode: Arithmetic Slices Solution

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}/**2 * @param {number[]} A3 * @return {number}4 */5var numberOfArithmeticSlices = function (A) {6  const N = A.length7  let res = 08  let acc = 09  for (let i = 2; i < N; i++) {10    if (A[i] - A[i - 1] === A[i - 1] - A[i - 2]) {11      acc++12      res += acc13    } else {14      acc = 015    }16  }17  return res18}19
20var numberOfArithmeticSlices = function (A) {21  const N = A.length22  let res = 023  let acc = 024  for (let i = 2; i < N; i++) {25    if (A[i] - A[i - 1] === A[i - 1] - A[i - 2]) {26      acc++27    } else {28      res += (acc * (acc + 1)) / 229      acc = 030    }31  }32  res += (acc * (acc + 1)) / 233  return res34}```

Tags

leetcode

math

dynamic programming

Next Post

LeetCode: Minimum Remove To Make Valid Parentheses

Feb 19, 2021

Search Posts