feat: populate note files with problem descriptions and code stubs
Add populate-notes.mjs that fetches problem descriptions and Python/C++ code stubs from LeetCode's GraphQL API. Populated all 197 NeetCode 150 note files with: - Problem description (examples, constraints) - Python code stub (function signature) - C++ code stub (function signature + includes) API responses cached in leetcode/.cache/leetcode/ for instant re-runs.
This commit is contained in:
@@ -1,18 +1,61 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0045. Jump Game II :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0045. Jump Game II][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0045. Jump Game II][0045. Jump Game II]]
|
||||
:END:
|
||||
|
||||
You are given a *0-indexed* array of integers ~nums~ of length ~n~. You are initially positioned at index 0.
|
||||
|
||||
Each element ~nums[i]~ represents the maximum length of a forward jump from index ~i~. In other words, if you are at index ~i~, you can jump to any index ~(i + j)~ where:
|
||||
|
||||
- ~0 <= j <= nums[i]~ and
|
||||
|
||||
- ~i + j < n~
|
||||
|
||||
Return /the minimum number of jumps to reach index /~n - 1~. The test cases are generated such that you can reach index ~n - 1~.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: nums = [2,3,1,1,4]
|
||||
Output: 2
|
||||
Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: nums = [2,3,0,1,4]
|
||||
Output: 2
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- ~1 <= nums.length <= 10^{4}~
|
||||
|
||||
- ~0 <= nums[i] <= 1000~
|
||||
|
||||
- It's guaranteed that you can reach ~nums[n - 1]~.
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
class Solution:
|
||||
def jump(self, nums: List[int]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int jump(vector<int>& nums) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,64 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0053. Maximum Subarray :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0053. Maximum Subarray][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0053. Maximum Subarray][0053. Maximum Subarray]]
|
||||
:END:
|
||||
|
||||
Given an integer array ~nums~, find the subarray with the largest sum, and return /its sum/.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
|
||||
Output: 6
|
||||
Explanation: The subarray [4,-1,2,1] has the largest sum 6.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: nums = [1]
|
||||
Output: 1
|
||||
Explanation: The subarray [1] has the largest sum 1.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 3:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: nums = [5,4,-1,7,8]
|
||||
Output: 23
|
||||
Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- ~1 <= nums.length <= 10^{5}~
|
||||
|
||||
- ~-10^{4} <= nums[i] <= 10^{4}~
|
||||
|
||||
*Follow up:* If you have figured out the ~O(n)~ solution, try coding another solution using the *divide and conquer* approach, which is more subtle.
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
class Solution:
|
||||
def maxSubArray(self, nums: List[int]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int maxSubArray(vector<int>& nums) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,54 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0055. Jump Game :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0055. Jump Game][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0055. Jump Game][0055. Jump Game]]
|
||||
:END:
|
||||
|
||||
You are given an integer array ~nums~. You are initially positioned at the array's *first index*, and each element in the array represents your maximum jump length at that position.
|
||||
|
||||
Return ~true~/ if you can reach the last index, or /~false~/ otherwise/.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: nums = [2,3,1,1,4]
|
||||
Output: true
|
||||
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: nums = [3,2,1,0,4]
|
||||
Output: false
|
||||
Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- ~1 <= nums.length <= 10^{4}~
|
||||
|
||||
- ~0 <= nums[i] <= 10^{5}~
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
class Solution:
|
||||
def canJump(self, nums: List[int]) -> bool:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
bool canJump(vector<int>& nums) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,73 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0134. Gas Station :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0134. Gas Station][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0134. Gas Station][0134. Gas Station]]
|
||||
:END:
|
||||
|
||||
There are ~n~ gas stations along a circular route, where the amount of gas at the ~i^{th}~ station is ~gas[i]~.
|
||||
|
||||
You have a car with an unlimited gas tank and it costs ~cost[i]~ of gas to travel from the ~i^{th}~ station to its next ~(i + 1)^{th}~ station. You begin the journey with an empty tank at one of the gas stations.
|
||||
|
||||
Given two integer arrays ~gas~ and ~cost~, return /the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return/ ~-1~. If there exists a solution, it is *guaranteed* to be *unique*.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: gas = [1,2,3,4,5], cost = [3,4,5,1,2]
|
||||
Output: 3
|
||||
Explanation:
|
||||
Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
|
||||
Travel to station 4. Your tank = 4 - 1 + 5 = 8
|
||||
Travel to station 0. Your tank = 8 - 2 + 1 = 7
|
||||
Travel to station 1. Your tank = 7 - 3 + 2 = 6
|
||||
Travel to station 2. Your tank = 6 - 4 + 3 = 5
|
||||
Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
|
||||
Therefore, return 3 as the starting index.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: gas = [2,3,4], cost = [3,4,3]
|
||||
Output: -1
|
||||
Explanation:
|
||||
You can't start at station 0 or 1, as there is not enough gas to travel to the next station.
|
||||
Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
|
||||
Travel to station 0. Your tank = 4 - 3 + 2 = 3
|
||||
Travel to station 1. Your tank = 3 - 3 + 3 = 3
|
||||
You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.
|
||||
Therefore, you can't travel around the circuit once no matter where you start.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- ~n == gas.length == cost.length~
|
||||
|
||||
- ~1 <= n <= 10^{5}~
|
||||
|
||||
- ~0 <= gas[i], cost[i] <= 10^{4}~
|
||||
|
||||
- The input is generated such that the answer is unique.
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
class Solution:
|
||||
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,65 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0678. Valid Parenthesis String :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0678. Valid Parenthesis String][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0678. Valid Parenthesis String][0678. Valid Parenthesis String]]
|
||||
:END:
|
||||
|
||||
Given a string ~s~ containing only three types of characters: ~'('~, ~')'~ and ~'*'~, return ~true~ /if/ ~s~ /is *valid*/.
|
||||
|
||||
The following rules define a *valid* string:
|
||||
|
||||
- Any left parenthesis ~'('~ must have a corresponding right parenthesis ~')'~.
|
||||
|
||||
- Any right parenthesis ~')'~ must have a corresponding left parenthesis ~'('~.
|
||||
|
||||
- Left parenthesis ~'('~ must go before the corresponding right parenthesis ~')'~.
|
||||
|
||||
- ~'*'~ could be treated as a single right parenthesis ~')'~ or a single left parenthesis ~'('~ or an empty string ~""~.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: s = "()"
|
||||
Output: true
|
||||
#+end_src
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: s = "(*)"
|
||||
Output: true
|
||||
#+end_src
|
||||
*Example 3:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: s = "(*))"
|
||||
Output: true
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- ~1 <= s.length <= 100~
|
||||
|
||||
- ~s[i]~ is ~'('~, ~')'~ or ~'*'~.
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
class Solution:
|
||||
def checkValidString(self, s: str) -> bool:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
bool checkValidString(string s) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,58 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0763. Partition Labels :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0763. Partition Labels][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0763. Partition Labels][0763. Partition Labels]]
|
||||
:END:
|
||||
|
||||
You are given a string ~s~. We want to partition the string into as many parts as possible so that each letter appears in at most one part. For example, the string ~"ababcc"~ can be partitioned into ~["abab", "cc"]~, but partitions such as ~["aba", "bcc"]~ or ~["ab", "ab", "cc"]~ are invalid.
|
||||
|
||||
Note that the partition is done so that after concatenating all the parts in order, the resultant string should be ~s~.
|
||||
|
||||
Return /a list of integers representing the size of these parts/.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: s = "ababcbacadefegdehijhklij"
|
||||
Output: [9,7,8]
|
||||
Explanation:
|
||||
The partition is "ababcbaca", "defegde", "hijhklij".
|
||||
This is a partition so that each letter appears in at most one part.
|
||||
A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits s into less parts.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: s = "eccbbbbdec"
|
||||
Output: [10]
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- ~1 <= s.length <= 500~
|
||||
|
||||
- ~s~ consists of lowercase English letters.
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
class Solution:
|
||||
def partitionLabels(self, s: str) -> List[int]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> partitionLabels(string s) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,58 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0846. Hand of Straights :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0846. Hand of Straights][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0846. Hand of Straights][0846. Hand of Straights]]
|
||||
:END:
|
||||
|
||||
Alice has some number of cards and she wants to rearrange the cards into groups so that each group is of size ~groupSize~, and consists of ~groupSize~ consecutive cards.
|
||||
|
||||
Given an integer array ~hand~ where ~hand[i]~ is the value written on the ~i^{th}~ card and an integer ~groupSize~, return ~true~ if she can rearrange the cards, or ~false~ otherwise.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: hand = [1,2,3,6,2,3,4,7,8], groupSize = 3
|
||||
Output: true
|
||||
Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: hand = [1,2,3,4,5], groupSize = 4
|
||||
Output: false
|
||||
Explanation: Alice's hand can not be rearranged into groups of 4.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- ~1 <= hand.length <= 10^{4}~
|
||||
|
||||
- ~0 <= hand[i] <= 10^{9}~
|
||||
|
||||
- ~1 <= groupSize <= hand.length~
|
||||
|
||||
*Note:* This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
class Solution:
|
||||
def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
bool isNStraightHand(vector<int>& hand, int groupSize) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,57 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0945. Minimum Increment to Make Array Unique :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0945. Minimum Increment to Make Array Unique][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0945. Minimum Increment to Make Array Unique][0945. Minimum Increment to Make Array Unique]]
|
||||
:END:
|
||||
|
||||
You are given an integer array ~nums~. In one move, you can pick an index ~i~ where ~0 <= i < nums.length~ and increment ~nums[i]~ by ~1~.
|
||||
|
||||
Return /the minimum number of moves to make every value in /~nums~/ *unique*/.
|
||||
|
||||
The test cases are generated so that the answer fits in a 32-bit integer.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: nums = [1,2,2]
|
||||
Output: 1
|
||||
Explanation: After 1 move, the array could be [1, 2, 3].
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: nums = [3,2,1,2,1,7]
|
||||
Output: 6
|
||||
Explanation: After 6 moves, the array could be [3, 4, 1, 2, 5, 7].
|
||||
It can be shown that it is impossible for the array to have all unique values with 5 or less moves.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- ~1 <= nums.length <= 10^{5}~
|
||||
|
||||
- ~0 <= nums[i] <= 10^{5}~
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
class Solution:
|
||||
def minIncrementForUnique(self, nums: List[int]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int minIncrementForUnique(vector<int>& nums) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,76 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0978. Longest Turbulent Subarray :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0978. Longest Turbulent Subarray][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0978. Longest Turbulent Subarray][0978. Longest Turbulent Subarray]]
|
||||
:END:
|
||||
|
||||
Given an integer array ~arr~, return /the length of a maximum size turbulent subarray of/ ~arr~.
|
||||
|
||||
A subarray is *turbulent* if the comparison sign flips between each adjacent pair of elements in the subarray.
|
||||
|
||||
More formally, a subarray ~[arr[i], arr[i + 1], ..., arr[j]]~ of ~arr~ is said to be turbulent if and only if:
|
||||
|
||||
- For ~i <= k < j~:
|
||||
|
||||
- ~arr[k] > arr[k + 1]~ when ~k~ is odd, and
|
||||
|
||||
- ~arr[k] < arr[k + 1]~ when ~k~ is even.
|
||||
|
||||
- Or, for ~i <= k < j~:
|
||||
|
||||
- ~arr[k] > arr[k + 1]~ when ~k~ is even, and
|
||||
|
||||
- ~arr[k] < arr[k + 1]~ when ~k~ is odd.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: arr = [9,4,2,10,7,8,8,1,9]
|
||||
Output: 5
|
||||
Explanation: arr[1] > arr[2] < arr[3] > arr[4] < arr[5]
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: arr = [4,8,12,16]
|
||||
Output: 2
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 3:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: arr = [100]
|
||||
Output: 1
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- ~1 <= arr.length <= 4 * 10^{4}~
|
||||
|
||||
- ~0 <= arr[i] <= 10^{9}~
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
class Solution:
|
||||
def maxTurbulenceSize(self, arr: List[int]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int maxTurbulenceSize(vector<int>& arr) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,63 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 1871. Jump Game VII :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*1871. Jump Game VII][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*1871. Jump Game VII][1871. Jump Game VII]]
|
||||
:END:
|
||||
|
||||
You are given a *0-indexed* binary string ~s~ and two integers ~minJump~ and ~maxJump~. In the beginning, you are standing at index ~0~, which is equal to ~'0'~. You can move from index ~i~ to index ~j~ if the following conditions are fulfilled:
|
||||
|
||||
- ~i + minJump <= j <= min(i + maxJump, s.length - 1)~, and
|
||||
|
||||
- ~s[j] == '0'~.
|
||||
|
||||
Return ~true~ if you can reach index ~s.length - 1~ in ~s~/, or /~false~/ otherwise./
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: s = "011010", minJump = 2, maxJump = 3
|
||||
Output: true
|
||||
Explanation:
|
||||
In the first step, move from index 0 to index 3.
|
||||
In the second step, move from index 3 to index 5.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: s = "01101110", minJump = 2, maxJump = 3
|
||||
Output: false
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- ~2 <= s.length <= 10^{5}~
|
||||
|
||||
- ~s[i]~ is either ~'0'~ or ~'1'~.
|
||||
|
||||
- ~s[0] == '0'~
|
||||
|
||||
- ~1 <= minJump <= maxJump < s.length~
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
class Solution:
|
||||
def canReach(self, s: str, minJump: int, maxJump: int) -> bool:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
bool canReach(string s, int minJump, int maxJump) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,77 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 1899. Merge Triplets to Form Target Triplet :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*1899. Merge Triplets to Form Target Triplet][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*1899. Merge Triplets to Form Target Triplet][1899. Merge Triplets to Form Target Triplet]]
|
||||
:END:
|
||||
|
||||
A *triplet* is an array of three integers. You are given a 2D integer array ~triplets~, where ~triplets[i] = [a_{i}, b_{i}, c_{i}]~ describes the ~i^{th}~ *triplet*. You are also given an integer array ~target = [x, y, z]~ that describes the *triplet* you want to obtain.
|
||||
|
||||
To obtain ~target~, you may apply the following operation on ~triplets~ *any number* of times (possibly *zero*):
|
||||
|
||||
- Choose two indices (*0-indexed*) ~i~ and ~j~ (~i != j~) and *update* ~triplets[j]~ to become ~[max(a_{i}, a_{j}), max(b_{i}, b_{j}), max(c_{i}, c_{j})]~.
|
||||
|
||||
- For example, if ~triplets[i] = [2, 5, 3]~ and ~triplets[j] = [1, 7, 5]~, ~triplets[j]~ will be updated to ~[max(2, 1), max(5, 7), max(3, 5)] = [2, 7, 5]~.
|
||||
|
||||
Return ~true~ /if it is possible to obtain the /~target~/ *triplet* /~[x, y, z]~/ as an* element* of /~triplets~/, or /~false~/ otherwise/.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: triplets = [[2,5,3],[1,8,4],[1,7,5]], target = [2,7,5]
|
||||
Output: true
|
||||
Explanation: Perform the following operations:
|
||||
- Choose the first and last triplets [[2,5,3],[1,8,4],[1,7,5]]. Update the last triplet to be [max(2,1), max(5,7), max(3,5)] = [2,7,5]. triplets = [[2,5,3],[1,8,4],[2,7,5]]
|
||||
The target triplet [2,7,5] is now an element of triplets.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: triplets = [[3,4,5],[4,5,6]], target = [3,2,5]
|
||||
Output: false
|
||||
Explanation: It is impossible to have [3,2,5] as an element because there is no 2 in any of the triplets.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 3:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: triplets = [[2,5,3],[2,3,4],[1,2,5],[5,2,3]], target = [5,5,5]
|
||||
Output: true
|
||||
Explanation: Perform the following operations:
|
||||
- Choose the first and third triplets [[2,5,3],[2,3,4],[1,2,5],[5,2,3]]. Update the third triplet to be [max(2,1), max(5,2), max(3,5)] = [2,5,5]. triplets = [[2,5,3],[2,3,4],[2,5,5],[5,2,3]].
|
||||
- Choose the third and fourth triplets [[2,5,3],[2,3,4],[2,5,5],[5,2,3]]. Update the fourth triplet to be [max(2,5), max(5,2), max(5,3)] = [5,5,5]. triplets = [[2,5,3],[2,3,4],[2,5,5],[5,5,5]].
|
||||
The target triplet [5,5,5] is now an element of triplets.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- ~1 <= triplets.length <= 10^{5}~
|
||||
|
||||
- ~triplets[i].length == target.length == 3~
|
||||
|
||||
- ~1 <= a_{i}, b_{i}, c_{i}, x, y, z <= 1000~
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
class Solution:
|
||||
def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
bool mergeTriplets(vector<vector<int>>& triplets, vector<int>& target) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
Reference in New Issue
Block a user