2024-02-21
201. Bitwise AND of Numbers Range
Topic: Bit Manipulation
Difficulty: Medium
Problem:
Given two integers
Example 1:
Example 2:
Example 3:
Constraints:
•
201. Bitwise AND of Numbers Range
Topic: Bit Manipulation
Difficulty: Medium
Problem:
Given two integers
left and right that represent the range [left, right], return the bitwise AND of all numbers in this range, inclusive.Example 1:
Input: left = 5, right = 7
Output: 4
Example 2:
Input: left = 0, right = 0
Output: 0
Example 3:
Input: left = 1, right = 2147483647
Output: 0
Constraints:
•
0 <= left <= right <= 2^31 - 12024-02-22
997. Find the Town Judge
Topic: Array, Hash Table, Graph
Difficulty: Easy
Problem:
In a town, there are
If the town judge exists, then:
1. The town judge trusts nobody.
2. Everybody (except for the town judge) trusts the town judge.
3. There is exactly one person that satisfies properties 1 and 2.
You are given an array
Return the label of the town judge if the town judge exists and can be identified, or return
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
• All the pairs of
•
•
997. Find the Town Judge
Topic: Array, Hash Table, Graph
Difficulty: Easy
Problem:
In a town, there are
n people labeled from 1 to n. There is a rumor that one of these people is secretly the town judge.If the town judge exists, then:
1. The town judge trusts nobody.
2. Everybody (except for the town judge) trusts the town judge.
3. There is exactly one person that satisfies properties 1 and 2.
You are given an array
trust where trust[i] = [a_i, b_i] representing that the person labeled a_i trusts the person labeled b_i. If a trust relationship does not exist in trust array, then such a trust relationship does not exist.Return the label of the town judge if the town judge exists and can be identified, or return
-1 otherwise.Example 1:
Input: n = 2, trust = [[1,2]]
Output: 2
Example 2:
Input: n = 3, trust = [[1,3],[2,3]]
Output: 3
Example 3:
Input: n = 3, trust = [[1,3],[2,3],[3,1]]
Output: -1
Constraints:
•
1 <= n <= 1000•
0 <= trust.length <= 10^4•
trust[i].length == 2• All the pairs of
trust are unique.•
a_i != b_i•
1 <= a_i, b_i <= n2024-02-23
787. Cheapest Flights Within K Stops
Topic: Dynamic Programming, Depth-First Search, Breadth-First Search, Graph, Heap (Priority Queue), Shortest Path
Difficulty: Medium
Problem:
There are
You are also given three integers
Example 1:
Image: https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-3drawio.png
Example 2:
Image: https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-1drawio.png
Example 3:
Image: https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-2drawio.png
Constraints:
•
•
•
•
•
•
• There will not be any multiple flights between two cities.
•
•
787. Cheapest Flights Within K Stops
Topic: Dynamic Programming, Depth-First Search, Breadth-First Search, Graph, Heap (Priority Queue), Shortest Path
Difficulty: Medium
Problem:
There are
n cities connected by some number of flights. You are given an array flights where flights[i] = [from_i, to_i, price_i] indicates that there is a flight from city from_i to city to_i with cost price_i.You are also given three integers
src, dst, and k, return the cheapest price from src to dst with at most k stops. If there is no such route, return -1.Example 1:
Image: https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-3drawio.png
Input: n = 4, flights = [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]], src = 0, dst = 3, k = 1
Output: 700
Explanation:
The graph is shown above.
The optimal path with at most 1 stop from city 0 to 3 is marked in red and has cost 100 + 600 = 700.
Note that the path through cities [0,1,2,3] is cheaper but is invalid because it uses 2 stops.
Example 2:
Image: https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-1drawio.png
Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1
Output: 200
Explanation:
The graph is shown above.
The optimal path with at most 1 stop from city 0 to 2 is marked in red and has cost 100 + 100 = 200.
Example 3:
Image: https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-2drawio.png
Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0
Output: 500
Explanation:
The graph is shown above.
The optimal path with no stops from city 0 to 2 is marked in red and has cost 500.
Constraints:
•
1 <= n <= 100•
0 <= flights.length <= (n * (n - 1) / 2)•
flights[i].length == 3•
0 <= from_i, to_i < n•
from_i != to_i•
1 <= price_i <= 10^4• There will not be any multiple flights between two cities.
•
0 <= src, dst, k < n•
src != dst2024-02-24
2092. Find All People With Secret
Topic: Depth-First Search, Breadth-First Search, Union Find, Graph, Sorting
Difficulty: Hard
Problem:
You are given an integer
Person
The secrets are shared instantaneously. That is, a person may receive the secret and share it with people in other meetings within the same time frame.
Return a list of all the people that have the secret after all the meetings have taken place. You may return the answer in any order.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
•
•
•
2092. Find All People With Secret
Topic: Depth-First Search, Breadth-First Search, Union Find, Graph, Sorting
Difficulty: Hard
Problem:
You are given an integer
n indicating there are n people numbered from 0 to n - 1. You are also given a 0-indexed 2D integer array meetings where meetings[i] = [x_i, y_i, time_i] indicates that person x_i and person y_i have a meeting at time_i. A person may attend multiple meetings at the same time. Finally, you are given an integer firstPerson.Person
0 has a secret and initially shares the secret with a person firstPerson at time 0. This secret is then shared every time a meeting takes place with a person that has the secret. More formally, for every meeting, if a person x_i has the secret at time_i, then they will share the secret with person y_i, and vice versa.The secrets are shared instantaneously. That is, a person may receive the secret and share it with people in other meetings within the same time frame.
Return a list of all the people that have the secret after all the meetings have taken place. You may return the answer in any order.
Example 1:
Input: n = 6, meetings = [[1,2,5],[2,3,8],[1,5,10]], firstPerson = 1
Output: [0,1,2,3,5]
Explanation:
At time 0, person 0 shares the secret with person 1.
At time 5, person 1 shares the secret with person 2.
At time 8, person 2 shares the secret with person 3.
At time 10, person 1 shares the secret with person 5.
Thus, people 0, 1, 2, 3, and 5 know the secret after all the meetings.
Example 2:
Input: n = 4, meetings = [[3,1,3],[1,2,2],[0,3,3]], firstPerson = 3
Output: [0,1,3]
Explanation:
At time 0, person 0 shares the secret with person 3.
At time 2, neither person 1 nor person 2 know the secret.
At time 3, person 3 shares the secret with person 0 and person 1.
Thus, people 0, 1, and 3 know the secret after all the meetings.
Example 3:
Input: n = 5, meetings = [[3,4,2],[1,2,1],[2,3,1]], firstPerson = 1
Output: [0,1,2,3,4]
Explanation:
At time 0, person 0 shares the secret with person 1.
At time 1, person 1 shares the secret with person 2, and person 2 shares the secret with person 3.
Note that person 2 can share the secret at the same time as receiving it.
At time 2, person 3 shares the secret with person 4.
Thus, people 0, 1, 2, 3, and 4 know the secret after all the meetings.
Constraints:
•
2 <= n <= 10^5•
1 <= meetings.length <= 10^5•
meetings[i].length == 3•
0 <= x_i, y_i <= n - 1•
x_i != y_i•
1 <= time_i <= 10^5•
1 <= firstPerson <= n - 12024-02-25
2709. Greatest Common Divisor Traversal
Topic: Array, Math, Union Find, Number Theory
Difficulty: Hard
Problem:
You are given a 0-indexed integer array
Your task is to determine if for every pair of indices
Return
Example 1:
Example 2:
Example 3:
Constraints:
•
•
2709. Greatest Common Divisor Traversal
Topic: Array, Math, Union Find, Number Theory
Difficulty: Hard
Problem:
You are given a 0-indexed integer array
nums, and you are allowed to traverse between its indices. You can traverse between index i and index j, i != j, if and only if gcd(nums[i], nums[j]) > 1, where gcd is the greatest common divisor.Your task is to determine if for every pair of indices
i and j in nums, where i < j, there exists a sequence of traversals that can take us from i to j.Return
true if it is possible to traverse between all such pairs of indices, or false otherwise.Example 1:
Input: nums = [2,3,6]
Output: true
Explanation: In this example, there are 3 possible pairs of indices: (0, 1), (0, 2), and (1, 2).
To go from index 0 to index 1, we can use the sequence of traversals 0 -> 2 -> 1, where we move from index 0 to index 2 because gcd(nums[0], nums[2]) = gcd(2, 6) = 2 > 1, and then move from index 2 to index 1 because gcd(nums[2], nums[1]) = gcd(6, 3) = 3 > 1.
To go from index 0 to index 2, we can just go directly because gcd(nums[0], nums[2]) = gcd(2, 6) = 2 > 1. Likewise, to go from index 1 to index 2, we can just go directly because gcd(nums[1], nums[2]) = gcd(3, 6) = 3 > 1.
Example 2:
Input: nums = [3,9,5]
Output: false
Explanation: No sequence of traversals can take us from index 0 to index 2 in this example. So, we return false.
Example 3:
Input: nums = [4,3,12,8]
Output: true
Explanation: There are 6 possible pairs of indices to traverse between: (0, 1), (0, 2), (0, 3), (1, 2), (1, 3), and (2, 3). A valid sequence of traversals exists for each pair, so we return true.
Constraints:
•
1 <= nums.length <= 10^5•
1 <= nums[i] <= 10^52024-02-26
100. Same Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Easy
Problem:
Given the roots of two binary trees
Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/12/20/ex1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/12/20/ex2.jpg
Example 3:
Image: https://assets.leetcode.com/uploads/2020/12/20/ex3.jpg
Constraints:
• The number of nodes in both trees is in the range
•
100. Same Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Easy
Problem:
Given the roots of two binary trees
p and q, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/12/20/ex1.jpg
Input: p = [1,2,3], q = [1,2,3]
Output: true
Example 2:
Image: https://assets.leetcode.com/uploads/2020/12/20/ex2.jpg
Input: p = [1,2], q = [1,null,2]
Output: false
Example 3:
Image: https://assets.leetcode.com/uploads/2020/12/20/ex3.jpg
Input: p = [1,2,1], q = [1,1,2]
Output: false
Constraints:
• The number of nodes in both trees is in the range
[0, 100].•
-10^4 <= Node.val <= 10^42024-02-27
543. Diameter of Binary Tree
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Easy
Problem:
Given the
The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the
The length of a path between two nodes is represented by the number of edges between them.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/06/diamtree.jpg
Example 2:
Constraints:
• The number of nodes in the tree is in the range
•
543. Diameter of Binary Tree
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Easy
Problem:
Given the
root of a binary tree, return the length of the diameter of the tree.The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the
root.The length of a path between two nodes is represented by the number of edges between them.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/06/diamtree.jpg
Input: root = [1,2,3,4,5]
Output: 3
Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].
Example 2:
Input: root = [1,2]
Output: 1
Constraints:
• The number of nodes in the tree is in the range
[1, 10^4].•
-100 <= Node.val <= 1002024-02-28
513. Find Bottom Left Tree Value
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
Example 1:
Image: https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg
Constraints:
• The number of nodes in the tree is in the range
•
513. Find Bottom Left Tree Value
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a binary tree, return the leftmost value in the last row of the tree.Example 1:
Image: https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg
Input: root = [2,1,3]
Output: 1
Example 2:
Image: https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg
Input: root = [1,2,3,4,null,5,6,null,null,7]
Output: 7
Constraints:
• The number of nodes in the tree is in the range
[1, 10^4].•
-2^31 <= Node.val <= 2^31 - 12024-02-29
1609. Even Odd Tree
Topic: Tree, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
A binary tree is named Even-Odd if it meets the following conditions:
• The root of the binary tree is at level index
• For every even-indexed level, all nodes at the level have odd integer values in strictly increasing order (from left to right).
• For every odd-indexed level, all nodes at the level have even integer values in strictly decreasing order (from left to right).
Given the
Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/15/sample_1_1966.png
Example 2:
Image: https://assets.leetcode.com/uploads/2020/09/15/sample_2_1966.png
Example 3:
Image: https://assets.leetcode.com/uploads/2020/09/22/sample_1_333_1966.png
Constraints:
• The number of nodes in the tree is in the range
•
1609. Even Odd Tree
Topic: Tree, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
A binary tree is named Even-Odd if it meets the following conditions:
• The root of the binary tree is at level index
0, its children are at level index 1, their children are at level index 2, etc.• For every even-indexed level, all nodes at the level have odd integer values in strictly increasing order (from left to right).
• For every odd-indexed level, all nodes at the level have even integer values in strictly decreasing order (from left to right).
Given the
root of a binary tree, return true if the binary tree is Even-Odd, otherwise return false.Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/15/sample_1_1966.png
Input: root = [1,10,4,3,null,7,9,12,8,6,null,null,2]
Output: true
Explanation: The node values on each level are:
Level 0: [1]
Level 1: [10,4]
Level 2: [3,7,9]
Level 3: [12,8,6,2]
Since levels 0 and 2 are all odd and increasing and levels 1 and 3 are all even and decreasing, the tree is Even-Odd.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/09/15/sample_2_1966.png
Input: root = [5,4,2,3,3,7]
Output: false
Explanation: The node values on each level are:
Level 0: [5]
Level 1: [4,2]
Level 2: [3,3,7]
Node values in level 2 must be in strictly increasing order, so the tree is not Even-Odd.
Example 3:
Image: https://assets.leetcode.com/uploads/2020/09/22/sample_1_333_1966.png
Input: root = [5,9,1,3,5,7]
Output: false
Explanation: Node values in the level 1 should be even integers.
Constraints:
• The number of nodes in the tree is in the range
[1, 10^5].•
1 <= Node.val <= 10^62024-03-01
2864. Maximum Odd Binary Number
Topic: Math, String, Greedy
Difficulty: Easy
Problem:
You are given a binary string
You have to rearrange the bits in such a way that the resulting binary number is the maximum odd binary number that can be created from this combination.
Return a string representing the maximum odd binary number that can be created from the given combination.
Note that the resulting string can have leading zeros.
Example 1:
Example 2:
Constraints:
•
•
•
2864. Maximum Odd Binary Number
Topic: Math, String, Greedy
Difficulty: Easy
Problem:
You are given a binary string
s that contains at least one '1'.You have to rearrange the bits in such a way that the resulting binary number is the maximum odd binary number that can be created from this combination.
Return a string representing the maximum odd binary number that can be created from the given combination.
Note that the resulting string can have leading zeros.
Example 1:
Input: s = "010"
Output: "001"
Explanation: Because there is just one '1', it must be in the last position. So the answer is "001".
Example 2:
Input: s = "0101"
Output: "1001"
Explanation: One of the '1's must be in the last position. The maximum number that can be made with the remaining digits is "100". So the answer is "1001".
Constraints:
•
1 <= s.length <= 100•
s consists only of '0' and '1'.•
s contains at least one '1'.2024-03-02
977. Squares of a Sorted Array
Topic: Array, Two Pointers, Sorting
Difficulty: Easy
Problem:
Given an integer array
Example 1:
Example 2:
Constraints:
•
•
•
Follow up: Squaring each element and sorting the new array is very trivial, could you find an
977. Squares of a Sorted Array
Topic: Array, Two Pointers, Sorting
Difficulty: Easy
Problem:
Given an integer array
nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.Example 1:
Input: nums = [-4,-1,0,3,10]
Output: [0,1,9,16,100]
Explanation: After squaring, the array becomes [16,1,0,9,100].
After sorting, it becomes [0,1,9,16,100].
Example 2:
Input: nums = [-7,-3,2,3,11]
Output: [4,9,9,49,121]
Constraints:
•
1 <= nums.length <= 10^4•
-10^4 <= nums[i] <= 10^4•
nums is sorted in non-decreasing order.Follow up: Squaring each element and sorting the new array is very trivial, could you find an
O(n) solution using a different approach?2024-03-03
19. Remove Nth Node From End of List
Topic: Linked List, Two Pointers
Difficulty: Medium
Problem:
Given the
Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg
Example 2:
Example 3:
Constraints:
• The number of nodes in the list is
•
•
•
Follow up: Could you do this in one pass?
19. Remove Nth Node From End of List
Topic: Linked List, Two Pointers
Difficulty: Medium
Problem:
Given the
head of a linked list, remove the n^th node from the end of the list and return its head.Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg
Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
Example 2:
Input: head = [1], n = 1
Output: []
Example 3:
Input: head = [1,2], n = 1
Output: [1]
Constraints:
• The number of nodes in the list is
sz.•
1 <= sz <= 30•
0 <= Node.val <= 100•
1 <= n <= szFollow up: Could you do this in one pass?
2024-03-05
1750. Minimum Length of String After Deleting Similar Ends
Topic: Two Pointers, String
Difficulty: Medium
Problem:
Given a string
1. Pick a non-empty prefix from the string
2. Pick a non-empty suffix from the string
3. The prefix and the suffix should not intersect at any index.
4. The characters from the prefix and suffix must be the same.
5. Delete both the prefix and the suffix.
Return the minimum length of
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1750. Minimum Length of String After Deleting Similar Ends
Topic: Two Pointers, String
Difficulty: Medium
Problem:
Given a string
s consisting only of characters 'a', 'b', and 'c'. You are asked to apply the following algorithm on the string any number of times:1. Pick a non-empty prefix from the string
s where all the characters in the prefix are equal.2. Pick a non-empty suffix from the string
s where all the characters in this suffix are equal.3. The prefix and the suffix should not intersect at any index.
4. The characters from the prefix and suffix must be the same.
5. Delete both the prefix and the suffix.
Return the minimum length of
s after performing the above operation any number of times (possibly zero times).Example 1:
Input: s = "ca"
Output: 2
Explanation: You can't remove any characters, so the string stays as is.
Example 2:
Input: s = "cabaabac"
Output: 0
Explanation: An optimal sequence of operations is:
- Take prefix = "c" and suffix = "c" and remove them, s = "abaaba".
- Take prefix = "a" and suffix = "a" and remove them, s = "baab".
- Take prefix = "b" and suffix = "b" and remove them, s = "aa".
- Take prefix = "a" and suffix = "a" and remove them, s = "".
Example 3:
Input: s = "aabccabba"
Output: 3
Explanation: An optimal sequence of operations is:
- Take prefix = "aa" and suffix = "a" and remove them, s = "bccabb".
- Take prefix = "b" and suffix = "bb" and remove them, s = "cca".
Constraints:
•
1 <= s.length <= 10^5•
s only consists of characters 'a', 'b', and 'c'.2024-03-06
141. Linked List Cycle
Topic: Hash Table, Linked List, Two Pointers
Difficulty: Easy
Problem:
Given
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the
Return
Example 1:
Image: https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png
Example 2:
Image: https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png
Example 3:
Image: https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png
Constraints:
• The number of the nodes in the list is in the range
•
•
Follow up: Can you solve it using
141. Linked List Cycle
Topic: Hash Table, Linked List, Two Pointers
Difficulty: Easy
Problem:
Given
head, the head of a linked list, determine if the linked list has a cycle in it.There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the
next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.Return
true if there is a cycle in the linked list. Otherwise, return false.Example 1:
Image: https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
Example 2:
Image: https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png
Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.
Example 3:
Image: https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png
Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.
Constraints:
• The number of the nodes in the list is in the range
[0, 10^4].•
-10^5 <= Node.val <= 10^5•
pos is -1 or a valid index in the linked-list.Follow up: Can you solve it using
O(1) (i.e. constant) memory?2024-03-12
1171. Remove Zero Sum Consecutive Nodes from Linked List
Topic: Hash Table, Linked List
Difficulty: Medium
Problem:
Given the
After doing so, return the head of the final linked list. You may return any such answer.
(Note that in the examples below, all sequences are serializations of
Example 1:
Example 2:
Example 3:
Constraints:
• The given linked list will contain between
• Each node in the linked list has
1171. Remove Zero Sum Consecutive Nodes from Linked List
Topic: Hash Table, Linked List
Difficulty: Medium
Problem:
Given the
head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences.After doing so, return the head of the final linked list. You may return any such answer.
(Note that in the examples below, all sequences are serializations of
ListNode objects.)Example 1:
Input: head = [1,2,-3,3,1]
Output: [3,1]
Note: The answer [1,2,1] would also be accepted.
Example 2:
Input: head = [1,2,3,-3,4]
Output: [1,2,4]
Example 3:
Input: head = [1,2,3,-3,-2]
Output: [1]
Constraints:
• The given linked list will contain between
1 and 1000 nodes.• Each node in the linked list has
-1000 <= node.val <= 1000.2024-03-13
2485. Find the Pivot Integer
Topic: Math, Prefix Sum
Difficulty: Easy
Problem:
Given a positive integer
• The sum of all elements between
Return the pivot integer
Example 1:
Example 2:
Example 3:
Constraints:
•
2485. Find the Pivot Integer
Topic: Math, Prefix Sum
Difficulty: Easy
Problem:
Given a positive integer
n, find the pivot integer x such that:• The sum of all elements between
1 and x inclusively equals the sum of all elements between x and n inclusively.Return the pivot integer
x. If no such integer exists, return -1. It is guaranteed that there will be at most one pivot index for the given input.Example 1:
Input: n = 8
Output: 6
Explanation: 6 is the pivot integer since: 1 + 2 + 3 + 4 + 5 + 6 = 6 + 7 + 8 = 21.
Example 2:
Input: n = 1
Output: 1
Explanation: 1 is the pivot integer since: 1 = 1.
Example 3:
Input: n = 4
Output: -1
Explanation: It can be proved that no such integer exist.
Constraints:
•
1 <= n <= 10002024-03-14
930. Binary Subarrays With Sum
Topic: Array, Hash Table, Sliding Window, Prefix Sum
Difficulty: Medium
Problem:
Given a binary array
A subarray is a contiguous part of the array.
Example 1:
Example 2:
Constraints:
•
•
•
930. Binary Subarrays With Sum
Topic: Array, Hash Table, Sliding Window, Prefix Sum
Difficulty: Medium
Problem:
Given a binary array
nums and an integer goal, return the number of non-empty subarrays with a sum goal.A subarray is a contiguous part of the array.
Example 1:
Input: nums = [1,0,1,0,1], goal = 2
Output: 4
Explanation: The 4 subarrays are bolded and underlined below:
[1,0,1,0,1]
[1,0,1,0,1]
[1,0,1,0,1]
[1,0,1,0,1]
Example 2:
Input: nums = [0,0,0,0,0], goal = 0
Output: 15
Constraints:
•
1 <= nums.length <= 3 * 10^4•
nums[i] is either 0 or 1.•
0 <= goal <= nums.length2024-03-15
238. Product of Array Except Self
Topic: Array, Prefix Sum
Difficulty: Medium
Problem:
Given an integer array
The product of any prefix or suffix of
You must write an algorithm that runs in
Example 1:
Example 2:
Constraints:
•
•
• The product of any prefix or suffix of
Follow up: Can you solve the problem in
238. Product of Array Except Self
Topic: Array, Prefix Sum
Difficulty: Medium
Problem:
Given an integer array
nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].The product of any prefix or suffix of
nums is guaranteed to fit in a 32-bit integer.You must write an algorithm that runs in
O(n) time and without using the division operation.Example 1:
Input: nums = [1,2,3,4]
Output: [24,12,8,6]
Example 2:
Input: nums = [-1,1,0,-3,3]
Output: [0,0,9,0,0]
Constraints:
•
2 <= nums.length <= 10^5•
-30 <= nums[i] <= 30• The product of any prefix or suffix of
nums is guaranteed to fit in a 32-bit integer.Follow up: Can you solve the problem in
O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)2024-03-16
525. Contiguous Array
Topic: Array, Hash Table, Prefix Sum
Difficulty: Medium
Problem:
Given a binary array
Example 1:
Example 2:
Constraints:
•
•
525. Contiguous Array
Topic: Array, Hash Table, Prefix Sum
Difficulty: Medium
Problem:
Given a binary array
nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1.Example 1:
Input: nums = [0,1]
Output: 2
Explanation: [0, 1] is the longest contiguous subarray with an equal number of 0 and 1.
Example 2:
Input: nums = [0,1,0]
Output: 2
Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.
Constraints:
•
1 <= nums.length <= 10^5•
nums[i] is either 0 or 1.2024-03-17
57. Insert Interval
Topic: Array
Difficulty: Medium
Problem:
You are given an array of non-overlapping intervals
Insert
Return
Note that you don't need to modify
Example 1:
Example 2:
Constraints:
•
•
•
•
•
•
57. Insert Interval
Topic: Array
Difficulty: Medium
Problem:
You are given an array of non-overlapping intervals
intervals where intervals[i] = [start_i, end_i] represent the start and the end of the i^th interval and intervals is sorted in ascending order by start_i. You are also given an interval newInterval = [start, end] that represents the start and end of another interval.Insert
newInterval into intervals such that intervals is still sorted in ascending order by start_i and intervals still does not have any overlapping intervals (merge overlapping intervals if necessary).Return
intervals after the insertion.Note that you don't need to modify
intervals in-place. You can make a new array and return it.Example 1:
Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
Output: [[1,5],[6,9]]
Example 2:
Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
Output: [[1,2],[3,10],[12,16]]
Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].
Constraints:
•
0 <= intervals.length <= 10^4•
intervals[i].length == 2•
0 <= start_i <= end_i <= 10^5•
intervals is sorted by start_i in ascending order.•
newInterval.length == 2•
0 <= start <= end <= 10^5