Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/leetcode/question_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ func (question questionType) getHints() []byte {
hints := question.Hints
var buf bytes.Buffer
if len(hints) > 0 {
buf.WriteString("\n### Hints\n")
buf.WriteString("\n### Hints")
}
for i, hint := range hints {
buf.WriteString(fmt.Sprintf("<details>\n<summary>Hint %d</summary>\n%s\n</details>\n", i+1, filterContents(hint)))
buf.WriteString(fmt.Sprintf("\n<details>\n<summary>Hint %d</summary>\n%s\n</details>\n", i+1, filterContents(hint)))
}
return buf.Bytes()
}
Expand Down
3 changes: 3 additions & 0 deletions problems/add-digits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ Could you do it without any loop/recursion in O(1) runtime?</p>
<summary>Hint 1</summary>
A naive implementation of the above process is trivial. Could you come up with other methods?
</details>

<details>
<summary>Hint 2</summary>
What are all the possible results?
</details>

<details>
<summary>Hint 3</summary>
How do they occur, periodically or randomly?
</details>

<details>
<summary>Hint 4</summary>
You may find this <a href="https://en.wikipedia.org/wiki/Digital_root" target="_blank">Wikipedia article</a> useful.
Expand Down
3 changes: 3 additions & 0 deletions problems/array-partition-i/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ Given an array of <b>2n</b> integers, your task is to group these integers into
<summary>Hint 1</summary>
Obviously, brute force won't help here. Think of something else, take some example like 1,2,3,4.
</details>

<details>
<summary>Hint 2</summary>
How will you make pairs to get the result? There must be some pattern.
</details>

<details>
<summary>Hint 3</summary>
Did you observe that- Minimum element gets add into the result in sacrifice of maximum element.
</details>

<details>
<summary>Hint 4</summary>
Still won't able to find pairs? Sort the array and try to find the pattern.
Expand Down
3 changes: 3 additions & 0 deletions problems/binary-tree-tilt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,17 @@ Tilt of binary tree : 0 + 0 + 1 = 1
<summary>Hint 1</summary>
Don't think too much, this is an easy problem. Take some small tree as an example.
</details>

<details>
<summary>Hint 2</summary>
Can a parent node use the values of its child nodes? How will you implement it?
</details>

<details>
<summary>Hint 3</summary>
May be recursion and tree traversal can help you in implementing.
</details>

<details>
<summary>Hint 4</summary>
What about postorder traversal, using values of left and right childs?
Expand Down
1 change: 1 addition & 0 deletions problems/binary-watch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<summary>Hint 1</summary>
Simplify by seeking for solutions that involve comparing bit counts.
</details>

<details>
<summary>Hint 2</summary>
Consider calculating all possible times for comparison purposes.
Expand Down
2 changes: 2 additions & 0 deletions problems/camelcase-matching/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@
<summary>Hint 1</summary>
Given a single pattern and word, how can we solve it?
</details>

<details>
<summary>Hint 2</summary>
One way to do it is using a DP (pos1, pos2) where pos1 is a pointer to the word and pos2 to the pattern and returns true if we can match the pattern with the given word.
</details>

<details>
<summary>Hint 3</summary>
We have two scenarios: The first one is when `word[pos1] == pattern[pos2]`, then the transition will be just DP(pos1 + 1, pos2 + 1). The second scenario is when `word[pos1]` is lowercase then we can add this character to the pattern so that the transition is just DP(pos1 + 1, pos2)
Expand Down
3 changes: 3 additions & 0 deletions problems/closest-binary-search-tree-value-ii/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ Consider implement these two helper functions:
<li><code>getSuccessor(N)</code>, which returns the next larger node to N.</li>
</ol>
</details>

<details>
<summary>Hint 2</summary>
Try to assume that each node has a parent pointer, it makes the problem much easier.
</details>

<details>
<summary>Hint 3</summary>
Without parent pointer we just need to keep track of the path from the root to the current node using a stack.
</details>

<details>
<summary>Hint 4</summary>
You would need two stacks to track the path in finding predecessor and successor node separately.
Expand Down
1 change: 1 addition & 0 deletions problems/contains-duplicate-iii/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<summary>Hint 1</summary>
Time complexity O(n logk) - This will give an indication that sorting is involved for k elements.
</details>

