2022-10-14
2095. Delete the Middle Node of a Linked List
Topic: Linked List, Two Pointers
Difficulty: Medium
Problem:
You are given the
The middle node of a linked list of size
• For
Example 1:
Image: https://assets.leetcode.com/uploads/2021/11/16/eg1drawio.png
Example 2:
Image: https://assets.leetcode.com/uploads/2021/11/16/eg2drawio.png
Example 3:
Image: https://assets.leetcode.com/uploads/2021/11/16/eg3drawio.png
Constraints:
• The number of nodes in the list is in the range
•
2095. Delete the Middle Node of a Linked List
Topic: Linked List, Two Pointers
Difficulty: Medium
Problem:
You are given the
head of a linked list. Delete the middle node, and return the head of the modified linked list.The middle node of a linked list of size
n is the ⌊n / 2⌋^th node from the start using 0-based indexing, where ⌊x⌋ denotes the largest integer less than or equal to x.• For
n = 1, 2, 3, 4, and 5, the middle nodes are 0, 1, 1, 2, and 2, respectively.Example 1:
Image: https://assets.leetcode.com/uploads/2021/11/16/eg1drawio.png
Input: head = [1,3,4,7,1,2,6]
Output: [1,3,4,1,2,6]
Explanation:
The above figure represents the given linked list. The indices of the nodes are written below.
Since n = 7, node 3 with value 7 is the middle node, which is marked in red.
We return the new list after removing this node.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/11/16/eg2drawio.png
Input: head = [1,2,3,4]
Output: [1,2,4]
Explanation:
The above figure represents the given linked list.
For n = 4, node 2 with value 3 is the middle node, which is marked in red.
Example 3:
Image: https://assets.leetcode.com/uploads/2021/11/16/eg3drawio.png
Input: head = [2,1]
Output: [2]
Explanation:
The above figure represents the given linked list.
For n = 2, node 1 with value 1 is the middle node, which is marked in red.
Node 0 with value 2 is the only node remaining after removing node 1.
Constraints:
• The number of nodes in the list is in the range
[1, 10^5].•
1 <= Node.val <= 10^52022-10-15
1531. String Compression II
Topic: String, Dynamic Programming
Difficulty: Hard
Problem:
Run-length encoding is a string compression method that works by replacing consecutive identical characters (repeated 2 or more times) with the concatenation of the character and the number marking the count of the characters (length of the run). For example, to compress the string
Notice that in this problem, we are not adding
Given a string
Find the minimum length of the run-length encoded version of
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
1531. String Compression II
Topic: String, Dynamic Programming
Difficulty: Hard
Problem:
Run-length encoding is a string compression method that works by replacing consecutive identical characters (repeated 2 or more times) with the concatenation of the character and the number marking the count of the characters (length of the run). For example, to compress the string
"aabccc" we replace "aa" by "a2" and replace "ccc" by "c3". Thus the compressed string becomes "a2bc3".Notice that in this problem, we are not adding
'1' after single characters.Given a string
s and an integer k. You need to delete at most k characters from s such that the run-length encoded version of s has minimum length.Find the minimum length of the run-length encoded version of
s after deleting at most k characters.Example 1:
Input: s = "aaabcccd", k = 2
Output: 4
Explanation: Compressing s without deleting anything will give us "a3bc3d" of length 6. Deleting any of the characters 'a' or 'c' would at most decrease the length of the compressed string to 5, for instance delete 2 'a' then we will have s = "abcccd" which compressed is abc3d. Therefore, the optimal way is to delete 'b' and 'd', then the compressed version of s will be "a3c3" of length 4.
Example 2:
Input: s = "aabbaa", k = 2
Output: 2
Explanation: If we delete both 'b' characters, the resulting compressed string would be "a4" of length 2.
Example 3:
Input: s = "aaaaaaaaaaa", k = 0
Output: 3
Explanation: Since k is zero, we cannot delete anything. The compressed string is "a11" of length 3.
Constraints:
•
1 <= s.length <= 100•
0 <= k <= s.length•
s contains only lowercase English letters.2022-10-16
1335. Minimum Difficulty of a Job Schedule
Topic: Array, Dynamic Programming
Difficulty: Hard
Problem:
You want to schedule a list of jobs in
You have to finish at least one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the
You are given an integer array
Return the minimum difficulty of a job schedule. If you cannot find a schedule for the jobs return
Example 1:
Image: https://assets.leetcode.com/uploads/2020/01/16/untitled.png
Example 2:
Example 3:
Constraints:
•
•
•
1335. Minimum Difficulty of a Job Schedule
Topic: Array, Dynamic Programming
Difficulty: Hard
Problem:
You want to schedule a list of jobs in
d days. Jobs are dependent (i.e To work on the i^th job, you have to finish all the jobs j where 0 <= j < i).You have to finish at least one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the
d days. The difficulty of a day is the maximum difficulty of a job done on that day.You are given an integer array
jobDifficulty and an integer d. The difficulty of the i^th job is jobDifficulty[i].Return the minimum difficulty of a job schedule. If you cannot find a schedule for the jobs return
-1.Example 1:
Image: https://assets.leetcode.com/uploads/2020/01/16/untitled.png
Input: jobDifficulty = [6,5,4,3,2,1], d = 2
Output: 7
Explanation: First day you can finish the first 5 jobs, total difficulty = 6.
Second day you can finish the last job, total difficulty = 1.
The difficulty of the schedule = 6 + 1 = 7
Example 2:
Input: jobDifficulty = [9,9,9], d = 4
Output: -1
Explanation: If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs.
Example 3:
Input: jobDifficulty = [1,1,1], d = 3
Output: 3
Explanation: The schedule is one job per day. total difficulty will be 3.
Constraints:
•
1 <= jobDifficulty.length <= 300•
0 <= jobDifficulty[i] <= 1000•
1 <= d <= 102022-10-17
1832. Check if the Sentence Is Pangram
Topic: Hash Table, String
Difficulty: Easy
Problem:
A pangram is a sentence where every letter of the English alphabet appears at least once.
Given a string
Example 1:
Example 2:
Constraints:
•
•
1832. Check if the Sentence Is Pangram
Topic: Hash Table, String
Difficulty: Easy
Problem:
A pangram is a sentence where every letter of the English alphabet appears at least once.
Given a string
sentence containing only lowercase English letters, return true if sentence is a pangram, or false otherwise.Example 1:
Input: sentence = "thequickbrownfoxjumpsoverthelazydog"
Output: true
Explanation: sentence contains at least one of every letter of the English alphabet.
Example 2:
Input: sentence = "leetcode"
Output: false
Constraints:
•
1 <= sentence.length <= 1000•
sentence consists of lowercase English letters.2022-10-18
38. Count and Say
Topic: String
Difficulty: Medium
Problem:
The count-and-say sequence is a sequence of digit strings defined by the recursive formula:
•
•
To determine how you "say" a digit string, split it into the minimal number of substrings such that each substring contains exactly one unique digit. Then for each substring, say the number of digits, then say the digit. Finally, concatenate every said digit.
For example, the saying and conversion for digit string
Image: https://assets.leetcode.com/uploads/2020/10/23/countandsay.jpg
Given a positive integer
Example 1:
Example 2:
Constraints:
•
38. Count and Say
Topic: String
Difficulty: Medium
Problem:
The count-and-say sequence is a sequence of digit strings defined by the recursive formula:
•
countAndSay(1) = "1"•
countAndSay(n) is the way you would "say" the digit string from countAndSay(n-1), which is then converted into a different digit string.To determine how you "say" a digit string, split it into the minimal number of substrings such that each substring contains exactly one unique digit. Then for each substring, say the number of digits, then say the digit. Finally, concatenate every said digit.
For example, the saying and conversion for digit string
"3322251":Image: https://assets.leetcode.com/uploads/2020/10/23/countandsay.jpg
Given a positive integer
n, return the n^th term of the count-and-say sequence.Example 1:
Input: n = 1
Output: "1"
Explanation: This is the base case.
Example 2:
Input: n = 4
Output: "1211"
Explanation:
countAndSay(1) = "1"
countAndSay(2) = say "1" = one 1 = "11"
countAndSay(3) = say "11" = two 1's = "21"
countAndSay(4) = say "21" = one 2 + one 1 = "12" + "11" = "1211"
Constraints:
•
1 <= n <= 302022-10-19
692. Top K Frequent Words
Topic: Hash Table, String, Trie, Sorting, Heap (Priority Queue), Bucket Sort, Counting
Difficulty: Medium
Problem:
Given an array of strings
Return the answer sorted by the frequency from highest to lowest. Sort the words with the same frequency by their lexicographical order.
Example 1:
Example 2:
Constraints:
•
•
•
•
Follow-up: Could you solve it in
692. Top K Frequent Words
Topic: Hash Table, String, Trie, Sorting, Heap (Priority Queue), Bucket Sort, Counting
Difficulty: Medium
Problem:
Given an array of strings
words and an integer k, return the k most frequent strings.Return the answer sorted by the frequency from highest to lowest. Sort the words with the same frequency by their lexicographical order.
Example 1:
Input: words = ["i","love","leetcode","i","love","coding"], k = 2
Output: ["i","love"]
Explanation: "i" and "love" are the two most frequent words.
Note that "i" comes before "love" due to a lower alphabetical order.
Example 2:
Input: words = ["the","day","is","sunny","the","the","the","sunny","is","is"], k = 4
Output: ["the","is","sunny","day"]
Explanation: "the", "is", "sunny" and "day" are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively.
Constraints:
•
1 <= words.length <= 500•
1 <= words[i].length <= 10•
words[i] consists of lowercase English letters.•
k is in the range [1, The number of unique words[i]]Follow-up: Could you solve it in
O(n log(k)) time and O(n) extra space?2022-10-20
12. Integer to Roman
Topic: Hash Table, Math, String
Difficulty: Medium
Problem:
Roman numerals are represented by seven different symbols:
For example,
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not
•
•
•
Given an integer, convert it to a roman numeral.
Example 1:
Example 2:
Example 3:
Constraints:
•
12. Integer to Roman
Topic: Hash Table, Math, String
Difficulty: Medium
Problem:
Roman numerals are represented by seven different symbols:
I, V, X, L, C, D and M.Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example,
2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not
IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:•
I can be placed before V (5) and X (10) to make 4 and 9.•
X can be placed before L (50) and C (100) to make 40 and 90.•
C can be placed before D (500) and M (1000) to make 400 and 900.Given an integer, convert it to a roman numeral.
Example 1:
Input: num = 3
Output: "III"
Explanation: 3 is represented as 3 ones.
Example 2:
Input: num = 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.
Example 3:
Input: num = 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Constraints:
•
1 <= num <= 39992022-10-21
219. Contains Duplicate II
Topic: Array, Hash Table, Sliding Window
Difficulty: Easy
Problem:
Given an integer array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
219. Contains Duplicate II
Topic: Array, Hash Table, Sliding Window
Difficulty: Easy
Problem:
Given an integer array
nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k.Example 1:
Input: nums = [1,2,3,1], k = 3
Output: true
Example 2:
Input: nums = [1,0,1,1], k = 1
Output: true
Example 3:
Input: nums = [1,2,3,1,2,3], k = 2
Output: false
Constraints:
•
1 <= nums.length <= 10^5•
-10^9 <= nums[i] <= 10^9•
0 <= k <= 10^52022-10-22
76. Minimum Window Substring
Topic: Hash Table, String, Sliding Window
Difficulty: Hard
Problem:
Given two strings
The testcases will be generated such that the answer is unique.
A substring is a contiguous sequence of characters within the string.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
Follow up: Could you find an algorithm that runs in
76. Minimum Window Substring
Topic: Hash Table, String, Sliding Window
Difficulty: Hard
Problem:
Given two strings
s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string "".The testcases will be generated such that the answer is unique.
A substring is a contiguous sequence of characters within the string.
Example 1:
Input: s = "ADOBECODEBANC", t = "ABC"
Output: "BANC"
Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t.
Example 2:
Input: s = "a", t = "a"
Output: "a"
Explanation: The entire string s is the minimum window.
Example 3:
Input: s = "a", t = "aa"
Output: ""
Explanation: Both 'a's from t must be included in the window.
Since the largest window of s only has one 'a', return empty string.
Constraints:
•
m == s.length•
n == t.length•
1 <= m, n <= 10^5•
s and t consist of uppercase and lowercase English letters.Follow up: Could you find an algorithm that runs in
O(m + n) time?2022-10-23
645. Set Mismatch
Topic: Array, Hash Table, Bit Manipulation, Sorting
Difficulty: Easy
Problem:
You have a set of integers
You are given an integer array
Find the number that occurs twice and the number that is missing and return them in the form of an array.
Example 1:
Example 2:
Constraints:
•
•
645. Set Mismatch
Topic: Array, Hash Table, Bit Manipulation, Sorting
Difficulty: Easy
Problem:
You have a set of integers
s, which originally contains all the numbers from 1 to n. Unfortunately, due to some error, one of the numbers in s got duplicated to another number in the set, which results in repetition of one number and loss of another number.You are given an integer array
nums representing the data status of this set after the error.Find the number that occurs twice and the number that is missing and return them in the form of an array.
Example 1:
Input: nums = [1,2,2,4]
Output: [2,3]
Example 2:
Input: nums = [1,1]
Output: [1,2]
Constraints:
•
2 <= nums.length <= 10^4•
1 <= nums[i] <= 10^42022-10-24
1239. Maximum Length of a Concatenated String with Unique Characters
Topic: Array, String, Backtracking, Bit Manipulation
Difficulty: Medium
Problem:
You are given an array of strings
Return the maximum possible length of
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:
Example 3:
Constraints:
•
•
•
1239. Maximum Length of a Concatenated String with Unique Characters
Topic: Array, String, Backtracking, Bit Manipulation
Difficulty: Medium
Problem:
You are given an array of strings
arr. A string s is formed by the concatenation of a subsequence of arr that has unique characters.Return the maximum possible length of
s.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: arr = ["un","iq","ue"]
Output: 4
Explanation: All the valid concatenations are:
- ""
- "un"
- "iq"
- "ue"
- "uniq" ("un" + "iq")
- "ique" ("iq" + "ue")
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible longest valid concatenations are "chaers" ("cha" + "ers") and "acters" ("act" + "ers").
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Explanation: The only string in arr has all 26 characters.
Constraints:
•
1 <= arr.length <= 16•
1 <= arr[i].length <= 26•
arr[i] contains only lowercase English letters.2022-10-25
1662. Check If Two String Arrays are Equivalent
Topic: Array, String
Difficulty: Easy
Problem:
Given two string arrays
A string is represented by an array if the array elements concatenated in order forms the string.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
1662. Check If Two String Arrays are Equivalent
Topic: Array, String
Difficulty: Easy
Problem:
Given two string arrays
word1 and word2, return true if the two arrays represent the same string, and false otherwise.A string is represented by an array if the array elements concatenated in order forms the string.
Example 1:
Input: word1 = ["ab", "c"], word2 = ["a", "bc"]
Output: true
Explanation:
word1 represents string "ab" + "c" -> "abc"
word2 represents string "a" + "bc" -> "abc"
The strings are the same, so return true.
Example 2:
Input: word1 = ["a", "cb"], word2 = ["ab", "c"]
Output: false
Example 3:
Input: word1 = ["abc", "d", "defg"], word2 = ["abcddefg"]
Output: true
Constraints:
•
1 <= word1.length, word2.length <= 10^3•
1 <= word1[i].length, word2[i].length <= 10^3•
1 <= sum(word1[i].length), sum(word2[i].length) <= 10^3•
word1[i] and word2[i] consist of lowercase letters.2022-10-26
523. Continuous Subarray Sum
Topic: Array, Hash Table, Math, Prefix Sum
Difficulty: Medium
Problem:
Given an integer array
An integer
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
523. Continuous Subarray Sum
Topic: Array, Hash Table, Math, Prefix Sum
Difficulty: Medium
Problem:
Given an integer array
nums and an integer k, return true if nums has a continuous subarray of size at least two whose elements sum up to a multiple of k, or false otherwise.An integer
x is a multiple of k if there exists an integer n such that x = n * k. 0 is always a multiple of k.Example 1:
Input: nums = [23,2,4,6,7], k = 6
Output: true
Explanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
Example 2:
Input: nums = [23,2,6,4,7], k = 6
Output: true
Explanation: [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
Example 3:
Input: nums = [23,2,6,4,7], k = 13
Output: false
Constraints:
•
1 <= nums.length <= 10^5•
0 <= nums[i] <= 10^9•
0 <= sum(nums[i]) <= 2^31 - 1•
1 <= k <= 2^31 - 12022-10-27
835. Image Overlap
Topic: Array, Matrix
Difficulty: Medium
Problem:
You are given two images,
We translate one image however we choose by sliding all the
Note also that a translation does not include any kind of rotation. Any
Return the largest possible overlap.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/09/overlap1.jpg
Example 2:
Example 3:
Constraints:
•
•
•
•
•
835. Image Overlap
Topic: Array, Matrix
Difficulty: Medium
Problem:
You are given two images,
img1 and img2, represented as binary, square matrices of size n x n. A binary matrix has only 0s and 1s as values.We translate one image however we choose by sliding all the
1 bits left, right, up, and/or down any number of units. We then place it on top of the other image. We can then calculate the overlap by counting the number of positions that have a 1 in both images.Note also that a translation does not include any kind of rotation. Any
1 bits that are translated outside of the matrix borders are erased.Return the largest possible overlap.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/09/overlap1.jpg
Input: img1 = [[1,1,0],[0,1,0],[0,1,0]], img2 = [[0,0,0],[0,1,1],[0,0,1]]
Output: 3
Explanation: We translate img1 to right by 1 unit and down by 1 unit.
Image: [https://assets.leetcode.com/uploads/2020/09/09/overlap_step1.jpg](https://assets.leetcode.com/uploads/2020/09/09/overlap_step1.jpg)
The number of positions that have a 1 in both images is 3 (shown in red).
Image: [https://assets.leetcode.com/uploads/2020/09/09/overlap_step2.jpg](https://assets.leetcode.com/uploads/2020/09/09/overlap_step2.jpg)
Example 2:
Input: img1 = [[1]], img2 = [[1]]
Output: 1
Example 3:
Input: img1 = [[0]], img2 = [[0]]
Output: 0
Constraints:
•
n == img1.length == img1[i].length•
n == img2.length == img2[i].length•
1 <= n <= 30•
img1[i][j] is either 0 or 1.•
img2[i][j] is either 0 or 1.2022-10-28
49. Group Anagrams
Topic: Array, Hash Table, String, Sorting
Difficulty: Medium
Problem:
Given an array of 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:
Example 3:
Constraints:
•
•
•
49. Group Anagrams
Topic: Array, Hash Table, String, Sorting
Difficulty: Medium
Problem:
Given an array of strings
strs, group the anagrams together. You can 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: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
Example 2:
Input: strs = [""]
Output: [[""]]
Example 3:
Input: strs = ["a"]
Output: [["a"]]
Constraints:
•
1 <= strs.length <= 10^4•
0 <= strs[i].length <= 100•
strs[i] consists of lowercase English letters.2022-10-29
2136. Earliest Possible Day of Full Bloom
Topic: Array, Greedy, Sorting
Difficulty: Hard
Problem:
You have
•
•
From the beginning of day
Return the earliest possible day where all seeds are blooming.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/12/21/1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2021/12/21/2.png
Example 3:
Constraints:
•
•
•
2136. Earliest Possible Day of Full Bloom
Topic: Array, Greedy, Sorting
Difficulty: Hard
Problem:
You have
n flower seeds. Every seed must be planted first before it can begin to grow, then bloom. Planting a seed takes time and so does the growth of a seed. You are given two 0-indexed integer arrays plantTime and growTime, of length n each:•
plantTime[i] is the number of full days it takes you to plant the i^th seed. Every day, you can work on planting exactly one seed. You do not have to work on planting the same seed on consecutive days, but the planting of a seed is not complete until you have worked plantTime[i] days on planting it in total.•
growTime[i] is the number of full days it takes the i^th seed to grow after being completely planted. After the last day of its growth, the flower blooms and stays bloomed forever.From the beginning of day
0, you can plant the seeds in any order.Return the earliest possible day where all seeds are blooming.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/12/21/1.png
Input: plantTime = [1,4,3], growTime = [2,3,1]
Output: 9
Explanation: The grayed out pots represent planting days, colored pots represent growing days, and the flower represents the day it blooms.
One optimal way is:
On day 0, plant the 0^th seed. The seed grows for 2 full days and blooms on day 3.
On days 1, 2, 3, and 4, plant the 1^st seed. The seed grows for 3 full days and blooms on day 8.
On days 5, 6, and 7, plant the 2^nd seed. The seed grows for 1 full day and blooms on day 9.
Thus, on day 9, all the seeds are blooming.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/12/21/2.png
Input: plantTime = [1,2,3,2], growTime = [2,1,2,1]
Output: 9
Explanation: The grayed out pots represent planting days, colored pots represent growing days, and the flower represents the day it blooms.
One optimal way is:
On day 1, plant the 0^th seed. The seed grows for 2 full days and blooms on day 4.
On days 0 and 3, plant the 1^st seed. The seed grows for 1 full day and blooms on day 5.
On days 2, 4, and 5, plant the 2^nd seed. The seed grows for 2 full days and blooms on day 8.
On days 6 and 7, plant the 3^rd seed. The seed grows for 1 full day and blooms on day 9.
Thus, on day 9, all the seeds are blooming.
Example 3:
Input: plantTime = [1], growTime = [1]
Output: 2
Explanation: On day 0, plant the 0^th seed. The seed grows for 1 full day and blooms on day 2.
Thus, on day 2, all the seeds are blooming.
Constraints:
•
n == plantTime.length == growTime.length•
1 <= n <= 10^5•
1 <= plantTime[i], growTime[i] <= 10^42022-10-30
1293. Shortest Path in a Grid with Obstacles Elimination
Topic: Array, Breadth-First Search, Matrix
Difficulty: Hard
Problem:
You are given an
Return the minimum number of steps to walk from the upper left corner
Example 1:
Image: https://assets.leetcode.com/uploads/2021/09/30/short1-grid.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/09/30/short2-grid.jpg
Constraints:
•
•
•
•
•
•
1293. Shortest Path in a Grid with Obstacles Elimination
Topic: Array, Breadth-First Search, Matrix
Difficulty: Hard
Problem:
You are given an
m x n integer matrix grid where each cell is either 0 (empty) or 1 (obstacle). You can move up, down, left, or right from and to an empty cell in one step.Return the minimum number of steps to walk from the upper left corner
(0, 0) to the lower right corner (m - 1, n - 1) given that you can eliminate at most k obstacles. If it is not possible to find such walk return -1.Example 1:
Image: https://assets.leetcode.com/uploads/2021/09/30/short1-grid.jpg
Input: grid = [[0,0,0],[1,1,0],[0,0,0],[0,1,1],[0,0,0]], k = 1
Output: 6
Explanation:
The shortest path without eliminating any obstacle is 10.
The shortest path with one obstacle elimination at position (3,2) is 6. Such path is (0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2) -> (3,2) -> (4,2).
Example 2:
Image: https://assets.leetcode.com/uploads/2021/09/30/short2-grid.jpg
Input: grid = [[0,1,1],[1,1,1],[1,0,0]], k = 1
Output: -1
Explanation: We need to eliminate at least two obstacles to find such a walk.
Constraints:
•
m == grid.length•
n == grid[i].length•
1 <= m, n <= 40•
1 <= k <= m * n•
grid[i][j] is either 0 or 1.•
grid[0][0] == grid[m - 1][n - 1] == 02022-10-31
766. Toeplitz Matrix
Topic: Array, Matrix
Difficulty: Easy
Problem:
Given an
A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/04/ex1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/04/ex2.jpg
Constraints:
•
•
•
•
Follow up:
• What if the
• What if the
766. Toeplitz Matrix
Topic: Array, Matrix
Difficulty: Easy
Problem:
Given an
m x n matrix, return true if the matrix is Toeplitz. Otherwise, return false.A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/04/ex1.jpg
Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]
Output: true
Explanation:
In the above grid, the diagonals are:
"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]".
In each diagonal all elements are the same, so the answer is True.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/04/ex2.jpg
Input: matrix = [[1,2],[2,2]]
Output: false
Explanation:
The diagonal "[1, 2]" has different elements.
Constraints:
•
m == matrix.length•
n == matrix[i].length•
1 <= m, n <= 20•
0 <= matrix[i][j] <= 99Follow up:
• What if the
matrix is stored on disk, and the memory is limited such that you can only load at most one row of the matrix into the memory at once?• What if the
matrix is so large that you can only load up a partial row into the memory at once?2022-11-01
1706. Where Will the Ball Fall
Topic: Array, Dynamic Programming, Depth-First Search, Matrix, Simulation
Difficulty: Medium
Problem:
You have a 2-D
Each cell in the box has a diagonal board spanning two corners of the cell that can redirect a ball to the right or to the left.
• A board that redirects the ball to the right spans the top-left corner to the bottom-right corner and is represented in the grid as
• A board that redirects the ball to the left spans the top-right corner to the bottom-left corner and is represented in the grid as
We drop one ball at the top of each column of the box. Each ball can get stuck in the box or fall out of the bottom. A ball gets stuck if it hits a "V" shaped pattern between two boards or if a board redirects the ball into either wall of the box.
Return an array
Example 1:
Image: https://assets.leetcode.com/uploads/2019/09/26/ball.jpg
Example 2:
Example 3:
Constraints:
•
•
•
•
1706. Where Will the Ball Fall
Topic: Array, Dynamic Programming, Depth-First Search, Matrix, Simulation
Difficulty: Medium
Problem:
You have a 2-D
grid of size m x n representing a box, and you have n balls. The box is open on the top and bottom sides.Each cell in the box has a diagonal board spanning two corners of the cell that can redirect a ball to the right or to the left.
• A board that redirects the ball to the right spans the top-left corner to the bottom-right corner and is represented in the grid as
1.• A board that redirects the ball to the left spans the top-right corner to the bottom-left corner and is represented in the grid as
-1.We drop one ball at the top of each column of the box. Each ball can get stuck in the box or fall out of the bottom. A ball gets stuck if it hits a "V" shaped pattern between two boards or if a board redirects the ball into either wall of the box.
Return an array
answer of size n where answer[i] is the column that the ball falls out of at the bottom after dropping the ball from the i^th column at the top, or -1 if the ball gets stuck in the box.Example 1:
Image: https://assets.leetcode.com/uploads/2019/09/26/ball.jpg
Input: grid = [[1,1,1,-1,-1],[1,1,1,-1,-1],[-1,-1,-1,1,1],[1,1,1,1,-1],[-1,-1,-1,-1,-1]]
Output: [1,-1,-1,-1,-1]
Explanation: This example is shown in the photo.
Ball b0 is dropped at column 0 and falls out of the box at column 1.
Ball b1 is dropped at column 1 and will get stuck in the box between column 2 and 3 and row 1.
Ball b2 is dropped at column 2 and will get stuck on the box between column 2 and 3 and row 0.
Ball b3 is dropped at column 3 and will get stuck on the box between column 2 and 3 and row 0.
Ball b4 is dropped at column 4 and will get stuck on the box between column 2 and 3 and row 1.
Example 2:
Input: grid = [[-1]]
Output: [-1]
Explanation: The ball gets stuck against the left wall.
Example 3:
Input: grid = [[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1],[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1]]
Output: [0,1,2,3,4,-1]
Constraints:
•
m == grid.length•
n == grid[i].length•
1 <= m, n <= 100•
grid[i][j] is 1 or -1.2022-11-02
433. Minimum Genetic Mutation
Topic: Hash Table, String, Breadth-First Search
Difficulty: Medium
Problem:
A gene string can be represented by an 8-character long string, with choices from
Suppose we need to investigate a mutation from a gene string
• For example,
There is also a gene bank
Given the two gene strings
Note that the starting point is assumed to be valid, so it might not be included in the bank.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
•
433. Minimum Genetic Mutation
Topic: Hash Table, String, Breadth-First Search
Difficulty: Medium
Problem:
A gene string can be represented by an 8-character long string, with choices from
'A', 'C', 'G', and 'T'.Suppose we need to investigate a mutation from a gene string
start to a gene string end where one mutation is defined as one single character changed in the gene string.• For example,
"AACCGGTT" --> "AACCGGTA" is one mutation.There is also a gene bank
bank that records all the valid gene mutations. A gene must be in bank to make it a valid gene string.Given the two gene strings
start and end and the gene bank bank, return the minimum number of mutations needed to mutate from start to end. If there is no such a mutation, return -1.Note that the starting point is assumed to be valid, so it might not be included in the bank.
Example 1:
Input: start = "AACCGGTT", end = "AACCGGTA", bank = ["AACCGGTA"]
Output: 1
Example 2:
Input: start = "AACCGGTT", end = "AAACGGTA", bank = ["AACCGGTA","AACCGCTA","AAACGGTA"]
Output: 2
Example 3:
Input: start = "AAAAACCC", end = "AACCCCCC", bank = ["AAAACCCC","AAACCCCC","AACCCCCC"]
Output: 3
Constraints:
•
start.length == 8•
end.length == 8•
0 <= bank.length <= 10•
bank[i].length == 8•
start, end, and bank[i] consist of only the characters ['A', 'C', 'G', 'T'].