Mixture

身未动,心已远

导航

codility:Maximum slice problem (MaxDoubleSliceSum, MaxProfit, MaxSliceSum)

唯一一个全部一次100%的lesson。(实在因为太简单。。。)

MaxSliceSum:

A non-empty zero-indexed array A consisting of N integers is given. A pair of integers (P, Q), such that 0 ≤ P ≤ Q < N, is called a slice of array A. The sum of a slice (P, Q) is the total of A[P] + A[P+1] + ... + A[Q].

Write a function:

int solution(const vector<int> &A);

that, given an array A consisting of N integers, returns the maximum sum of any slice of A.

For example, given array A such that:

 

A[0] = 3  A[1] = 2  A[2] = -6
A[3] = 4  A[4] = 0

the function should return 5 because:

  • (3, 4) is a slice of A that has sum 4,
  • (2, 2) is a slice of A that has sum −6,
  • (0, 1) is a slice of A that has sum 5,
  • no other slice of A has sum greater than (0, 1).

Assume that:

  • N is an integer within the range [1..1,000,000];
  • each element of array A is an integer within the range [−1,000,000..1,000,000];
  • the result will be an integer within the range [−2,147,483,648..2,147,483,647].

Complexity:

  • expected worst-case time complexity is O(N);
  • expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).

就是最大连续子串和么,只要遍历的时候记录以当前位置结尾的子串的最大和就好。

// you can also use includes, for example:
#include <algorithm>
int solution(const vector<int> &A) {
    // write your code in C++98
    int size = A.size();
    int res = A[0];
    int last = res;
    for(int i=1;i<size;i++) {
        last = max(A[i],A[i]+last);
        if(last>res)
            res = last;
    }
    return res;
}

MaxProfit:

A zero-indexed array A consisting of N integers is given. It contains daily prices of a stock share for a period of N consecutive days. If a single share was bought on day P and sold on day Q, where 0 ≤ P ≤ Q < N, then the profit of such transaction is equal to A[Q] − A[P], provided that A[Q] ≥ A[P]. Otherwise, the transaction brings loss of A[P] − A[Q].

For example, consider the following array A consisting of six elements such that:

 

  A[0] = 23171  
  A[1] = 21011  
  A[2] = 21123
  A[3] = 21366  
  A[4] = 21013  
  A[5] = 21367

If a share was bought on day 0 and sold on day 2, a loss of 2048 would occur because A[2] − A[0] = 21123 − 23171 = −2048. If a share was bought on day 4 and sold on day 5, a profit of 354 would occur because A[5] − A[4] = 21367 − 21013 = 354. Maximum possible profit was 356. It would occur if a share was bought on day 1 and sold on day 5.

Write a function,

int solution(const vector<int> &A);

that, given a zero-indexed array A consisting of N integers containing daily prices of a stock share for a period of N consecutive days, returns the maximum possible profit from one transaction during this period. The function should return 0 if it was impossible to gain any profit.

For example, given array A consisting of six elements such that:

 

  A[0] = 23171  
  A[1] = 21011  
  A[2] = 21123
  A[3] = 21366  
  A[4] = 21013  
  A[5] = 21367

the function should return 356, as explained above.

Assume that:

  • N is an integer within the range [0..400,000];
  • each element of array A is an integer within the range [0..200,000].

Complexity:

  • expected worst-case time complexity is O(N);
  • expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).

给了一串交易记录,问最大获利。思想和上面的最大连续子串和类似。只是这次在遍历的时候要记录的是当前时间卖出的最大利润。所以还需要多维护一个到当前时间为止的最低股价。

// you can also use includes, for example:
// #include <algorithm>
int solution(const vector<int> &A) {
    // write your code in C++98
    int size = A.size();
    if(size<=1) {
        return 0;
    }
    int lowestPrice = A[0];
    int maxProfit = 0;
    for(int i=1;i<size;i++) {
        if(A[i]<=lowestPrice) {
            lowestPrice = A[i];
        }else {
            if(A[i]-lowestPrice>maxProfit) {
                maxProfit = A[i]-lowestPrice;
            }
        }
    }
    return maxProfit;
}

MaxDoubleSliceSum:

A non-empty zero-indexed array A consisting of N integers is given.

A triplet (X, Y, Z), such that 0 ≤ X < Y < Z < N, is called a double slice.

The sum of double slice (X, Y, Z) is the total of A[X + 1] + A[X + 2] + ... + A[Y − 1] + A[Y + 1] + A[Y + 2] + ... + A[Z − 1].

For example, array A such that:

 

    A[0] = 3
    A[1] = 2
    A[2] = 6
    A[3] = -1
    A[4] = 4
    A[5] = 5
    A[6] = -1
    A[7] = 2

contains the following example double slices:

  • double slice (0, 3, 6), sum is 2 + 6 + 4 + 5 = 17,
  • double slice (0, 3, 7), sum is 2 + 6 + 4 + 5 − 1 = 16,
  • double slice (3, 4, 5), sum is 0.

The goal is to find the maximal sum of any double slice.

Write a function:

int solution(vector<int> &A);

that, given a non-empty zero-indexed array A consisting of N integers, returns the maximal sum of any double slice.

For example, given:

 

    A[0] = 3
    A[1] = 2
    A[2] = 6
    A[3] = -1
    A[4] = 4
    A[5] = 5
    A[6] = -1
    A[7] = 2

the function should return 17, because no double slice of array A has a sum of greater than 17.

Assume that:

  • N is an integer within the range [3..100,000];
  • each element of array A is an integer within the range [−10,000..10,000].

Complexity:

  • expected worst-case time complexity is O(N);
  • expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).

这里是让double slice的sum最大。The sum of double slice (X, Y, Z) is the total of A[X + 1] + A[X + 2] + ... + A[Y − 1] + A[Y + 1] + A[Y + 2] + ... + A[Z − 1]。其实还是最大子串和的变种,只是需要从左往右遍历一次,记住从左到右这个方向上到每个位置的最大子串和。然后从右往左遍历一次,记录从右往左这个方向上到每个位置的最大子串和。然后相加再做下处理找出最大值就好。需要注意的是头和尾是不能参与计算的。因为他们无论如何不可能为double slice的sum做贡献。

// you can also use includes, for example:
// #include <algorithm>
#include <vector>
#include <algorithm>
int solution(vector<int> &A) {
    // write your code in C++98
    if(A.size()==3) {
        return 0;
    }
    int len = A.size();
    A[0]=0;
    A[len-1]=0;
    vector<int> leftVec(A);
    vector<int> rightVec(A);
    
    for(int i=1;i<len-1;i++) {
        leftVec[i] = max(leftVec[i],leftVec[i]+leftVec[i-1]);
        rightVec[len-1-i] = max(rightVec[len-1-i],rightVec[len-1-i]+rightVec[len-i]);
    }
    int res = A[1];
    for(int i=1;i<len-1;i++) {
        int tmp = leftVec[i]+rightVec[i]-A[i]*2;
        if(tmp>res) {
            res = tmp;
        }
    }
    return res;
}

 

posted on 2014-04-04 12:34  parapax  阅读(3277)  评论(0编辑  收藏  举报