413. Array slicing arithmetic Slices

Source: Internet
Author: User

A sequence of number is called arithmetic if it consists of in least three elements and if the difference between any Consecutive elements is the same.

For example, these is arithmetic sequence:

1, 3, 5, 7, 97, 7, 7, 73,-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 was 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.


This question let us calculate a kind of arithmetic slice, plainly is to find arithmetic progression, limit the length of the arithmetic progression is at least 3, then [1,2,3,4] contains 3 length of at least 3 of the arithmetic slices, we see [1,2,3,4,5] how many: len = 3: [Three-way], [2,3,4] , [3,4,5]len = 4: [1,2,3,4], [2,3,4,5]len = 5: [1,2,3,4,5] then we can find a recursive type of n arithmetic progression with a length of at least 3 of the number of arithmetic slices (n-1) (n-2)/ 2, then the title becomes the original array to find the length of the arithmetic progression, and then into the formula to calculate the number can be
  
 
  1. class Solution {
  2. public:
  3. int numberOfArithmeticSlices(vector<int>& A) {
  4. int = 0 len = 2 n = A size
  5. for (int i = 2; i < n; ++i) {
  6. if (A[I] -A[I- 1] ==A[I- 1] -A[I- 2]) {
  7. ++len;
  8. } else {
  9. if ( len > 2 ) res += ( len - 1 ) * ( len - 2 ) * 0.5 ;
  10. len = 2;
  11. }
  12. }
  13. if ( len > 2 ) res += ( len - 1 ) * ( len - 2 ) * 0.5 ;
  14. return res;
  15. }
  16. };


  
 
  1. public int NumberOfArithmeticSlices(int[] A)
  2. {
  3. int n = A.Length;
  4. if (n < 3) return 0;
  5. int[] dp = new int[n];
  6. int result = 0;
  7. for ( int i = 2 I < n ++ i ) {
  8. if ( a [ i " - A Span class= "pun" >[ i - 1 ] == A [ i - 1 ] - A [ i - 2
  9. {
  10. dp[i] = dp[i - 1] + 1;
  11. }
  12. result += dp[i];
  13. }
  14. return result;
  15. }



From for notes (Wiz)

413. Array slicing arithmetic Slices

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.