Dynamic_Practice
70.Climbing Stairs
Description:
You are climbing a staircase. It takes n
steps to reach the top.
Each time you can either climb 1
or 2
steps. In how many distinct ways can you climb to the top?
Example 1:
1 | Input: n = 2 |
Example 2:
1 | Input: n = 3 |
Constraints:
1 <= n <= 45
Code:
1 | class Solution { |
746.Min Cost Climbing Stairs
Description:
You are given an integer array cost
where cost[i]
is the cost of ith
step on a staircase. Once you pay the cost, you can either climb one or two steps.
You can either start from the step with index 0
, or the step with index 1
.
Return the minimum cost to reach the top of the floor.
Example 1:
1 | Input: cost = [10,15,20] |
Example 2:
1 | Input: cost = [1,100,1,1,1,100,1,1,100,1] |
Constraints:
2 <= cost.length <= 1000
0 <= cost[i] <= 999
Code:
1 | class Solution { |
62. Unique Paths
Description:
There is a robot on an m x n
grid. The robot is initially located at the top-left corner (i.e., grid[0][0]
). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]
). The robot can only move either down or right at any point in time.
Given the two integers m
and n
, return the number of possible unique paths that the robot can take to reach the bottom-right corner.
The test cases are generated so that the answer will be less than or equal to 2 * 109
.
Example 1:
1 | Input: m = 3, n = 7 |
Example 2:
1 | Input: m = 3, n = 2 |
Constraints:
1 <= m, n <= 100
Code:
1 | class Solution { |
*Number Theory
Code:
1 | class Solution { |
Reference:
63. Unique Paths II
Description:
You are given an m x n
integer array grid
. There is a robot initially located at the top-left corner (i.e., grid[0][0]
). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]
). The robot can only move either down or right at any point in time.
An obstacle and space are marked as 1
or 0
respectively in grid
. A path that the robot takes cannot include any square that is an obstacle.
Return the number of possible unique paths that the robot can take to reach the bottom-right corner.
The testcases are generated so that the answer will be less than or equal to 2 * 109
.
Example 1:
1 | Input: obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]] |
Example 2:
1 | Input: obstacleGrid = [[0,1],[0,0]] |
Constraints:
m == obstacleGrid.length
n == obstacleGrid[i].length
1 <= m, n <= 100
obstacleGrid[i][j]
is0
or1
.
Dode:
1 | class Solution { |
343.Integer Break
Description:
Given an integer n
, break it into the sum of k
positive integers, where k >= 2
, and maximize the product of those integers.
Return the maximum product you can get.
Example 1:
1 | Input: n = 2 |
Example 2:
1 | Input: n = 10 |
Constraints:
2 <= n <= 58
Code:
1 | class Solution { |
96. Unique Binary Search Trees
Description:
Given an integer n
, return *the number of structurally unique **BST’*s (binary search trees) which has exactly n
nodes of unique values from 1
to n
.
Example 1:
1 | Input: n = 3 |
Example 2:
1 | Input: n = 1 |
Constraints:
1 <= n <= 19
Code:
Choose every number as the root for one time. Calculate right and left dividedly.
1 | class Solution { |
416. Partition Equal Subset Sum(01背包问题)
Description
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.
Example 1:
1 | Input: nums = [1,5,11,5] |
Example 2:
1 | Input: nums = [1,2,3,5] |
Constraints:
1 <= nums.length <= 200
1 <= nums[i] <= 100
Code:
1 | class Solution { |
1049 Last Stone Weight II
Description:
You are given an array of integers stones
where stones[i]
is the weight of the ith
stone.
We are playing a game with the stones. On each turn, we choose any two stones and smash them together. Suppose the stones have weights x
and y
with x <= y
. The result of this smash is:
- If
x == y
, both stones are destroyed, and - If
x != y
, the stone of weightx
is destroyed, and the stone of weighty
has new weighty - x
.
At the end of the game, there is at most one stone left.
Return the smallest possible weight of the left stone. If there are no stones left, return 0
.
Example 1:
1 | Input: stones = [2,7,4,1,8,1] |
Example 2:
1 | Input: stones = [31,26,33,21,40] |
Constraints:
1 <= stones.length <= 30
1 <= stones[i] <= 100
Code:
1 | class Solution { |
* 494 Target Sum
Description:
You are given an integer array nums
and an integer target
.
You want to build an expression out of nums by adding one of the symbols '+'
and '-'
before each integer in nums and then concatenate all the integers.
- For example, if
nums = [2, 1]
, you can add a'+'
before2
and a'-'
before1
and concatenate them to build the expression"+2-1"
.
Return the number of different expressions that you can build, which evaluates to target
.
Example 1:
1 | Input: nums = [1,1,1,1,1], target = 3 |
Example 2:
1 | Input: nums = [1], target = 1 |
Constraints:
1 <= nums.length <= 20
0 <= nums[i] <= 1000
0 <= sum(nums[i]) <= 1000
-1000 <= target <= 1000
Code:
1 | class Solution { |
* 474. Ones and Zeroes
Description:
You are given an array of binary strings strs
and two integers m
and n
.
Return the size of the largest subset of strs
such that there are at most m
0
‘s and n
1
‘s in the subset.
A set x
is a subset of a set y
if all elements of x
are also elements of y
.
Example 1:
1 | Input: strs = ["10","0001","111001","1","0"], m = 5, n = 3 |
Example 2:
1 | Input: strs = ["10","0","1"], m = 1, n = 1 |
Constraints:
1 <= strs.length <= 600
1 <= strs[i].length <= 100
strs[i]
consists only of digits'0'
and'1'
.1 <= m, n <= 100
Code:
1 | class Solution { |
518. Coin Change II
Description:
You are given an integer array coins
representing coins of different denominations and an integer amount
representing a total amount of money.
Return the number of combinations that make up that amount. If that amount of money cannot be made up by any combination of the coins, return 0
.
You may assume that you have an infinite number of each kind of coin.
The answer is guaranteed to fit into a signed 32-bit integer.
Example 1:
1 | Input: amount = 5, coins = [1,2,5] |
Example 2:
1 | Input: amount = 3, coins = [2] |
Example 3:
1 | Input: amount = 10, coins = [10] |
Constraints:
1 <= coins.length <= 300
1 <= coins[i] <= 5000
- All the values of
coins
are unique. 0 <= amount <= 5000
Code:
1 | class Solution { |
377. Combination Sum IV
Description:
Given an array of distinct integers nums
and a target integer target
, return the number of possible combinations that add up to target
.
The test cases are generated so that the answer can fit in a 32-bit integer.
Example 1:
1 | Input: nums = [1,2,3], target = 4 |
Example 2:
1 | Input: nums = [9], target = 3 |
Constraints:
1 <= nums.length <= 200
1 <= nums[i] <= 1000
- All the elements of
nums
are unique. 1 <= target <= 1000
Follow up: What if negative numbers are allowed in the given array? How does it change the problem? What limitation we need to add to the question to allow negative numbers?
Code:
1 | class Solution { |
322. Coin Change
Description:
You are given an integer array coins
representing coins of different denominations and an integer amount
representing a total amount of money.
Return 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
.
You may assume that you have an infinite number of each kind of coin.
Example 1:
1 | Input: coins = [1,2,5], amount = 11 |
Example 2:
1 | Input: coins = [2], amount = 3 |
Example 3:
1 | Input: coins = [1], amount = 0 |
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104
Code:
1 | class Solution { |
279. Perfect Squares
Description:
Given an integer n
, return the least number of perfect square numbers that sum to n
.
A perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, 1
, 4
, 9
, and 16
are perfect squares while 3
and 11
are not.
Example 1:
1 | Input: n = 12 |
Example 2:
1 | Input: n = 13 |
Constraints:
1 <= n <= 104
Code:
1 | class Solution { |
* 139. Word Break
Description:
Given a string s
and a dictionary of strings wordDict
, return true
if s
can be segmented into a space-separated sequence of one or more dictionary words.
Note that the same word in the dictionary may be reused multiple times in the segmentation.
Example 1:
1 | Input: s = "leetcode", wordDict = ["leet","code"] |
Example 2:
1 | Input: s = "applepenapple", wordDict = ["apple","pen"] |
Example 3:
1 | Input: s = "catsandog", wordDict = ["cats","dog","sand","and","cat"] |
Constraints:
1 <= s.length <= 300
1 <= wordDict.length <= 1000
1 <= wordDict[i].length <= 20
s
andwordDict[i]
consist of only lowercase English letters.- All the strings of
wordDict
are unique.
code:
1 | class Solution { |
* 198. House Robber
Desription:
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 systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array nums
representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.
Example 1:
1 | Input: nums = [1,2,3,1] |
Example 2:
1 | Input: nums = [2,7,9,3,1] |
Constraints:
1 <= nums.length <= 100
0 <= nums[i] <= 400
Code:
1 | class Solution { |
* 213. House Robber II
Description:
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 have a security system connected, and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array nums
representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.
Example 1:
1 | Input: nums = [2,3,2] |
Example 2:
1 | Input: nums = [1,2,3,1] |
Example 3:
1 | Input: nums = [1,2,3] |
Constraints:
1 <= nums.length <= 100
0 <= nums[i] <= 1000
Code:
1 | class Solution { |
337. * House Robber III
Description:
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called root
.
Besides the root
, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if two directly-linked houses were broken into on the same night.
Given the root
of the binary tree, return the maximum amount of money the thief can rob without alerting the police.
Example 1:
1 | Input: root = [3,2,3,null,3,null,1] |
Example 2:
1 | Input: root = [3,4,5,1,3,null,1] |
Constraints:
- The number of nodes in the tree is in the range
[1, 104]
. 0 <= Node.val <= 104
Code:
1 | /** |
1035. Uncrossed Lines
Description:
You are given two integer arrays nums1
and nums2
. We write the integers of nums1
and nums2
(in the order they are given) on two separate horizontal lines.
We may draw connecting lines: a straight line connecting two numbers nums1[i]
and nums2[j]
such that:
nums1[i] == nums2[j]
, and- the line we draw does not intersect any other connecting (non-horizontal) line.
Note that a connecting line cannot intersect even at the endpoints (i.e., each number can only belong to one connecting line).
Return the maximum number of connecting lines we can draw in this way.
Example 1:
1 | Input: nums1 = [1,4,2], nums2 = [1,2,4] |
Example 2:
1 | Input: nums1 = [2,5,1,2,5], nums2 = [10,5,2,1,5,2] |
Example 3:
1 | Input: nums1 = [1,3,7,1,7,5], nums2 = [1,9,2,5,1] |
Constraints:
1 <= nums1.length, nums2.length <= 500
1 <= nums1[i], nums2[j] <= 2000
Code:
1 |
|
53. Maximum Subarray
Description:
Given an integer array nums
, find the subarray with the largest sum, and return its sum.
Example 1:
1 | Input: nums = [-2,1,-3,4,-1,2,1,-5,4] |
Example 2:
1 | Input: nums = [1] |
Example 3:
1 | Input: nums = [5,4,-1,7,8] |
Constraints:
1 <= nums.length <= 105
-104 <= nums[i] <= 104
Code:
1 | class Solution { |
392. Is Subsequence
Description:
Given two strings s
and t
, return true
if s
is a subsequence of t
, or false
otherwise.
A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace"
is a subsequence of "abcde"
while "aec"
is not).
Example 1:
1 | Input: s = "abc", t = "ahbgdc" |
Example 2:
1 | Input: s = "axc", t = "ahbgdc" |
Constraints:
0 <= s.length <= 100
0 <= t.length <= 104
s
andt
consist only of lowercase English letters.
Code:
1 | class Solution { |
115. *Distinct Subsequences
Description:
Given two strings s
and t
, return the number of distinct
*subsequences*
of s
which equals t
.
The test cases are generated so that the answer fits on a 32-bit signed integer.
Example 1:
1 | Input: s = "rabbbit", t = "rabbit" |
Example 2:
1 | Input: s = "babgbag", t = "bag" |
Constraints:
1 <= s.length, t.length <= 1000
s
andt
consist of English letters.
Code:
1 | class Solution { |
583. Delete Operation for Two Strings
Description:
Given two strings word1
and word2
, return the minimum number of steps required to make word1
and word2
the same.
In one step, you can delete exactly one character in either string.
Example 1:
1 | Input: word1 = "sea", word2 = "eat" |
Example 2:
1 | Input: word1 = "leetcode", word2 = "etco" |
Constraints:
1 <= word1.length, word2.length <= 500
word1
andword2
consist of only lowercase English letters.
Code:
1 | class Solution { |
72. Edit Distance
Description:
Given two strings word1
and word2
, return the minimum number of operations required to convert word1
to word2
.
You have the following three operations permitted on a word:
- Insert a character
- Delete a character
- Replace a character
Example 1:
1 | Input: word1 = "horse", word2 = "ros" |
Example 2:
1 | Input: word1 = "intention", word2 = "execution" |
Constraints:
0 <= word1.length, word2.length <= 500
word1
andword2
consist of lowercase English letters.
Code:
1 | class Solution { |
647. Palindromic Substrings
Description:
Given a string s
, return the number of palindromic substrings in it.
A string is a palindrome when it reads the same backward as forward.
A substring is a contiguous sequence of characters within the string.
Example 1:
1 | Input: s = "abc" |
Example 2:
1 | Input: s = "aaa" |
Constraints:
1 <= s.length <= 1000
s
consists of lowercase English letters.
Code:
1 | class Solution { |