<details>
<summary>Hint 2</summary>
Use already existing state to evaluate next state - Like, a set of k sorted numbers are only needed to be tracked. When we are processing the next number in array, then we can utilize the existing sorted state and it is not necessary to sort next overlapping set of k numbers again.
Expand Down
3 changes: 3 additions & 0 deletions problems/copy-list-with-random-pointer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ Node 2&#39;s value is 2, its next pointer points to null and its random pointer
<summary>Hint 1</summary>
Just iterate the linked list and create copies of the nodes on the go. Since a node can be referenced from multiple nodes due to the random pointers, make sure you are not making multiple copies of the same node.
</details>

<details>
<summary>Hint 2</summary>
You may want to use extra space to keep <b>old node ---> new node</b> mapping to prevent creating multiples copies of same node.
</details>

<details>
<summary>Hint 3</summary>
We can avoid using extra space for old node ---> new node mapping, by tweaking the original linked list. Simply interweave the nodes of the old and copied list.
Expand All @@ -63,6 +65,7 @@ Old List: A --> B --> C --> D
InterWeaved List: A --> A' --> B --> B' --> C --> C' --> D --> D'
</pre>
</details>

<details>
<summary>Hint 4</summary>
The interweaving is done using <b>next</b> pointers and we can make use of interweaved structure to get the correct reference nodes for <b>random</b> pointers.
Expand Down
1 change: 1 addition & 0 deletions problems/count-and-say/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ The following are the terms from n=1 to n=10 of the count-and-say sequence:
10. 13211311123113112211
</pre>
</details>

<details>
<summary>Hint 2</summary>
To generate the <i>n</i><sup>th</sup> term, just <i>count and say</i> the <i>n</i>-1<sup>th</sup> term.
Expand Down
4 changes: 4 additions & 0 deletions problems/count-numbers-with-unique-digits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,22 @@
<summary>Hint 1</summary>
A direct way is to use the backtracking approach.
</details>

<details>
<summary>Hint 2</summary>
Backtracking should contains three states which are (the current number, number of steps to get that number and a bitmask which represent which number is marked as visited so far in the current number). Start with state (0,0,0) and count all valid number till we reach number of steps equals to 10<sup>n</sup>.
</details>

<details>
<summary>Hint 3</summary>
This problem can also be solved using a dynamic programming approach and some knowledge of combinatorics.
</details>

<details>
<summary>Hint 4</summary>
Let f(k) = count of numbers with unique digits with length equals k.
</details>

<details>
<summary>Hint 5</summary>
f(1) = 10, ..., f(k) = 9 * 9 * 8 * ... (9 - k + 2) [The first factor is 9 because a number cannot start with 0].
Expand Down
7 changes: 7 additions & 0 deletions problems/count-primes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@
<summary>Hint 1</summary>
<p>Let's start with a <i>isPrime</i> function. To determine if a number is prime, we need to check if it is not divisible by any number less than <i>n</i>. The runtime complexity of <i>isPrime</i> function would be O(<i>n</i>) and hence counting the total prime numbers up to <i>n</i> would be O(<i>n</i><sup>2</sup>). Could we do better?</p>
</details>

<details>
<summary>Hint 2</summary>
<p>As we know the number must not be divisible by any number > <i>n</i> / 2, we can immediately cut the total iterations half by dividing only up to <i>n</i> / 2. Could we still do better?</p>
</details>

<details>
<summary>Hint 3</summary>
<p>Let's write down all of 12's factors:</p>
Expand Down Expand Up @@ -73,6 +75,7 @@ private boolean isPrime(int num) {
}
</pre>
</details>

