• 【LeetCode】最大子阵列 Maximum Subarray(贪婪&分治)


    描述:

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

    Example:

    Input: [-2,1,-3,4,-1,2,1,-5,4],
    Output: 6
    Explanation: [4,-1,2,1] has the largest sum = 6.
    

    Follow up:

    If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

    思路一:动态规划

    因为这是个优化问题。并且问题可以分解为一个个子问题,所以利用DP来解决此问题是一种很好的解决方案:

    dp[i-1]到dp[i]的转换分两种情况:

            1)dp[i-1] > 0: 当大于0时,dp[i] = dp[i-1] + num[i]

            2) dp[i-1] < 0:当大于0时,dp[i] = 0  (因为此时将剔除dp[i-1]的影响)

    class Solution {
    public:
        int maxSubArray(vector<int>& nums) {
            vector<int> dp(nums.size());
            dp[0] = nums[0];
            int max_ans = dp[0];
            for(int i = 1;i<nums.size();++i){
                dp[i] = nums[i] + (dp[i-1] > 0 ? dp[i-1] : 0);
                max_ans = max(max_ans, dp[i]);
            }
            return max_ans;
        }
    };

    思路二:贪婪

    从左到右汇总数组时找到总和最优解,和dp类似,但是我们这里不保存dp的状态,只记录临时sum和最大sum

    class Solution {
    public:
        int maxSubArray(vector<int>& nums) {
            int sum = 0;
            int ans;
            ans = nums[0];
            for(int i = 0;i<nums.size();i++){
                sum+=nums[i];
                ans = max(ans,sum);
                sum = max(sum,0);
            }
            return ans;
        }
    };

    思路三:分治法

     分治法的思路是将问题不断二分,分到不能再分,然后再将计算完的数据整合归一,最后得出最优解,这里,如图所示,将数组不断二分,然后取出每一段的最大sum,然后传回总函数,然后输出最优解

    class Solution {
    public:
        int maxSubArray(vector<int>& nums) {
            if(nums.size()==0) return 0;
            return maxSubArray(nums, 0 , nums.size() - 1);
        }
        
        
        // l代表数组左端low,h代表数组右端high,返回最大sum
        int maxSubArray(vector<int>& arr, int l, int h)
        {
            // Base Case: Only one element
            if (l == h)
                return arr[l];
    
            // Find middle point
            int m = (l + h)/2;
    
            /* Return maximum of following three possible cases
               a) Maximum subarray sum in left half
               b) Maximum subarray sum in right half
               c) Maximum subarray sum such that the subarray crosses the midpoint */
            return max(max(maxSubArray(arr, l, m),  //最左侧数组求最大sum
                           maxSubArray(arr, m+1, h)),  //对右侧数组求最大sum ,之后求左右的最大值
                       maxCrossingSum(arr, l, m, h));  //对整个数组以mid为分界线求最大sum
        }
        
        
        int maxCrossingSum(vector<int>& arr, int l, int m, int h)
        {
            // Include elements on left of mid.
            int sum = 0;
            int left_sum = INT_MIN;
            for (int i = m; i >= l; i--)
            {
                sum = sum + arr[i];
                if (sum > left_sum)
                    left_sum = sum;
            }
    
            // Include elements on right of mid
            sum = 0;
            int right_sum = INT_MIN;
            for (int i = m+1; i <= h; i++)
            {
                sum = sum + arr[i];
                if (sum > right_sum)
                    right_sum = sum;
            }
    
            // Return sum of elements on left and right of mid
            return left_sum + right_sum;
        }
    };
  • 相关阅读:
    vue 对图片进行拖拽到另一个位置
    vue自定义拖动指令
    使用pm2启动nodejs+express+mysql管理系统步骤
    重新学习html和css
    vue监听页面大小变化重新刷新布局
    Docker可视化管理工具DockerUI ,Portainer ,Shipyard对比(转)
    js删除html标记 去掉所有html标记 百度文库内容copy
    安卓模拟器连接端口一览表
    springmvc在使用@ModelAttribute注解获取Request和Response会产生线程并发不安全问题(转)
    常用软件测试工具的对比
  • 原文地址:https://www.cnblogs.com/ygh1229/p/9771441.html
Copyright © 2020-2023  润新知