Codility-MaxSliceSum&MaxDoubleSlice

MaxSliceSum:

Task description

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.

此题的思路就是设置一个max_ending,记录右边界固定为i时的最大部分和。max_slice不断更新为max_ending与max_slice中较大者。

代码:
// you can use includes, for example:
 #include 
        
        
         
         

// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;

int solution(const vector
         
         
          
           &A) {
    // write your code in C++11
    if(A.empty())
    return 0;
    int max_ending = A[0];
    int max_slice = A[0];
    for(unsigned int i=1;i
          
          
         
         
        
        



MaxDoubleSlice:

Task description

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).

Elements of input arrays can be modified.

此题是maxslice的进阶题。事实上就是求A[x]+...+A[z],然后减去A[x],A[y],A[z]后的部分和。和maxsilice的思路一样,我们仍然是通过枚举右边界来更新maxdoubleslice。但有三个点x,y,z,只枚举右边界就能行吗?是的,因为当右边界从i变成i+1时:1、若之前的max_ending为负值,则新的max_ending直接更新成A[i](可找出xyz使得max_ending=A[i])2、若之前的max_ending非负,但A[i]小于A[y],则将y更新成i(y处必须为最小值,因A[y]不会被加入部分和中),max_ending更新成以i为右边界的最大部分和(max_left);否则保持y不变,max_ending更新成max_ending+A[i]。

当左边序列的右边界为i时,max_left更新成max_left+A[i-1]和A[i-1]中较大者。

每次遍历都将max_slice更新为原max_slice和max_ending中较大者,则遍历完成后得到最优解。

代码:

// you can use includes, for example:
 #include 
   
   
    
    

// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
inline int max(int a,int b,int c)
{
    if(a
    
    
     
      &A) {
    // write your code in C++11
    if(A.size()<4)
    return 0;
    //右边界为i时两段最大值max_ending
    //中间点为i时左子列最大部分和max_left
    //整个数组最大部分和max_slice
    int max_ending = 0 , max_left = 0 , max_slice = 0;
    for(unsigned int i=3;i
     
     
    
    
   
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值