2022-12-14
198. House Robber
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
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
Example 1:
Example 2:
Constraints:
•
•
198. House Robber
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
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:
Input: nums = [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.
Example 2:
Input: nums = [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
Total amount you can rob = 2 + 9 + 1 = 12.
Constraints:
•
1 <= nums.length <= 100•
0 <= nums[i] <= 4002022-12-15
1143. Longest Common Subsequence
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
Given two strings
A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
• For example,
A common subsequence of two strings is a subsequence that is common to both strings.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1143. Longest Common Subsequence
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
Given two strings
text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
• For example,
"ace" is a subsequence of "abcde".A common subsequence of two strings is a subsequence that is common to both strings.
Example 1:
Input: text1 = "abcde", text2 = "ace"
Output: 3
Explanation: The longest common subsequence is "ace" and its length is 3.
Example 2:
Input: text1 = "abc", text2 = "abc"
Output: 3
Explanation: The longest common subsequence is "abc" and its length is 3.
Example 3:
Input: text1 = "abc", text2 = "def"
Output: 0
Explanation: There is no such common subsequence, so the result is 0.
Constraints:
•
1 <= text1.length, text2.length <= 1000•
text1 and text2 consist of only lowercase English characters.2022-12-16
232. Implement Queue using Stacks
Topic: Stack, Design, Queue
Difficulty: Easy
Problem:
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (
Implement the
•
•
•
•
Notes:
• You must use only standard operations of a stack, which means only
• Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack's standard operations.
Example 1:
Constraints:
•
• At most
• All the calls to
Follow-up: Can you implement the queue such that each operation is amortized
232. Implement Queue using Stacks
Topic: Stack, Design, Queue
Difficulty: Easy
Problem:
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (
push, peek, pop, and empty).Implement the
MyQueue class:•
void push(int x) Pushes element x to the back of the queue.•
int pop() Removes the element from the front of the queue and returns it.•
int peek() Returns the element at the front of the queue.•
boolean empty() Returns true if the queue is empty, false otherwise.Notes:
• You must use only standard operations of a stack, which means only
push to top, peek/pop from top, size, and is empty operations are valid.• Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack's standard operations.
Example 1:
Input
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
Output
[null, null, null, 1, 1, false]
Explanation
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false
Constraints:
•
1 <= x <= 9• At most
100 calls will be made to push, pop, peek, and empty.• All the calls to
pop and peek are valid.Follow-up: Can you implement the queue such that each operation is amortized
O(1) time complexity? In other words, performing n operations will take overall O(n) time even if one of those operations may take longer.2022-12-17
150. Evaluate Reverse Polish Notation
Topic: Array, Math, Stack
Difficulty: Medium
Problem:
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are
Note that division between two integers should truncate toward zero.
It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
150. Evaluate Reverse Polish Notation
Topic: Array, Math, Stack
Difficulty: Medium
Problem:
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are
+, -, *, and /. Each operand may be an integer or another expression.Note that division between two integers should truncate toward zero.
It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation.
Example 1:
Input: tokens = ["2","1","+","3","*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9
Example 2:
Input: tokens = ["4","13","5","/","+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6
Example 3:
Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
Output: 22
Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
Constraints:
•
1 <= tokens.length <= 10^4•
tokens[i] is either an operator: "+", "-", "*", or "/", or an integer in the range [-200, 200].2022-12-18
739. Daily Temperatures
Topic: Array, Stack, Monotonic Stack
Difficulty: Medium
Problem:
Given an array of integers
Example 1:
Example 2:
Example 3:
Constraints:
•
•
739. Daily Temperatures
Topic: Array, Stack, Monotonic Stack
Difficulty: Medium
Problem:
Given an array of integers
temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the i^th day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead.Example 1:
Input: temperatures = [73,74,75,71,69,72,76,73]
Output: [1,1,4,2,1,1,0,0]
Example 2:
Input: temperatures = [30,40,50,60]
Output: [1,1,1,0]
Example 3:
Input: temperatures = [30,60,90]
Output: [1,1,0]
Constraints:
•
1 <= temperatures.length <= 10^5•
30 <= temperatures[i] <= 1002022-12-19
1971. Find if Path Exists in Graph
Topic: Depth-First Search, Breadth-First Search, Union Find, Graph
Difficulty: Easy
Problem:
There is a bi-directional graph with
You want to determine if there is a valid path that exists from vertex
Given
Example 1:
Image: https://assets.leetcode.com/uploads/2021/08/14/validpath-ex1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2021/08/14/validpath-ex2.png
Constraints:
•
•
•
•
•
•
• There are no duplicate edges.
• There are no self edges.
1971. Find if Path Exists in Graph
Topic: Depth-First Search, Breadth-First Search, Union Find, Graph
Difficulty: Easy
Problem:
There is a bi-directional graph with
n vertices, where each vertex is labeled from 0 to n - 1 (inclusive). The edges in the graph are represented as a 2D integer array edges, where each edges[i] = [u_i, v_i] denotes a bi-directional edge between vertex u_i and vertex v_i. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself.You want to determine if there is a valid path that exists from vertex
source to vertex destination.Given
edges and the integers n, source, and destination, return true if there is a valid path from source to destination, or false otherwise.Example 1:
Image: https://assets.leetcode.com/uploads/2021/08/14/validpath-ex1.png
Input: n = 3, edges = [[0,1],[1,2],[2,0]], source = 0, destination = 2
Output: true
Explanation: There are two paths from vertex 0 to vertex 2:
- 0 → 1 → 2
- 0 → 2
Example 2:
Image: https://assets.leetcode.com/uploads/2021/08/14/validpath-ex2.png
Input: n = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], source = 0, destination = 5
Output: false
Explanation: There is no path from vertex 0 to vertex 5.
Constraints:
•
1 <= n <= 2 * 10^5•
0 <= edges.length <= 2 * 10^5•
edges[i].length == 2•
0 <= u_i, v_i <= n - 1•
u_i != v_i•
0 <= source, destination <= n - 1• There are no duplicate edges.
• There are no self edges.
2022-12-20
841. Keys and Rooms
Topic: Depth-First Search, Breadth-First Search, Graph
Difficulty: Medium
Problem:
There are
When you visit a room, you may find a set of distinct keys in it. Each key has a number on it, denoting which room it unlocks, and you can take all of them with you to unlock the other rooms.
Given an array
Example 1:
Example 2:
Constraints:
•
•
•
•
•
• All the values of
841. Keys and Rooms
Topic: Depth-First Search, Breadth-First Search, Graph
Difficulty: Medium
Problem:
There are
n rooms labeled from 0 to n - 1 and all the rooms are locked except for room 0. Your goal is to visit all the rooms. However, you cannot enter a locked room without having its key.When you visit a room, you may find a set of distinct keys in it. Each key has a number on it, denoting which room it unlocks, and you can take all of them with you to unlock the other rooms.
Given an array
rooms where rooms[i] is the set of keys that you can obtain if you visited room i, return true if you can visit all the rooms, or false otherwise.Example 1:
Input: rooms = [[1],[2],[3],[]]
Output: true
Explanation:
We visit room 0 and pick up key 1.
We then visit room 1 and pick up key 2.
We then visit room 2 and pick up key 3.
We then visit room 3.
Since we were able to visit every room, we return true.
Example 2:
Input: rooms = [[1,3],[3,0,1],[2],[0]]
Output: false
Explanation: We can not enter room number 2 since the only key that unlocks it is in that room.
Constraints:
•
n == rooms.length•
2 <= n <= 1000•
0 <= rooms[i].length <= 1000•
1 <= sum(rooms[i].length) <= 3000•
0 <= rooms[i][j] < n• All the values of
rooms[i] are unique.2022-12-21
886. Possible Bipartition
Topic: Depth-First Search, Breadth-First Search, Union Find, Graph
Difficulty: Medium
Problem:
We want to split a group of
Given the integer
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
•
• All the pairs of
886. Possible Bipartition
Topic: Depth-First Search, Breadth-First Search, Union Find, Graph
Difficulty: Medium
Problem:
We want to split a group of
n people (labeled from 1 to n) into two groups of any size. Each person may dislike some other people, and they should not go into the same group.Given the integer
n and the array dislikes where dislikes[i] = [a_i, b_i] indicates that the person labeled a_i does not like the person labeled b_i, return true if it is possible to split everyone into two groups in this way.Example 1:
Input: n = 4, dislikes = [[1,2],[1,3],[2,4]]
Output: true
Explanation: group1 [1,4] and group2 [2,3].
Example 2:
Input: n = 3, dislikes = [[1,2],[1,3],[2,3]]
Output: false
Example 3:
Input: n = 5, dislikes = [[1,2],[2,3],[3,4],[4,5],[1,5]]
Output: false
Constraints:
•
1 <= n <= 2000•
0 <= dislikes.length <= 10^4•
dislikes[i].length == 2•
1 <= dislikes[i][j] <= n•
a_i < b_i• All the pairs of
dislikes are unique.2022-12-22
834. Sum of Distances in Tree
Topic: Dynamic Programming, Tree, Depth-First Search, Graph
Difficulty: Hard
Problem:
There is an undirected connected tree with
You are given the integer
Return an array
Example 1:
Image: https://assets.leetcode.com/uploads/2021/07/23/lc-sumdist1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/07/23/lc-sumdist2.jpg
Example 3:
Image: https://assets.leetcode.com/uploads/2021/07/23/lc-sumdist3.jpg
Constraints:
•
•
•
•
•
• The given input represents a valid tree.
834. Sum of Distances in Tree
Topic: Dynamic Programming, Tree, Depth-First Search, Graph
Difficulty: Hard
Problem:
There is an undirected connected tree with
n nodes labeled from 0 to n - 1 and n - 1 edges.You are given the integer
n and the array edges where edges[i] = [a_i, b_i] indicates that there is an edge between nodes a_i and b_i in the tree.Return an array
answer of length n where answer[i] is the sum of the distances between the i^th node in the tree and all other nodes.Example 1:
Image: https://assets.leetcode.com/uploads/2021/07/23/lc-sumdist1.jpg
Input: n = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]]
Output: [8,12,6,10,10,10]
Explanation: The tree is shown above.
We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5)
equals 1 + 1 + 2 + 2 + 2 = 8.
Hence, answer[0] = 8, and so on.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/07/23/lc-sumdist2.jpg
Input: n = 1, edges = []
Output: [0]
Example 3:
Image: https://assets.leetcode.com/uploads/2021/07/23/lc-sumdist3.jpg
Input: n = 2, edges = [[1,0]]
Output: [1,1]
Constraints:
•
1 <= n <= 3 * 10^4•
edges.length == n - 1•
edges[i].length == 2•
0 <= a_i, b_i < n•
a_i != b_i• The given input represents a valid tree.
2022-12-23
309. Best Time to Buy and Sell Stock with Cooldown
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
You are given an array
Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions:
• After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day).
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
Example 1:
Example 2:
Constraints:
•
•
309. Best Time to Buy and Sell Stock with Cooldown
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
You are given an array
prices where prices[i] is the price of a given stock on the i^th day.Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions:
• After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day).
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
Example 1:
Input: prices = [1,2,3,0,2]
Output: 3
Explanation: transactions = [buy, sell, cooldown, buy, sell]
Example 2:
Input: prices = [1]
Output: 0
Constraints:
•
1 <= prices.length <= 5000•
0 <= prices[i] <= 10002022-12-24
790. Domino and Tromino Tiling
Topic: Dynamic Programming
Difficulty: Medium
Problem:
You have two types of tiles: a
Image: https://assets.leetcode.com/uploads/2021/07/15/lc-domino.jpg
Given an integer n, return the number of ways to tile an
In a tiling, every square must be covered by a tile. Two tilings are different if and only if there are two 4-directionally adjacent cells on the board such that exactly one of the tilings has both squares occupied by a tile.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/07/15/lc-domino1.jpg
Example 2:
Constraints:
•
790. Domino and Tromino Tiling
Topic: Dynamic Programming
Difficulty: Medium
Problem:
You have two types of tiles: a
2 x 1 domino shape and a tromino shape. You may rotate these shapes.Image: https://assets.leetcode.com/uploads/2021/07/15/lc-domino.jpg
Given an integer n, return the number of ways to tile an
2 x n board. Since the answer may be very large, return it modulo 10^9 + 7.In a tiling, every square must be covered by a tile. Two tilings are different if and only if there are two 4-directionally adjacent cells on the board such that exactly one of the tilings has both squares occupied by a tile.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/07/15/lc-domino1.jpg
Input: n = 3
Output: 5
Explanation: The five different ways are show above.
Example 2:
Input: n = 1
Output: 1
Constraints:
•
1 <= n <= 10002022-12-25
2389. Longest Subsequence With Limited Sum
Topic: Array, Binary Search, Greedy, Sorting, Prefix Sum
Difficulty: Easy
Problem:
You are given an integer array
Return an array
A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Example 2:
Constraints:
•
•
•
•
2389. Longest Subsequence With Limited Sum
Topic: Array, Binary Search, Greedy, Sorting, Prefix Sum
Difficulty: Easy
Problem:
You are given an integer array
nums of length n, and an integer array queries of length m.Return an array
answer of length m where answer[i] is the maximum size of a subsequence that you can take from nums such that the sum of its elements is less than or equal to queries[i].A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Input: nums = [4,5,2,1], queries = [3,10,21]
Output: [2,3,4]
Explanation: We answer the queries as follows:
- The subsequence [2,1] has a sum less than or equal to 3. It can be proven that 2 is the maximum size of such a subsequence, so answer[0] = 2.
- The subsequence [4,5,1] has a sum less than or equal to 10. It can be proven that 3 is the maximum size of such a subsequence, so answer[1] = 3.
- The subsequence [4,5,2,1] has a sum less than or equal to 21. It can be proven that 4 is the maximum size of such a subsequence, so answer[2] = 4.
Example 2:
Input: nums = [2,3,4,5], queries = [1]
Output: [0]
Explanation: The empty subsequence is the only subsequence that has a sum less than or equal to 1, so answer[0] = 0.
Constraints:
•
n == nums.length•
m == queries.length•
1 <= n, m <= 1000•
1 <= nums[i], queries[i] <= 10^62022-12-26
55. Jump Game
Topic: Array, Dynamic Programming, Greedy
Difficulty: Medium
Problem:
You are given an integer array
Return
Example 1:
Example 2:
Constraints:
•
•
55. Jump Game
Topic: Array, Dynamic Programming, Greedy
Difficulty: Medium
Problem:
You are given an integer array
nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.Return
true if you can reach the last index, or false otherwise.Example 1:
Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
Input: nums = [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
Constraints:
•
1 <= nums.length <= 10^4•
0 <= nums[i] <= 10^52022-12-27
2279. Maximum Bags With Full Capacity of Rocks
Topic: Array, Greedy, Sorting
Difficulty: Medium
Problem:
You have
Return the maximum number of bags that could have full capacity after placing the additional rocks in some bags.
Example 1:
Example 2:
Constraints:
•
•
•
•
•
2279. Maximum Bags With Full Capacity of Rocks
Topic: Array, Greedy, Sorting
Difficulty: Medium
Problem:
You have
n bags numbered from 0 to n - 1. You are given two 0-indexed integer arrays capacity and rocks. The i^th bag can hold a maximum of capacity[i] rocks and currently contains rocks[i] rocks. You are also given an integer additionalRocks, the number of additional rocks you can place in any of the bags.Return the maximum number of bags that could have full capacity after placing the additional rocks in some bags.
Example 1:
Input: capacity = [2,3,4,5], rocks = [1,2,4,4], additionalRocks = 2
Output: 3
Explanation:
Place 1 rock in bag 0 and 1 rock in bag 1.
The number of rocks in each bag are now [2,3,4,4].
Bags 0, 1, and 2 have full capacity.
There are 3 bags at full capacity, so we return 3.
It can be shown that it is not possible to have more than 3 bags at full capacity.
Note that there may be other ways of placing the rocks that result in an answer of 3.
Example 2:
Input: capacity = [10,2,2], rocks = [2,2,0], additionalRocks = 100
Output: 3
Explanation:
Place 8 rocks in bag 0 and 2 rocks in bag 2.
The number of rocks in each bag are now [10,2,2].
Bags 0, 1, and 2 have full capacity.
There are 3 bags at full capacity, so we return 3.
It can be shown that it is not possible to have more than 3 bags at full capacity.
Note that we did not use all of the additional rocks.
Constraints:
•
n == capacity.length == rocks.length•
1 <= n <= 5 * 10^4•
1 <= capacity[i] <= 10^9•
0 <= rocks[i] <= capacity[i]•
1 <= additionalRocks <= 10^92022-12-28
1962. Remove Stones to Minimize the Total
Topic: Array, Heap (Priority Queue)
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
• Choose any
Notice that you can apply the operation on the same pile more than once.
Return the minimum possible total number of stones remaining after applying the
Example 1:
Example 2:
Constraints:
•
•
•
1962. Remove Stones to Minimize the Total
Topic: Array, Heap (Priority Queue)
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
piles, where piles[i] represents the number of stones in the i^th pile, and an integer k. You should apply the following operation exactly k times:• Choose any
piles[i] and remove floor(piles[i] / 2) stones from it.Notice that you can apply the operation on the same pile more than once.
Return the minimum possible total number of stones remaining after applying the
k operations.floor(x) is the greatest integer that is smaller than or equal to x (i.e., rounds x down).Example 1:
Input: piles = [5,4,9], k = 2
Output: 12
Explanation: Steps of a possible scenario are:
- Apply the operation on pile 2. The resulting piles are [5,4,5].
- Apply the operation on pile 0. The resulting piles are [3,4,5].
The total number of stones in [3,4,5] is 12.
Example 2:
Input: piles = [4,3,6,7], k = 3
Output: 12
Explanation: Steps of a possible scenario are:
- Apply the operation on pile 2. The resulting piles are [4,3,3,7].
- Apply the operation on pile 3. The resulting piles are [4,3,3,4].
- Apply the operation on pile 0. The resulting piles are [2,3,3,4].
The total number of stones in [2,3,3,4] is 12.
Constraints:
•
1 <= piles.length <= 10^5•
1 <= piles[i] <= 10^4•
1 <= k <= 10^52022-12-29
1834. Single-Threaded CPU
Topic: Array, Sorting, Heap (Priority Queue)
Difficulty: Medium
Problem:
You are given
You have a single-threaded CPU that can process at most one task at a time and will act in the following way:
• If the CPU is idle and there are no available tasks to process, the CPU remains idle.
• If the CPU is idle and there are available tasks, the CPU will choose the one with the shortest processing time. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.
• Once a task is started, the CPU will process the entire task without stopping.
• The CPU can finish a task then start a new one instantly.
Return the order in which the CPU will process the tasks.
Example 1:
Example 2:
Constraints:
•
•
•
1834. Single-Threaded CPU
Topic: Array, Sorting, Heap (Priority Queue)
Difficulty: Medium
Problem:
You are given
n tasks labeled from 0 to n - 1 represented by a 2D integer array tasks, where tasks[i] = [enqueueTime_i, processingTime_i] means that the i^th task will be available to process at enqueueTime_i and will take processingTime_i to finish processing.You have a single-threaded CPU that can process at most one task at a time and will act in the following way:
• If the CPU is idle and there are no available tasks to process, the CPU remains idle.
• If the CPU is idle and there are available tasks, the CPU will choose the one with the shortest processing time. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.
• Once a task is started, the CPU will process the entire task without stopping.
• The CPU can finish a task then start a new one instantly.
Return the order in which the CPU will process the tasks.
Example 1:
Input: tasks = [[1,2],[2,4],[3,2],[4,1]]
Output: [0,2,3,1]
Explanation: The events go as follows:
- At time = 1, task 0 is available to process. Available tasks = {0}.
- Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}.
- At time = 2, task 1 is available to process. Available tasks = {1}.
- At time = 3, task 2 is available to process. Available tasks = {1, 2}.
- Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}.
- At time = 4, task 3 is available to process. Available tasks = {1, 3}.
- At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}.
- At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}.
- At time = 10, the CPU finishes task 1 and becomes idle.
Example 2:
Input: tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]]
Output: [4,3,2,0,1]
Explanation: The events go as follows:
- At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}.
- Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}.
- At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}.
- At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}.
- At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}.
- At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}.
- At time = 40, the CPU finishes task 1 and becomes idle.
Constraints:
•
tasks.length == n•
1 <= n <= 10^5•
1 <= enqueueTime_i, processingTime_i <= 10^92022-12-30
797. All Paths From Source to Target
Topic: Backtracking, Depth-First Search, Breadth-First Search, Graph
Difficulty: Medium
Problem:
Given a directed acyclic graph (DAG) of
The graph is given as follows:
Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/28/all_1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/09/28/all_2.jpg
Constraints:
•
•
•
•
• All the elements of
• The input graph is guaranteed to be a DAG.
797. All Paths From Source to Target
Topic: Backtracking, Depth-First Search, Breadth-First Search, Graph
Difficulty: Medium
Problem:
Given a directed acyclic graph (DAG) of
n nodes labeled from 0 to n - 1, find all possible paths from node 0 to node n - 1 and return them in any order.The graph is given as follows:
graph[i] is a list of all nodes you can visit from node i (i.e., there is a directed edge from node i to node graph[i][j]).Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/28/all_1.jpg
Input: graph = [[1,2],[3],[3],[]]
Output: [[0,1,3],[0,2,3]]
Explanation: There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/09/28/all_2.jpg
Input: graph = [[4,3,1],[3,2,4],[3],[4],[]]
Output: [[0,4],[0,3,4],[0,1,3,4],[0,1,2,3,4],[0,1,4]]
Constraints:
•
n == graph.length•
2 <= n <= 15•
0 <= graph[i][j] < n•
graph[i][j] != i (i.e., there will be no self-loops).• All the elements of
graph[i] are unique.• The input graph is guaranteed to be a DAG.
2022-12-31
980. Unique Paths III
Topic: Array, Backtracking, Bit Manipulation, Matrix
Difficulty: Hard
Problem:
You are given an
•
•
•
•
Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/08/02/lc-unique1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/08/02/lc-unique2.jpg
Example 3:
Image: https://assets.leetcode.com/uploads/2021/08/02/lc-unique3-.jpg
Constraints:
•
•
•
•
•
• There is exactly one starting cell and one ending cell.
980. Unique Paths III
Topic: Array, Backtracking, Bit Manipulation, Matrix
Difficulty: Hard
Problem:
You are given an
m x n integer array grid where grid[i][j] could be:•
1 representing the starting square. There is exactly one starting square.•
2 representing the ending square. There is exactly one ending square.•
0 representing empty squares we can walk over.•
-1 representing obstacles that we cannot walk over.Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/08/02/lc-unique1.jpg
Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output: 2
Explanation: We have the following two paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
Example 2:
Image: https://assets.leetcode.com/uploads/2021/08/02/lc-unique2.jpg
Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output: 4
Explanation: We have the following four paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
Example 3:
Image: https://assets.leetcode.com/uploads/2021/08/02/lc-unique3-.jpg
Input: grid = [[0,1],[2,0]]
Output: 0
Explanation: There is no path that walks over every empty square exactly once.
Note that the starting and ending square can be anywhere in the grid.
Constraints:
•
m == grid.length•
n == grid[i].length•
1 <= m, n <= 20•
1 <= m * n <= 20•
-1 <= grid[i][j] <= 2• There is exactly one starting cell and one ending cell.
2023-01-01
290. Word Pattern
Topic: Hash Table, String
Difficulty: Easy
Problem:
Given a
Here follow means a full match, such that there is a bijection between a letter in
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
•
• All the words in
290. Word Pattern
Topic: Hash Table, String
Difficulty: Easy
Problem:
Given a
pattern and a string s, find if s follows the same pattern.Here follow means a full match, such that there is a bijection between a letter in
pattern and a non-empty word in s.Example 1:
Input: pattern = "abba", s = "dog cat cat dog"
Output: true
Example 2:
Input: pattern = "abba", s = "dog cat cat fish"
Output: false
Example 3:
Input: pattern = "aaaa", s = "dog cat cat dog"
Output: false
Constraints:
•
1 <= pattern.length <= 300•
pattern contains only lower-case English letters.•
1 <= s.length <= 3000•
s contains only lowercase English letters and spaces ' '.•
s does not contain any leading or trailing spaces.• All the words in
s are separated by a single space.2023-01-02
520. Detect Capital
Topic: String
Difficulty: Easy
Problem:
We define the usage of capitals in a word to be right when one of the following cases holds:
• All letters in this word are capitals, like
• All letters in this word are not capitals, like
• Only the first letter in this word is capital, like
Given a string
Example 1:
Example 2:
Constraints:
•
•
520. Detect Capital
Topic: String
Difficulty: Easy
Problem:
We define the usage of capitals in a word to be right when one of the following cases holds:
• All letters in this word are capitals, like
"USA".• All letters in this word are not capitals, like
"leetcode".• Only the first letter in this word is capital, like
"Google".Given a string
word, return true if the usage of capitals in it is right.Example 1:
Input: word = "USA"
Output: true
Example 2:
Input: word = "FlaG"
Output: false
Constraints:
•
1 <= word.length <= 100•
word consists of lowercase and uppercase English letters.