2022-06-10
3. Longest Substring Without Repeating Characters
Topic: Hash Table, String, Sliding Window
Difficulty: Medium
Problem:
Given a string
Example 1:
Example 2:
Example 3:
Constraints:
•
•
3. Longest Substring Without Repeating Characters
Topic: Hash Table, String, Sliding Window
Difficulty: Medium
Problem:
Given a string
s, find the length of the longest substring without repeating characters.Example 1:
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
Constraints:
•
0 <= s.length <= 5 * 10^4•
s consists of English letters, digits, symbols and spaces.2022-06-11
1658. Minimum Operations to Reduce X to Zero
Topic: Array, Hash Table, Binary Search, Sliding Window, Prefix Sum
Difficulty: Medium
Problem:
You are given an integer array
Return the minimum number of operations to reduce
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
1658. Minimum Operations to Reduce X to Zero
Topic: Array, Hash Table, Binary Search, Sliding Window, Prefix Sum
Difficulty: Medium
Problem:
You are given an integer array
nums and an integer x. In one operation, you can either remove the leftmost or the rightmost element from the array nums and subtract its value from x. Note that this modifies the array for future operations.Return the minimum number of operations to reduce
x to exactly 0 if it is possible, otherwise, return -1.Example 1:
Input: nums = [1,1,4,2,3], x = 5
Output: 2
Explanation: The optimal solution is to remove the last two elements to reduce x to zero.
Example 2:
Input: nums = [5,6,7,8,9], x = 4
Output: -1
Example 3:
Input: nums = [3,2,20,1,1,3], x = 10
Output: 5
Explanation: The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero.
Constraints:
•
1 <= nums.length <= 10^5•
1 <= nums[i] <= 10^4•
1 <= x <= 10^92022-06-12
1695. Maximum Erasure Value
Topic: Array, Hash Table, Sliding Window
Difficulty: Medium
Problem:
You are given an array of positive integers
Return the maximum score you can get by erasing exactly one subarray.
An array
Example 1:
Example 2:
Constraints:
•
•
1695. Maximum Erasure Value
Topic: Array, Hash Table, Sliding Window
Difficulty: Medium
Problem:
You are given an array of positive integers
nums and want to erase a subarray containing unique elements. The score you get by erasing the subarray is equal to the sum of its elements.Return the maximum score you can get by erasing exactly one subarray.
An array
b is called to be a subarray of a if it forms a contiguous subsequence of a, that is, if it is equal to a[l],a[l+1],...,a[r] for some (l,r).Example 1:
Input: nums = [4,2,4,5,6]
Output: 17
Explanation: The optimal subarray here is [2,4,5,6].
Example 2:
Input: nums = [5,2,1,2,5,2,1,2,5]
Output: 8
Explanation: The optimal subarray here is [5,2,1] or [1,2,5].
Constraints:
•
1 <= nums.length <= 10^5•
1 <= nums[i] <= 10^42022-06-13
120. Triangle
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
Given a
For each step, you may move to an adjacent number of the row below. More formally, if you are on index
Example 1:
Example 2:
Constraints:
•
•
•
•
Follow up: Could you do this using only
120. Triangle
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
Given a
triangle array, return the minimum path sum from top to bottom.For each step, you may move to an adjacent number of the row below. More formally, if you are on index
i on the current row, you may move to either index i or index i + 1 on the next row.Example 1:
Input: triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
Output: 11
Explanation: The triangle looks like:
2
3 4
6 5 7
4 1 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
Example 2:
Input: triangle = [[-10]]
Output: -10
Constraints:
•
1 <= triangle.length <= 200•
triangle[0].length == 1•
triangle[i].length == triangle[i - 1].length + 1•
-10^4 <= triangle[i][j] <= 10^4Follow up: Could you do this using only
O(n) extra space, where n is the total number of rows in the triangle?2022-06-14
583. Delete Operation for Two Strings
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
Given two strings
In one step, you can delete exactly one character in either string.
Example 1:
Example 2:
Constraints:
•
•
583. Delete Operation for Two Strings
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
Given two strings
word1 and word2, return the minimum number of steps required to make word1 and word2 the same.In one step, you can delete exactly one character in either string.
Example 1:
Input: word1 = "sea", word2 = "eat"
Output: 2
Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
Example 2:
Input: word1 = "leetcode", word2 = "etco"
Output: 4
Constraints:
•
1 <= word1.length, word2.length <= 500•
word1 and word2 consist of only lowercase English letters.2022-06-15
1048. Longest String Chain
Topic: Array, Hash Table, Two Pointers, String, Dynamic Programming
Difficulty: Medium
Problem:
You are given an array of
• For example,
A word chain is a sequence of words
Return the length of the longest possible word chain with words chosen from the given list of
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
1048. Longest String Chain
Topic: Array, Hash Table, Two Pointers, String, Dynamic Programming
Difficulty: Medium
Problem:
You are given an array of
words where each word consists of lowercase English letters.word_A is a predecessor of word_B if and only if we can insert exactly one letter anywhere in word_A without changing the order of the other characters to make it equal to word_B.• For example,
"abc" is a predecessor of "abac", while "cba" is not a predecessor of "bcad".A word chain is a sequence of words
[word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. A single word is trivially a word chain with k == 1.Return the length of the longest possible word chain with words chosen from the given list of
words.Example 1:
Input: words = ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: One of the longest word chains is ["a","ba","bda","bdca"].
Example 2:
Input: words = ["xbc","pcxbcf","xb","cxbc","pcxbc"]
Output: 5
Explanation: All the words can be put in a word chain ["xb", "xbc", "cxbc", "pcxbc", "pcxbcf"].
Example 3:
Input: words = ["abcd","dbqca"]
Output: 1
Explanation: The trivial word chain ["abcd"] is one of the longest word chains.
["abcd","dbqca"] is not a valid word chain because the ordering of the letters is changed.
Constraints:
•
1 <= words.length <= 1000•
1 <= words[i].length <= 16•
words[i] only consists of lowercase English letters.2022-06-16
5. Longest Palindromic Substring
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
Given a string
Example 1:
Example 2:
Constraints:
•
•
5. Longest Palindromic Substring
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
Given a string
s, return the longest palindromic substring in s.Example 1:
Input: s = "babad"
Output: "bab"
Explanation: "aba" is also a valid answer.
Example 2:
Input: s = "cbbd"
Output: "bb"
Constraints:
•
1 <= s.length <= 1000•
s consist of only digits and English letters.2022-06-17
968. Binary Tree Cameras
Topic: Dynamic Programming, Tree, Depth-First Search, Binary Tree
Difficulty: Hard
Problem:
You are given the
Return the minimum number of cameras needed to monitor all nodes of the tree.
Example 1:
Image: https://assets.leetcode.com/uploads/2018/12/29/bst_cameras_01.png
Example 2:
Image: https://assets.leetcode.com/uploads/2018/12/29/bst_cameras_02.png
Constraints:
• The number of nodes in the tree is in the range
•
968. Binary Tree Cameras
Topic: Dynamic Programming, Tree, Depth-First Search, Binary Tree
Difficulty: Hard
Problem:
You are given the
root of a binary tree. We install cameras on the tree nodes where each camera at a node can monitor its parent, itself, and its immediate children.Return the minimum number of cameras needed to monitor all nodes of the tree.
Example 1:
Image: https://assets.leetcode.com/uploads/2018/12/29/bst_cameras_01.png
Input: root = [0,0,null,0,0]
Output: 1
Explanation: One camera is enough to monitor all nodes if placed as shown.
Example 2:
Image: https://assets.leetcode.com/uploads/2018/12/29/bst_cameras_02.png
Input: root = [0,0,null,0,null,0,null,null,0]
Output: 2
Explanation: At least two cameras are needed to monitor all nodes of the tree. The above image shows one of the valid configurations of camera placement.
Constraints:
• The number of nodes in the tree is in the range
[1, 1000].•
Node.val == 02022-06-18
745. Prefix and Suffix Search
Topic: String, Design, Trie
Difficulty: Hard
Problem:
Design a special dictionary with some words that searchs the words in it by a prefix and a suffix.
Implement the
•
•
Example 1:
Constraints:
•
•
•
•
• At most
745. Prefix and Suffix Search
Topic: String, Design, Trie
Difficulty: Hard
Problem:
Design a special dictionary with some words that searchs the words in it by a prefix and a suffix.
Implement the
WordFilter class:•
WordFilter(string[] words) Initializes the object with the words in the dictionary.•
f(string prefix, string suffix) Returns the index of the word in the dictionary, which has the prefix prefix and the suffix suffix. If there is more than one valid index, return the largest of them. If there is no such word in the dictionary, return -1.Example 1:
Input
["WordFilter", "f"]
[[["apple"]], ["a", "e"]]
Output
[null, 0]
Explanation
WordFilter wordFilter = new WordFilter(["apple"]);
wordFilter.f("a", "e"); // return 0, because the word at index 0 has prefix = "a" and suffix = 'e".
Constraints:
•
1 <= words.length <= 15000•
1 <= words[i].length <= 10•
1 <= prefix.length, suffix.length <= 10•
words[i], prefix and suffix consist of lower-case English letters only.• At most
15000 calls will be made to the function f.2022-06-19
1268. Search Suggestions System
Topic: Array, String, Trie
Difficulty: Medium
Problem:
You are given an array of strings
Design a system that suggests at most three product names from
Return a list of lists of the suggested products after each character of
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
• All the strings of
•
•
•
1268. Search Suggestions System
Topic: Array, String, Trie
Difficulty: Medium
Problem:
You are given an array of strings
products and a string searchWord.Design a system that suggests at most three product names from
products after each character of searchWord is typed. Suggested products should have common prefix with searchWord. If there are more than three products with a common prefix return the three lexicographically minimums products.Return a list of lists of the suggested products after each character of
searchWord is typed.Example 1:
Input: products = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse"
Output: [
["mobile","moneypot","monitor"],
["mobile","moneypot","monitor"],
["mouse","mousepad"],
["mouse","mousepad"],
["mouse","mousepad"]
]
Explanation: products sorted lexicographically = ["mobile","moneypot","monitor","mouse","mousepad"]
After typing m and mo all products match and we show user ["mobile","moneypot","monitor"]
After typing mou, mous and mouse the system suggests ["mouse","mousepad"]
Example 2:
Input: products = ["havana"], searchWord = "havana"
Output: [["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]]
Example 3:
Input: products = ["bags","baggage","banner","box","cloths"], searchWord = "bags"
Output: [["baggage","bags","banner"],["baggage","bags","banner"],["baggage","bags"],["bags"]]
Constraints:
•
1 <= products.length <= 1000•
1 <= products[i].length <= 3000•
1 <= sum(products[i].length) <= 2 * 10^4• All the strings of
products are unique.•
products[i] consists of lowercase English letters.•
1 <= searchWord.length <= 1000•
searchWord consists of lowercase English letters.2022-06-20
820. Short Encoding of Words
Topic: Array, Hash Table, String, Trie
Difficulty: Medium
Problem:
A valid encoding of an array of
•
• The reference string
• For each index
Given an array of
Example 1:
Example 2:
Constraints:
•
•
•
820. Short Encoding of Words
Topic: Array, Hash Table, String, Trie
Difficulty: Medium
Problem:
A valid encoding of an array of
words is any reference string s and array of indices indices such that:•
words.length == indices.length• The reference string
s ends with the '#' character.• For each index
indices[i], the substring of s starting from indices[i] and up to (but not including) the next '#' character is equal to words[i].Given an array of
words, return the length of the shortest reference string s possible of any valid encoding of words.Example 1:
Input: words = ["time", "me", "bell"]
Output: 10
Explanation: A valid encoding would be s = "time#bell#" and indices = [0, 2, 5].
words[0] = "time", the substring of s starting from indices[0] = 0 to the next '#' is underlined in "time#bell#"
words[1] = "me", the substring of s starting from indices[1] = 2 to the next '#' is underlined in "time#bell#"
words[2] = "bell", the substring of s starting from indices[2] = 5 to the next '#' is underlined in "time#bell#"
Example 2:
Input: words = ["t"]
Output: 2
Explanation: A valid encoding would be s = "t#" and indices = [0].
Constraints:
•
1 <= words.length <= 2000•
1 <= words[i].length <= 7•
words[i] consists of only lowercase letters.2022-06-21
1642. Furthest Building You Can Reach
Topic: Array, Greedy, Heap (Priority Queue)
Difficulty: Medium
Problem:
You are given an integer array
You start your journey from building
While moving from building
• If the current building's height is greater than or equal to the next building's height, you do not need a ladder or bricks.
• If the current building's height is less than the next building's height, you can either use one ladder or
Return the furthest building index (0-indexed) you can reach if you use the given ladders and bricks optimally.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/27/q4.gif
Example 2:
Example 3:
Constraints:
•
•
•
•
1642. Furthest Building You Can Reach
Topic: Array, Greedy, Heap (Priority Queue)
Difficulty: Medium
Problem:
You are given an integer array
heights representing the heights of buildings, some bricks, and some ladders.You start your journey from building
0 and move to the next building by possibly using bricks or ladders.While moving from building
i to building i+1 (0-indexed),• If the current building's height is greater than or equal to the next building's height, you do not need a ladder or bricks.
• If the current building's height is less than the next building's height, you can either use one ladder or
(h[i+1] - h[i]) bricks.Return the furthest building index (0-indexed) you can reach if you use the given ladders and bricks optimally.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/27/q4.gif
Input: heights = [4,2,7,6,9,14,12], bricks = 5, ladders = 1
Output: 4
Explanation: Starting at building 0, you can follow these steps:
- Go to building 1 without using ladders nor bricks since 4 >= 2.
- Go to building 2 using 5 bricks. You must use either bricks or ladders because 2 < 7.
- Go to building 3 without using ladders nor bricks since 7 >= 6.
- Go to building 4 using your only ladder. You must use either bricks or ladders because 6 < 9.
It is impossible to go beyond building 4 because you do not have any more bricks or ladders.
Example 2:
Input: heights = [4,12,2,7,3,18,20,3,19], bricks = 10, ladders = 2
Output: 7
Example 3:
Input: heights = [14,3,19,3], bricks = 17, ladders = 0
Output: 3
Constraints:
•
1 <= heights.length <= 10^5•
1 <= heights[i] <= 10^6•
0 <= bricks <= 10^9•
0 <= ladders <= heights.length2022-06-22
215. Kth Largest Element in an Array
Topic: Array, Divide and Conquer, Sorting, Heap (Priority Queue), Quickselect
Difficulty: Medium
Problem:
Given an integer array
Note that it is the
Example 1:
Example 2:
Constraints:
•
•
215. Kth Largest Element in an Array
Topic: Array, Divide and Conquer, Sorting, Heap (Priority Queue), Quickselect
Difficulty: Medium
Problem:
Given an integer array
nums and an integer k, return the k^th largest element in the array.Note that it is the
k^th largest element in the sorted order, not the k^th distinct element.Example 1:
Input: nums = [3,2,1,5,6,4], k = 2
Output: 5
Example 2:
Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
Output: 4
Constraints:
•
1 <= k <= nums.length <= 10^4•
-10^4 <= nums[i] <= 10^42022-06-23
630. Course Schedule III
Topic: Array, Greedy, Heap (Priority Queue)
Difficulty: Hard
Problem:
There are
You will start on the
Return the maximum number of courses that you can take.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
630. Course Schedule III
Topic: Array, Greedy, Heap (Priority Queue)
Difficulty: Hard
Problem:
There are
n different online courses numbered from 1 to n. You are given an array courses where courses[i] = [duration_i, lastDay_i] indicate that the i^th course should be taken continuously for duration_i days and must be finished before or on lastDay_i.You will start on the
1^st day and you cannot take two or more courses simultaneously.Return the maximum number of courses that you can take.
Example 1:
Input: courses = [[100,200],[200,1300],[1000,1250],[2000,3200]]
Output: 3
Explanation:
There are totally 4 courses, but you can take 3 courses at most:
First, take the 1^st course, it costs 100 days so you will finish it on the 100^th day, and ready to take the next course on the 101^st day.
Second, take the 3^rd course, it costs 1000 days so you will finish it on the 1100^th day, and ready to take the next course on the 1101^st day.
Third, take the 2^nd course, it costs 200 days so you will finish it on the 1300^th day.
The 4^th course cannot be taken now, since you will finish it on the 3300^th day, which exceeds the closed date.
Example 2:
Input: courses = [[1,2]]
Output: 1
Example 3:
Input: courses = [[3,2],[4,3]]
Output: 0
Constraints:
•
1 <= courses.length <= 10^4•
1 <= duration_i, lastDay_i <= 10^42022-06-24
1354. Construct Target Array With Multiple Sums
Topic: Array, Heap (Priority Queue)
Difficulty: Hard
Problem:
You are given an array
• let
• choose index
• You may repeat this procedure as many times as needed.
Return
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
1354. Construct Target Array With Multiple Sums
Topic: Array, Heap (Priority Queue)
Difficulty: Hard
Problem:
You are given an array
target of n integers. From a starting array arr consisting of n 1's, you may perform the following procedure :• let
x be the sum of all elements currently in your array.• choose index
i, such that 0 <= i < n and set the value of arr at index i to x.• You may repeat this procedure as many times as needed.
Return
true if it is possible to construct the target array from arr, otherwise, return false.Example 1:
Input: target = [9,3,5]
Output: true
Explanation: Start with arr = [1, 1, 1]
[1, 1, 1], sum = 3 choose index 1
[1, 3, 1], sum = 5 choose index 2
[1, 3, 5], sum = 9 choose index 0
[9, 3, 5] Done
Example 2:
Input: target = [1,1,1,2]
Output: false
Explanation: Impossible to create target array from [1,1,1,1].
Example 3:
Input: target = [8,5]
Output: true
Constraints:
•
n == target.length•
1 <= n <= 5 * 10^4•
1 <= target[i] <= 10^92022-06-25
665. Non-decreasing Array
Topic: Array
Difficulty: Medium
Problem:
Given an array
We define an array is non-decreasing if
Example 1:
Example 2:
Constraints:
•
•
•
665. Non-decreasing Array
Topic: Array
Difficulty: Medium
Problem:
Given an array
nums with n integers, your task is to check if it could become non-decreasing by modifying at most one element.We define an array is non-decreasing if
nums[i] <= nums[i + 1] holds for every i (0-based) such that (0 <= i <= n - 2).Example 1:
Input: nums = [4,2,3]
Output: true
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
Example 2:
Input: nums = [4,2,1]
Output: false
Explanation: You can't get a non-decreasing array by modify at most one element.
Constraints:
•
n == nums.length•
1 <= n <= 10^4•
-10^5 <= nums[i] <= 10^52022-06-26
1423. Maximum Points You Can Obtain from Cards
Topic: Array, Sliding Window, Prefix Sum
Difficulty: Medium
Problem:
There are several cards arranged in a row, and each card has an associated number of points. The points are given in the integer array
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly
Your score is the sum of the points of the cards you have taken.
Given the integer array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
1423. Maximum Points You Can Obtain from Cards
Topic: Array, Sliding Window, Prefix Sum
Difficulty: Medium
Problem:
There are several cards arranged in a row, and each card has an associated number of points. The points are given in the integer array
cardPoints.In one step, you can take one card from the beginning or from the end of the row. You have to take exactly
k cards.Your score is the sum of the points of the cards you have taken.
Given the integer array
cardPoints and the integer k, return the maximum score you can obtain.Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Constraints:
•
1 <= cardPoints.length <= 10^5•
1 <= cardPoints[i] <= 10^4•
1 <= k <= cardPoints.length2022-06-27
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
Topic: String, Greedy
Difficulty: Medium
Problem:
A decimal number is called deci-binary if each of its digits is either
Given a string
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
Topic: String, Greedy
Difficulty: Medium
Problem:
A decimal number is called deci-binary if each of its digits is either
0 or 1 without any leading zeros. For example, 101 and 1100 are deci-binary, while 112 and 3001 are not.Given a string
n that represents a positive decimal integer, return the minimum number of positive deci-binary numbers needed so that they sum up to n.Example 1:
Input: n = "32"
Output: 3
Explanation: 10 + 11 + 11 = 32
Example 2:
Input: n = "82734"
Output: 8
Example 3:
Input: n = "27346209830709182346"
Output: 9
Constraints:
•
1 <= n.length <= 10^5•
n consists of only digits.•
n does not contain any leading zeros and represents a positive integer.2022-06-28
1647. Minimum Deletions to Make Character Frequencies Unique
Topic: String, Greedy, Sorting
Difficulty: Medium
Problem:
A string
Given a string
The frequency of a character in a string is the number of times it appears in the string. For example, in the string
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1647. Minimum Deletions to Make Character Frequencies Unique
Topic: String, Greedy, Sorting
Difficulty: Medium
Problem:
A string
s is called good if there are no two different characters in s that have the same frequency.Given a string
s, return the minimum number of characters you need to delete to make s good.The frequency of a character in a string is the number of times it appears in the string. For example, in the string
"aab", the frequency of 'a' is 2, while the frequency of 'b' is 1.Example 1:
Input: s = "aab"
Output: 0
Explanation: s is already good.
Example 2:
Input: s = "aaabbbcc"
Output: 2
Explanation: You can delete two 'b's resulting in the good string "aaabcc".
Another way it to delete one 'b' and one 'c' resulting in the good string "aaabbc".
Example 3:
Input: s = "ceabaacb"
Output: 2
Explanation: You can delete both 'c's resulting in the good string "eabaab".
Note that we only care about characters that are still in the string at the end (i.e. frequency of 0 is ignored).
Constraints:
•
1 <= s.length <= 10^5•
s contains only lowercase English letters.2022-06-29
406. Queue Reconstruction by Height
Topic: Array, Greedy, Binary Indexed Tree, Segment Tree, Sorting
Difficulty: Medium
Problem:
You are given an array of people,
Reconstruct and return the queue that is represented by the input array
Example 1:
Example 2:
Constraints:
•
•
•
• It is guaranteed that the queue can be reconstructed.
406. Queue Reconstruction by Height
Topic: Array, Greedy, Binary Indexed Tree, Segment Tree, Sorting
Difficulty: Medium
Problem:
You are given an array of people,
people, which are the attributes of some people in a queue (not necessarily in order). Each people[i] = [h_i, k_i] represents the i^th person of height h_i with exactly k_i other people in front who have a height greater than or equal to h_i.Reconstruct and return the queue that is represented by the input array
people. The returned queue should be formatted as an array queue, where queue[j] = [h_j, k_j] is the attributes of the j^th person in the queue (queue[0] is the person at the front of the queue).Example 1:
Input: people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
Output: [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
Explanation:
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
Example 2:
Input: people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
Output: [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
Constraints:
•
1 <= people.length <= 2000•
0 <= h_i <= 10^6•
0 <= k_i < people.length• It is guaranteed that the queue can be reconstructed.