leetcode算法题解(Java版)-4-动态规划(杨辉三角问题)

简介: 肯定是低价买,高价卖出。但是有个限制就是在买进的时候,必须卖出手上的股票。 最低价买,最高价的时候卖。在递增数列中,末项减首项=每项与后一项之差的和。这里不需要考虑交易次数最小,所以可以这么写

一、简单模拟

题目描述

Say you have an array for which the i th element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

思路

  • 肯定是低价买,高价卖出。但是有个限制就是在买进的时候,必须卖出手上的股票。
  • 最低价买,最高价的时候卖。在递增数列中,末项减首项=每项与后一项之差的和。这里不需要考虑交易次数最小,所以可以这么写:

代码

public class Solution {
    public int maxProfit(int[] prices) {
        int len=prices.length;
        int maxPro=0;
        for(int i=1;i<len;i++){
            if(prices[i]>prices[i-1]){
                maxPro+=prices[i]-prices[i-1];
            }
            
        }
        return maxPro;
    }
}

二、模拟的升级版

题目描述

Say you have an array for which the i th element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

思路

  • 这题跟上一题不一样的地方在于,是能买卖一次了。
  • 可以先固定局部最小值,后面不断维护跟新局部最大值和局部最小值的差,直到最后找到结果。
  • 做了两道这种最大值和最小值差的问题,总结一下,因为最大值最小值都是在数组中变化的,所以可以考虑先固定出局部的最小值

代码

import java.lang.Math;

public class Solution {
    public int maxProfit(int[] prices) {
        int len=prices.length;
        if(prices==null||len==0){
        return 0;
        }
        int min=prices[0];
        int maxPro=0;
        for(int i=1;i<len;i++){
        if(prices[i]<min){
        min=prices[i];
        }
        else{
        maxPro=Math.max(prices[i]-min,maxPro);
        }
        }
        return maxPro;
    }
}

三、动态规划

题目描述
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
For example, given the following triangle

[
     [2],
    [3,4],
   [6,5,7],
  [4,1,8,3]
]

The minimum path sum from top to bottom is11(i.e., 2 + 3 + 5 + 1 = 11).

Note:
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.

思路

  • 经典的三角数组,动态规划问题
  • 详细的请看代码。这里没有开辟新的存储,只是在最后一行上跟新维护每一行的最优解,最后最后一行的第一个数即为所求。
  • 语法点:import java.util.ArrayList;ArrayList.get(index);ArrayList.set(index,value);import java.lang.Math;Math.min(value1,value2);

代码

import java.lang.Math;
import java.util.ArrayList;

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

四、动态规划(杨辉三角)

题目描述

Given an index k, return the k th row of the Pascal's triangle.
For example, given k = 3,
Return[1,3,3,1].
Note:
Could you optimize your algorithm to use only O(k) extra space?

思路

  • 又是个动态规划问题,一开始想复杂了,或者说是没有按计算机思维来思考:我在算C{n_m},就是在算组合数,想通过组合数来打印出数组。
  • 其实是个动态规划,当前行的值是依赖于上一行的。题目要求用O(n)空间来求解,那就设置了一个动态数组,每次从后往前刷新这个数组,即为所求。

代码


import java.util.ArrayList;

public class Solution {
    public ArrayList<Integer> getRow(int rowIndex) {
        ArrayList<Integer> res=new ArrayList<>();
        res.add(1);
        for(int i=0;i<rowIndex;i++){
            for(int j=i;j>0;j--){//注意要从后往前刷新,因为用的是一个动态数组,防止仍有用的数据被新值覆盖
                res.set(j,res.get(j)+res.get(j- 1));
            }
            res.add(1);
        }
        return res;
    }
}
目录
相关文章
|
12天前
|
算法 安全 Java
性能工具之 JMeter 自定义 Java Sampler 支持国密 SM2 算法
【4月更文挑战第28天】性能工具之 JMeter 自定义 Java Sampler 支持国密 SM2 算法
27 1
性能工具之 JMeter 自定义 Java Sampler 支持国密 SM2 算法
|
3天前
leetcode代码记录(杨辉三角
leetcode代码记录(杨辉三角
7 1
|
4天前
leetcode代码记录(动态规划基础题(斐波那契数列)
leetcode代码记录(动态规划基础题(斐波那契数列)
7 0
|
13天前
|
人工智能 Java
用 Java 打印杨辉三角
用 Java 打印杨辉三角
|
18天前
|
设计模式 算法 Java
[设计模式Java实现附plantuml源码~行为型]定义算法的框架——模板方法模式
[设计模式Java实现附plantuml源码~行为型]定义算法的框架——模板方法模式
|
18天前
[leetcode~数位动态规划] 2719. 统计整数数目 hard
[leetcode~数位动态规划] 2719. 统计整数数目 hard
|
19天前
|
搜索推荐 算法 Java
Java实现的常用八种排序算法
提到数据结构与算法,无法避免的一点就包含排序,熟练的掌握各种排序算法则是一个程序员必备的素质之一,除此之外,排序算法也是当下各大技术公司比较喜欢问的技术点,所以,就这一点JavaBuild整理了常见的8种排序算法
8 0
|
23天前
|
机器学习/深度学习 数据采集 算法
使用 Java 实现机器学习算法
【4月更文挑战第19天】Java在数据驱动时代为机器学习提供支持,具备丰富的数学和数据结构库,适用于实现线性回归、决策树、SVM和随机森林等算法。实现时注意数据预处理、模型选择、评估指标和可视化。利用Java的库和编程能力可构建高效模型,但需按问题需求选择合适技术和优化方法。
|
23天前
|
算法
代码随想录算法训练营第五十六天 | LeetCode 647. 回文子串、516. 最长回文子序列、动态规划总结
代码随想录算法训练营第五十六天 | LeetCode 647. 回文子串、516. 最长回文子序列、动态规划总结
34 1
|
26天前
|
JavaScript
【leetcode】221--最大正方形-动态规划法
【leetcode】221--最大正方形-动态规划法
10 0

热门文章

最新文章