DP动态规划
JackZhangNJU
未来的路还很长
展开
-
leetcode 303. Range Sum Query - Immutable 字串求和 + DP
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Example: Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 su...原创 2017-09-29 10:27:14 · 292 阅读 · 0 评论 -
leetcode 304. Range Sum Query 2D - Immutable 子矩阵求和 + DP
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). Range Sum Query 2D The above rectang...原创 2017-09-29 10:31:34 · 358 阅读 · 0 评论 -
leetcode 118. Pascal's Triangle 杨辉三角形
Given numRows, generate the first numRows of Pascal’s triangle.For example, given numRows = 5, Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]这个就是中国最伟大的杨辉三角形的问题。代码如下:import原创 2017-09-14 13:20:31 · 258 阅读 · 0 评论 -
leetcode 119. Pascal's Triangle II 杨辉三角形2
Given an index k, return the kth row of the Pascal’s triangle.For example, given k = 3, Return [1,3,3,1].就是中国最伟大的杨辉三角形。代码如下:import java.util.ArrayList;import java.util.List;public class So原创 2017-09-14 13:22:55 · 254 阅读 · 0 评论 -
leetcode 120. Triangle 杨辉三角形3 + BFS广度优先遍历
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原创 2017-09-14 13:29:56 · 421 阅读 · 0 评论 -
leetcode 53. Maximum Subarray 动态规划DP + 最大子串和
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] has ...原创 2017-09-04 10:00:41 · 575 阅读 · 0 评论 -
leetcode 55. Jump Game 跳跃游戏 + 贪心算法
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if yo...原创 2017-09-04 14:40:33 · 657 阅读 · 0 评论 -
leetcode 63. Unique Paths II DP动态规划
Follow up for “Unique Paths”:Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectively in the grid.For ex原创 2017-09-05 10:32:21 · 439 阅读 · 0 评论 -
leetcode 64. Minimum Path Sum DP动态规划
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at any原创 2017-09-05 10:37:16 · 364 阅读 · 0 评论 -
leetcode 70. Climbing Stairs DP动态规划 + 斐波那契序列
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?Note: Given n will be a positive原创 2017-09-05 14:46:48 · 267 阅读 · 0 评论 -
leetcode 174. Dungeon Game 一个逆着推导计算的DP动态规划问题
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially posi原创 2017-09-20 09:11:02 · 350 阅读 · 0 评论 -
leetcode 72. Edit Distance DP动态规划 + 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2。 (each operation is counted as 1 step。)You have the following 3 operations permitted on a word:a) In原创 2017-09-10 21:13:23 · 355 阅读 · 1 评论 -
leetcode 74. Search a 2D Matrix 右上角搜索
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right. The first integer of each row is原创 2017-09-10 21:40:03 · 211 阅读 · 0 评论 -
leetcode 75. Sort Colors 很不错的3种元素排序方法 + O(n)
Given an array with n objects colored red。 white or blue。 sort them so that objects of the same color are adjacent。 with the colors in the order red。 white and blue。Here。 we will use the integers 0。 1。原创 2017-09-10 21:51:04 · 608 阅读 · 0 评论 -
leetcode 11. Container With Most Water 双指针 + 最短木桶理论
Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lin原创 2017-08-30 15:05:54 · 533 阅读 · 0 评论 -
leetcode 3. Longest Substring Without Repeating Characters 最长不重复子串和重复子串
Given a string, find the length of the longest substring without repeating characters. Examples:Given “abcabcbb”, the answer is “abc”, which the length is 3.Given “bbbbb”, the answer is “b”, with the原创 2017-08-29 19:07:10 · 351 阅读 · 0 评论 -
leetcode 600. Non-negative Integers without Consecutive Ones 非负整数不包括连续的1 + DP动态规划
Given a positive integer n, find the number of non-negative integers less than or equal to n, whose binary representations do NOT contain consecutive ones.Example 1: Input: 5 Output: 5 Explanation:原创 2017-12-21 09:06:27 · 625 阅读 · 0 评论 -
leetcode 313. Super Ugly Number 超级丑数
Write a program to find the nth super ugly number.Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 1...原创 2017-10-02 10:15:30 · 430 阅读 · 0 评论 -
leetcode 630. Course Schedule III 课程调度 + 选择最多的课 + 任务安排 + 贪心算法
There are n different online courses numbered from 1 to n. Each course has some duration(course length) t and closed on dth day. A course should be taken continuously for t days and must be finished b...原创 2017-12-20 11:30:11 · 1281 阅读 · 0 评论 -
leetcode 392. Is Subsequence 子序列判断 深度优先遍历DFS + 一个很简单的循环
Given a string s and a string t, check if s is subsequence of t.You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, an原创 2017-12-05 16:53:55 · 571 阅读 · 0 评论 -
leetcode 664. Strange Printer 奇怪的打印机+最小打印次数 + 动态规划DP
There is a strange printer with the following two special requirements:The printer can only print a sequence of the same character each time. At each turn, the printer can print new characters star...原创 2017-12-22 13:26:47 · 837 阅读 · 0 评论 -
leetcode 516. Longest Palindromic Subsequence 最长回文子序列 + DP动态规划
Given a string s, find the longest palindromic subsequence’s length in s. You may assume that the maximum length of s is 1000.Example 1: Input:“bbbab” Output: 4 One possible longest palindromic sub原创 2017-12-16 15:10:47 · 588 阅读 · 0 评论 -
leetcode 213. House Robber II 入室抢劫 抢劫问题 + 一道经典的DP动态规划问题
Note: This is an extension of House Robber.After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all原创 2017-09-21 13:06:44 · 1379 阅读 · 0 评论 -
leetcode 198. House Robber 入室抢劫 + DP动态规划求解
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 houses原创 2017-09-20 10:27:21 · 684 阅读 · 0 评论 -
leetcode 718. Maximum Length of Repeated Subarray 最长公共子串 + 动态规划DP
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays.Example 1: Input: A: [1,2,3,2,1] B: [3,2,1,4,7] Output: 3 Explanation: The repeated subarra原创 2017-12-24 15:10:12 · 378 阅读 · 2 评论 -
leetcode 221. Maximal Square 最大正方形面积 + 动态规划DP实现
Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing only 1’s and return its area.For example, given the following matrix:1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0原创 2017-09-25 09:51:29 · 1677 阅读 · 0 评论 -
leetcode 238. Product of Array Except Self 一个动态规划DP的简单应用
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and in O(n).原创 2017-09-26 09:27:05 · 240 阅读 · 0 评论 -
leetcode 96. Unique Binary Search Trees 卡特兰数:BST的数量 + 栈出栈数量
Given n, how many structurally unique BST’s (binary search trees) that store values 1…n?For example, Given n = 3, there are a total of 5 unique BST’s.1 3 3 2 1 \原创 2017-09-12 18:12:15 · 414 阅读 · 0 评论 -
leetcode 32. Longest Valid Parentheses 最长有效括号长度
Given a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (well-formed) parentheses substring.For “(()”, the longest valid parentheses substring is “()”, which原创 2017-09-02 16:20:57 · 750 阅读 · 0 评论 -
leetcode 62. Unique Paths DP动态规划
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 bo原创 2017-09-05 10:03:31 · 390 阅读 · 0 评论 -
leetcode 188. Best Time to Buy and Sell Stock IV 最大子段和
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most k transactions.Note: You may not en原创 2017-09-20 10:07:22 · 595 阅读 · 0 评论 -
leetcode 123. Best Time to Buy and Sell Stock III 最大k次字段和 + DP
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most two transactions.Note: You may not原创 2017-09-15 09:48:52 · 548 阅读 · 0 评论 -
leetcode 122. Best Time to Buy and Sell Stock II 最大子段和 + DP
Say you have an array for which the ith 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原创 2017-09-15 09:20:53 · 487 阅读 · 0 评论 -
leetcode 121. Best Time to Buy and Sell Stock 最大字段和问题 + DP动态规划
Say you have an array for which the ith 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), d原创 2017-09-15 09:16:29 · 553 阅读 · 0 评论 -
leetcode 309. Best Time to Buy and Sell Stock with Cooldown 动态规划DP
Say you have an array for which the ith 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原创 2017-09-29 15:19:14 · 372 阅读 · 0 评论 -
leetcode 714. Best Time to Buy and Sell Stock with Transaction Fee 动态规划DP
Your are given an array of integers prices, for which the i-th element is the price of a given stock on day i; and a non-negative integer fee representing a transaction fee.You may complete as many原创 2017-12-24 14:10:52 · 442 阅读 · 0 评论 -
leetcode 312. Burst Balloons 气球爆炸计算分数 + 一个按照length做动态规划DP的很棒的做法
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[le原创 2017-10-02 10:04:22 · 855 阅读 · 0 评论 -
leetcode 263. Ugly Number 丑数
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it原创 2017-09-27 09:35:16 · 275 阅读 · 0 评论 -
leetcode 264. Ugly Number II 计算第K个丑数
Write a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 u原创 2017-09-27 09:39:19 · 311 阅读 · 0 评论 -
leetcode 5. Longest Palindromic Substring 最长回文子串的查找 + 按照length做DP
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example:Input: “babad”Output: “bab”Note: “aba” is also a valid answer. Examp原创 2017-08-30 11:50:39 · 542 阅读 · 0 评论