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,70 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0020. Valid Parentheses :easy:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0020. Valid Parentheses][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0020. Valid Parentheses][0020. Valid Parentheses]]
|
||||
:END:
|
||||
|
||||
Given a string ~s~ containing just the characters ~'('~, ~')'~, ~'{'~, ~'}'~, ~'['~ and ~']'~, determine if the input string is valid.
|
||||
|
||||
An input string is valid if:
|
||||
|
||||
- Open brackets must be closed by the same type of brackets.
|
||||
|
||||
- Open brackets must be closed in the correct order.
|
||||
|
||||
- Every close bracket has a corresponding open bracket of the same type.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
*Input:* s = "()"
|
||||
|
||||
*Output:* true
|
||||
|
||||
*Example 2:*
|
||||
|
||||
*Input:* s = "()[]{}"
|
||||
|
||||
*Output:* true
|
||||
|
||||
*Example 3:*
|
||||
|
||||
*Input:* s = "(]"
|
||||
|
||||
*Output:* false
|
||||
|
||||
*Example 4:*
|
||||
|
||||
*Input:* s = "([])"
|
||||
|
||||
*Output:* true
|
||||
|
||||
*Example 5:*
|
||||
|
||||
*Input:* s = "([)]"
|
||||
|
||||
*Output:* false
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- ~1 <= s.length <= 10^{4}~
|
||||
|
||||
- ~s~ consists of parentheses only ~'()[]{}'~.
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
class Solution:
|
||||
def isValid(self, s: str) -> bool:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
bool isValid(string s) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,52 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0084. Largest Rectangle In Histogram :hard:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0084. Largest Rectangle In Histogram][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0084. Largest Rectangle In Histogram][0084. Largest Rectangle In Histogram]]
|
||||
:END:
|
||||
|
||||
Given an array of integers ~heights~ representing the histogram's bar height where the width of each bar is ~1~, return /the area of the largest rectangle in the histogram/.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: heights = [2,1,5,6,2,3]
|
||||
Output: 10
|
||||
Explanation: The above is a histogram where width of each bar is 1.
|
||||
The largest rectangle is shown in the red area, which has an area = 10 units.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: heights = [2,4]
|
||||
Output: 4
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- ~1 <= heights.length <= 10^{5}~
|
||||
|
||||
- ~0 <= heights[i] <= 10^{4}~
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
class Solution:
|
||||
def largestRectangleArea(self, heights: List[int]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int largestRectangleArea(vector<int>& heights) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,84 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0150. Evaluate Reverse Polish Notation :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0150. Evaluate Reverse Polish Notation][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0150. Evaluate Reverse Polish Notation][0150. Evaluate Reverse Polish Notation]]
|
||||
:END:
|
||||
|
||||
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:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: tokens = ["2","1","+","3","*"]
|
||||
Output: 9
|
||||
Explanation: ((2 + 1) * 3) = 9
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: tokens = ["4","13","5","/","+"]
|
||||
Output: 6
|
||||
Explanation: (4 + (13 / 5)) = 6
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 3:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
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
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- ~1 <= tokens.length <= 10^{4}~
|
||||
|
||||
- ~tokens[i]~ is either an operator: ~"+"~, ~"-"~, ~"*"~, or ~"/"~, or an integer in the range ~[-200, 200]~.
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
class Solution:
|
||||
def evalRPN(self, tokens: List[str]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int evalRPN(vector<string>& tokens) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,118 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0155. Min Stack :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0155. Min Stack][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0155. Min Stack][0155. Min Stack]]
|
||||
:END:
|
||||
|
||||
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
|
||||
|
||||
Implement the ~MinStack~ class:
|
||||
|
||||
- ~MinStack()~ initializes the stack object.
|
||||
|
||||
- ~void push(int val)~ pushes the element ~val~ onto the stack.
|
||||
|
||||
- ~void pop()~ removes the element on the top of the stack.
|
||||
|
||||
- ~int top()~ gets the top element of the stack.
|
||||
|
||||
- ~int getMin()~ retrieves the minimum element in the stack.
|
||||
|
||||
You must implement a solution with ~O(1)~ time complexity for each function.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input
|
||||
["MinStack","push","push","push","getMin","pop","top","getMin"]
|
||||
[[],[-2],[0],[-3],[],[],[],[]]
|
||||
|
||||
Output
|
||||
[null,null,null,null,-3,null,0,-2]
|
||||
|
||||
Explanation
|
||||
MinStack minStack = new MinStack();
|
||||
minStack.push(-2);
|
||||
minStack.push(0);
|
||||
minStack.push(-3);
|
||||
minStack.getMin(); // return -3
|
||||
minStack.pop();
|
||||
minStack.top(); // return 0
|
||||
minStack.getMin(); // return -2
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- ~-2^{31} <= val <= 2^{31} - 1~
|
||||
|
||||
- Methods ~pop~, ~top~ and ~getMin~ operations will always be called on *non-empty* stacks.
|
||||
|
||||
- At most ~3 * 10^{4}~ calls will be made to ~push~, ~pop~, ~top~, and ~getMin~.
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
class MinStack:
|
||||
|
||||
def __init__(self):
|
||||
|
||||
|
||||
def push(self, val: int) -> None:
|
||||
|
||||
|
||||
def pop(self) -> None:
|
||||
|
||||
|
||||
def top(self) -> int:
|
||||
|
||||
|
||||
def getMin(self) -> int:
|
||||
|
||||
|
||||
|
||||
# Your MinStack object will be instantiated and called as such:
|
||||
# obj = MinStack()
|
||||
# obj.push(val)
|
||||
# obj.pop()
|
||||
# param_3 = obj.top()
|
||||
# param_4 = obj.getMin()
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
class MinStack {
|
||||
public:
|
||||
MinStack() {
|
||||
|
||||
}
|
||||
|
||||
void push(int val) {
|
||||
|
||||
}
|
||||
|
||||
void pop() {
|
||||
|
||||
}
|
||||
|
||||
int top() {
|
||||
|
||||
}
|
||||
|
||||
int getMin() {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Your MinStack object will be instantiated and called as such:
|
||||
* MinStack* obj = new MinStack();
|
||||
* obj->push(val);
|
||||
* obj->pop();
|
||||
* int param_3 = obj->top();
|
||||
* int param_4 = obj->getMin();
|
||||
*/
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,106 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0682. Baseball Game :easy:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0682. Baseball Game][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0682. Baseball Game][0682. Baseball Game]]
|
||||
:END:
|
||||
|
||||
You are keeping the scores for a baseball game with strange rules. At the beginning of the game, you start with an empty record.
|
||||
|
||||
You are given a list of strings ~operations~, where ~operations[i]~ is the ~i^{th}~ operation you must apply to the record and is one of the following:
|
||||
|
||||
- An integer ~x~.
|
||||
|
||||
- Record a new score of ~x~.
|
||||
|
||||
- ~'+'~.
|
||||
|
||||
- Record a new score that is the sum of the previous two scores.
|
||||
|
||||
- ~'D'~.
|
||||
|
||||
- Record a new score that is the double of the previous score.
|
||||
|
||||
- ~'C'~.
|
||||
|
||||
- Invalidate the previous score, removing it from the record.
|
||||
|
||||
Return /the sum of all the scores on the record after applying all the operations/.
|
||||
|
||||
The test cases are generated such that the answer and all intermediate calculations fit in a *32-bit* integer and that all operations are valid.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: ops = ["5","2","C","D","+"]
|
||||
Output: 30
|
||||
Explanation:
|
||||
"5" - Add 5 to the record, record is now [5].
|
||||
"2" - Add 2 to the record, record is now [5, 2].
|
||||
"C" - Invalidate and remove the previous score, record is now [5].
|
||||
"D" - Add 2 * 5 = 10 to the record, record is now [5, 10].
|
||||
"+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
|
||||
The total sum is 5 + 10 + 15 = 30.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: ops = ["5","-2","4","C","D","9","+","+"]
|
||||
Output: 27
|
||||
Explanation:
|
||||
"5" - Add 5 to the record, record is now [5].
|
||||
"-2" - Add -2 to the record, record is now [5, -2].
|
||||
"4" - Add 4 to the record, record is now [5, -2, 4].
|
||||
"C" - Invalidate and remove the previous score, record is now [5, -2].
|
||||
"D" - Add 2 * -2 = -4 to the record, record is now [5, -2, -4].
|
||||
"9" - Add 9 to the record, record is now [5, -2, -4, 9].
|
||||
"+" - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5].
|
||||
"+" - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14].
|
||||
The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 3:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: ops = ["1","C"]
|
||||
Output: 0
|
||||
Explanation:
|
||||
"1" - Add 1 to the record, record is now [1].
|
||||
"C" - Invalidate and remove the previous score, record is now [].
|
||||
Since the record is empty, the total sum is 0.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- ~1 <= operations.length <= 1000~
|
||||
|
||||
- ~operations[i]~ is ~"C"~, ~"D"~, ~"+"~, or a string representing an integer in the range ~[-3 * 10^{4}, 3 * 10^{4}]~.
|
||||
|
||||
- For operation ~"+"~, there will always be at least two previous scores on the record.
|
||||
|
||||
- For operations ~"C"~ and ~"D"~, there will always be at least one previous score on the record.
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
class Solution:
|
||||
def calPoints(self, operations: List[str]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int calPoints(vector<string>& operations) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,82 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0726. Number of Atoms :hard:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0726. Number of Atoms][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0726. Number of Atoms][0726. Number of Atoms]]
|
||||
:END:
|
||||
|
||||
Given a string ~formula~ representing a chemical formula, return /the count of each atom/.
|
||||
|
||||
The atomic element always starts with an uppercase character, then zero or more lowercase letters, representing the name.
|
||||
|
||||
One or more digits representing that element's count may follow if the count is greater than ~1~. If the count is ~1~, no digits will follow.
|
||||
|
||||
- For example, ~"H2O"~ and ~"H2O2"~ are possible, but ~"H1O2"~ is impossible.
|
||||
|
||||
Two formulas are concatenated together to produce another formula.
|
||||
|
||||
- For example, ~"H2O2He3Mg4"~ is also a formula.
|
||||
|
||||
A formula placed in parentheses, and a count (optionally added) is also a formula.
|
||||
|
||||
- For example, ~"(H2O2)"~ and ~"(H2O2)3"~ are formulas.
|
||||
|
||||
Return the count of all elements as a string in the following form: the first name (in sorted order), followed by its count (if that count is more than ~1~), followed by the second name (in sorted order), followed by its count (if that count is more than ~1~), and so on.
|
||||
|
||||
The test cases are generated so that all the values in the output fit in a *32-bit* integer.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: formula = "H2O"
|
||||
Output: "H2O"
|
||||
Explanation: The count of elements are {'H': 2, 'O': 1}.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: formula = "Mg(OH)2"
|
||||
Output: "H2MgO2"
|
||||
Explanation: The count of elements are {'H': 2, 'Mg': 1, 'O': 2}.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 3:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: formula = "K4(ON(SO3)2)2"
|
||||
Output: "K4N2O14S4"
|
||||
Explanation: The count of elements are {'K': 4, 'N': 2, 'O': 14, 'S': 4}.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- ~1 <= formula.length <= 1000~
|
||||
|
||||
- ~formula~ consists of English letters, digits, ~'('~, and ~')'~.
|
||||
|
||||
- ~formula~ is always valid.
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
class Solution:
|
||||
def countOfAtoms(self, formula: str) -> str:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
string countOfAtoms(string formula) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,55 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0739. Daily Temperatures :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0739. Daily Temperatures][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0739. Daily Temperatures][0739. Daily Temperatures]]
|
||||
:END:
|
||||
|
||||
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:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: temperatures = [73,74,75,71,69,72,76,73]
|
||||
Output: [1,1,4,2,1,1,0,0]
|
||||
#+end_src
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: temperatures = [30,40,50,60]
|
||||
Output: [1,1,1,0]
|
||||
#+end_src
|
||||
*Example 3:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: temperatures = [30,60,90]
|
||||
Output: [1,1,0]
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- ~1 <= temperatures.length <= 10^{5}~
|
||||
|
||||
- ~30 <= temperatures[i] <= 100~
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
class Solution:
|
||||
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> dailyTemperatures(vector<int>& temperatures) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,86 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0853. Car Fleet :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0853. Car Fleet][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0853. Car Fleet][0853. Car Fleet]]
|
||||
:END:
|
||||
|
||||
There are ~n~ cars at given miles away from the starting mile 0, traveling to reach the mile ~target~.
|
||||
|
||||
You are given two integer arrays ~position~ and ~speed~, both of length ~n~, where ~position[i]~ is the starting mile of the ~i^{th}~ car and ~speed[i]~ is the speed of the ~i^{th}~ car in miles per hour.
|
||||
|
||||
A car cannot pass another car, but it can catch up and then travel next to it at the speed of the slower car.
|
||||
|
||||
A *car fleet* is a single car or a group of cars driving next to each other. The speed of the car fleet is the *minimum* speed of any car in the fleet.
|
||||
|
||||
If a car catches up to a car fleet at the mile ~target~, it will still be considered as part of the car fleet.
|
||||
|
||||
Return the number of car fleets that will arrive at the destination.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
*Input:* target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
|
||||
|
||||
*Output:* 3
|
||||
|
||||
*Explanation:*
|
||||
|
||||
- The cars starting at 10 (speed 2) and 8 (speed 4) become a fleet, meeting each other at 12. The fleet forms at ~target~.
|
||||
|
||||
- The car starting at 0 (speed 1) does not catch up to any other car, so it is a fleet by itself.
|
||||
|
||||
- The cars starting at 5 (speed 1) and 3 (speed 3) become a fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches ~target~.
|
||||
|
||||
*Example 2:*
|
||||
|
||||
*Input:* target = 10, position = [3], speed = [3]
|
||||
|
||||
*Output:* 1
|
||||
|
||||
*Explanation:*
|
||||
|
||||
There is only one car, hence there is only one fleet.
|
||||
|
||||
*Example 3:*
|
||||
|
||||
*Input:* target = 100, position = [0,2,4], speed = [4,2,1]
|
||||
|
||||
*Output:* 1
|
||||
|
||||
*Explanation:*
|
||||
|
||||
- The cars starting at 0 (speed 4) and 2 (speed 2) become a fleet, meeting each other at 4. The car starting at 4 (speed 1) travels to 5.
|
||||
|
||||
- Then, the fleet at 4 (speed 2) and the car at position 5 (speed 1) become one fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches ~target~.
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- ~n == position.length == speed.length~
|
||||
|
||||
- ~1 <= n <= 10^{5}~
|
||||
|
||||
- ~0 < target <= 10^{6}~
|
||||
|
||||
- ~0 <= position[i] < target~
|
||||
|
||||
- All the values of ~position~ are *unique*.
|
||||
|
||||
- ~0 < speed[i] <= 10^{6}~
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
class Solution:
|
||||
def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int carFleet(int target, vector<int>& position, vector<int>& speed) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,86 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0901. Online Stock Span :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0901. Online Stock Span][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0901. Online Stock Span][0901. Online Stock Span]]
|
||||
:END:
|
||||
|
||||
Design an algorithm that collects daily price quotes for some stock and returns *the span* of that stock's price for the current day.
|
||||
|
||||
The *span* of the stock's price in one day is the maximum number of consecutive days (starting from that day and going backward) for which the stock price was less than or equal to the price of that day.
|
||||
|
||||
- For example, if the prices of the stock in the last four days is ~[7,2,1,2]~ and the price of the stock today is ~2~, then the span of today is ~4~ because starting from today, the price of the stock was less than or equal ~2~ for ~4~ consecutive days.
|
||||
|
||||
- Also, if the prices of the stock in the last four days is ~[7,34,1,2]~ and the price of the stock today is ~8~, then the span of today is ~3~ because starting from today, the price of the stock was less than or equal ~8~ for ~3~ consecutive days.
|
||||
|
||||
Implement the ~StockSpanner~ class:
|
||||
|
||||
- ~StockSpanner()~ Initializes the object of the class.
|
||||
|
||||
- ~int next(int price)~ Returns the *span* of the stock's price given that today's price is ~price~.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input
|
||||
["StockSpanner", "next", "next", "next", "next", "next", "next", "next"]
|
||||
[[], [100], [80], [60], [70], [60], [75], [85]]
|
||||
Output
|
||||
[null, 1, 1, 1, 2, 1, 4, 6]
|
||||
|
||||
Explanation
|
||||
StockSpanner stockSpanner = new StockSpanner();
|
||||
stockSpanner.next(100); // return 1
|
||||
stockSpanner.next(80); // return 1
|
||||
stockSpanner.next(60); // return 1
|
||||
stockSpanner.next(70); // return 2
|
||||
stockSpanner.next(60); // return 1
|
||||
stockSpanner.next(75); // return 4, because the last 4 prices (including today's price of 75) were less than or equal to today's price.
|
||||
stockSpanner.next(85); // return 6
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- ~1 <= price <= 10^{5}~
|
||||
|
||||
- At most ~10^{4}~ calls will be made to ~next~.
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
class StockSpanner:
|
||||
|
||||
def __init__(self):
|
||||
|
||||
|
||||
def next(self, price: int) -> int:
|
||||
|
||||
|
||||
|
||||
# Your StockSpanner object will be instantiated and called as such:
|
||||
# obj = StockSpanner()
|
||||
# param_1 = obj.next(price)
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
class StockSpanner {
|
||||
public:
|
||||
StockSpanner() {
|
||||
|
||||
}
|
||||
|
||||
int next(int price) {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Your StockSpanner object will be instantiated and called as such:
|
||||
* StockSpanner* obj = new StockSpanner();
|
||||
* int param_1 = obj->next(price);
|
||||
*/
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,75 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 1544. Make The String Great :easy:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*1544. Make The String Great][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*1544. Make The String Great][1544. Make The String Great]]
|
||||
:END:
|
||||
|
||||
Given a string ~s~ of lower and upper case English letters.
|
||||
|
||||
A good string is a string which doesn't have *two adjacent characters* ~s[i]~ and ~s[i + 1]~ where:
|
||||
|
||||
- ~0 <= i <= s.length - 2~
|
||||
|
||||
- ~s[i]~ is a lower-case letter and ~s[i + 1]~ is the same letter but in upper-case or *vice-versa*.
|
||||
|
||||
To make the string good, you can choose *two adjacent* characters that make the string bad and remove them. You can keep doing this until the string becomes good.
|
||||
|
||||
Return /the string/ after making it good. The answer is guaranteed to be unique under the given constraints.
|
||||
|
||||
*Notice* that an empty string is also good.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: s = "leEeetcode"
|
||||
Output: "leetcode"
|
||||
Explanation: In the first step, either you choose i = 1 or i = 2, both will result "leEeetcode" to be reduced to "leetcode".
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: s = "abBAcC"
|
||||
Output: ""
|
||||
Explanation: We have many possible scenarios, and all lead to the same answer. For example:
|
||||
"abBAcC" --> "aAcC" --> "cC" --> ""
|
||||
"abBAcC" --> "abBA" --> "aA" --> ""
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 3:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: s = "s"
|
||||
Output: "s"
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- ~1 <= s.length <= 100~
|
||||
|
||||
- ~s~ contains only lower and upper case English letters.
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
class Solution:
|
||||
def makeGood(self, s: str) -> str:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
string makeGood(string s) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
Reference in New Issue
Block a user