2024-01-24
1457. Pseudo-Palindromic Paths in a Binary Tree
Topic: Bit Manipulation, Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given a binary tree where node values are digits from 1 to 9. A path in the binary tree is said to be pseudo-palindromic if at least one permutation of the node values in the path is a palindrome.
Return the number of pseudo-palindromic paths going from the root node to leaf nodes.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/05/06/palindromic_paths_1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2020/05/07/palindromic_paths_2.png
Example 3:
Constraints:
• The number of nodes in the tree is in the range
•
1457. Pseudo-Palindromic Paths in a Binary Tree
Topic: Bit Manipulation, Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given a binary tree where node values are digits from 1 to 9. A path in the binary tree is said to be pseudo-palindromic if at least one permutation of the node values in the path is a palindrome.
Return the number of pseudo-palindromic paths going from the root node to leaf nodes.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/05/06/palindromic_paths_1.png
Input: root = [2,3,1,3,1,null,1]
Output: 2
Explanation: The figure above represents the given binary tree. There are three paths going from the root node to leaf nodes: the red path [2,3,3], the green path [2,1,1], and the path [2,3,1]. Among these paths only red path and green path are pseudo-palindromic paths since the red path [2,3,3] can be rearranged in [3,2,3] (palindrome) and the green path [2,1,1] can be rearranged in [1,2,1] (palindrome).
Example 2:
Image: https://assets.leetcode.com/uploads/2020/05/07/palindromic_paths_2.png
Input: root = [2,1,1,1,3,null,null,null,null,null,1]
Output: 1
Explanation: The figure above represents the given binary tree. There are three paths going from the root node to leaf nodes: the green path [2,1,1], the path [2,1,3,1], and the path [2,1]. Among these paths only the green path is pseudo-palindromic since [2,1,1] can be rearranged in [1,2,1] (palindrome).
Example 3:
Input: root = [9]
Output: 1
Constraints:
• The number of nodes in the tree is in the range
[1, 10^5].•
1 <= Node.val <= 92024-01-25
1143. Longest Common Subsequence
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
Given two strings
A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
• For example,
A common subsequence of two strings is a subsequence that is common to both strings.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1143. Longest Common Subsequence
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
Given two strings
text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
• For example,
"ace" is a subsequence of "abcde".A common subsequence of two strings is a subsequence that is common to both strings.
Example 1:
Input: text1 = "abcde", text2 = "ace"
Output: 3
Explanation: The longest common subsequence is "ace" and its length is 3.
Example 2:
Input: text1 = "abc", text2 = "abc"
Output: 3
Explanation: The longest common subsequence is "abc" and its length is 3.
Example 3:
Input: text1 = "abc", text2 = "def"
Output: 0
Explanation: There is no such common subsequence, so the result is 0.
Constraints:
•
1 <= text1.length, text2.length <= 1000•
text1 and text2 consist of only lowercase English characters.2024-01-26
576. Out of Boundary Paths
Topic: Dynamic Programming
Difficulty: Medium
Problem:
There is an
Given the five integers
Example 1:
Image: https://assets.leetcode.com/uploads/2021/04/28/out_of_boundary_paths_1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2021/04/28/out_of_boundary_paths_2.png
Constraints:
•
•
•
•
576. Out of Boundary Paths
Topic: Dynamic Programming
Difficulty: Medium
Problem:
There is an
m x n grid with a ball. The ball is initially at the position [startRow, startColumn]. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid crossing the grid boundary). You can apply at most maxMove moves to the ball.Given the five integers
m, n, maxMove, startRow, startColumn, return the number of paths to move the ball out of the grid boundary. Since the answer can be very large, return it modulo 10^9 + 7.Example 1:
Image: https://assets.leetcode.com/uploads/2021/04/28/out_of_boundary_paths_1.png
Input: m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0
Output: 6
Example 2:
Image: https://assets.leetcode.com/uploads/2021/04/28/out_of_boundary_paths_2.png
Input: m = 1, n = 3, maxMove = 3, startRow = 0, startColumn = 1
Output: 12
Constraints:
•
1 <= m, n <= 50•
0 <= maxMove <= 50•
0 <= startRow < m•
0 <= startColumn < n2024-01-27
629. K Inverse Pairs Array
Topic: Dynamic Programming
Difficulty: Hard
Problem:
For an integer array
Given two integers n and k, return the number of different arrays consist of numbers from
Example 1:
Example 2:
Constraints:
•
•
629. K Inverse Pairs Array
Topic: Dynamic Programming
Difficulty: Hard
Problem:
For an integer array
nums, an inverse pair is a pair of integers [i, j] where 0 <= i < j < nums.length and nums[i] > nums[j].Given two integers n and k, return the number of different arrays consist of numbers from
1 to n such that there are exactly k inverse pairs. Since the answer can be huge, return it modulo 10^9 + 7.Example 1:
Input: n = 3, k = 0
Output: 1
Explanation: Only the array [1,2,3] which consists of numbers from 1 to 3 has exactly 0 inverse pairs.
Example 2:
Input: n = 3, k = 1
Output: 2
Explanation: The array [1,3,2] and [2,1,3] have exactly 1 inverse pair.
Constraints:
•
1 <= n <= 1000•
0 <= k <= 10002024-01-28
1074. Number of Submatrices That Sum to Target
Topic: Array, Hash Table, Matrix, Prefix Sum
Difficulty: Hard
Problem:
Given a
A submatrix
Two submatrices
Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/02/mate1.jpg
Example 2:
Example 3:
Constraints:
•
•
•
•
1074. Number of Submatrices That Sum to Target
Topic: Array, Hash Table, Matrix, Prefix Sum
Difficulty: Hard
Problem:
Given a
matrix and a target, return the number of non-empty submatrices that sum to target.A submatrix
x1, y1, x2, y2 is the set of all cells matrix[x][y] with x1 <= x <= x2 and y1 <= y <= y2.Two submatrices
(x1, y1, x2, y2) and (x1', y1', x2', y2') are different if they have some coordinate that is different: for example, if x1 != x1'.Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/02/mate1.jpg
Input: matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0
Output: 4
Explanation: The four 1x1 submatrices that only contain 0.
Example 2:
Input: matrix = [[1,-1],[-1,1]], target = 0
Output: 5
Explanation: The two 1x2 submatrices, plus the two 2x1 submatrices, plus the 2x2 submatrix.
Example 3:
Input: matrix = [[904]], target = 0
Output: 0
Constraints:
•
1 <= matrix.length <= 100•
1 <= matrix[0].length <= 100•
-1000 <= matrix[i] <= 1000•
-10^8 <= target <= 10^82024-01-29
232. Implement Queue using Stacks
Topic: Stack, Design, Queue
Difficulty: Easy
Problem:
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (
Implement the
•
•
•
•
Notes:
• You must use only standard operations of a stack, which means only
• Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack's standard operations.
Example 1:
Constraints:
•
• At most
• All the calls to
Follow-up: Can you implement the queue such that each operation is amortized
232. Implement Queue using Stacks
Topic: Stack, Design, Queue
Difficulty: Easy
Problem:
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (
push, peek, pop, and empty).Implement the
MyQueue class:•
void push(int x) Pushes element x to the back of the queue.•
int pop() Removes the element from the front of the queue and returns it.•
int peek() Returns the element at the front of the queue.•
boolean empty() Returns true if the queue is empty, false otherwise.Notes:
• You must use only standard operations of a stack, which means only
push to top, peek/pop from top, size, and is empty operations are valid.• Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack's standard operations.
Example 1:
Input
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
Output
[null, null, null, 1, 1, false]
Explanation
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false
Constraints:
•
1 <= x <= 9• At most
100 calls will be made to push, pop, peek, and empty.• All the calls to
pop and peek are valid.Follow-up: Can you implement the queue such that each operation is amortized
O(1) time complexity? In other words, performing n operations will take overall O(n) time even if one of those operations may take longer.2024-01-30
150. Evaluate Reverse Polish Notation
Topic: Array, Math, Stack
Difficulty: Medium
Problem:
You are given an array of strings
Evaluate the expression. Return an integer that represents the value of the expression.
Note that:
• The valid operators are
• Each operand may be an integer or another expression.
• The division between two integers always truncates toward zero.
• There will not be any division by zero.
• The input represents a valid arithmetic expression in a reverse polish notation.
• The answer and all the intermediate calculations can be represented in a 32-bit integer.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
150. Evaluate Reverse Polish Notation
Topic: Array, Math, Stack
Difficulty: Medium
Problem:
You are given an array of strings
tokens that represents an arithmetic expression in a Reverse Polish Notation.Evaluate the expression. Return an integer that represents the value of the expression.
Note that:
• The valid operators are
'+', '-', '*', and '/'.• Each operand may be an integer or another expression.
• The division between two integers always truncates toward zero.
• There will not be any division by zero.
• The input represents a valid arithmetic expression in a reverse polish notation.
• The answer and all the intermediate calculations can be represented in a 32-bit integer.
Example 1:
Input: tokens = ["2","1","+","3","*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9
Example 2:
Input: tokens = ["4","13","5","/","+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6
Example 3:
Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
Output: 22
Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
Constraints:
•
1 <= tokens.length <= 10^4•
tokens[i] is either an operator: "+", "-", "*", or "/", or an integer in the range [-200, 200].2024-01-31
739. Daily Temperatures
Topic: Array, Stack, Monotonic Stack
Difficulty: Medium
Problem:
Given an array of integers
Example 1:
Example 2:
Example 3:
Constraints:
•
•
739. Daily Temperatures
Topic: Array, Stack, Monotonic Stack
Difficulty: Medium
Problem:
Given an array of integers
temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the i^th day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead.Example 1:
Input: temperatures = [73,74,75,71,69,72,76,73]
Output: [1,1,4,2,1,1,0,0]
Example 2:
Input: temperatures = [30,40,50,60]
Output: [1,1,1,0]
Example 3:
Input: temperatures = [30,60,90]
Output: [1,1,0]
Constraints:
•
1 <= temperatures.length <= 10^5•
30 <= temperatures[i] <= 1002024-02-01
2966. Divide Array Into Arrays With Max Difference
Topic: Array, Greedy, Sorting
Difficulty: Medium
Problem:
You are given an integer array
Divide the array into one or more arrays of size
• Each element of
• The difference between any two elements in one array is less than or equal to
Return a 2D array containing all the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return any of them.
Example 1:
Example 2:
Constraints:
•
•
•
•
•
2966. Divide Array Into Arrays With Max Difference
Topic: Array, Greedy, Sorting
Difficulty: Medium
Problem:
You are given an integer array
nums of size n and a positive integer k.Divide the array into one or more arrays of size
3 satisfying the following conditions:• Each element of
nums should be in exactly one array.• The difference between any two elements in one array is less than or equal to
k.Return a 2D array containing all the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return any of them.
Example 1:
Input: nums = [1,3,4,8,7,9,3,5,1], k = 2
Output: [[1,1,3],[3,4,5],[7,8,9]]
Explanation: We can divide the array into the following arrays: [1,1,3], [3,4,5] and [7,8,9].
The difference between any two elements in each array is less than or equal to 2.
Note that the order of elements is not important.
Example 2:
Input: nums = [1,3,3,2,7,3], k = 3
Output: []
Explanation: It is not possible to divide the array satisfying all the conditions.
Constraints:
•
n == nums.length•
1 <= n <= 10^5•
n is a multiple of 3.•
1 <= nums[i] <= 10^5•
1 <= k <= 10^52024-02-02
1291. Sequential Digits
Topic: Enumeration
Difficulty: Medium
Problem:
An integer has sequential digits if and only if each digit in the number is one more than the previous digit.
Return a sorted list of all the integers in the range
Example 1:
Example 2:
Constraints:
•
1291. Sequential Digits
Topic: Enumeration
Difficulty: Medium
Problem:
An integer has sequential digits if and only if each digit in the number is one more than the previous digit.
Return a sorted list of all the integers in the range
[low, high] inclusive that have sequential digits.Example 1:
Input: low = 100, high = 300
Output: [123,234]
Example 2:
Input: low = 1000, high = 13000
Output: [1234,2345,3456,4567,5678,6789,12345]
Constraints:
•
10 <= low <= high <= 10^92024-02-03
1043. Partition Array for Maximum Sum
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
Given an integer array
Return the largest sum of the given array after partitioning. Test cases are generated so that the answer fits in a 32-bit integer.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
1043. Partition Array for Maximum Sum
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
Given an integer array
arr, partition the array into (contiguous) subarrays of length at most k. After partitioning, each subarray has their values changed to become the maximum value of that subarray.Return the largest sum of the given array after partitioning. Test cases are generated so that the answer fits in a 32-bit integer.
Example 1:
Input: arr = [1,15,7,9,2,5,10], k = 3
Output: 84
Explanation: arr becomes [15,15,15,9,10,10,10]
Example 2:
Input: arr = [1,4,1,5,7,3,6,1,9,9,3], k = 4
Output: 83
Example 3:
Input: arr = [1], k = 1
Output: 1
Constraints:
•
1 <= arr.length <= 500•
0 <= arr[i] <= 10^9•
1 <= k <= arr.length2024-02-04
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.
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.
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?2024-02-05
387. First Unique Character in a String
Topic: Hash Table, String, Queue, Counting
Difficulty: Easy
Problem:
Given a string
Example 1:
Example 2:
Example 3:
Constraints:
•
•
387. First Unique Character in a String
Topic: Hash Table, String, Queue, Counting
Difficulty: Easy
Problem:
Given a string
s, find the first non-repeating character in it and return its index. If it does not exist, return -1.Example 1:
Input: s = "leetcode"
Output: 0
Example 2:
Input: s = "loveleetcode"
Output: 2
Example 3:
Input: s = "aabb"
Output: -1
Constraints:
•
1 <= s.length <= 10^5•
s consists of only lowercase English letters.2024-02-06
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.2024-02-07
451. Sort Characters By Frequency
Topic: Hash Table, String, Sorting, Heap (Priority Queue), Bucket Sort, Counting
Difficulty: Medium
Problem:
Given a string
Return the sorted string. If there are multiple answers, return any of them.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
451. Sort Characters By Frequency
Topic: Hash Table, String, Sorting, Heap (Priority Queue), Bucket Sort, Counting
Difficulty: Medium
Problem:
Given a string
s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string.Return the sorted string. If there are multiple answers, return any of them.
Example 1:
Input: s = "tree"
Output: "eert"
Explanation: 'e' appears twice while 'r' and 't' both appear once.
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
Example 2:
Input: s = "cccaaa"
Output: "aaaccc"
Explanation: Both 'c' and 'a' appear three times, so both "cccaaa" and "aaaccc" are valid answers.
Note that "cacaca" is incorrect, as the same characters must be together.
Example 3:
Input: s = "Aabb"
Output: "bbAa"
Explanation: "bbaA" is also a valid answer, but "Aabb" is incorrect.
Note that 'A' and 'a' are treated as two different characters.
Constraints:
•
1 <= s.length <= 5 * 10^5•
s consists of uppercase and lowercase English letters and digits.2024-02-08
279. Perfect Squares
Topic: Math, Dynamic Programming, Breadth-First Search
Difficulty: Medium
Problem:
Given an integer
A perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example,
Example 1:
Example 2:
Constraints:
•
279. Perfect Squares
Topic: Math, Dynamic Programming, Breadth-First Search
Difficulty: Medium
Problem:
Given an integer
n, return the least number of perfect square numbers that sum to n.A perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example,
1, 4, 9, and 16 are perfect squares while 3 and 11 are not.Example 1:
Input: n = 12
Output: 3
Explanation: 12 = 4 + 4 + 4.
Example 2:
Input: n = 13
Output: 2
Explanation: 13 = 4 + 9.
Constraints:
•
1 <= n <= 10^42024-02-09
368. Largest Divisible Subset
Topic: Array, Math, Dynamic Programming, Sorting
Difficulty: Medium
Problem:
Given a set of distinct positive integers
•
•
If there are multiple solutions, return any of them.
Example 1:
Example 2:
Constraints:
•
•
• All the integers in
368. Largest Divisible Subset
Topic: Array, Math, Dynamic Programming, Sorting
Difficulty: Medium
Problem:
Given a set of distinct positive integers
nums, return the largest subset answer such that every pair (answer[i], answer[j]) of elements in this subset satisfies:•
answer[i] % answer[j] == 0, or•
answer[j] % answer[i] == 0If there are multiple solutions, return any of them.
Example 1:
Input: nums = [1,2,3]
Output: [1,2]
Explanation: [1,3] is also accepted.
Example 2:
Input: nums = [1,2,4,8]
Output: [1,2,4,8]
Constraints:
•
1 <= nums.length <= 1000•
1 <= nums[i] <= 2 * 10^9• All the integers in
nums are unique.2024-02-10
647. Palindromic Substrings
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
Given a string
A string is a palindrome when it reads the same backward as forward.
A substring is a contiguous sequence of characters within the string.
Example 1:
Example 2:
Constraints:
•
•
647. Palindromic Substrings
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
Given a string
s, return the number of palindromic substrings in it.A string is a palindrome when it reads the same backward as forward.
A substring is a contiguous sequence of characters within the string.
Example 1:
Input: s = "abc"
Output: 3
Explanation: Three palindromic strings: "a", "b", "c".
Example 2:
Input: s = "aaa"
Output: 6
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
Constraints:
•
1 <= s.length <= 1000•
s consists of lowercase English letters.2024-02-11
1463. Cherry Pickup II
Topic: Array, Dynamic Programming, Matrix
Difficulty: Hard
Problem:
You are given a
You have two robots that can collect cherries for you:
• Robot #1 is located at the top-left corner
• Robot #2 is located at the top-right corner
Return the maximum number of cherries collection using both robots by following the rules below:
• From a cell
• When any robot passes through a cell, It picks up all cherries, and the cell becomes an empty cell.
• When both robots stay in the same cell, only one takes the cherries.
• Both robots cannot move outside of the grid at any moment.
• Both robots should reach the bottom row in
Example 1:
Image: https://assets.leetcode.com/uploads/2020/04/29/sample_1_1802.png
Example 2:
Image: https://assets.leetcode.com/uploads/2020/04/23/sample_2_1802.png
Constraints:
•
•
•
•
1463. Cherry Pickup II
Topic: Array, Dynamic Programming, Matrix
Difficulty: Hard
Problem:
You are given a
rows x cols matrix grid representing a field of cherries where grid[i][j] represents the number of cherries that you can collect from the (i, j) cell.You have two robots that can collect cherries for you:
• Robot #1 is located at the top-left corner
(0, 0), and• Robot #2 is located at the top-right corner
(0, cols - 1).Return the maximum number of cherries collection using both robots by following the rules below:
• From a cell
(i, j), robots can move to cell (i + 1, j - 1), (i + 1, j), or (i + 1, j + 1).• When any robot passes through a cell, It picks up all cherries, and the cell becomes an empty cell.
• When both robots stay in the same cell, only one takes the cherries.
• Both robots cannot move outside of the grid at any moment.
• Both robots should reach the bottom row in
grid.Example 1:
Image: https://assets.leetcode.com/uploads/2020/04/29/sample_1_1802.png
Input: grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]]
Output: 24
Explanation: Path of robot #1 and #2 are described in color green and blue respectively.
Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12.
Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12.
Total of cherries: 12 + 12 = 24.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/04/23/sample_2_1802.png
Input: grid = [[1,0,0,0,0,0,1],[2,0,0,0,0,3,0],[2,0,9,0,0,0,0],[0,3,0,5,4,0,0],[1,0,2,3,0,0,6]]
Output: 28
Explanation: Path of robot #1 and #2 are described in color green and blue respectively.
Cherries taken by Robot #1, (1 + 9 + 5 + 2) = 17.
Cherries taken by Robot #2, (1 + 3 + 4 + 3) = 11.
Total of cherries: 17 + 11 = 28.
Constraints:
•
rows == grid.length•
cols == grid[i].length•
2 <= rows, cols <= 70•
0 <= grid[i][j] <= 1002024-02-12
169. Majority Element
Topic: Array, Hash Table, Divide and Conquer, Sorting, Counting
Difficulty: Easy
Problem:
Given an array
The majority element is the element that appears more than
Example 1:
Example 2:
Constraints:
•
•
•
Follow-up: Could you solve the problem in linear time and in
169. Majority Element
Topic: Array, Hash Table, Divide and Conquer, Sorting, Counting
Difficulty: Easy
Problem:
Given an array
nums of size n, return the majority element.The majority element is the element that appears more than
⌊n / 2⌋ times. You may assume that the majority element always exists in the array.Example 1:
Input: nums = [3,2,3]
Output: 3
Example 2:
Input: nums = [2,2,1,1,1,2,2]
Output: 2
Constraints:
•
n == nums.length•
1 <= n <= 5 * 10^4•
-10^9 <= nums[i] <= 10^9Follow-up: Could you solve the problem in linear time and in
O(1) space?2024-02-13
2108. Find First Palindromic String in the Array
Topic: Array, Two Pointers, String
Difficulty: Easy
Problem:
Given an array of strings
A string is palindromic if it reads the same forward and backward.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
2108. Find First Palindromic String in the Array
Topic: Array, Two Pointers, String
Difficulty: Easy
Problem:
Given an array of strings
words, return the first palindromic string in the array. If there is no such string, return an empty string "".A string is palindromic if it reads the same forward and backward.
Example 1:
Input: words = ["abc","car","ada","racecar","cool"]
Output: "ada"
Explanation: The first string that is palindromic is "ada".
Note that "racecar" is also palindromic, but it is not the first.
Example 2:
Input: words = ["notapalindrome","racecar"]
Output: "racecar"
Explanation: The first and only string that is palindromic is "racecar".
Example 3:
Input: words = ["def","ghi"]
Output: ""
Explanation: There are no palindromic strings, so the empty string is returned.
Constraints:
•
1 <= words.length <= 100•
1 <= words[i].length <= 100•
words[i] consists only of lowercase English letters.