动态规划
江南路漫
这个作者很懒,什么都没留下…
展开
-
Leetcode动态规划 不同路径
A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the dia原创 2021-08-26 15:02:12 · 84 阅读 · 0 评论 -
Leetcode动态规划 等差数列划分
An integer array is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.For example, [1,3,5,7,9], [7,7,7,7], and [3,-1,-5,-9] are arithmetic sequences.Given an integer array n原创 2021-08-21 10:09:38 · 86 阅读 · 0 评论 -
Leetcode动态规划 接雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]Output: 6Explanation: The above elevation map (black section) is rep原创 2021-08-19 13:22:32 · 144 阅读 · 0 评论 -
LeetCode动态规划 分割等和子集
Given a non-empty array nums containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.代码class Solution {public: bool canPartition(vector<int>& nums) {原创 2021-08-15 22:35:21 · 167 阅读 · 0 评论 -
LeetCode动态规划 杨辉三角
Given an integer numRows, return the first numRows of Pascal’s triangle.In Pascal’s triangle, each number is the sum of the two numbers directly above it as shown:前几天做的动态规划题好难呜呜,做道水题找回自信~状态转移方程triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j]边界条件原创 2021-08-15 17:13:45 · 45 阅读 · 0 评论 -
动态规划 0-1背包问题 滚动数组
定义dp[j]是从物品0到i中挑选物品,放进容量为j的背包中的最大价值总和。初始化int dp[maxn];memset(dp, sizeof(dp), -0x3f3f3f3f);一维滚动数组法for(int i = 0; i < weight.size(); i++) for(int j = bag_size; j >= 0; j--) { if(j < weight[i]) dp[j] = dp[j-1]; else dp[j] = max(dp[j原创 2021-08-12 17:07:30 · 174 阅读 · 0 评论 -
动态规划 0-1背包问题 二维数组
定义dp[i][j]是从物品0到i中挑选物品,放进容量为j的背包中的最大价值总和。初始化int dp[maxn][maxn];memset(dp, sizeof(dp), -0x3f3f3f3f);for(int j = bag_size; j >= 0; j--)dp[0][j] = dp[0][j-weight[0]] + values[0];先遍历物品法for(int i = 0; i < weight.size(); i++) for(int j = 1; j &l原创 2021-08-12 16:01:36 · 172 阅读 · 0 评论 -
LeetCode动态规划+贪心 买卖股票的最佳时机含手续费
You are given an array prices where prices[i] is the price of a given stock on the ith day, and an integer fee representing a transaction fee.Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay t原创 2021-08-05 13:59:35 · 166 阅读 · 0 评论 -
LeetCode动态规划 最佳买卖股票时机含冷冻期
You are given an array prices where prices[i] is the price of a given stock on the ith day.Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the f原创 2021-08-02 09:43:52 · 124 阅读 · 0 评论 -
LeetCode动态规划+贪心 买卖股票的最佳时机II
You are given an array prices where prices[i] is the price of a given stock on the ith day.Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).Note: You原创 2021-07-31 09:51:53 · 187 阅读 · 0 评论 -
LeetCode动态规划 买卖股票的最佳时机
You are given an array prices where prices[i] is the price of a given stock on the ith day.You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.Return the maximum profit原创 2021-07-31 09:23:17 · 119 阅读 · 0 评论 -
LeetCode动态规划 最佳观光组合
You are given an integer array values where values[i] represents the value of the ith sightseeing spot. Two sightseeing spots i and j have a distance j - i between them.The score of a pair (i < j) of sightseeing spots is values[i] + values[j] + i - j:原创 2021-07-30 22:22:03 · 135 阅读 · 0 评论 -
LeetCode动态规划 乘积为正数的最长子数组长度
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive.A subarray of an array is a consecutive sequence of zero or more values taken out of that array.Return the maximum length of a subarr原创 2021-07-29 14:51:42 · 147 阅读 · 0 评论 -
LeetCode动态规划 乘积最大子数组
Given an integer array nums, find a contiguous non-empty subarray within the array that has the largest product, and return the product.It is guaranteed that the answer will fit in a 32-bit integer.A subarray is a contiguous subsequence of the array.动态转原创 2021-07-29 11:00:26 · 66 阅读 · 0 评论 -
LeetCode动态规划 环形子数组的最大和
给定一个由整数数组 A 表示的环形数组 C,求 C 的非空子数组的最大可能和。此外,子数组最多只能包含固定缓冲区 A 中的每个元素一次。思路本道题需要分类成两种情况,题目答案为以下两种情况的较大值。1.最大子区间在序列的中间这种情况是常规的最长子序列做法。状态转移方程dp[i] = max(nums[i], dp[i-1] + nums[i])边界条件dp[0] = nums[0]2.最大子区间被分割成两部分(即头尾各一部分)处理分段区间比较麻烦,当遇到最大最小问题的时候,可以想办法将原创 2021-07-29 09:00:24 · 424 阅读 · 0 评论 -
LeetCode动态规划 最大子序和
给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。ps: 清华大学912曾经考过。状态转移方程dp[i] = max(nums[i], dp[i-1] + nums[i])边界条件dp[0] = nums[0]代码class Solution {public: int maxSubArray(vector<int>& nums) { int dp[30005] = {0}; int原创 2021-07-28 20:06:39 · 83 阅读 · 0 评论 -
LeetCode动态规划 跳跃游戏II
给你一个非负整数数组 nums ,你最初位于数组的第一个位置。数组中的每个元素代表你在该位置可以跳跃的最大长度。你的目标是使用最少的跳跃次数到达数组的最后一个位置。假设你总是可以到达数组的最后一个位置。这道题用动态规划做,时间复杂度很高但毕竟在学动态规划嘛,那就不用贪心算法优化了。状态转移方程dp[i+j] = min(dp[i+j], dp[i] + 1)边界条件dp[0] = 0class Solution {public: int jump(vector<int&g原创 2021-07-19 11:04:47 · 200 阅读 · 2 评论 -
LeetCode动态规划 删除并获得点数
You are given an integer array nums. You want to maximize the number of points you get by performing the following operation any number of times:Pick any nums[i] and delete it to earn nums[i] points. Afterwards, you must delete every element equal to nums原创 2021-07-16 22:03:00 · 118 阅读 · 2 评论 -
LeetCode动态规划 打家劫舍II
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses原创 2021-07-16 16:05:55 · 90 阅读 · 0 评论 -
LeetCode动态规划 斐波那契数
斐波那契数,通常用 F(n) 表示,形成的序列称为 斐波那契数列 。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是:F(0) = 0,F(1) = 1F(n) = F(n - 1) + F(n - 2),其中 n > 1给你 n ,请计算 F(n) 。之前基本没有碰过动态规划的题,这个暑假开始猛攻动态规划,先从简单的斐波那契数列开始吧~这个题的边界和状态转移方程题目都给了,挺简单的。倒是发现了可以秒杀此题的公式,公式如下:class Solution {pu原创 2021-07-15 11:48:42 · 83 阅读 · 0 评论 -
LeetCode动态规划 使用最小花费爬楼梯
数组的每个下标作为一个阶梯,第 i 个阶梯对应着一个非负数的体力花费值 cost[i](下标从 0 开始)。每当你爬上一个阶梯你都要花费对应的体力值,一旦支付了相应的体力值,你就可以选择向上爬一个阶梯或者爬两个阶梯。请你找出达到楼层顶部的最低花费。在开始时,你可以选择从下标为 0 或 1 的元素作为初始阶梯。状态转移方程dp[i] = min(dp[i-1], dp[i-2]) + dp[i];边界条件dp[0] = cost[0], dp[1] = cost[1]这题总感觉题目描述得不太行,原创 2021-07-15 12:44:31 · 167 阅读 · 0 评论