LeetCode: Arithmetic Slices Solution
1/**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}1920var 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}
Comments
Loading comments...
Tags
leetcode
math
dynamic programming
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
Previous Post
LeetCode: Container With Most Water
Feb 17, 2021