2023-01-17
926. Flip String to Monotone Increasing
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
A binary string is monotone increasing if it consists of some number of
You are given a binary string
Return the minimum number of flips to make
Example 1:
Example 2:
Example 3:
Constraints:
•
•
926. Flip String to Monotone Increasing
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
A binary string is monotone increasing if it consists of some number of
0's (possibly none), followed by some number of 1's (also possibly none).You are given a binary string
s. You can flip s[i] changing it from 0 to 1 or from 1 to 0.Return the minimum number of flips to make
s monotone increasing.Example 1:
Input: s = "00110"
Output: 1
Explanation: We flip the last digit to get 00111.
Example 2:
Input: s = "010110"
Output: 2
Explanation: We flip to get 011111, or alternatively 000111.
Example 3:
Input: s = "00011000"
Output: 2
Explanation: We flip to get 00000000.
Constraints:
•
1 <= s.length <= 10^5•
s[i] is either '0' or '1'.2023-01-18
918. Maximum Sum Circular Subarray
Topic: Array, Divide and Conquer, Dynamic Programming, Queue, Monotonic Queue
Difficulty: Medium
Problem:
Given a circular integer array
A circular array means the end of the array connects to the beginning of the array. Formally, the next element of
A subarray may only include each element of the fixed buffer
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
918. Maximum Sum Circular Subarray
Topic: Array, Divide and Conquer, Dynamic Programming, Queue, Monotonic Queue
Difficulty: Medium
Problem:
Given a circular integer array
nums of length n, return the maximum possible sum of a non-empty subarray of nums.A circular array means the end of the array connects to the beginning of the array. Formally, the next element of
nums[i] is nums[(i + 1) % n] and the previous element of nums[i] is nums[(i - 1 + n) % n].A subarray may only include each element of the fixed buffer
nums at most once. Formally, for a subarray nums[i], nums[i + 1], ..., nums[j], there does not exist i <= k1, k2 <= j with k1 % n == k2 % n.Example 1:
Input: nums = [1,-2,3,-2]
Output: 3
Explanation: Subarray [3] has maximum sum 3.
Example 2:
Input: nums = [5,-3,5]
Output: 10
Explanation: Subarray [5,5] has maximum sum 5 + 5 = 10.
Example 3:
Input: nums = [-3,-2,-3]
Output: -2
Explanation: Subarray [-2] has maximum sum -2.
Constraints:
•
n == nums.length•
1 <= n <= 3 * 10^4•
-3 * 10^4 <= nums[i] <= 3 * 10^42023-01-19
974. Subarray Sums Divisible by K
Topic: Array, Hash Table, Prefix Sum
Difficulty: Medium
Problem:
Given an integer array
A subarray is a contiguous part of an array.
Example 1:
Example 2:
Constraints:
•
•
•
974. Subarray Sums Divisible by K
Topic: Array, Hash Table, Prefix Sum
Difficulty: Medium
Problem:
Given an integer array
nums and an integer k, return the number of non-empty subarrays that have a sum divisible by k.A subarray is a contiguous part of an array.
Example 1:
Input: nums = [4,5,0,-2,-3,1], k = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by k = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Example 2:
Input: nums = [5], k = 9
Output: 0
Constraints:
•
1 <= nums.length <= 3 * 10^4•
-10^4 <= nums[i] <= 10^4•
2 <= k <= 10^42023-01-20
491. Non-decreasing Subsequences
Topic: Array, Hash Table, Backtracking, Bit Manipulation
Difficulty: Medium
Problem:
Given an integer array
Example 1:
Example 2:
Constraints:
•
•
491. Non-decreasing Subsequences
Topic: Array, Hash Table, Backtracking, Bit Manipulation
Difficulty: Medium
Problem:
Given an integer array
nums, return all the different possible non-decreasing subsequences of the given array with at least two elements. You may return the answer in any order.Example 1:
Input: nums = [4,6,7,7]
Output: [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
Example 2:
Input: nums = [4,4,3,2,1]
Output: [[4,4]]
Constraints:
•
1 <= nums.length <= 15•
-100 <= nums[i] <= 1002023-01-21
93. Restore IP Addresses
Topic: String, Backtracking
Difficulty: Medium
Problem:
A valid IP address consists of exactly four integers separated by single dots. Each integer is between
• For example,
Given a string
Example 1:
Example 2:
Example 3:
Constraints:
•
•
93. Restore IP Addresses
Topic: String, Backtracking
Difficulty: Medium
Problem:
A valid IP address consists of exactly four integers separated by single dots. Each integer is between
0 and 255 (inclusive) and cannot have leading zeros.• For example,
"0.1.2.201" and "192.168.1.1" are valid IP addresses, but "0.011.255.245", "192.168.1.312" and "192.168@1.1" are invalid IP addresses.Given a string
s containing only digits, return all possible valid IP addresses that can be formed by inserting dots into s. You are not allowed to reorder or remove any digits in s. You may return the valid IP addresses in any order.Example 1:
Input: s = "25525511135"
Output: ["255.255.11.135","255.255.111.35"]
Example 2:
Input: s = "0000"
Output: ["0.0.0.0"]
Example 3:
Input: s = "101023"
Output: ["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]
Constraints:
•
1 <= s.length <= 20•
s consists of digits only.2023-01-22
131. Palindrome Partitioning
Topic: String, Dynamic Programming, Backtracking
Difficulty: Medium
Problem:
Given a string
Example 1:
Example 2:
Constraints:
•
•
131. Palindrome Partitioning
Topic: String, Dynamic Programming, Backtracking
Difficulty: Medium
Problem:
Given a string
s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.Example 1:
Input: s = "aab"
Output: [["a","a","b"],["aa","b"]]
Example 2:
Input: s = "a"
Output: [["a"]]
Constraints:
•
1 <= s.length <= 16•
s contains only lowercase English letters.2023-01-23
997. Find the Town Judge
Topic: Array, Hash Table, Graph
Difficulty: Easy
Problem:
In a town, there are
If the town judge exists, then:
1. The town judge trusts nobody.
2. Everybody (except for the town judge) trusts the town judge.
3. There is exactly one person that satisfies properties 1 and 2.
You are given an array
Return the label of the town judge if the town judge exists and can be identified, or return
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
• All the pairs of
•
•
997. Find the Town Judge
Topic: Array, Hash Table, Graph
Difficulty: Easy
Problem:
In a town, there are
n people labeled from 1 to n. There is a rumor that one of these people is secretly the town judge.If the town judge exists, then:
1. The town judge trusts nobody.
2. Everybody (except for the town judge) trusts the town judge.
3. There is exactly one person that satisfies properties 1 and 2.
You are given an array
trust where trust[i] = [a_i, b_i] representing that the person labeled a_i trusts the person labeled b_i.Return the label of the town judge if the town judge exists and can be identified, or return
-1 otherwise.Example 1:
Input: n = 2, trust = [[1,2]]
Output: 2
Example 2:
Input: n = 3, trust = [[1,3],[2,3]]
Output: 3
Example 3:
Input: n = 3, trust = [[1,3],[2,3],[3,1]]
Output: -1
Constraints:
•
1 <= n <= 1000•
0 <= trust.length <= 10^4•
trust[i].length == 2• All the pairs of
trust are unique.•
a_i != b_i•
1 <= a_i, b_i <= n2023-01-24
909. Snakes and Ladders
Topic: Array, Breadth-First Search, Matrix
Difficulty: Medium
Problem:
You are given an
You start on square
• Choose a destination square
• This choice simulates the result of a standard 6-sided die roll: i.e., there are always at most 6 destinations, regardless of the size of the board.
• If
• The game ends when you reach the square
A board square on row
Note that you only take a snake or ladder at most once per move. If the destination to a snake or ladder is the start of another snake or ladder, you do not follow the subsequent snake or ladder.
• For example, suppose the board is
Return the least number of moves required to reach the square
Example 1:
Image: https://assets.leetcode.com/uploads/2018/09/23/snakes.png
Example 2:
Constraints:
•
•
•
• The squares labeled
909. Snakes and Ladders
Topic: Array, Breadth-First Search, Matrix
Difficulty: Medium
Problem:
You are given an
n x n integer matrix board where the cells are labeled from 1 to n^2 in a Boustrophedon style starting from the bottom left of the board (i.e. board[n - 1][0]) and alternating direction each row.You start on square
1 of the board. In each move, starting from square curr, do the following:• Choose a destination square
next with a label in the range [curr + 1, min(curr + 6, n^2)].• This choice simulates the result of a standard 6-sided die roll: i.e., there are always at most 6 destinations, regardless of the size of the board.
• If
next has a snake or ladder, you must move to the destination of that snake or ladder. Otherwise, you move to next.• The game ends when you reach the square
n^2.A board square on row
r and column c has a snake or ladder if board[r][c] != -1. The destination of that snake or ladder is board[r][c]. Squares 1 and n^2 do not have a snake or ladder.Note that you only take a snake or ladder at most once per move. If the destination to a snake or ladder is the start of another snake or ladder, you do not follow the subsequent snake or ladder.
• For example, suppose the board is
[[-1,4],[-1,3]], and on the first move, your destination square is 2. You follow the ladder to square 3, but do not follow the subsequent ladder to 4.Return the least number of moves required to reach the square
n^2. If it is not possible to reach the square, return -1.Example 1:
Image: https://assets.leetcode.com/uploads/2018/09/23/snakes.png
Input: board = [[-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1],[-1,35,-1,-1,13,-1],[-1,-1,-1,-1,-1,-1],[-1,15,-1,-1,-1,-1]]
Output: 4
Explanation:
In the beginning, you start at square 1 (at row 5, column 0).
You decide to move to square 2 and must take the ladder to square 15.
You then decide to move to square 17 and must take the snake to square 13.
You then decide to move to square 14 and must take the ladder to square 35.
You then decide to move to square 36, ending the game.
This is the lowest possible number of moves to reach the last square, so return 4.
Example 2:
Input: board = [[-1,-1],[-1,3]]
Output: 1
Constraints:
•
n == board.length == board[i].length•
2 <= n <= 20•
grid[i][j] is either -1 or in the range [1, n^2].• The squares labeled
1 and n^2 do not have any ladders or snakes.2023-01-25
2359. Find Closest Node to Given Two Nodes
Topic: Depth-First Search, Graph
Difficulty: Medium
Problem:
You are given a directed graph of
The graph is represented with a given 0-indexed array
You are also given two integers
Return the index of the node that can be reached from both
Note that
Example 1:
Image: https://assets.leetcode.com/uploads/2022/06/07/graph4drawio-2.png
Example 2:
Image: https://assets.leetcode.com/uploads/2022/06/07/graph4drawio-4.png
Constraints:
•
•
•
•
•
2359. Find Closest Node to Given Two Nodes
Topic: Depth-First Search, Graph
Difficulty: Medium
Problem:
You are given a directed graph of
n nodes numbered from 0 to n - 1, where each node has at most one outgoing edge.The graph is represented with a given 0-indexed array
edges of size n, indicating that there is a directed edge from node i to node edges[i]. If there is no outgoing edge from i, then edges[i] == -1.You are also given two integers
node1 and node2.Return the index of the node that can be reached from both
node1 and node2, such that the maximum between the distance from node1 to that node, and from node2 to that node is minimized. If there are multiple answers, return the node with the smallest index, and if no possible answer exists, return -1.Note that
edges may contain cycles.Example 1:
Image: https://assets.leetcode.com/uploads/2022/06/07/graph4drawio-2.png
Input: edges = [2,2,3,-1], node1 = 0, node2 = 1
Output: 2
Explanation: The distance from node 0 to node 2 is 1, and the distance from node 1 to node 2 is 1.
The maximum of those two distances is 1. It can be proven that we cannot get a node with a smaller maximum distance than 1, so we return node 2.
Example 2:
Image: https://assets.leetcode.com/uploads/2022/06/07/graph4drawio-4.png
Input: edges = [1,2,-1], node1 = 0, node2 = 2
Output: 2
Explanation: The distance from node 0 to node 2 is 2, and the distance from node 2 to itself is 0.
The maximum of those two distances is 2. It can be proven that we cannot get a node with a smaller maximum distance than 2, so we return node 2.
Constraints:
•
n == edges.length•
2 <= n <= 10^5•
-1 <= edges[i] < n•
edges[i] != i•
0 <= node1, node2 < n2023-01-26
787. Cheapest Flights Within K Stops
Topic: Dynamic Programming, Depth-First Search, Breadth-First Search, Graph, Heap (Priority Queue), Shortest Path
Difficulty: Medium
Problem:
There are
You are also given three integers
Example 1:
Image: https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-3drawio.png
Example 2:
Image: https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-1drawio.png
Example 3:
Image: https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-2drawio.png
Constraints:
•
•
•
•
•
•
• There will not be any multiple flights between two cities.
•
•
787. Cheapest Flights Within K Stops
Topic: Dynamic Programming, Depth-First Search, Breadth-First Search, Graph, Heap (Priority Queue), Shortest Path
Difficulty: Medium
Problem:
There are
n cities connected by some number of flights. You are given an array flights where flights[i] = [from_i, to_i, price_i] indicates that there is a flight from city from_i to city to_i with cost price_i.You are also given three integers
src, dst, and k, return the cheapest price from src to dst with at most k stops. If there is no such route, return -1.Example 1:
Image: https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-3drawio.png
Input: n = 4, flights = [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]], src = 0, dst = 3, k = 1
Output: 700
Explanation:
The graph is shown above.
The optimal path with at most 1 stop from city 0 to 3 is marked in red and has cost 100 + 600 = 700.
Note that the path through cities [0,1,2,3] is cheaper but is invalid because it uses 2 stops.
Example 2:
Image: https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-1drawio.png
Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1
Output: 200
Explanation:
The graph is shown above.
The optimal path with at most 1 stop from city 0 to 2 is marked in red and has cost 100 + 100 = 200.
Example 3:
Image: https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-2drawio.png
Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0
Output: 500
Explanation:
The graph is shown above.
The optimal path with no stops from city 0 to 2 is marked in red and has cost 500.
Constraints:
•
1 <= n <= 100•
0 <= flights.length <= (n * (n - 1) / 2)•
flights[i].length == 3•
0 <= from_i, to_i < n•
from_i != to_i•
1 <= price_i <= 10^4• There will not be any multiple flights between two cities.
•
0 <= src, dst, k < n•
src != dst2023-01-27
472. Concatenated Words
Topic: Array, String, Dynamic Programming, Depth-First Search, Trie
Difficulty: Hard
Problem:
Given an array of strings
A concatenated word is defined as a string that is comprised entirely of at least two shorter words in the given array.
Example 1:
Example 2:
Constraints:
•
•
•
• All the strings of
•
472. Concatenated Words
Topic: Array, String, Dynamic Programming, Depth-First Search, Trie
Difficulty: Hard
Problem:
Given an array of strings
words (without duplicates), return all the concatenated words in the given list of words.A concatenated word is defined as a string that is comprised entirely of at least two shorter words in the given array.
Example 1:
Input: words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
Output: ["catsdogcats","dogcatsdog","ratcatdogcat"]
Explanation: "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".
Example 2:
Input: words = ["cat","dog","catdog"]
Output: ["catdog"]
Constraints:
•
1 <= words.length <= 10^4•
1 <= words[i].length <= 30•
words[i] consists of only lowercase English letters.• All the strings of
words are unique.•
1 <= sum(words[i].length) <= 10^52023-01-28
352. Data Stream as Disjoint Intervals
Topic: Binary Search, Design, Ordered Set
Difficulty: Hard
Problem:
Given a data stream input of non-negative integers
Implement the
•
•
•
Example 1:
Constraints:
•
• At most
Follow up: What if there are lots of merges and the number of disjoint intervals is small compared to the size of the data stream?
352. Data Stream as Disjoint Intervals
Topic: Binary Search, Design, Ordered Set
Difficulty: Hard
Problem:
Given a data stream input of non-negative integers
a_1, a_2, ..., a_n, summarize the numbers seen so far as a list of disjoint intervals.Implement the
SummaryRanges class:•
SummaryRanges() Initializes the object with an empty stream.•
void addNum(int value) Adds the integer value to the stream.•
int[][] getIntervals() Returns a summary of the integers in the stream currently as a list of disjoint intervals [start_i, end_i]. The answer should be sorted by start_i.Example 1:
Input
["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"]
[[], [1], [], [3], [], [7], [], [2], [], [6], []]
Output
[null, null, [[1, 1]], null, [[1, 1], [3, 3]], null, [[1, 1], [3, 3], [7, 7]], null, [[1, 3], [7, 7]], null, [[1, 3], [6, 7]]]
Explanation
SummaryRanges summaryRanges = new SummaryRanges();
summaryRanges.addNum(1); // arr = [1]
summaryRanges.getIntervals(); // return [[1, 1]]
summaryRanges.addNum(3); // arr = [1, 3]
summaryRanges.getIntervals(); // return [[1, 1], [3, 3]]
summaryRanges.addNum(7); // arr = [1, 3, 7]
summaryRanges.getIntervals(); // return [[1, 1], [3, 3], [7, 7]]
summaryRanges.addNum(2); // arr = [1, 2, 3, 7]
summaryRanges.getIntervals(); // return [[1, 3], [7, 7]]
summaryRanges.addNum(6); // arr = [1, 2, 3, 6, 7]
summaryRanges.getIntervals(); // return [[1, 3], [6, 7]]
Constraints:
•
0 <= value <= 10^4• At most
3 * 10^4 calls will be made to addNum and getIntervals.Follow up: What if there are lots of merges and the number of disjoint intervals is small compared to the size of the data stream?
2023-01-29
460. LFU Cache
Topic: Hash Table, Linked List, Design, Doubly-Linked List
Difficulty: Hard
Problem:
Design and implement a data structure for a Least Frequently Used (LFU) cache.
Implement the
•
•
•
To determine the least frequently used key, a use counter is maintained for each key in the cache. The key with the smallest use counter is the least frequently used key.
When a key is first inserted into the cache, its use counter is set to
The functions
Example 1:
Constraints:
•
•
•
• At most
460. LFU Cache
Topic: Hash Table, Linked List, Design, Doubly-Linked List
Difficulty: Hard
Problem:
Design and implement a data structure for a Least Frequently Used (LFU) cache.
Implement the
LFUCache class:•
LFUCache(int capacity) Initializes the object with the capacity of the data structure.•
int get(int key) Gets the value of the key if the key exists in the cache. Otherwise, returns -1.•
void put(int key, int value) Update the value of the key if present, or inserts the key if not already present. When the cache reaches its capacity, it should invalidate and remove the least frequently used key before inserting a new item. For this problem, when there is a tie (i.e., two or more keys with the same frequency), the least recently used key would be invalidated.To determine the least frequently used key, a use counter is maintained for each key in the cache. The key with the smallest use counter is the least frequently used key.
When a key is first inserted into the cache, its use counter is set to
1 (due to the put operation). The use counter for a key in the cache is incremented either a get or put operation is called on it.The functions
get and put must each run in O(1) average time complexity.Example 1:
Input
["LFUCache", "put", "put", "get", "put", "get", "get", "put", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [3], [4, 4], [1], [3], [4]]
Output
[null, null, null, 1, null, -1, 3, null, -1, 3, 4]
Explanation
// cnt(x) = the use counter for key x
// cache=[] will show the last used order for tiebreakers (leftmost element is most recent)
LFUCache lfu = new LFUCache(2);
lfu.put(1, 1); // cache=[1,_], cnt(1)=1
lfu.put(2, 2); // cache=[2,1], cnt(2)=1, cnt(1)=1
lfu.get(1); // return 1
// cache=[1,2], cnt(2)=1, cnt(1)=2
lfu.put(3, 3); // 2 is the LFU key because cnt(2)=1 is the smallest, invalidate 2.
// cache=[3,1], cnt(3)=1, cnt(1)=2
lfu.get(2); // return -1 (not found)
lfu.get(3); // return 3
// cache=[3,1], cnt(3)=2, cnt(1)=2
lfu.put(4, 4); // Both 1 and 3 have the same cnt, but 1 is LRU, invalidate 1.
// cache=[4,3], cnt(4)=1, cnt(3)=2
lfu.get(1); // return -1 (not found)
lfu.get(3); // return 3
// cache=[3,4], cnt(4)=1, cnt(3)=3
lfu.get(4); // return 4
// cache=[4,3], cnt(4)=2, cnt(3)=3
Constraints:
•
0 <= capacity <= 10^4•
0 <= key <= 10^5•
0 <= value <= 10^9• At most
2 * 10^5 calls will be made to get and put.2023-01-30
1137. N-th Tribonacci Number
Topic: Math, Dynamic Programming, Memoization
Difficulty: Easy
Problem:
The Tribonacci sequence
Given
Example 1:
Example 2:
Constraints:
•
• The answer is guaranteed to fit within a 32-bit integer, ie.
1137. N-th Tribonacci Number
Topic: Math, Dynamic Programming, Memoization
Difficulty: Easy
Problem:
The Tribonacci sequence
T_n is defined as follows: T_0 = 0, T_1 = 1, T_2 = 1, and T_n+3 = T_n + T_n+1 + T_n+2 for n >= 0.Given
n, return the value of T_n.Example 1:
Input: n = 4
Output: 4
Explanation:
T_3 = 0 + 1 + 1 = 2
T_4 = 1 + 1 + 2 = 4
Example 2:
Input: n = 25
Output: 1389537
Constraints:
•
0 <= n <= 37• The answer is guaranteed to fit within a 32-bit integer, ie.
answer <= 2^31 - 1.2023-01-31
1626. Best Team With No Conflicts
Topic: Array, Dynamic Programming, Sorting
Difficulty: Medium
Problem:
You are the manager of a basketball team. For the upcoming tournament, you want to choose the team with the highest overall score. The score of the team is the sum of scores of all the players in the team.
However, the basketball team is not allowed to have conflicts. A conflict exists if a younger player has a strictly higher score than an older player. A conflict does not occur between players of the same age.
Given two lists,
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
1626. Best Team With No Conflicts
Topic: Array, Dynamic Programming, Sorting
Difficulty: Medium
Problem:
You are the manager of a basketball team. For the upcoming tournament, you want to choose the team with the highest overall score. The score of the team is the sum of scores of all the players in the team.
However, the basketball team is not allowed to have conflicts. A conflict exists if a younger player has a strictly higher score than an older player. A conflict does not occur between players of the same age.
Given two lists,
scores and ages, where each scores[i] and ages[i] represents the score and age of the i^th player, respectively, return the highest overall score of all possible basketball teams.Example 1:
Input: scores = [1,3,5,10,15], ages = [1,2,3,4,5]
Output: 34
Explanation: You can choose all the players.
Example 2:
Input: scores = [4,5,6,5], ages = [2,1,2,1]
Output: 16
Explanation: It is best to choose the last 3 players. Notice that you are allowed to choose multiple people of the same age.
Example 3:
Input: scores = [1,2,3,5], ages = [8,9,10,1]
Output: 6
Explanation: It is best to choose the first 3 players.
Constraints:
•
1 <= scores.length, ages.length <= 1000•
scores.length == ages.length•
1 <= scores[i] <= 10^6•
1 <= ages[i] <= 10002023-02-01
1071. Greatest Common Divisor of Strings
Topic: Math, String
Difficulty: Easy
Problem:
For two strings
Given two strings
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1071. Greatest Common Divisor of Strings
Topic: Math, String
Difficulty: Easy
Problem:
For two strings
s and t, we say "t divides s" if and only if s = t + ... + t (i.e., t is concatenated with itself one or more times).Given two strings
str1 and str2, return the largest string x such that x divides both str1 and str2.Example 1:
Input: str1 = "ABCABC", str2 = "ABC"
Output: "ABC"
Example 2:
Input: str1 = "ABABAB", str2 = "ABAB"
Output: "AB"
Example 3:
Input: str1 = "LEET", str2 = "CODE"
Output: ""
Constraints:
•
1 <= str1.length, str2.length <= 1000•
str1 and str2 consist of English uppercase letters.2023-02-02
953. Verifying an Alien Dictionary
Topic: Array, Hash Table, String
Difficulty: Easy
Problem:
In an alien language, surprisingly, they also use English lowercase letters, but possibly in a different
Given a sequence of
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
• All characters in
953. Verifying an Alien Dictionary
Topic: Array, Hash Table, String
Difficulty: Easy
Problem:
In an alien language, surprisingly, they also use English lowercase letters, but possibly in a different
order. The order of the alphabet is some permutation of lowercase letters.Given a sequence of
words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographically in this alien language.Example 1:
Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
Output: true
Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.
Example 2:
Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
Output: false
Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.
Example 3:
Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
Output: false
Explanation: The first three characters "app" match, and the second string is shorter (in size.) According to lexicographical rules "apple" > "app", because 'l' > '∅', where '∅' is defined as the blank character which is less than any other character ([More info](https://en.wikipedia.org/wiki/Lexicographical_order)).
Constraints:
•
1 <= words.length <= 100•
1 <= words[i].length <= 20•
order.length == 26• All characters in
words[i] and order are English lowercase letters.2023-02-03
6. Zigzag Conversion
Topic: String
Difficulty: Medium
Problem:
The string
And then read line by line:
Write the code that will take a string and make this conversion given a number of rows:
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
6. Zigzag Conversion
Topic: String
Difficulty: Medium
Problem:
The string
"PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H N
A P L S I I G
Y I R
And then read line by line:
"PAHNAPLSIIGYIR"Write the code that will take a string and make this conversion given a number of rows:
string convert(string s, int numRows);
Example 1:
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Example 2:
Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P I N
A L S I G
Y A H R
P I
Example 3:
Input: s = "A", numRows = 1
Output: "A"
Constraints:
•
1 <= s.length <= 1000•
s consists of English letters (lower-case and upper-case), ',' and '.'.•
1 <= numRows <= 10002023-02-04
567. Permutation in String
Topic: Hash Table, Two Pointers, String, Sliding Window
Difficulty: Medium
Problem:
Given two strings
In other words, return
Example 1:
Example 2:
Constraints:
•
•
567. Permutation in String
Topic: Hash Table, Two Pointers, String, Sliding Window
Difficulty: Medium
Problem:
Given two strings
s1 and s2, return true if s2 contains a permutation of s1, or false otherwise.In other words, return
true if one of s1's permutations is the substring of s2.Example 1:
Input: s1 = "ab", s2 = "eidbaooo"
Output: true
Explanation: s2 contains one permutation of s1 ("ba").
Example 2:
Input: s1 = "ab", s2 = "eidboaoo"
Output: false
Constraints:
•
1 <= s1.length, s2.length <= 10^4•
s1 and s2 consist of lowercase English letters.2023-02-05
438. Find All Anagrams in a String
Topic: Hash Table, String, Sliding Window
Difficulty: Medium
Problem:
Given two strings
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Example 2:
Constraints:
•
•
438. Find All Anagrams in a String
Topic: Hash Table, String, Sliding Window
Difficulty: Medium
Problem:
Given two strings
s and p, return an array of all the start indices of p's anagrams in s. You may return the answer in any order.An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: s = "cbaebabacd", p = "abc"
Output: [0,6]
Explanation:
The substring with start index = 0 is "cba", which is an anagram of "abc".
The substring with start index = 6 is "bac", which is an anagram of "abc".
Example 2:
Input: s = "abab", p = "ab"
Output: [0,1,2]
Explanation:
The substring with start index = 0 is "ab", which is an anagram of "ab".
The substring with start index = 1 is "ba", which is an anagram of "ab".
The substring with start index = 2 is "ab", which is an anagram of "ab".
Constraints:
•
1 <= s.length, p.length <= 3 * 10^4•
s and p consist of lowercase English letters.