- Notifications
You must be signed in to change notification settings - Fork2.4k
Sri Hari: Batch-3/Neetcode-150/Added hints#3741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
7c8ce94
8c16f2b
c9fa8f3
06863ba
83ee29c
21bdf48
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Recommended Time & Space Complexity</summary> | ||
<p> | ||
You should aim for a solution with <code>O(logn)</code> time and <code>O(1)</code> space, where <code>n</code> is the size of the input array. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 1</summary> | ||
<p> | ||
Can you find an algorithm that is useful when the array is sorted? Maybe other than linear seacrh. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 2</summary> | ||
<p> | ||
The problem name is the name of the algorithm that we can use. We need to find a target value and if it does not exist in the array return <code>-1</code>. We have <code>l</code> and <code>r</code> as the boundaries of the segment of the array in which we are searching. Try building conditions to eliminate half of the search segment at each step. Maybe sorted nature of the array can be helpful. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 3</summary> | ||
<p> | ||
We compare the target value with the <code>mid</code> of the segment. For example, consider the array <code>[1, 2, 3, 4, 5]</code> and <code>target = 4</code>. The <code>mid</code> value is <code>3</code>, thus, on the next iteration we search to the right of <code>mid</code>. The remaining segment is <code>[4,5]</code>. Why? | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 4</summary> | ||
<p> | ||
Because the array is sorted, all elements to the left of <code>mid</code> (including <code>3</code>) are guaranteed to be smaller than the target. Therefore, we can safely eliminate that half of the array from consideration, narrowing the search to the right half and repeat this search until we find the target. | ||
</p> | ||
</details> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Recommended Time & Space Complexity</summary> | ||
<p> | ||
You should aim for a solution with <code>O(n)</code> time and <code>O(1)</code> space, where <code>n</code> is the size of the input array. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 1</summary> | ||
<p> | ||
A brute force solution would be to iterate through the array with index <code>i</code>, considering it as the day to buy, and trying all possible options for selling it on the days to the right of index <code>i</code>. This would be an <code>O(n^2)</code> solution. Can you think of a better way? | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 2</summary> | ||
<p> | ||
You should buy at a price and always sell at a higher price. Can you iterate through the array with index <code>i</code>, considering it as either the buying price or the selling price? | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 3</summary> | ||
<p> | ||
We can iterate through the array with index <code>i</code>, considering it as the selling value. But what value will it be optimal to consider as buying point on the left of index <code>i</code>? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. This is an excellent hint, good job! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Thanks. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 4</summary> | ||
<p> | ||
We are trying to maximize <code>profit = sell - buy</code>. If the current <code>i</code> is the sell value, we want to choose the minimum buy value to the left of <code>i</code> to maximize the profit. The result will be the maximum profit among all. However, if all profits are negative, we can return <code>0</code> since we are allowed to skip doing transaction. | ||
</p> | ||
</details> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Recommended Time & Space Complexity</summary> | ||
<p> | ||
You should aim for a solution with <code>O(nlogn)</code> time and <code>O(n)</code> space, where <code>n</code> is the size of the input array. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 1</summary> | ||
<p> | ||
First draw a picture of all the points which represents the positions and respective speeds of the cars. It is appropriate to represent the position and speed of each car as an array, where each cell corresponds to a car. It is also logical to sort this array based on the positions in descending order. Why? | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 2</summary> | ||
<p> | ||
Because a car can only form a fleet with another car that is ahead of it, sorting the array in descending order ensures clarity about the final speed of each car. Sorting in ascending order would create ambiguity, as the next car might form a fleet with another car while reaching the target, making it difficult to determine its final speed. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 3</summary> | ||
<p> | ||
Calculating the time for a car to reach the target is straightforward and can be done using the formula: <code>time = (target - position) / speed</code>. Now, it becomes easy to identify that two cars will form a fleet if and only if the car ahead has a time that is greater than or equal to the time of the car behind it. How can we maintain the total number of fleets happened while going through the array? Maybe a data structure is helpful. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 4</summary> | ||
<p> | ||
We can use a stack to maintain the times of the fleets. As we iterate through the array (sorted in descending order of positions), we compute the time for each car to reach the target and check if it can form a fleet with the car ahead. If the current car's time is less than or equal to the top of the stack, it joins the same fleet. Otherwise, it forms a new fleet, and we push its time onto the stack. The length of the stack at the end represents the total number of fleets formed. | ||
</p> | ||
</details> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Recommended Time & Space Complexity</summary> | ||
<p> | ||
You should aim for a solution as good or better than <code>O(n)</code> time and <code>O(n)</code> space, where <code>n</code> is the size of the input array. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 1</summary> | ||
<p> | ||
A brute force solution would involve iterating through the array with index <code>i</code> and checking how far is the next greater element to the right of <code>i</code>. This would be an <code>O(n^2)</code> solution. Can you think of a better way? | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 2</summary> | ||
<p> | ||
Can you consider a reverse approach? For example, in <code>[2, 1, 1, 3]</code>, the next greater element for the numbers <code>[2, 1, 1]</code> is <code>3</code>. Instead of checking for each element individually, can you think of a way where, by standing at the element <code>3</code>, you compute the result for the elements <code>[2, 1, 1]</code>? Maybe there's a data structure that is useful here. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 3</summary> | ||
<p> | ||
We can use a stack to maintain indices in a monotonically decreasing order, popping indices where the values are smaller than the current element. This helps us find the result by using the difference between indices while considering the values at those indices. Can you see how the stack is useful? | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 4</summary> | ||
<p> | ||
In the array <code>[2, 1, 1, 3]</code>, we don't perform any pop operations while processing <code>[2, 1, 1]</code> because these elements are already in decreasing order. However, when we reach <code>3</code>, we pop elements from the stack until the top element of the stack is no longer less than the current element. For each popped element, we compute the difference between the indices and store it in the position corresponding to the popped element. | ||
</p> | ||
</details> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Recommended Time & Space Complexity</summary> | ||
<p> | ||
You should aim for a solution with <code>O(nlogm)</code> time and <code>O(1)</code> space, where <code>n</code> is the size of the input array, and <code>m</code> is the maximum value in the array. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 1</summary> | ||
<p> | ||
Given <code>h</code> is always greater than or equal to the length of piles, can you determine the upper bound for the answer? How much time does it take Koko to eat a pile with <code>x</code> bananas? | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 2</summary> | ||
<p> | ||
It takes <code>ceil(x / k)</code> time to finish the <code>x</code> pile when Koko eats at a rate of <code>k</code> bananas per hour. Our task is to determine the minimum possible value of <code>k</code>. However, we must also ensure that at this rate, <code>k</code>, Koko can finish eating all the piles within the given <code>h</code> hours. Can you now think of the upper bound for <code>k</code>? | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 3</summary> | ||
<p> | ||
The upper bound for <code>k</code> is the maximum size of all the piles. Why? Because if Koko eats the largest pile in one hour, then it is straightforward that she can eat any other pile in an hour only. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 4</summary> | ||
<p> | ||
Consider <code>m</code> to be the largest pile and <code>n</code> to be the number of piles. A brute force solution would be to linearly check all values from <code>1</code> to <code>m</code> and find the minimum possible value at which Koko can complete the task. This approach would take <code>O(n * m)</code> time. Can you think of a more efficient method? Perhaps an efficient searching algorithm could help. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 5</summary> | ||
<p> | ||
Rather than linearly scanning, we can use binary search. The upper bound of <code>k</code> is <code>max(piles)</code> and since we are only dealing with positive values, the lower bound is <code>1</code>. The search space of our binary search is <code>1</code> through <code>max(piles)</code>. This allows us to find the smallest possible <code>k</code> using binary search. | ||
</p> | ||
</details> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Recommended Time & Space Complexity</summary> | ||
<p> | ||
You should aim for a solution with <code>O(n)</code> time and <code>O(n)</code> space, where <code>n</code> is the size of the input array. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 1</summary> | ||
<p> | ||
A brute force solution would involve repeatedly finding an operator <code>+ - * /</code> in the array and modifying the array by computing the result for that operator and two operands to its left. This would be an <code>O(n^2)</code> solution. Can you think of a better way? Maybe we can use a data structure to handle operations efficiently. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 2</summary> | ||
<p> | ||
We can use a stack. We iterate through the array, and if we encounter a number, we push it onto the stack. If we encounter an operator, we pop two elements from the stack, treat them as operands, and solve the equation using the current operator. Then, we push the result back onto the stack. Why does this work? | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 3</summary> | ||
<p> | ||
As the array has postfix expression, stack helps us to maintain the correct order of operations by ensuring that we always use the most recent operands (those closest to the operator) when performing the operation. After the iteration, the final result is left in the stack. | ||
</p> | ||
</details> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Recommended Time & Space Complexity</summary> | ||
<p> | ||
You should aim for a solution as good or better than <code>O(4^n / sqrt(n))</code> time and <code>O(n)</code> space, where <code>n</code> is the number of parenthesis pairs in the string. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 1</summary> | ||
<p> | ||
A brute force solution would be to generate all possible strings of size <code>2n</code> and add only the valid strings. This would be an <code>O(n * 2 ^ (2n))</code> solution. Can you think of a better way? Maybe you can use pruning to avoid generating invalid strings. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 2</summary> | ||
<p> | ||
We can use backtracking with pruning. But what makes a string invalid? Can you think of a condition for this? | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 3</summary> | ||
<p> | ||
When the count of closing brackets exceeds the count of opening brackets, the string becomes invalid. Therefore, we can maintain two variables, <code>open</code> and <code>close</code>, to track the number of opening and closing brackets. We avoid exploring paths where <code>close > open</code>. Once the string length reaches <code>2n</code>, we add it to the result. | ||
</p> | ||
</details> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Recommended Time & Space Complexity</summary> | ||
<p> | ||
You should aim for a solution with <code>O(n)</code> time and <code>O(n)</code> space, where <code>n</code> is the size of the input array. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 1</summary> | ||
<p> | ||
A rectangle has a height and a width. Can you visualize how rectangles are formed in the given input? Considering one bar at a time might help. We can try to form rectangles by going through every bar and current bar's height will be the height of the rectangle. How can you determine the width of the rectangle for the current bar being the height of the rectangle? Extending the current bar to the left and right might help determine the rectangle's width. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 2</summary> | ||
<p> | ||
For a bar with height <code>h</code>, try extending it to the left and right. We can see that we can't extend further when we encounter a bar with a smaller height than <code>h</code>. The width will be the number of bars within this extended range. A brute force solution would be to go through every bar and find the area of the rectangle it can form by extending towards the left and right. This would be an <code>O(n^2)</code> solution. Can you think of a better way? Maybe precomputing the left and right boundaries might be helpful. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 3</summary> | ||
<p> | ||
The left and right boundaries are the positions up to which we can extend the bar at index <code>i</code>. The area of the rectangle will be <code>height[i] * (right - left + 1)</code>, which is the general formula for <code>height * width</code>. These boundaries are determined by the first smaller bars encountered to the left and right of the current bar. How can we find the left and right boundaries now? Maybe a data structure is helpful. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 4</summary> | ||
<p> | ||
We can use a stack with a monotonically strictly increasing nature, but instead of storing values, we store indices in the stack and perform operations based on the values at those indices. The top of the stack will represent the smaller bar that we encounter while extending the current bar. To find the left and right boundaries, we perform this algorithm from left to right and vice versa, storing the boundaries. Then, we iterate through the array to find the area for each bar and return the maximum area we get. | ||
</p> | ||
</details> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Recommended Time & Space Complexity</summary> | ||
<p> | ||
You should aim for a solution with <code>O(n)</code> time and <code>O(m)</code> space, where <code>n</code> is the length of the given string and <code>m</code> is the number of unique characters in the string. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 1</summary> | ||
<p> | ||
Which characters would you replace in a string to make all its characters unique? Can you think with respect to the frequency of the characters? | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 2</summary> | ||
<p> | ||
It is always optimal to replace characters with the most frequent character in the string. Why? Because using the most frequent character minimizes the number of replacements required to make all characters in the string identical. How can you find the number of replacements now? | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 3</summary> | ||
<p> | ||
The number of replacements is equal to the difference between the length of the string and the frequency of the most frequent character in the string. A brute force solution would be to consider all substrings, use a hash map for frequency counting, and return the maximum length of the substring that has at most <code>k</code> replacements. This would be an <code>O(n^2)</code> solution. Can you think of a better way? | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 4</summary> | ||
<p> | ||
We can use the sliding window approach. The window size will be dynamic, and we will shrink the window when the number of replacements exceeds <code>k</code>. The result will be the maximum window size observed at each iteration. | ||
</p> | ||
</details> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Recommended Time & Space Complexity</summary> | ||
<p> | ||
You should aim for a solution with <code>O(n)</code> time and <code>O(m)</code> space, where <code>n</code> is the length of the string and <code>m</code> is the number of unique characters in the string. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 1</summary> | ||
<p> | ||
A brute force solution would be to try the substring starting at index <code>i</code> and try to find the maximum length we can form without duplicates by starting at that index. we can use a hash set to detect duplicates in <code>O(1)</code> time. Can you think of a better way? | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 2</summary> | ||
<p> | ||
We can use the sliding window algorithm. Since we only care about substrings without duplicate characters, the sliding window can help us maintain valid substring with its dynamic nature. | ||
</p> | ||
</details> | ||
<br> | ||
<details class="hint-accordion"> | ||
<summary>Hint 3</summary> | ||
<p> | ||
We can iterate through the given string with index <code>r</code> as the right boundary and <code>l</code> as the left boundary of the window. We use a hash set to check if the character is present in the window or not. When we encounter a character at index <code>r</code> that is already present in the window, we shrink the window by incrementing the <code>l</code> pointer until the window no longer contains any duplicates. Also, we remove characters from the hash set that are excluded from the window as the <code>l</code> pointer moves. At each iteration, we update the result with the length of the current window, <code>r - l + 1</code>, if this length is greater than the current result. | ||
</p> | ||
</details> |
Uh oh!
There was an error while loading.Please reload this page.