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;
}

 

转载于:https://www.cnblogs.com/parapax/p/3644922.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这个错误通常表示你正在尝试使用不可哈希的切片类型作为字典的键或集合的元素,但是切片类型是不可哈希的,因此会引发该错误。 要解决这个问题,你可以考虑将切片类型转换为元组类型,因为元组类型是可哈希的,例如: ``` my_dict = {(1,2,3): 'value'} # 使用元组类型作为字典的键 my_set = {(1,2,3), (4,5,6)} # 使用元组类型作为集合的元素 my_slice = slice(1, 5) my_tuple = tuple(my_slice) # 将切片类型转换为元组类型 ``` 这样就可以避免 TypeError: unhashable type: 'slice' 这个错误了。 ### 回答2: TypeError: unhashable type: 'slice'是Python中的一个错误类型。这个错误通常在使用slice类型的对象作为字典的键或集合的元素时出现。 slice是Python中的一个内置函数,用于切片操作。它通常用于从序列中选取一部分元素形成新的序列。 但是,slice对象是不可哈希的(unhashable),这意味着它们不能作为字典的键或集合的元素。字典和集合在内部使用哈希表来实现快速的查找和访问,而哈希表要求键或元素必须是可哈希的类型,也就是能够通过hash()函数生成哈希值的类型。 如果我们尝试将slice对象作为字典的键或集合的元素,Python解释器会抛出TypeError: unhashable type: 'slice'错误,提示我们slice对象是不可哈希的。 解决这个问题的方法是,将slice对象转换为可哈希的类型,例如将其转换为元组。这样就可以将它们用作字典的键或集合的元素,在进行操作时不会出现TypeError。例如: ```python my_dict = {} my_slice = slice(1, 5) my_tuple = tuple(my_slice) my_dict[my_tuple] = "Hello" print(my_dict) # 输出:{(1, 5): "Hello"} ``` 总结来说,TypeError: unhashable type: 'slice'错误是由于尝试将不可哈希的slice对象用作字典键或集合元素而引起的。解决这个问题的方法是将slice对象转换为可哈希的类型,例如元组。 ### 回答3: TypeError: unhashable type: 'slice' 是一种常见的错误类型,它通常发生在以切片作为字典或集合的键时。 在Python中,字典和集合的键需要是可哈希(hashable)的。可哈希的对象是指在其生命周期中不可改变的对象,比如字符串、整数和元组。然而,切片对象是不可哈希的,因为它们可以被修改。 例如,以下代码会引发该错误: ``` my_dict = {slice(1, 5): "value"} ``` 由于切片对象是不可哈希的,因此将其作为字典的键是不允许的,会导致抛出 TypeError: unhashable type: 'slice'。 解决该错误的方法有以下几种: 1. 修改代码逻辑,不要使用切片作为字典或集合的键。 2. 将切片对象转换为可哈希的对象,比如将切片对象转换为元组,然后将元组作为键。 例如,可以修改以上示例代码的方式如下: ``` my_dict = {(1, 5): "value"} ``` 这样就可以避免 TypeError: unhashable type: 'slice' 错误的发生。 总结来说,TypeError: unhashable type: 'slice' 错误通常发生在尝试将切片对象作为字典或集合的键时。解决该错误的方法包括修改代码逻辑,或将切片对象转换为可哈希的对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值