04、字节跳动-动态与贪心

1、买卖股票的最佳时机

04、字节跳动-动态与贪心_第1张图片

class Solution {
    public int maxProfit(int[] prices) {
         if (prices == null || prices.length < 1) {
            return 0;
        }
        int max = 0;    
        int min = prices[0];
        for(int i = 0; i 

参考:https://blog.csdn.net/weixin_41876155/article/details/80036893

 

2、买卖股票的最佳时机 II

04、字节跳动-动态与贪心_第2张图片

public class OneHundredAndTwentyTwo {

    public static int maxProfit(int[] prices) {
        int sum = 0;
        for(int i = prices.length - 1;i>0;i--) {
            if(prices[i] <= prices[i-1]) {
                continue;
            } else {
                sum += prices[i] - prices[i-1];
            }
        }
        return sum;
    }
    public static void main(String[] args) {
        int[] nums = new int[] {7,6,5,9,1,3};
        System.out.println(maxProfit(nums));
    }
}

原文:https://blog.csdn.net/xushiyu1996818/article/details/81200240

 

3、最大正方形

04、字节跳动-动态与贪心_第3张图片

public class Solution {
    public int maximalSquare(char[][] matrix) {
        if(matrix.length == 0) return 0;
        int m = matrix.length, n = matrix[0].length;
        int max = 0;
        int[][] dp = new int[m][n];
        // 第一列赋值
        for(int i = 0; i < m; i++){
            dp[i][0] = matrix[i][0] - '0';
            max = Math.max(max, dp[i][0]);
        }
        // 第一行赋值
        for(int i = 0; i < n; i++){
            dp[0][i] = matrix[0][i] - '0';
            max = Math.max(max, dp[0][i]);
        }
        // 递推
        for(int i = 1; i < m; i++){
            for(int j = 1; j < n; j++){
                dp[i][j] = matrix[i][j] == '1' ? Math.min(dp[i-1][j-1], Math.min(dp[i-1][j], dp[i][j-1])) + 1 : 0;
                max = Math.max(max, dp[i][j]);
            }
        }
        return max * max;
    }
}

参考:https://segmentfault.com/a/1190000003709497

           https://blog.csdn.net/qq_35170267/article/details/81202103

 

4、最大子序和

04、字节跳动-动态与贪心_第4张图片

 

class Solution {
    public int maxSubArray(int[] nums) {
         int current=nums[0];
        int sum=nums[0];
        //我们考虑如果全是负数,那么返回最大的负数,如果最后的和为正,那么就使用扫描法
        for(int i=1;isum)
                sum=current;//这里既实现了负数返回最大也实现了扫描法
            //这里其实已经隐式的列举了所有可能,保留了所有可能的最大值
        }
        return sum;
 
    }
}

 

5、三角形最小路径和

04、字节跳动-动态与贪心_第5张图片

class Solution {
    public int minimumTotal(List> triangle) {
        for (int i = triangle.size()-2; i >= 0; i--){
            for (int j = 0; j < triangle.get(i).size(); j++){
                triangle.get(i).set(j, triangle.get(i).get(j) + Math.min(triangle.get(i + 1).get(j), triangle.get(i + 1).get(j + 1)));
            }
        }
 
        return triangle.get(0).get(0);
    }
}

参考:https://blog.csdn.net/fmuma/article/details/80167433

 

6、俄罗斯套娃信封问题

04、字节跳动-动态与贪心_第6张图片

class Solution {
    public int maxEnvelopes(int[][] envelopes) {
        if(envelopes.length==0){
            return 0;
        }
        Arrays.sort( envelopes, new Comparator() {
            @Override
            public int compare(int[] ints, int[] t1) {
                return ints[0]-t1[0]!=0?ints[0]-t1[0]:ints[1]-t1[1];
            }
        });
        int[] dp=new int[envelopes.length];
        Arrays.fill(dp,1);
        for(int i=0;ist[0]&&ed[1]>st[1]&&dp[j]+1>dp[i]){
                    dp[i]=dp[j]+1;
                }
            }
        }
        int ans=0;
        for(int i=0;i

原文:https://blog.csdn.net/Viscu/article/details/82354257 

你可能感兴趣的:(程序人生)