2023-07-17
445. Add Two Numbers II
Topic: Linked List, Math, Stack
Difficulty: Medium
Problem:
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/04/09/sumii-linked-list.jpg
Example 2:
Example 3:
Constraints:
• The number of nodes in each linked list is in the range
•
• It is guaranteed that the list represents a number that does not have leading zeros.
Follow up: Could you solve it without reversing the input lists?
445. Add Two Numbers II
Topic: Linked List, Math, Stack
Difficulty: Medium
Problem:
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/04/09/sumii-linked-list.jpg
Input: l1 = [7,2,4,3], l2 = [5,6,4]
Output: [7,8,0,7]
Example 2:
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [8,0,7]
Example 3:
Input: l1 = [0], l2 = [0]
Output: [0]
Constraints:
• The number of nodes in each linked list is in the range
[1, 100].•
0 <= Node.val <= 9• It is guaranteed that the list represents a number that does not have leading zeros.
Follow up: Could you solve it without reversing the input lists?
2023-07-18
146. LRU Cache
Topic: Hash Table, Linked List, Design, Doubly-Linked List
Difficulty: Medium
Problem:
Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
Implement the
•
•
•
The functions
Example 1:
Constraints:
•
•
•
• At most
146. LRU Cache
Topic: Hash Table, Linked List, Design, Doubly-Linked List
Difficulty: Medium
Problem:
Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
Implement the
LRUCache class:•
LRUCache(int capacity) Initialize the LRU cache with positive size capacity.•
int get(int key) Return the value of the key if the key exists, otherwise return -1.•
void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key.The functions
get and put must each run in O(1) average time complexity.Example 1:
Input
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
Output
[null, null, null, 1, null, -1, null, -1, 3, 4]
Explanation
LRUCache lRUCache = new LRUCache(2);
lRUCache.put(1, 1); // cache is {1=1}
lRUCache.put(2, 2); // cache is {1=1, 2=2}
lRUCache.get(1); // return 1
lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3}
lRUCache.get(2); // returns -1 (not found)
lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3}
lRUCache.get(1); // return -1 (not found)
lRUCache.get(3); // return 3
lRUCache.get(4); // return 4
Constraints:
•
1 <= capacity <= 3000•
0 <= key <= 10^4•
0 <= value <= 10^5• At most
2 * 10^5 calls will be made to get and put.2023-07-19
435. Non-overlapping Intervals
Topic: Array, Dynamic Programming, Greedy, Sorting
Difficulty: Medium
Problem:
Given an array of intervals
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
435. Non-overlapping Intervals
Topic: Array, Dynamic Programming, Greedy, Sorting
Difficulty: Medium
Problem:
Given an array of intervals
intervals where intervals[i] = [start_i, end_i], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.Example 1:
Input: intervals = [[1,2],[2,3],[3,4],[1,3]]
Output: 1
Explanation: [1,3] can be removed and the rest of the intervals are non-overlapping.
Example 2:
Input: intervals = [[1,2],[1,2],[1,2]]
Output: 2
Explanation: You need to remove two [1,2] to make the rest of the intervals non-overlapping.
Example 3:
Input: intervals = [[1,2],[2,3]]
Output: 0
Explanation: You don't need to remove any of the intervals since they're already non-overlapping.
Constraints:
•
1 <= intervals.length <= 10^5•
intervals[i].length == 2•
-5 * 10^4 <= start_i < end_i <= 5 * 10^42023-07-20
735. Asteroid Collision
Topic: Array, Stack, Simulation
Difficulty: Medium
Problem:
We are given an array
For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.
Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
735. Asteroid Collision
Topic: Array, Stack, Simulation
Difficulty: Medium
Problem:
We are given an array
asteroids of integers representing asteroids in a row.For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.
Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.
Example 1:
Input: asteroids = [5,10,-5]
Output: [5,10]
Explanation: The 10 and -5 collide resulting in 10. The 5 and 10 never collide.
Example 2:
Input: asteroids = [8,-8]
Output: []
Explanation: The 8 and -8 collide exploding each other.
Example 3:
Input: asteroids = [10,2,-5]
Output: [10]
Explanation: The 2 and -5 collide resulting in -5. The 10 and -5 collide resulting in 10.
Constraints:
•
2 <= asteroids.length <= 10^4•
-1000 <= asteroids[i] <= 1000•
asteroids[i] != 02023-07-21
673. Number of Longest Increasing Subsequence
Topic: Array, Dynamic Programming, Binary Indexed Tree, Segment Tree
Difficulty: Medium
Problem:
Given an integer array
Notice that the sequence has to be strictly increasing.
Example 1:
Example 2:
Constraints:
•
•
673. Number of Longest Increasing Subsequence
Topic: Array, Dynamic Programming, Binary Indexed Tree, Segment Tree
Difficulty: Medium
Problem:
Given an integer array
nums, return the number of longest increasing subsequences.Notice that the sequence has to be strictly increasing.
Example 1:
Input: nums = [1,3,5,4,7]
Output: 2
Explanation: The two longest increasing subsequences are [1, 3, 4, 7] and [1, 3, 5, 7].
Example 2:
Input: nums = [2,2,2,2,2]
Output: 5
Explanation: The length of the longest increasing subsequence is 1, and there are 5 increasing subsequences of length 1, so output 5.
Constraints:
•
1 <= nums.length <= 2000•
-10^6 <= nums[i] <= 10^62023-07-22
688. Knight Probability in Chessboard
Topic: Dynamic Programming
Difficulty: Medium
Problem:
On an
A chess knight has eight possible moves it can make, as illustrated below. Each move is two cells in a cardinal direction, then one cell in an orthogonal direction.
Image: https://assets.leetcode.com/uploads/2018/10/12/knight.png
Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there.
The knight continues moving until it has made exactly
Return the probability that the knight remains on the board after it has stopped moving.
Example 1:
Example 2:
Constraints:
•
•
•
688. Knight Probability in Chessboard
Topic: Dynamic Programming
Difficulty: Medium
Problem:
On an
n x n chessboard, a knight starts at the cell (row, column) and attempts to make exactly k moves. The rows and columns are 0-indexed, so the top-left cell is (0, 0), and the bottom-right cell is (n - 1, n - 1).A chess knight has eight possible moves it can make, as illustrated below. Each move is two cells in a cardinal direction, then one cell in an orthogonal direction.
Image: https://assets.leetcode.com/uploads/2018/10/12/knight.png
Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there.
The knight continues moving until it has made exactly
k moves or has moved off the chessboard.Return the probability that the knight remains on the board after it has stopped moving.
Example 1:
Input: n = 3, k = 2, row = 0, column = 0
Output: 0.06250
Explanation: There are two moves (to (1,2), (2,1)) that will keep the knight on the board.
From each of those positions, there are also two moves that will keep the knight on the board.
The total probability the knight stays on the board is 0.0625.
Example 2:
Input: n = 1, k = 0, row = 0, column = 0
Output: 1.00000
Constraints:
•
1 <= n <= 25•
0 <= k <= 100•
0 <= row, column <= n - 12023-07-23
894. All Possible Full Binary Trees
Topic: Dynamic Programming, Tree, Recursion, Memoization, Binary Tree
Difficulty: Medium
Problem:
Given an integer
Each element of the answer is the root node of one possible tree. You may return the final list of trees in any order.
A full binary tree is a binary tree where each node has exactly
Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/08/22/fivetrees.png
Example 2:
Constraints:
•
894. All Possible Full Binary Trees
Topic: Dynamic Programming, Tree, Recursion, Memoization, Binary Tree
Difficulty: Medium
Problem:
Given an integer
n, return a list of all possible full binary trees with n nodes. Each node of each tree in the answer must have Node.val == 0.Each element of the answer is the root node of one possible tree. You may return the final list of trees in any order.
A full binary tree is a binary tree where each node has exactly
0 or 2 children.Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/08/22/fivetrees.png
Input: n = 7
Output: [[0,0,0,null,null,0,0,null,null,0,0],[0,0,0,null,null,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,null,null,null,null,0,0],[0,0,0,0,0,null,null,0,0]]
Example 2:
Input: n = 3
Output: [[0,0,0]]
Constraints:
•
1 <= n <= 202023-07-24
50. Pow(x, n)
Topic: Math, Recursion
Difficulty: Medium
Problem:
Implement pow(x, n), which calculates
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
• Either
•
50. Pow(x, n)
Topic: Math, Recursion
Difficulty: Medium
Problem:
Implement pow(x, n), which calculates
x raised to the power n (i.e., x^n).Example 1:
Input: x = 2.00000, n = 10
Output: 1024.00000
Example 2:
Input: x = 2.10000, n = 3
Output: 9.26100
Example 3:
Input: x = 2.00000, n = -2
Output: 0.25000
Explanation: 2^-2 = 1/2^2 = 1/4 = 0.25
Constraints:
•
-100.0 < x < 100.0•
-2^31 <= n <= 2^31-1•
n is an integer.• Either
x is not zero or n > 0.•
-10^4 <= x^n <= 10^42023-07-25
852. Peak Index in a Mountain Array
Topic: Array, Binary Search
Difficulty: Medium
Problem:
An array
•
• There exists some
•
•
Given a mountain array
You must solve it in
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
852. Peak Index in a Mountain Array
Topic: Array, Binary Search
Difficulty: Medium
Problem:
An array
arr a mountain if the following properties hold:•
arr.length >= 3• There exists some
i with 0 < i < arr.length - 1 such that:•
arr[0] < arr[1] < ... < arr[i - 1] < arr[i]•
arr[i] > arr[i + 1] > ... > arr[arr.length - 1]Given a mountain array
arr, return the index i such that arr[0] < arr[1] < ... < arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1].You must solve it in
O(log(arr.length)) time complexity.Example 1:
Input: arr = [0,1,0]
Output: 1
Example 2:
Input: arr = [0,2,1,0]
Output: 1
Example 3:
Input: arr = [0,10,5,2]
Output: 1
Constraints:
•
3 <= arr.length <= 10^5•
0 <= arr[i] <= 10^6•
arr is guaranteed to be a mountain array.2023-07-26
1870. Minimum Speed to Arrive on Time
Topic: Array, Binary Search
Difficulty: Medium
Problem:
You are given a floating-point number
Each train can only depart at an integer hour, so you may need to wait in between each train ride.
• For example, if the
Return the minimum positive integer speed (in kilometers per hour) that all the trains must travel at for you to reach the office on time, or
Tests are generated such that the answer will not exceed
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
• There will be at most two digits after the decimal point in
1870. Minimum Speed to Arrive on Time
Topic: Array, Binary Search
Difficulty: Medium
Problem:
You are given a floating-point number
hour, representing the amount of time you have to reach the office. To commute to the office, you must take n trains in sequential order. You are also given an integer array dist of length n, where dist[i] describes the distance (in kilometers) of the i^th train ride.Each train can only depart at an integer hour, so you may need to wait in between each train ride.
• For example, if the
1^st train ride takes 1.5 hours, you must wait for an additional 0.5 hours before you can depart on the 2^nd train ride at the 2 hour mark.Return the minimum positive integer speed (in kilometers per hour) that all the trains must travel at for you to reach the office on time, or
-1 if it is impossible to be on time.Tests are generated such that the answer will not exceed
10^7 and hour will have at most two digits after the decimal point.Example 1:
Input: dist = [1,3,2], hour = 6
Output: 1
Explanation: At speed 1:
- The first train ride takes 1/1 = 1 hour.
- Since we are already at an integer hour, we depart immediately at the 1 hour mark. The second train takes 3/1 = 3 hours.
- Since we are already at an integer hour, we depart immediately at the 4 hour mark. The third train takes 2/1 = 2 hours.
- You will arrive at exactly the 6 hour mark.
Example 2:
Input: dist = [1,3,2], hour = 2.7
Output: 3
Explanation: At speed 3:
- The first train ride takes 1/3 = 0.33333 hours.
- Since we are not at an integer hour, we wait until the 1 hour mark to depart. The second train ride takes 3/3 = 1 hour.
- Since we are already at an integer hour, we depart immediately at the 2 hour mark. The third train takes 2/3 = 0.66667 hours.
- You will arrive at the 2.66667 hour mark.
Example 3:
Input: dist = [1,3,2], hour = 1.9
Output: -1
Explanation: It is impossible because the earliest the third train can depart is at the 2 hour mark.
Constraints:
•
n == dist.length•
1 <= n <= 10^5•
1 <= dist[i] <= 10^5•
1 <= hour <= 10^9• There will be at most two digits after the decimal point in
hour.2023-07-27
2141. Maximum Running Time of N Computers
Topic: Array, Binary Search, Greedy, Sorting
Difficulty: Hard
Problem:
You have
Initially, you can insert at most one battery into each computer. After that and at any integer time moment, you can remove a battery from a computer and insert another battery any number of times. The inserted battery can be a totally new battery or a battery from another computer. You may assume that the removing and inserting processes take no time.
Note that the batteries cannot be recharged.
Return the maximum number of minutes you can run all the
Example 1:
Image: https://assets.leetcode.com/uploads/2022/01/06/example1-fit.png
Example 2:
Image: https://assets.leetcode.com/uploads/2022/01/06/example2.png
Constraints:
•
•
2141. Maximum Running Time of N Computers
Topic: Array, Binary Search, Greedy, Sorting
Difficulty: Hard
Problem:
You have
n computers. You are given the integer n and a 0-indexed integer array batteries where the i^th battery can run a computer for batteries[i] minutes. You are interested in running all n computers simultaneously using the given batteries.Initially, you can insert at most one battery into each computer. After that and at any integer time moment, you can remove a battery from a computer and insert another battery any number of times. The inserted battery can be a totally new battery or a battery from another computer. You may assume that the removing and inserting processes take no time.
Note that the batteries cannot be recharged.
Return the maximum number of minutes you can run all the
n computers simultaneously.Example 1:
Image: https://assets.leetcode.com/uploads/2022/01/06/example1-fit.png
Input: n = 2, batteries = [3,3,3]
Output: 4
Explanation:
Initially, insert battery 0 into the first computer and battery 1 into the second computer.
After two minutes, remove battery 1 from the second computer and insert battery 2 instead. Note that battery 1 can still run for one minute.
At the end of the third minute, battery 0 is drained, and you need to remove it from the first computer and insert battery 1 instead.
By the end of the fourth minute, battery 1 is also drained, and the first computer is no longer running.
We can run the two computers simultaneously for at most 4 minutes, so we return 4.
Example 2:
Image: https://assets.leetcode.com/uploads/2022/01/06/example2.png
Input: n = 2, batteries = [1,1,1,1]
Output: 2
Explanation:
Initially, insert battery 0 into the first computer and battery 2 into the second computer.
After one minute, battery 0 and battery 2 are drained so you need to remove them and insert battery 1 into the first computer and battery 3 into the second computer.
After another minute, battery 1 and battery 3 are also drained so the first and second computers are no longer running.
We can run the two computers simultaneously for at most 2 minutes, so we return 2.
Constraints:
•
1 <= n <= batteries.length <= 10^5•
1 <= batteries[i] <= 10^92023-07-28
486. Predict the Winner
Topic: Array, Math, Dynamic Programming, Recursion, Game Theory
Difficulty: Medium
Problem:
You are given an integer array
Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of
Return
Example 1:
Example 2:
Constraints:
•
•
486. Predict the Winner
Topic: Array, Math, Dynamic Programming, Recursion, Game Theory
Difficulty: Medium
Problem:
You are given an integer array
nums. Two players are playing a game with this array: player 1 and player 2.Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of
0. At each turn, the player takes one of the numbers from either end of the array (i.e., nums[0] or nums[nums.length - 1]) which reduces the size of the array by 1. The player adds the chosen number to their score. The game ends when there are no more elements in the array.Return
true if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return true. You may assume that both players are playing optimally.Example 1:
Input: nums = [1,5,2]
Output: false
Explanation: Initially, player 1 can choose between 1 and 2.
If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return false.
Example 2:
Input: nums = [1,5,233,7]
Output: true
Explanation: Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
Constraints:
•
1 <= nums.length <= 20•
0 <= nums[i] <= 10^72023-07-29
808. Soup Servings
Topic: Math, Dynamic Programming, Probability and Statistics
Difficulty: Medium
Problem:
There are two types of soup: type A and type B. Initially, we have
1. Serve
2. Serve
3. Serve
4. Serve
When we serve some soup, we give it to someone, and we no longer have it. Each turn, we will choose from the four operations with an equal probability
Note that we do not have an operation where all
Return the probability that soup A will be empty first, plus half the probability that A and B become empty at the same time. Answers within
Example 1:
Example 2:
Constraints:
•
808. Soup Servings
Topic: Math, Dynamic Programming, Probability and Statistics
Difficulty: Medium
Problem:
There are two types of soup: type A and type B. Initially, we have
n ml of each type of soup. There are four kinds of operations:1. Serve
100 ml of soup A and 0 ml of soup B,2. Serve
75 ml of soup A and 25 ml of soup B,3. Serve
50 ml of soup A and 50 ml of soup B, and4. Serve
25 ml of soup A and 75 ml of soup B.When we serve some soup, we give it to someone, and we no longer have it. Each turn, we will choose from the four operations with an equal probability
0.25. If the remaining volume of soup is not enough to complete the operation, we will serve as much as possible. We stop once we no longer have some quantity of both types of soup.Note that we do not have an operation where all
100 ml's of soup B are used first.Return the probability that soup A will be empty first, plus half the probability that A and B become empty at the same time. Answers within
10^-5 of the actual answer will be accepted.Example 1:
Input: n = 50
Output: 0.62500
Explanation: If we choose the first two operations, A will become empty first.
For the third operation, A and B will become empty at the same time.
For the fourth operation, B will become empty first.
So the total probability of A becoming empty first plus half the probability that A and B become empty at the same time, is 0.25 * (1 + 1 + 0.5 + 0) = 0.625.
Example 2:
Input: n = 100
Output: 0.71875
Constraints:
•
0 <= n <= 10^92023-07-30
664. Strange Printer
Topic: String, Dynamic Programming
Difficulty: Hard
Problem:
There is a strange printer with the following two special properties:
• The printer can only print a sequence of the same character each time.
• At each turn, the printer can print new characters starting from and ending at any place and will cover the original existing characters.
Given a string
Example 1:
Example 2:
Constraints:
•
•
664. Strange Printer
Topic: String, Dynamic Programming
Difficulty: Hard
Problem:
There is a strange printer with the following two special properties:
• The printer can only print a sequence of the same character each time.
• At each turn, the printer can print new characters starting from and ending at any place and will cover the original existing characters.
Given a string
s, return the minimum number of turns the printer needed to print it.Example 1:
Input: s = "aaabbb"
Output: 2
Explanation: Print "aaa" first and then print "bbb".
Example 2:
Input: s = "aba"
Output: 2
Explanation: Print "aaa" first and then print "b" from the second place of the string, which will cover the existing character 'a'.
Constraints:
•
1 <= s.length <= 100•
s consists of lowercase English letters.2023-07-31
712. Minimum ASCII Delete Sum for Two Strings
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
Given two strings
Example 1:
Example 2:
Constraints:
•
•
712. Minimum ASCII Delete Sum for Two Strings
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
Given two strings
s1 and s2, return the lowest ASCII sum of deleted characters to make two strings equal.Example 1:
Input: s1 = "sea", s2 = "eat"
Output: 231
Explanation: Deleting "s" from "sea" adds the ASCII value of "s" (115) to the sum.
Deleting "t" from "eat" adds 116 to the sum.
At the end, both strings are equal, and 115 + 116 = 231 is the minimum sum possible to achieve this.
Example 2:
Input: s1 = "delete", s2 = "leet"
Output: 403
Explanation: Deleting "dee" from "delete" to turn the string into "let",
adds 100[d] + 101[e] + 101[e] to the sum.
Deleting "e" from "leet" adds 101[e] to the sum.
At the end, both strings are equal to "let", and the answer is 100+101+101+101 = 403.
If instead we turned both strings into "lee" or "eet", we would get answers of 433 or 417, which are higher.
Constraints:
•
1 <= s1.length, s2.length <= 1000•
s1 and s2 consist of lowercase English letters.2023-08-01
77. Combinations
Topic: Backtracking
Difficulty: Medium
Problem:
Given two integers
You may return the answer in any order.
Example 1:
Example 2:
Constraints:
•
•
77. Combinations
Topic: Backtracking
Difficulty: Medium
Problem:
Given two integers
n and k, return all possible combinations of k numbers chosen from the range [1, n].You may return the answer in any order.
Example 1:
Input: n = 4, k = 2
Output: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
Explanation: There are 4 choose 2 = 6 total combinations.
Note that combinations are unordered, i.e., [1,2] and [2,1] are considered to be the same combination.
Example 2:
Input: n = 1, k = 1
Output: [[1]]
Explanation: There is 1 choose 1 = 1 total combination.
Constraints:
•
1 <= n <= 20•
1 <= k <= n2023-08-02
46. Permutations
Topic: Array, Backtracking
Difficulty: Medium
Problem:
Given an array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
• All the integers of
46. Permutations
Topic: Array, Backtracking
Difficulty: Medium
Problem:
Given an array
nums of distinct integers, return all the possible permutations. You can return the answer in any order.Example 1:
Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Example 2:
Input: nums = [0,1]
Output: [[0,1],[1,0]]
Example 3:
Input: nums = [1]
Output: [[1]]
Constraints:
•
1 <= nums.length <= 6•
-10 <= nums[i] <= 10• All the integers of
nums are unique.2023-08-03
17. Letter Combinations of a Phone Number
Topic: Hash Table, String, Backtracking
Difficulty: Medium
Problem:
Given a string containing digits from
A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Image: https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png
Example 1:
Example 2:
Example 3:
Constraints:
•
•
17. Letter Combinations of a Phone Number
Topic: Hash Table, String, Backtracking
Difficulty: Medium
Problem:
Given a string containing digits from
2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Image: https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png
Example 1:
Input: digits = "23"
Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
Example 2:
Input: digits = ""
Output: []
Example 3:
Input: digits = "2"
Output: ["a","b","c"]
Constraints:
•
0 <= digits.length <= 4•
digits[i] is a digit in the range ['2', '9'].2023-08-04
139. Word Break
Topic: Array, Hash Table, String, Dynamic Programming, Trie, Memoization
Difficulty: Medium
Problem:
Given a string
Note that the same word in the dictionary may be reused multiple times in the segmentation.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
• All the strings of
139. Word Break
Topic: Array, Hash Table, String, Dynamic Programming, Trie, Memoization
Difficulty: Medium
Problem:
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:
Input: s = "leetcode", wordDict = ["leet","code"]
Output: true
Explanation: Return true because "leetcode" can be segmented as "leet code".
Example 2:
Input: s = "applepenapple", wordDict = ["apple","pen"]
Output: true
Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
Note that you are allowed to reuse a dictionary word.
Example 3:
Input: s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
Output: false
Constraints:
•
1 <= s.length <= 300•
1 <= wordDict.length <= 1000•
1 <= wordDict[i].length <= 20•
s and wordDict[i] consist of only lowercase English letters.• All the strings of
wordDict are unique.2023-08-05
95. Unique Binary Search Trees II
Topic: Dynamic Programming, Backtracking, Tree, Binary Search Tree, Binary Tree
Difficulty: Medium
Problem:
Given an integer
Example 1:
Image: https://assets.leetcode.com/uploads/2021/01/18/uniquebstn3.jpg
Example 2:
Constraints:
•
95. Unique Binary Search Trees II
Topic: Dynamic Programming, Backtracking, Tree, Binary Search Tree, Binary Tree
Difficulty: Medium
Problem:
Given an integer
n, return all the structurally unique BST's (binary search trees), which has exactly n nodes of unique values from 1 to n. Return the answer in any order.Example 1:
Image: https://assets.leetcode.com/uploads/2021/01/18/uniquebstn3.jpg
Input: n = 3
Output: [[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]
Example 2:
Input: n = 1
Output: [[1]]
Constraints:
•
1 <= n <= 8