leetcode dynamic planning a series of problem-solving java

121. Best Time to Buy and Sell Stock easy题目

 public int maxProfit(int[] prices) {
        int buy=Integer.MAX_VALUE;
        int maxpro=0;
        for(int i=0;i<prices.length;i++){
            buy=Math.min(prices[i],buy);
            maxpro=Math.max(maxpro,prices[i]-buy);
        }
        return maxpro;
    }

746. Min Cost Climbing Stairs

The current cost depends on the previous one or two takes

public int minCostClimbingStairs(int[] cost) {
        int dp[]=new int[1002];
        dp[0]=cost[0];
        dp[1]=cost[1];
        int n=cost.length;
        for(int i=2;i<n;i++){
            dp[i]=cost[i]+Math.min(dp[i-1],dp[i-2]);
        }
        return Math.min(dp[n-1],dp[n-2]);
    }

70. Climbing Stairs

Thinking: i-th first i-1 depends on a number of compositions and the second step of the two i-2

    int climbStairs(int n) {
       if (n == 1)
            return 1;
        if (n < 1)
            return 0;
        
        int dp[n];
        dp[0] = 1;
        dp[1] = 2;

        for (auto i = 2; i < n; i++)
            dp[i] = dp[i - 1] + dp[i - 2];

        return dp[n-1];
    }

53. Maximum Subarray

Thinking: maximum continuous array, cursum initial value is 0, each traversed a number, in comparison cursum num + num maximum value and, if cursum is negative, then the scratch, the largest deposit in res cursum

In the case of additive, as long as the relationship between the handle and the global maximum local maximum to

local[i + 1] = Max(local[i] + A[i], A[i]);

global[i + 1] = Max(local[i + 1], global[i]);

    public int maxSubArray(int[] nums) {
        int res=Integer.MIN_VALUE;
        int cursum=0;
        for(int num:nums){
            cursum=Math.max(cursum+num,num);
            res=Math.max(cursum,res);
        }
        return res;
    }

152. Maximum Product Subarray

Ideas: the greatest opportunity subarray. Wherein f [i] denotes the sub-array [0, i] within the scope and must contain the maximum sub nums [i] an array of numbers product, g [i] denotes the sub-array [0, i] within the scope and must contain nums [i] a minimal product of the number of arrays, initialization F [0] and G [0] is initialized to the nums [0], the rest are initialized to zero. Then starting from the second digital array traversal, then the time will only produce maximum and minimum values ​​among the three figures, i.e., f [i-1] * nums [i], g [i-1] * nums [i], and nums [i]. Therefore, the maximum value among the three is updated f [i], is updated with the minimum value g [i], then f [i] is updated to the result res

    public int maxProduct(int[] nums) {
        int n=nums.length;
        int f[]=new int[n];
        int g[]=new int[n];
        f[0]=nums[0];
        g[0]=nums[0];
        int res=nums[0];
        for(int i=1;i<n;i++){
            f[i]=Math.max(Math.max(f[i-1]*nums[i],g[i-1]*nums[i]),nums[i]);
            g[i]=Math.min(Math.min(f[i-1]*nums[i],g[i-1]*nums[i]),nums[i]);
            res=Math.max(res,f[i]);
        }
        return res;
    }

198. House Robber

Thinking: Recurrence Formula dp [i] = max (num [i] + dp [i - 2], dp [i - 1]), which shows that we need to initialize dp [0] and dp [1], wherein dp [0] is the num [0], dp [1] at this time should be max (num [0], num [1]).

 public int rob(int[] nums) {
      int n=nums.length;
        if(n==0)return 0;
        if(n==1)return nums[0];
        int dp[]=new int[n];
        dp[0]=nums[0];
        dp[1]=Math.max(nums[0],nums[1]);
        for(int i=2;i<n;i++){
            dp[i]=Math.max(dp[i-2]+nums[i],dp[i-1]);
        }
        return Math.max(dp[n-1],dp[n-2]);
    }

213. House Robber II

198 upgrade version, you need to remove the head and tail, it is divided into two cases, the calling code to 198

public int subrob(int[] nums,int s,int e) {
        int n=e-s+1;
        int dp[]=new int[n];
        dp[0]=nums[s];
        dp[1]=Math.max(nums[s],nums[s+1]);
        for(int i=2;i<n;i++){
            dp[i]=Math.max(dp[i-2]+nums[s+i],dp[i-1]);
        }
        return Math.max(dp[n-1],dp[n-2]);
    }
    public int rob(int[] nums) {
        int n=nums.length;
        if(n==0)return 0;
        if(n==1)return nums[0];
        if(n==2)return Math.max(nums[0],nums[1]);
        return Math.max(subrob(nums,0,nums.length-2),subrob(nums,1,nums.length-1));
    }

 

He published 192 original articles · won praise 27 · Views 100,000 +

Guess you like

Origin blog.csdn.net/lovely_girl1126/article/details/103496698