2023-04-15
2218. Maximum Value of K Coins From Piles
Topic: Array, Dynamic Programming, Prefix Sum
Difficulty: Hard
Problem:
There are
In one move, you can choose any coin on top of any pile, remove it, and add it to your wallet.
Given a list
Example 1:
Image: https://assets.leetcode.com/uploads/2019/11/09/e1.png
Example 2:
Constraints:
•
•
•
•
2218. Maximum Value of K Coins From Piles
Topic: Array, Dynamic Programming, Prefix Sum
Difficulty: Hard
Problem:
There are
n piles of coins on a table. Each pile consists of a positive number of coins of assorted denominations.In one move, you can choose any coin on top of any pile, remove it, and add it to your wallet.
Given a list
piles, where piles[i] is a list of integers denoting the composition of the i^th pile from top to bottom, and a positive integer k, return the maximum total value of coins you can have in your wallet if you choose exactly k coins optimally.Example 1:
Image: https://assets.leetcode.com/uploads/2019/11/09/e1.png
Input: piles = [[1,100,3],[7,8,9]], k = 2
Output: 101
Explanation:
The above diagram shows the different ways we can choose k coins.
The maximum total we can obtain is 101.
Example 2:
Input: piles = [[100],[100],[100],[100],[100],[100],[1,1,1,1,1,1,700]], k = 7
Output: 706
Explanation:
The maximum total can be obtained if we choose all coins from the last pile.
Constraints:
•
n == piles.length•
1 <= n <= 1000•
1 <= piles[i][j] <= 10^5•
1 <= k <= sum(piles[i].length) <= 20002023-04-16
1639. Number of Ways to Form a Target String Given a Dictionary
Topic: Array, String, Dynamic Programming
Difficulty: Hard
Problem:
You are given a list of strings of the same length
Your task is to form
•
• To form the
• Once you use the
• Repeat the process until you form the string
Notice that you can use multiple characters from the same string in
Return the number of ways to form
Example 1:
Example 2:
Constraints:
•
•
• All strings in
•
•
1639. Number of Ways to Form a Target String Given a Dictionary
Topic: Array, String, Dynamic Programming
Difficulty: Hard
Problem:
You are given a list of strings of the same length
words and a string target.Your task is to form
target using the given words under the following rules:•
target should be formed from left to right.• To form the
i^th character (0-indexed) of target, you can choose the k^th character of the j^th string in words if target[i] = words[j][k].• Once you use the
k^th character of the j^th string of words, you can no longer use the x^th character of any string in words where x <= k. In other words, all characters to the left of or at index k become unusuable for every string.• Repeat the process until you form the string
target.Notice that you can use multiple characters from the same string in
words provided the conditions above are met.Return the number of ways to form
target from words. Since the answer may be too large, return it modulo 10^9 + 7.Example 1:
Input: words = ["acca","bbbb","caca"], target = "aba"
Output: 6
Explanation: There are 6 ways to form target.
"aba" -> index 0 ("acca"), index 1 ("bbbb"), index 3 ("caca")
"aba" -> index 0 ("acca"), index 2 ("bbbb"), index 3 ("caca")
"aba" -> index 0 ("acca"), index 1 ("bbbb"), index 3 ("acca")
"aba" -> index 0 ("acca"), index 2 ("bbbb"), index 3 ("acca")
"aba" -> index 1 ("caca"), index 2 ("bbbb"), index 3 ("acca")
"aba" -> index 1 ("caca"), index 2 ("bbbb"), index 3 ("caca")
Example 2:
Input: words = ["abba","baab"], target = "bab"
Output: 4
Explanation: There are 4 ways to form target.
"bab" -> index 0 ("baab"), index 1 ("baab"), index 2 ("abba")
"bab" -> index 0 ("baab"), index 1 ("baab"), index 3 ("baab")
"bab" -> index 0 ("baab"), index 2 ("baab"), index 3 ("baab")
"bab" -> index 1 ("abba"), index 2 ("baab"), index 3 ("baab")
Constraints:
•
1 <= words.length <= 1000•
1 <= words[i].length <= 1000• All strings in
words have the same length.•
1 <= target.length <= 1000•
words[i] and target contain only lowercase English letters.2023-04-17
1431. Kids With the Greatest Number of Candies
Topic: Array
Difficulty: Easy
Problem:
There are
Return a boolean array
Note that multiple kids can have the greatest number of candies.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
1431. Kids With the Greatest Number of Candies
Topic: Array
Difficulty: Easy
Problem:
There are
n kids with candies. You are given an integer array candies, where each candies[i] represents the number of candies the i^th kid has, and an integer extraCandies, denoting the number of extra candies that you have.Return a boolean array
result of length n, where result[i] is true if, after giving the i^th kid all the extraCandies, they will have the greatest number of candies among all the kids, or false otherwise.Note that multiple kids can have the greatest number of candies.
Example 1:
Input: candies = [2,3,5,1,3], extraCandies = 3
Output: [true,true,true,false,true]
Explanation: If you give all extraCandies to:
- Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.
- Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
- Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.
- Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids.
- Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
Example 2:
Input: candies = [4,2,1,1,2], extraCandies = 1
Output: [true,false,false,false,false]
Explanation: There is only 1 extra candy.
Kid 1 will always have the greatest number of candies, even if a different kid is given the extra candy.
Example 3:
Input: candies = [12,1,12], extraCandies = 10
Output: [true,false,true]
Constraints:
•
n == candies.length•
2 <= n <= 100•
1 <= candies[i] <= 100•
1 <= extraCandies <= 502023-04-18
1768. Merge Strings Alternately
Topic: Two Pointers, String
Difficulty: Easy
Problem:
You are given two strings
Return the merged string.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1768. Merge Strings Alternately
Topic: Two Pointers, String
Difficulty: Easy
Problem:
You are given two strings
word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional letters onto the end of the merged string.Return the merged string.
Example 1:
Input: word1 = "abc", word2 = "pqr"
Output: "apbqcr"
Explanation: The merged string will be merged as so:
word1: a b c
word2: p q r
merged: a p b q c r
Example 2:
Input: word1 = "ab", word2 = "pqrs"
Output: "apbqrs"
Explanation: Notice that as word2 is longer, "rs" is appended to the end.
word1: a b
word2: p q r s
merged: a p b q r s
Example 3:
Input: word1 = "abcd", word2 = "pq"
Output: "apbqcd"
Explanation: Notice that as word1 is longer, "cd" is appended to the end.
word1: a b c d
word2: p q
merged: a p b q c d
Constraints:
•
1 <= word1.length, word2.length <= 100•
word1 and word2 consist of lowercase English letters.2023-04-19
1372. Longest ZigZag Path in a Binary Tree
Topic: Dynamic Programming, Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
You are given the
A ZigZag path for a binary tree is defined as follow:
• Choose any node in the binary tree and a direction (right or left).
• If the current direction is right, move to the right child of the current node; otherwise, move to the left child.
• Change the direction from right to left or from left to right.
• Repeat the second and third steps until you can't move in the tree.
Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0).
Return the longest ZigZag path contained in that tree.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/01/22/sample_1_1702.png
Example 2:
Image: https://assets.leetcode.com/uploads/2020/01/22/sample_2_1702.png
Example 3:
Constraints:
• The number of nodes in the tree is in the range
•
1372. Longest ZigZag Path in a Binary Tree
Topic: Dynamic Programming, Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
You are given the
root of a binary tree.A ZigZag path for a binary tree is defined as follow:
• Choose any node in the binary tree and a direction (right or left).
• If the current direction is right, move to the right child of the current node; otherwise, move to the left child.
• Change the direction from right to left or from left to right.
• Repeat the second and third steps until you can't move in the tree.
Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0).
Return the longest ZigZag path contained in that tree.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/01/22/sample_1_1702.png
Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1]
Output: 3
Explanation: Longest ZigZag path in blue nodes (right -> left -> right).
Example 2:
Image: https://assets.leetcode.com/uploads/2020/01/22/sample_2_1702.png
Input: root = [1,1,1,null,1,null,null,1,1,null,1]
Output: 4
Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right).
Example 3:
Input: root = [1]
Output: 0
Constraints:
• The number of nodes in the tree is in the range
[1, 5 * 10^4].•
1 <= Node.val <= 1002023-04-20
662. Maximum Width of Binary Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
The maximum width of a tree is the maximum width among all levels.
The width of one level is defined as the length between the end-nodes (the leftmost and rightmost non-null nodes), where the null nodes between the end-nodes that would be present in a complete binary tree extending down to that level are also counted into the length calculation.
It is guaranteed that the answer will in the range of a 32-bit signed integer.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/05/03/width1-tree.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2022/03/14/maximum-width-of-binary-tree-v3.jpg
Example 3:
Image: https://assets.leetcode.com/uploads/2021/05/03/width3-tree.jpg
Constraints:
• The number of nodes in the tree is in the range
•
662. Maximum Width of Binary Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a binary tree, return the maximum width of the given tree.The maximum width of a tree is the maximum width among all levels.
The width of one level is defined as the length between the end-nodes (the leftmost and rightmost non-null nodes), where the null nodes between the end-nodes that would be present in a complete binary tree extending down to that level are also counted into the length calculation.
It is guaranteed that the answer will in the range of a 32-bit signed integer.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/05/03/width1-tree.jpg
Input: root = [1,3,2,5,3,null,9]
Output: 4
Explanation: The maximum width exists in the third level with length 4 (5,3,null,9).
Example 2:
Image: https://assets.leetcode.com/uploads/2022/03/14/maximum-width-of-binary-tree-v3.jpg
Input: root = [1,3,2,5,null,null,9,6,null,7]
Output: 7
Explanation: The maximum width exists in the fourth level with length 7 (6,null,null,null,null,null,7).
Example 3:
Image: https://assets.leetcode.com/uploads/2021/05/03/width3-tree.jpg
Input: root = [1,3,2,5]
Output: 2
Explanation: The maximum width exists in the second level with length 2 (3,2).
Constraints:
• The number of nodes in the tree is in the range
[1, 3000].•
-100 <= Node.val <= 1002023-04-21
879. Profitable Schemes
Topic: Array, Dynamic Programming
Difficulty: Hard
Problem:
There is a group of
Let's call a profitable scheme any subset of these crimes that generates at least
Return the number of schemes that can be chosen. Since the answer may be very large, return it modulo
Example 1:
Example 2:
Constraints:
•
•
•
•
•
•
879. Profitable Schemes
Topic: Array, Dynamic Programming
Difficulty: Hard
Problem:
There is a group of
n members, and a list of various crimes they could commit. The i^th crime generates a profit[i] and requires group[i] members to participate in it. If a member participates in one crime, that member can't participate in another crime.Let's call a profitable scheme any subset of these crimes that generates at least
minProfit profit, and the total number of members participating in that subset of crimes is at most n.Return the number of schemes that can be chosen. Since the answer may be very large, return it modulo
10^9 + 7.Example 1:
Input: n = 5, minProfit = 3, group = [2,2], profit = [2,3]
Output: 2
Explanation: To make a profit of at least 3, the group could either commit crimes 0 and 1, or just crime 1.
In total, there are 2 schemes.
Example 2:
Input: n = 10, minProfit = 5, group = [2,3,5], profit = [6,7,8]
Output: 7
Explanation: To make a profit of at least 5, the group could commit any crimes, as long as they commit one.
There are 7 possible schemes: (0), (1), (2), (0,1), (0,2), (1,2), and (0,1,2).
Constraints:
•
1 <= n <= 100•
0 <= minProfit <= 100•
1 <= group.length <= 100•
1 <= group[i] <= 100•
profit.length == group.length•
0 <= profit[i] <= 1002023-04-22
1312. Minimum Insertion Steps to Make a String Palindrome
Topic: String, Dynamic Programming
Difficulty: Hard
Problem:
Given a string
Return the minimum number of steps to make
A Palindrome String is one that reads the same backward as well as forward.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1312. Minimum Insertion Steps to Make a String Palindrome
Topic: String, Dynamic Programming
Difficulty: Hard
Problem:
Given a string
s. In one step you can insert any character at any index of the string.Return the minimum number of steps to make
s palindrome.A Palindrome String is one that reads the same backward as well as forward.
Example 1:
Input: s = "zzazz"
Output: 0
Explanation: The string "zzazz" is already palindrome we do not need any insertions.
Example 2:
Input: s = "mbadm"
Output: 2
Explanation: String can be "mbdadbm" or "mdbabdm".
Example 3:
Input: s = "leetcode"
Output: 5
Explanation: Inserting 5 characters the string becomes "leetcodocteel".
Constraints:
•
1 <= s.length <= 500•
s consists of lowercase English letters.2023-04-23
1416. Restore The Array
Topic: String, Dynamic Programming
Difficulty: Hard
Problem:
A program was supposed to print an array of integers. The program forgot to print whitespaces and the array is printed as a string of digits
Given the string
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
1416. Restore The Array
Topic: String, Dynamic Programming
Difficulty: Hard
Problem:
A program was supposed to print an array of integers. The program forgot to print whitespaces and the array is printed as a string of digits
s and all we know is that all integers in the array were in the range [1, k] and there are no leading zeros in the array.Given the string
s and the integer k, return the number of the possible arrays that can be printed as s using the mentioned program. Since the answer may be very large, return it modulo 10^9 + 7.Example 1:
Input: s = "1000", k = 10000
Output: 1
Explanation: The only possible array is [1000]
Example 2:
Input: s = "1000", k = 10
Output: 0
Explanation: There cannot be an array that was printed this way and has all integer >= 1 and <= 10.
Example 3:
Input: s = "1317", k = 2000
Output: 8
Explanation: Possible arrays are [1317],[131,7],[13,17],[1,317],[13,1,7],[1,31,7],[1,3,17],[1,3,1,7]
Constraints:
•
1 <= s.length <= 10^5•
s consists of only digits and does not contain leading zeros.•
1 <= k <= 10^92023-04-24
1046. Last Stone Weight
Topic: Array, Heap (Priority Queue)
Difficulty: Easy
Problem:
You are given an array of integers
We are playing a game with the stones. On each turn, we choose the heaviest two stones and smash them together. Suppose the heaviest two stones have weights
• If
• If
At the end of the game, there is at most one stone left.
Return the weight of the last remaining stone. If there are no stones left, return
Example 1:
Example 2:
Constraints:
•
•
1046. Last Stone Weight
Topic: Array, Heap (Priority Queue)
Difficulty: Easy
Problem:
You are given an array of integers
stones where stones[i] is the weight of the i^th stone.We are playing a game with the stones. On each turn, we choose the heaviest two stones and smash them together. Suppose the heaviest two 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 weight x is destroyed, and the stone of weight y has new weight y - x.At the end of the game, there is at most one stone left.
Return the weight of the last remaining stone. If there are no stones left, return
0.Example 1:
Input: stones = [2,7,4,1,8,1]
Output: 1
Explanation:
We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,
we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of the last stone.
Example 2:
Input: stones = [1]
Output: 1
Constraints:
•
1 <= stones.length <= 30•
1 <= stones[i] <= 10002023-04-25
2336. Smallest Number in Infinite Set
Topic: Hash Table, Design, Heap (Priority Queue)
Difficulty: Medium
Problem:
You have a set which contains all positive integers
Implement the
•
•
•
Example 1:
Constraints:
•
• At most
2336. Smallest Number in Infinite Set
Topic: Hash Table, Design, Heap (Priority Queue)
Difficulty: Medium
Problem:
You have a set which contains all positive integers
[1, 2, 3, 4, 5, ...].Implement the
SmallestInfiniteSet class:•
SmallestInfiniteSet() Initializes the SmallestInfiniteSet object to contain all positive integers.•
int popSmallest() Removes and returns the smallest integer contained in the infinite set.•
void addBack(int num) Adds a positive integer num back into the infinite set, if it is not already in the infinite set.Example 1:
Input
["SmallestInfiniteSet", "addBack", "popSmallest", "popSmallest", "popSmallest", "addBack", "popSmallest", "popSmallest", "popSmallest"]
[[], [2], [], [], [], [1], [], [], []]
Output
[null, null, 1, 2, 3, null, 1, 4, 5]
Explanation
SmallestInfiniteSet smallestInfiniteSet = new SmallestInfiniteSet();
smallestInfiniteSet.addBack(2); // 2 is already in the set, so no change is made.
smallestInfiniteSet.popSmallest(); // return 1, since 1 is the smallest number, and remove it from the set.
smallestInfiniteSet.popSmallest(); // return 2, and remove it from the set.
smallestInfiniteSet.popSmallest(); // return 3, and remove it from the set.
smallestInfiniteSet.addBack(1); // 1 is added back to the set.
smallestInfiniteSet.popSmallest(); // return 1, since 1 was added back to the set and
// is the smallest number, and remove it from the set.
smallestInfiniteSet.popSmallest(); // return 4, and remove it from the set.
smallestInfiniteSet.popSmallest(); // return 5, and remove it from the set.
Constraints:
•
1 <= num <= 1000• At most
1000 calls will be made in total to popSmallest and addBack.2023-04-26
258. Add Digits
Topic: Math, Simulation, Number Theory
Difficulty: Easy
Problem:
Given an integer
Example 1:
Example 2:
Constraints:
•
Follow up: Could you do it without any loop/recursion in
258. Add Digits
Topic: Math, Simulation, Number Theory
Difficulty: Easy
Problem:
Given an integer
num, repeatedly add all its digits until the result has only one digit, and return it.Example 1:
Input: num = 38
Output: 2
Explanation: The process is
38 --> 3 + 8 --> 11
11 --> 1 + 1 --> 2
Since 2 has only one digit, return it.
Example 2:
Input: num = 0
Output: 0
Constraints:
•
0 <= num <= 2^31 - 1Follow up: Could you do it without any loop/recursion in
O(1) runtime?2023-04-27
319. Bulb Switcher
Topic: Math, Brainteaser
Difficulty: Medium
Problem:
There are
On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the
Return the number of bulbs that are on after
Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/05/bulb.jpg
Example 2:
Example 3:
Constraints:
•
319. Bulb Switcher
Topic: Math, Brainteaser
Difficulty: Medium
Problem:
There are
n bulbs that are initially off. You first turn on all the bulbs, then you turn off every second bulb.On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the
i^th round, you toggle every i bulb. For the n^th round, you only toggle the last bulb.Return the number of bulbs that are on after
n rounds.Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/05/bulb.jpg
Input: n = 3
Output: 1
Explanation: At first, the three bulbs are [off, off, off].
After the first round, the three bulbs are [on, on, on].
After the second round, the three bulbs are [on, off, on].
After the third round, the three bulbs are [on, off, off].
So you should return 1 because there is only one bulb is on.
Example 2:
Input: n = 0
Output: 0
Example 3:
Input: n = 1
Output: 1
Constraints:
•
0 <= n <= 10^92023-04-28
839. Similar String Groups
Topic: Array, String, Depth-First Search, Breadth-First Search, Union Find
Difficulty: Hard
Problem:
Two strings
For example,
Together, these form two connected groups by similarity:
We are given a list
Example 1:
Example 2:
Constraints:
•
•
•
• All words in
839. Similar String Groups
Topic: Array, String, Depth-First Search, Breadth-First Search, Union Find
Difficulty: Hard
Problem:
Two strings
X and Y are similar if we can swap two letters (in different positions) of X, so that it equals Y. Also two strings X and Y are similar if they are equal.For example,
"tars" and "rats" are similar (swapping at positions 0 and 2), and "rats" and "arts" are similar, but "star" is not similar to "tars", "rats", or "arts".Together, these form two connected groups by similarity:
{"tars", "rats", "arts"} and {"star"}. Notice that "tars" and "arts" are in the same group even though they are not similar. Formally, each group is such that a word is in the group if and only if it is similar to at least one other word in the group.We are given a list
strs of strings where every string in strs is an anagram of every other string in strs. How many groups are there?Example 1:
Input: strs = ["tars","rats","arts","star"]
Output: 2
Example 2:
Input: strs = ["omv","ovm"]
Output: 1
Constraints:
•
1 <= strs.length <= 300•
1 <= strs[i].length <= 300•
strs[i] consists of lowercase letters only.• All words in
strs have the same length and are anagrams of each other.2023-04-29
1697. Checking Existence of Edge Length Limited Paths
Topic: Array, Union Find, Graph, Sorting
Difficulty: Hard
Problem:
An undirected graph of
Given an array
Return a boolean array
Example 1:
Image: https://assets.leetcode.com/uploads/2020/12/08/h.png
Example 2:
Image: https://assets.leetcode.com/uploads/2020/12/08/q.png
Constraints:
•
•
•
•
•
•
•
•
• There may be multiple edges between two nodes.
1697. Checking Existence of Edge Length Limited Paths
Topic: Array, Union Find, Graph, Sorting
Difficulty: Hard
Problem:
An undirected graph of
n nodes is defined by edgeList, where edgeList[i] = [u_i, v_i, dis_i] denotes an edge between nodes u_i and v_i with distance dis_i. Note that there may be multiple edges between two nodes.Given an array
queries, where queries[j] = [p_j, q_j, limit_j], your task is to determine for each queries[j] whether there is a path between p_j and q_j such that each edge on the path has a distance strictly less than limit_j .Return a boolean array
answer, where answer.length == queries.length and the j^th value of answer is true if there is a path for queries[j] is true, and false otherwise.Example 1:
Image: https://assets.leetcode.com/uploads/2020/12/08/h.png
Input: n = 3, edgeList = [[0,1,2],[1,2,4],[2,0,8],[1,0,16]], queries = [[0,1,2],[0,2,5]]
Output: [false,true]
Explanation: The above figure shows the given graph. Note that there are two overlapping edges between 0 and 1 with distances 2 and 16.
For the first query, between 0 and 1 there is no path where each distance is less than 2, thus we return false for this query.
For the second query, there is a path (0 -> 1 -> 2) of two edges with distances less than 5, thus we return true for this query.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/12/08/q.png
Input: n = 5, edgeList = [[0,1,10],[1,2,5],[2,3,9],[3,4,13]], queries = [[0,4,14],[1,4,13]]
Output: [true,false]
Exaplanation: The above figure shows the given graph.
Constraints:
•
2 <= n <= 10^5•
1 <= edgeList.length, queries.length <= 10^5•
edgeList[i].length == 3•
queries[j].length == 3•
0 <= u_i, v_i, p_j, q_j <= n - 1•
u_i != v_i•
p_j != q_j•
1 <= dis_i, limit_j <= 10^9• There may be multiple edges between two nodes.
2023-04-30
1579. Remove Max Number of Edges to Keep Graph Fully Traversable
Topic: Union Find, Graph
Difficulty: Hard
Problem:
Alice and Bob have an undirected graph of
• Type 1: Can be traversed by Alice only.
• Type 2: Can be traversed by Bob only.
• Type 3: Can be traversed by both Alice and Bob.
Given an array
Return the maximum number of edges you can remove, or return
Example 1:
Image: https://assets.leetcode.com/uploads/2020/08/19/ex1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2020/08/19/ex2.png
Example 3:
Image: https://assets.leetcode.com/uploads/2020/08/19/ex3.png
Constraints:
•
•
•
•
•
• All tuples
1579. Remove Max Number of Edges to Keep Graph Fully Traversable
Topic: Union Find, Graph
Difficulty: Hard
Problem:
Alice and Bob have an undirected graph of
n nodes and three types of edges:• Type 1: Can be traversed by Alice only.
• Type 2: Can be traversed by Bob only.
• Type 3: Can be traversed by both Alice and Bob.
Given an array
edges where edges[i] = [type_i, u_i, v_i] represents a bidirectional edge of type type_i between nodes u_i and v_i, find the maximum number of edges you can remove so that after removing the edges, the graph can still be fully traversed by both Alice and Bob. The graph is fully traversed by Alice and Bob if starting from any node, they can reach all other nodes.Return the maximum number of edges you can remove, or return
-1 if Alice and Bob cannot fully traverse the graph.Example 1:
Image: https://assets.leetcode.com/uploads/2020/08/19/ex1.png
Input: n = 4, edges = [[3,1,2],[3,2,3],[1,1,3],[1,2,4],[1,1,2],[2,3,4]]
Output: 2
Explanation: If we remove the 2 edges [1,1,2] and [1,1,3]. The graph will still be fully traversable by Alice and Bob. Removing any additional edge will not make it so. So the maximum number of edges we can remove is 2.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/08/19/ex2.png
Input: n = 4, edges = [[3,1,2],[3,2,3],[1,1,4],[2,1,4]]
Output: 0
Explanation: Notice that removing any edge will not make the graph fully traversable by Alice and Bob.
Example 3:
Image: https://assets.leetcode.com/uploads/2020/08/19/ex3.png
Input: n = 4, edges = [[3,2,3],[1,1,2],[2,3,4]]
Output: -1
Explanation: In the current graph, Alice cannot reach node 4 from the other nodes. Likewise, Bob cannot reach 1. Therefore it's impossible to make the graph fully traversable.
Constraints:
•
1 <= n <= 10^5•
1 <= edges.length <= min(10^5, 3 * n * (n - 1) / 2)•
edges[i].length == 3•
1 <= type_i <= 3•
1 <= u_i < v_i <= n• All tuples
(type_i, u_i, v_i) are distinct.2023-05-01
1491. Average Salary Excluding the Minimum and Maximum Salary
Topic: Array, Sorting
Difficulty: Easy
Problem:
You are given an array of unique integers
Return the average salary of employees excluding the minimum and maximum salary. Answers within
Example 1:
Example 2:
Constraints:
•
•
• All the integers of
1491. Average Salary Excluding the Minimum and Maximum Salary
Topic: Array, Sorting
Difficulty: Easy
Problem:
You are given an array of unique integers
salary where salary[i] is the salary of the i^th employee.Return the average salary of employees excluding the minimum and maximum salary. Answers within
10^-5 of the actual answer will be accepted.Example 1:
Input: salary = [4000,3000,1000,2000]
Output: 2500.00000
Explanation: Minimum salary and maximum salary are 1000 and 4000 respectively.
Average salary excluding minimum and maximum salary is (2000+3000) / 2 = 2500
Example 2:
Input: salary = [1000,2000,3000]
Output: 2000.00000
Explanation: Minimum salary and maximum salary are 1000 and 3000 respectively.
Average salary excluding minimum and maximum salary is (2000) / 1 = 2000
Constraints:
•
3 <= salary.length <= 100•
1000 <= salary[i] <= 10^6• All the integers of
salary are unique.2023-05-02
1822. Sign of the Product of an Array
Topic: Array, Math
Difficulty: Easy
Problem:
There is a function
•
•
•
You are given an integer array
Return
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1822. Sign of the Product of an Array
Topic: Array, Math
Difficulty: Easy
Problem:
There is a function
signFunc(x) that returns:•
1 if x is positive.•
-1 if x is negative.•
0 if x is equal to 0.You are given an integer array
nums. Let product be the product of all values in the array nums.Return
signFunc(product).Example 1:
Input: nums = [-1,-2,-3,-4,3,2,1]
Output: 1
Explanation: The product of all values in the array is 144, and signFunc(144) = 1
Example 2:
Input: nums = [1,5,0,2,-3]
Output: 0
Explanation: The product of all values in the array is 0, and signFunc(0) = 0
Example 3:
Input: nums = [-1,1,-1,1,-1]
Output: -1
Explanation: The product of all values in the array is -1, and signFunc(-1) = -1
Constraints:
•
1 <= nums.length <= 1000•
-100 <= nums[i] <= 1002023-05-03
2215. Find the Difference of Two Arrays
Topic: Array, Hash Table
Difficulty: Easy
Problem:
Given two 0-indexed integer arrays
•
•
Note that the integers in the lists may be returned in any order.
Example 1:
Example 2:
Constraints:
•
•
2215. Find the Difference of Two Arrays
Topic: Array, Hash Table
Difficulty: Easy
Problem:
Given two 0-indexed integer arrays
nums1 and nums2, return a list answer of size 2 where:•
answer[0] is a list of all distinct integers in nums1 which are not present in nums2.•
answer[1] is a list of all distinct integers in nums2 which are not present in nums1.Note that the integers in the lists may be returned in any order.
Example 1:
Input: nums1 = [1,2,3], nums2 = [2,4,6]
Output: [[1,3],[4,6]]
Explanation:
For nums1, nums1[1] = 2 is present at index 0 of nums2, whereas nums1[0] = 1 and nums1[2] = 3 are not present in nums2. Therefore, answer[0] = [1,3].
For nums2, nums2[0] = 2 is present at index 1 of nums1, whereas nums2[1] = 4 and nums2[2] = 6 are not present in nums2. Therefore, answer[1] = [4,6].
Example 2:
Input: nums1 = [1,2,3,3], nums2 = [1,1,2,2]
Output: [[3],[]]
Explanation:
For nums1, nums1[2] and nums1[3] are not present in nums2. Since nums1[2] == nums1[3], their value is only included once and answer[0] = [3].
Every integer in nums2 is present in nums1. Therefore, answer[1] = [].
Constraints:
•
1 <= nums1.length, nums2.length <= 1000•
-1000 <= nums1[i], nums2[i] <= 10002023-05-04
649. Dota2 Senate
Topic: String, Greedy, Queue
Difficulty: Medium
Problem:
In the world of Dota2, there are two parties: the Radiant and the Dire.
The Dota2 senate consists of senators coming from two parties. Now the Senate wants to decide on a change in the Dota2 game. The voting for this change is a round-based procedure. In each round, each senator can exercise one of the two rights:
• Ban one senator's right: A senator can make another senator lose all his rights in this and all the following rounds.
• Announce the victory: If this senator found the senators who still have rights to vote are all from the same party, he can announce the victory and decide on the change in the game.
Given a string
The round-based procedure starts from the first senator to the last senator in the given order. This procedure will last until the end of voting. All the senators who have lost their rights will be skipped during the procedure.
Suppose every senator is smart enough and will play the best strategy for his own party. Predict which party will finally announce the victory and change the Dota2 game. The output should be
Example 1:
Example 2:
Constraints:
•
•
•
649. Dota2 Senate
Topic: String, Greedy, Queue
Difficulty: Medium
Problem:
In the world of Dota2, there are two parties: the Radiant and the Dire.
The Dota2 senate consists of senators coming from two parties. Now the Senate wants to decide on a change in the Dota2 game. The voting for this change is a round-based procedure. In each round, each senator can exercise one of the two rights:
• Ban one senator's right: A senator can make another senator lose all his rights in this and all the following rounds.
• Announce the victory: If this senator found the senators who still have rights to vote are all from the same party, he can announce the victory and decide on the change in the game.
Given a string
senate representing each senator's party belonging. The character 'R' and 'D' represent the Radiant party and the Dire party. Then if there are n senators, the size of the given string will be n.The round-based procedure starts from the first senator to the last senator in the given order. This procedure will last until the end of voting. All the senators who have lost their rights will be skipped during the procedure.
Suppose every senator is smart enough and will play the best strategy for his own party. Predict which party will finally announce the victory and change the Dota2 game. The output should be
"Radiant" or "Dire".Example 1:
Input: senate = "RD"
Output: "Radiant"
Explanation:
The first senator comes from Radiant and he can just ban the next senator's right in round 1.
And the second senator can't exercise any rights anymore since his right has been banned.
And in round 2, the first senator can just announce the victory since he is the only guy in the senate who can vote.
Example 2:
Input: senate = "RDD"
Output: "Dire"
Explanation:
The first senator comes from Radiant and he can just ban the next senator's right in round 1.
And the second senator can't exercise any rights anymore since his right has been banned.
And the third senator comes from Dire and he can ban the first senator's right in round 1.
And in round 2, the third senator can just announce the victory since he is the only guy in the senate who can vote.
Constraints:
•
n == senate.length•
1 <= n <= 10^4•
senate[i] is either 'R' or 'D'.