c++

剪绳子

http://www.nowcoder.com/questionTerminal/57d85990ba5b440ab888fc72b0751bf8

class Solution {
public:
    int cutRope(int number) {
        if (number < 2) return 0;
        if (number == 2) return 1;
        if (number == 3) return 2;
        vector<int> result(number, 0);
        result[1] = 1;
        result[2] = 2;
        result[3] = 3;
        for (int i = 4; i <= number; ++ i) {
            for (int j = 1; j <= (i+1)/2; ++ j) {
                result[i] = max(result[i], result[j]*result[i-j]);
            }
        }
        return result[number];
    }
};
全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务