<details>
<summary>Hint 4</summary>
<p>The <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" target="_blank">Sieve of Eratosthenes</a> is one of the most efficient ways to find all prime numbers up to <i>n</i>. But don't let that name scare you, I promise that the concept is surprisingly simple.</p>
Expand All @@ -84,18 +87,22 @@ private boolean isPrime(int num) {

<p>We start off with a table of <i>n</i> numbers. Let's look at the first number, 2. We know all multiples of 2 must not be primes, so we mark them off as non-primes. Then we look at the next number, 3. Similarly, all multiples of 3 such as 3 × 2 = 6, 3 × 3 = 9, ... must not be primes, so we mark them off as well. Now we look at the next number, 4, which was already marked off. What does this tell you? Should you mark off all multiples of 4 as well?</p>
</details>

<details>
<summary>Hint 5</summary>
<p>4 is not a prime because it is divisible by 2, which means all multiples of 4 must also be divisible by 2 and were already marked off. So we can skip 4 immediately and go to the next number, 5. Now, all multiples of 5 such as 5 × 2 = 10, 5 × 3 = 15, 5 × 4 = 20, 5 × 5 = 25, ... can be marked off. There is a slight optimization here, we do not need to start from 5 × 2 = 10. Where should we start marking off?</p>
</details>

<details>
<summary>Hint 6</summary>
<p>In fact, we can mark off multiples of 5 starting at 5 × 5 = 25, because 5 × 2 = 10 was already marked off by multiple of 2, similarly 5 × 3 = 15 was already marked off by multiple of 3. Therefore, if the current number is <i>p</i>, we can always mark off multiples of <i>p</i> starting at <i>p</i><sup>2</sup>, then in increments of <i>p</i>: <i>p</i><sup>2</sup> + <i>p</i>, <i>p</i><sup>2</sup> + 2<i>p</i>, ... Now what should be the terminating loop condition?</p>
</details>

<details>
<summary>Hint 7</summary>
<p>It is easy to say that the terminating loop condition is <i>p</i> < <i>n</i>, which is certainly correct but not efficient. Do you still remember <i>Hint #3</i>?</p>
</details>

<details>
<summary>Hint 8</summary>
<p>Yes, the terminating loop condition can be <i>p</i> < &radic;<i>n</i>, as all non-primes &ge; &radic;<i>n</i> must have already been marked off. When the loop terminates, all the numbers in the table that are non-marked are prime.</p>
Expand Down
1 change: 1 addition & 0 deletions problems/count-student-number-in-departments/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<summary>Hint 1</summary>
Still remember the difference between 'INNER JOIN' and 'OUTTER JOIN' in SQL?
</details>

<details>
<summary>Hint 2</summary>
Do you know other expressions using the 'COUNT' function besides 'COUNT(*)'?
Expand Down
2 changes: 2 additions & 0 deletions problems/counting-bits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@
<summary>Hint 1</summary>
You should make use of what you have produced already.
</details>

<details>
<summary>Hint 2</summary>
Divide the numbers in ranges like [2-3], [4-7], [8-15] and so on. And try to generate new range from previous.
</details>

<details>
<summary>Hint 3</summary>
Or does the odd/even status of the number help you in calculating the number of 1s?
Expand Down
2 changes: 2 additions & 0 deletions problems/course-schedule-ii/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@
<summary>Hint 1</summary>
This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
</details>

<details>
<summary>Hint 2</summary>
<a href="https://class.coursera.org/algo-003/lecture/52" target="_blank">Topological Sort via DFS</a> - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
</details>

<details>
<summary>Hint 3</summary>
Topological sort could also be done via <a href="http://en.wikipedia.org/wiki/Topological_sorting#Algorithms" target="_blank">BFS</a>.
Expand Down
1 change: 1 addition & 0 deletions problems/course-schedule-iii/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ During iteration, say I want to add the current course, currentTotalTime being t

1. If it doesn’t, then I have added one new course. Increment the currentTotalTime with duration of current course.
</details>

<details>
<summary>Hint 2</summary>
2. If it exceeds deadline, I can swap current course with current courses that has biggest duration.</br>
Expand Down
2 changes: 2 additions & 0 deletions problems/course-schedule/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@
<summary>Hint 1</summary>
This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
</details>

<details>
<summary>Hint 2</summary>
<a href="https://class.coursera.org/algo-003/lecture/52" target="_blank">Topological Sort via DFS</a> - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
</details>

<details>
<summary>Hint 3</summary>
Topological sort could also be done via <a href="http://en.wikipedia.org/wiki/Topological_sorting#Algorithms" target="_blank">BFS</a>.
Expand Down
1 change: 1 addition & 0 deletions problems/design-tic-tac-toe/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<summary>Hint 1</summary>
Could you trade extra space such that <code>move()</code> operation can be done in O(1)?
</details>

<details>
<summary>Hint 2</summary>
You need two arrays: int rows[n], int cols[n], plus two variables: diagonal, anti_diagonal.
Expand Down
3 changes: 3 additions & 0 deletions problems/distribute-candies/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,17 @@ The sister has two different kinds of candies, the brother has only one kind of
<summary>Hint 1</summary>
To maximize the number of kinds of candies, we should try to distribute candies such that sister will gain all kinds.
</details>

<details>
<summary>Hint 2</summary>
What is the upper limit of the number of kinds of candies sister will gain? Remember candies are to distributed equally.
</details>

<details>
<summary>Hint 3</summary>
Which data structure is the most suitable for finding the number of kinds of candies?
</details>

<details>
<summary>Hint 4</summary>
Will hashset solves the problem? Inserting all candies kind in the hashset and then checking its size with upper limit.
Expand Down
1 change: 1 addition & 0 deletions problems/employee-bonus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ empId is the primary key column for this table.
<summary>Hint 1</summary>
If the EmpId in table Employee has no match in table Bonus, we consider that the corresponding bonus is null and null is smaller than 1000.
</details>

<details>
<summary>Hint 2</summary>
Inner join is the default join, we can solve the mismatching problem by using outer join.
Expand Down
1 change: 1 addition & 0 deletions problems/escape-a-large-maze/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Because there are no blocked cells, it&#39;s possible to reach the target square
<summary>Hint 1</summary>
If we become stuck, there's either a loop around the source or around the target.
</details>

<details>
<summary>Hint 2</summary>
If there is a loop around say, the source, what is the maximum number of squares it can have?
Expand Down
4 changes: 4 additions & 0 deletions problems/expression-add-operators/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,17 @@
<summary>Hint 1</summary>
Note that a number can contain multiple digits.
</details>

<details>
<summary>Hint 2</summary>
Since the question asks us to find <b>all</b> of the valid expressions, we need a way to iterate over all of them. (<b>Hint:</b> Recursion!)
</details>

<details>
<summary>Hint 3</summary>
We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expression's value as well so as to avoid the evaluation at the very end of recursion?
</details>

<details>
<summary>Hint 4</summary>
Think carefully about the multiply operator. It has a higher precedence than the addition and subtraction operators.
Expand All @@ -78,6 +81,7 @@ Think carefully about the multiply operator. It has a higher precedence than the
1 + 2 - 4 * 12 --> -1 * 12 --> -12 (WRONG!) <br>
1 + 2 - 4 * 12 --> -1 - (-4) + (-4 * 12) --> 3 + (-48) --> -45 (CORRECT!)
</details>

<details>
<summary>Hint 5</summary>
We simply need to keep track of the last operand in our expression and reverse it's effect on the expression's value while considering the multiply operator.
Expand Down
2 changes: 2 additions & 0 deletions problems/find-cumulative-salary-of-an-employee/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ Employ &#39;3&#39; has two salary records except its most recent pay month &#39;
Seem hard at first glance? Try to divide this problem into some sub-problems.
Think about how to calculate the cumulative sum of one employee, how to get the cumulative sum for many employees, and how to except the most recent month of the result.
</details>

<details>
<summary>Hint 2</summary>
Use the technique of self-join if you have only one table but to write a complex query.
</details>

<details>
<summary>Hint 3</summary>
Still remember how to use the function `sum` and `max`?
Expand Down
2 changes: 2 additions & 0 deletions problems/find-minimum-in-rotated-sorted-array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@
<summary>Hint 1</summary>
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2].
</details>

<details>
<summary>Hint 2</summary>
You can divide the search space into two and see which direction to go.
Can you think of an algorithm which has O(logN) search complexity?
</details>

<details>
<summary>Hint 3</summary>
<ol>
Expand Down
3 changes: 3 additions & 0 deletions problems/find-the-closest-palindrome/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@
<summary>Hint 1</summary>
Will brute force work for this problem? Think of something else.
</details>

<details>
<summary>Hint 2</summary>
Take some examples like 1234, 999,1000, etc and check their closest palindromes. How many different cases are possible?
</details>

<details>
<summary>Hint 3</summary>
Do we have to consider only left half or right half of the string or both?
</details>

<details>
<summary>Hint 4</summary>
Try to find the closest palindrome of these numbers- 12932, 99800, 12120. Did you observe something?
Expand Down
2 changes: 2 additions & 0 deletions problems/first-missing-positive/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ Output: 1
<summary>Hint 1</summary>
Think about how you would solve the problem in non-constant space. Can you apply that logic to the existing space?
</details>

<details>
<summary>Hint 2</summary>
We don't care about duplicates or non-positive integers
</details>

<details>
<summary>Hint 3</summary>
Remember that O(2n) = O(n)
Expand Down
Loading