
Dynamic Programming
文章平均质量分 89
konsy_dong
Java,C++,Python,linux
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
《剑指Offer》 跳台阶
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。思路: 当要跳1阶时为1种,要跳2阶时为2种,当跳n(n>2)阶时,种数为前两个种数相加。 递归调用的时间太长,这在斐波那契数列的题中已经说过 代码:class Solution {public: int jumpFloor(int number) { if(numb原创 2017-04-06 16:15:56 · 369 阅读 · 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 to...原创 2018-03-23 00:35:20 · 203 阅读 · 0 评论 -
动态规划——最长公共子序列问题(LCS)
最长公共子序列问题也就是如在两个不同的字符串中找出最长的公共子序列例:字符串x = "ABCBDAB"字符串y = "BDCABA" 可以看出他们的公共子序列是 BCBA,但是对于较长的字符串,肉眼很难去判断,如果用枚举,那么其算法复杂度为O(2m{2^m})就要寻找一种算法复杂度较低的算法 ,就是先比较字符串x和y各个字符已经有几个相同的情况,存在c中,空间复杂度和时间复杂度都为O(m*n)原创 2017-09-22 16:28:52 · 1501 阅读 · 1 评论 -
LeetCode 91. Decode Ways
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1 'B' -> 2 ... 'Z' -> 26Given an encoded message containing digits, determine the total numb原创 2017-09-21 21:47:48 · 302 阅读 · 0 评论 -
LeetCode 300. Longest Increasing Subsequence
题目: Given an unsorted array of integers, find the length of longest increasing subsequence.For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], ther原创 2017-09-21 12:55:42 · 290 阅读 · 0 评论 -
LeetCode 523. Continuous Subarray Sum
题目: Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up原创 2017-09-20 16:40:32 · 409 阅读 · 0 评论 -
LeetCode 516. Longest Palindromic Subsequence
题目: 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: 4One possible longest palindro原创 2017-09-22 23:33:04 · 612 阅读 · 0 评论 -
LeetCode 413. Arithmetic Slices
题目: A sequence of number 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, these are arithmetic sequenc原创 2017-08-27 21:19:41 · 237 阅读 · 0 评论 -
LeetCode 646. Maximum Length of Pair Chain
题目: You are given n pairs of numbers. In every pair, the first number is always smaller than the second number.Now, we define a pair (c, d) can follow another pair (a, b) if and only if b < c. Chain o原创 2017-08-13 12:17:51 · 419 阅读 · 0 评论 -
LeetCode 343. Integer Break
题目: Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.For example, given n = 2, ret原创 2017-08-12 21:30:59 · 276 阅读 · 0 评论 -
LeetCode 70. Climbing Stairs
题目: 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-08-11 21:23:01 · 252 阅读 · 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 h原创 2017-08-11 21:07:37 · 370 阅读 · 0 评论 -
LeetCode 303. Range Sum Query - Immutable
题目: 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) -> 1sumRange(2, 5) -> -1sumRange(0,原创 2017-05-01 11:26:36 · 367 阅读 · 0 评论 -
LeetCode 264. Ugly Number II
题目: 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原创 2017-05-01 11:26:00 · 431 阅读 · 0 评论 -
LeetCode 53. Maximum Subarray
题目 : 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] h原创 2017-05-01 11:23:48 · 341 阅读 · 0 评论 -
LeetCode 121. Best Time to Buy and Sell Stock
题目: 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),原创 2017-04-06 16:43:59 · 438 阅读 · 0 评论 -
《剑指Offer》 矩形覆盖
题目描述: 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?思路: 这题和前面的那道跳台阶是一模一样的题,也是动态规划,这里的2*1的小矩形横着放就是跳2阶,竖着放就是跳1阶,当大矩形长度小于等于2时,方法数量为长度本身;当大于2时,即为d[i]=d[i-1]+d[i-2],但是递归调用太耗时,就用迭代。代码:cla原创 2017-04-07 10:22:50 · 332 阅读 · 0 评论 -
《剑指Offer》 变态跳台阶
题目描述: 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。思路: 这个其实是一道动态规划问题,d[1]=1,d[i]=d[i-1]+d[i-2]…+d[1]+1。但是有结果是pow(2,number-1)的规律,所以直接返回结果。代码:class Solution {public: int jumpFloorII(int原创 2017-04-06 17:34:58 · 559 阅读 · 0 评论 -
LeetCode 338. Counting Bits
题目: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array.Example: For num = ...原创 2018-03-23 02:12:59 · 253 阅读 · 0 评论