
动态规划
永远的EMT
每天时刻保持超越自我的意识
展开
-
【PAT】1007. Maximum Subsequence Sum
思路:dp提交情况:这里全部都是负数时输出的是整个序列的首尾元素#define LOCAL#include #include #include #include #include #include #include #include #include #include #include #define FOR(i, x, y) for(int i = x; i <=原创 2017-02-20 10:04:44 · 237 阅读 · 0 评论 -
【LeetCode】Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n?Example:Input: 3Output: 5Explanation:Given n = 3, there are a total of 5 unique BST's: 1 ...原创 2018-10-22 15:29:12 · 139 阅读 · 0 评论 -
【LeetCode】Scramble String
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 = "great": great / \ gr ...原创 2018-10-28 23:29:18 · 199 阅读 · 0 评论 -
【LeetCode】Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given a non-empty string containing only digits, determine t...原创 2018-10-23 13:06:43 · 269 阅读 · 1 评论 -
【LeetCode】Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.Example 1:Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"Output: trueExample 2:Input: s1 = "aabcc", s2 = "...原创 2018-10-22 01:54:48 · 211 阅读 · 0 评论 -
【LeetCode】Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of gr...原创 2018-10-27 23:43:19 · 168 阅读 · 0 评论 -
【LeetCode】Edit Distance
Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.You have the following 3 operations permitted on a word:Insert a character Delete a chara...原创 2018-11-07 00:10:37 · 179 阅读 · 0 评论 -
【LeetCode】Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.Example 1:Input: [ [1,1,1], [1,0,1], [1,1,1]]Output: [ [1,0,1], [0,0,0], [1,0,1]]E...原创 2018-11-07 00:17:32 · 174 阅读 · 0 评论 -
【LeetCode】Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.Example:Input:[ ["1","0","1","0","0"], ["1","0","1&quo原创 2018-11-18 23:35:15 · 189 阅读 · 0 评论 -
【LeetCode】813. 最大平均值和的分组
我们将给定的数组A分成K个相邻的非空子数组 ,我们的分数由每个子数组内的平均值的总和构成。计算我们所能得到的最大分数是多少。注意我们必须使用 A 数组中的每一个数进行分组,并且分数不一定需要是整数。示例:输入: A = [9,1,2,3,9]K = 3输出: 20解释: A 的最优分组是[9], [1, 2, 3], [9]. 得到的分数是 9 + (1 + 2 + ...原创 2019-06-30 20:22:24 · 282 阅读 · 0 评论 -
【LeetCode】410. 分割数组的最大值
给定一个非负整数数组和一个整数m,你需要将这个数组分成m个非空的连续子数组。设计一个算法使得这m个子数组各自和的最大值最小。注意:数组长度n满足以下条件:1 ≤ n ≤ 10001 ≤ m ≤ min(50, n)示例:输入:nums = [7,2,5,10,8]m = 2输出:18解释:一共有四种方法将nums分割为2个子数组。其中最好的方式是...原创 2019-06-30 22:42:39 · 444 阅读 · 0 评论 -
【LeetCode】198. House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house...原创 2018-10-09 16:23:30 · 136 阅读 · 0 评论 -
【LeetCode】131. Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.Example:Input: "aab"Output:[ ["aa","b"], ["a","a"...原创 2018-10-15 22:32:43 · 151 阅读 · 0 评论 -
【LeetCode】132. Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of s.Example:Input: "aab"Output: 1Explana...原创 2018-10-15 22:23:26 · 458 阅读 · 0 评论 -
【PAT】1045. Favorite Color Stripe
考查点:LIS或LCS提交情况:段错误,这里是dp数组开成跟h数组一样了,应该与接受数组一样大,以后检查段错误除了开小了以外有可能数组所用最大值开错了 第一种是LIS版本:#define LOCAL#include #include #include #include #include #include #include #include #include #inc原创 2017-02-20 11:43:05 · 416 阅读 · 0 评论 -
【PAT】1040. Longest Symmetric String
考查点:dp,最长回文串#define LOCAL#include #include #include #include #include #include #include #include #include #include #include #define FOR(i, x, y) for(int i = x; i <= y; i++)#define rFOR(原创 2017-02-20 16:53:19 · 263 阅读 · 0 评论 -
【PAT】1068. Find More Coins
思路:01背包,用到了滚动数组提交情况:solution输出时忘记大写#define LOCAL#include #include #include #include #include #include #include #include #include #include #include #define FOR(i, x, y) for(int i = x; i原创 2017-02-21 11:20:58 · 355 阅读 · 0 评论 -
动态规划常见问题
主要方法设置一个dp数组记录递归的各个状态,记忆化搜索。关键写出dp的状态转移方程和状态的定义01背包:定义状态dp[i+1][j]=从前i个物品中选出总重量不超过j的物品时总价值的最大值当j<w[i]时dp[i+1][j]=dp[i][j]其他情况,dp[i+1][j]=max(dp[i][j],dp[i][j-w[i]]+v[i]代码:void DP(){ ...原创 2018-10-11 22:31:34 · 235 阅读 · 0 评论 -
【LeetCode】140. Word Break II
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible ...原创 2018-10-12 01:27:23 · 666 阅读 · 0 评论 -
【LeetCode】124. Binary Tree Maximum Path Sum
Given a non-empty binary tree, find the maximum path sum.For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connectio...原创 2018-10-06 23:10:31 · 190 阅读 · 0 评论 -
【LeetCode】139. Word Break
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.Note:The ...原创 2018-10-13 17:25:22 · 185 阅读 · 0 评论 -
【LeetCode】53. Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.Example:Input: [-2,1,-3,4,-1,2,1,-5,4],Output: 6Explanati...原创 2018-10-07 20:59:58 · 143 阅读 · 0 评论 -
【LeetCode】746. Min Cost Climbing Stairs
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of ...原创 2018-10-08 20:05:41 · 130 阅读 · 0 评论 -
【LeetCode】Triangle
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...原创 2018-10-18 15:47:46 · 176 阅读 · 0 评论 -
【LeetCode】Best Time to Buy and Sell Stock总结
LeetCode上买卖股票的问题如下:121. Best Time to Buy and Sell Stock 122. Best Time to Buy and Sell Stock II 123. Best Time to Buy and Sell Stock III 188. Best Time to Buy and Sell Stock IV 309. Best Time to...原创 2019-07-29 00:39:02 · 343 阅读 · 0 评论