标签: DP

6 篇文章

力扣hot100—普通数组5题
最大子数组和 动态规划 public int maxSubArray(int[] nums) { if (nums == null || nums.length == 0) return 0; int[] dp = new int[nums.length]; dp[0] = nums[0]; int maxSum = dp[0]; for (int…
力扣hot100—动态规划&多维动态规划15题
爬楼梯 爬第n阶楼梯的方法数量,等于 2 部分之和: 爬上 n−1 阶楼梯的方法数量。因为再爬1阶就能到第n阶 爬上 n−2 阶楼梯的方法数量,因为再爬2阶就能到第n阶 class Solution { public int climbStairs(int n) { // dp[i] 表示爬到第i层台阶的方法有多少种 int[] dp = new …
剑指offer67题-No30.连续子树的最大和
动态规划 方法1,可以通过暴力On^2的方法,循环两次求出每个子数组的和对比。 法2.DP 设dp[i],dp[i]代表以array[i]为结尾的连续子数组最大和。 转移方程为,dp[i] = max(array[i],dp[i-1] + array[i]),理解如下: 因为以第n个数为结尾,所以array[n]是必然被选择的 基于dp[n-1]的…
剑指offer67题-No7-No10.简单dp递归合集
NO7斐波那契数列 用递归的写法,复杂度将是O(2^n)级别的,无法忍受。 用三个变量存储递归的中间值,时间复杂度能到O(n)。    public int Fibonacci (int n) {        // write code here       int t1 = 1;       int t2 = 1;       int t3 =…