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 integer.
Example 1:
|
|
Example 2:
|
|
递归版本
|
|
dp版本
|
|
120. 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.
Example:
|
|
Note:
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
解题思路:
自底向上的搜索并记录最小的路径
|
|
64. Minimum Path Sum
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 point in time.
Example:
|
|
解题思路:
|
|
|
|
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.
Example:
given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4).
Note:
You may assume that n is not less than 2 and not larger than 58.
递归版本
|
|
自顶向下的记忆化搜索
|
|
dp版本
|
|
279. Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, …) which sum to n.
For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9.
状态转移方程
|
|
|
|
91. Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:
Given an encoded message containing digits, determine the total number of ways to decode it.
Example:
Given encoded message “12”, it could be decoded as “AB” (1 2) or “L” (12).
The number of ways decoding “12” is 2.
解题思路:
自右往左
|
62. Unique Paths
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 diagram below).
How many possible unique paths are there?
解题思路:
对于第一行和第一列,每个元素的可到达路径只有1种
对于其他元素,每个元素可到达的路径都是由上和左两个元素的和
|
|
63. Unique Paths II
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.
Example:
|
|
解题思路:
若当前元素是1,会干扰到后续元素的判断,
因为它不知道这是障碍或者是路径1
所以开辟新空间给新数组
|
|
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 houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
解题思路:
|
|
暴力搜索法
|
|
记忆化搜索:
|
|
dp
|
|
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.
Example:
|
|
状态转移方程
|
|
|
|
0-1背包问题
有一个背包,它的容量为C(capacity)。现在有n种不同的物品,编号为0到n-1,其中每一个物品的重量为W(i),价值为V(i),问可以在这个背包中放哪些东西,使得在不超过C的基础上,物品的总价值最大。
|
|
Example:
|
|
|
|
dp优化
空间优化
第i行元素只依赖于第i-1行元素,理论上只需要保持两行元素
空间复杂度 O(2*C) = O(C)
|
|
空间和时间的同时优化
|
|
416. Partition Equal Subset Sum
Given a non-empty array 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.
Example:
|
|
Note:
- Each of the array element will not exceed 100.
- The array size will not exceed 200.
解题思路:
F(n, C)考虑将n个物品填满容量为C的背包
|
|
|
|
|
|
dp优化
|
|
322. Coin Change
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
Example:
|
|
Note: 完全背包问题
|
|
状态转移方程
|
|
|
|
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. You may assume the dictionary does not contain duplicate words.
Example:
|
|
状态转移方程
|
|
|
|
377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.
Example:
|
|
状态转移方程
|
|
|
|
|
|
最长上升子序列
300. Longest Increasing Subsequence
LIS(i) 表示以第i个数字为结尾的最长上升子序列的长度
即[0, …, i]的范围内,选择数字nums[i]可以获得的最长上升子序列的长度
|
|
|
|
300-1. Print LIS
376. Wiggle Subsequence
状态转移方程
|
|
|
|
优化版本
|
|
最长公共子序列
(Lintcode)77. Longest Common Subsequence
状态转移方程
|
|
|
|