zoukankan      html  css  js  c++  java
  • LintCode "Minimum Subarray"

    Typical solution: convert it to Maximum Array problem.

    And here is my solution: O(n) by using Greedy strategy on this equation: sum[i..j] = sum[0..j] - sum[0..i-1].

    class Solution {
    public:
        /**
        * @param nums: a list of integers
        * @return: A integer denote the sum of minimum subarray
        */
        int minSubArray(vector<int> nums) 
        {
            auto len = nums.size();
    
            int ret = nums[0];
            vector<int> sums(len), maxs(len);
            sums[0] = nums[0];
            int cmax = max(0, nums[0]);
            
            for (int i = 1; i < len; i++)
            {
                //  calc sum
                sums[i] = nums[i] + sums[i - 1];
                //  calc prev max sum
                cmax = max(cmax, sums[i - 1]);
                maxs[i] = cmax;
                //
                ret = min(ret, sums[i] - maxs[i]);
            }
    
            return ret;
        }
    };
  • 相关阅读:
    docker commit
    镜像原理
    docker command1
    docker镜像命令
    docker work machine
    视图
    后台管理
    模型类
    docker command
    安装virtualenv
  • 原文地址:https://www.cnblogs.com/tonix/p/4804853.html
Copyright © 2011-2022 走看看