Skip to content

Commit bb28371

Browse files
author
Shuo
authored
Merge pull request #573 from openset/develop
Update: Hints
2 parents a345b96 + 2f93e48 commit bb28371

File tree

81 files changed

+181
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+181
-2
lines changed

Diff for: internal/leetcode/question_data.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ func (question questionType) getHints() []byte {
172172
hints := question.Hints
173173
var buf bytes.Buffer
174174
if len(hints) > 0 {
175-
buf.WriteString("\n### Hints\n")
175+
buf.WriteString("\n### Hints")
176176
}
177177
for i, hint := range hints {
178-
buf.WriteString(fmt.Sprintf("<details>\n<summary>Hint %d</summary>\n%s\n</details>\n", i+1, filterContents(hint)))
178+
buf.WriteString(fmt.Sprintf("\n<details>\n<summary>Hint %d</summary>\n%s\n</details>\n", i+1, filterContents(hint)))
179179
}
180180
return buf.Bytes()
181181
}

Diff for: problems/add-digits/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,17 @@ Could you do it without any loop/recursion in O(1) runtime?</p>
3636
<summary>Hint 1</summary>
3737
A naive implementation of the above process is trivial. Could you come up with other methods?
3838
</details>
39+
3940
<details>
4041
<summary>Hint 2</summary>
4142
What are all the possible results?
4243
</details>
44+
4345
<details>
4446
<summary>Hint 3</summary>
4547
How do they occur, periodically or randomly?
4648
</details>
49+
4750
<details>
4851
<summary>Hint 4</summary>
4952
You may find this <a href="https://en.wikipedia.org/wiki/Digital_root" target="_blank">Wikipedia article</a> useful.

Diff for: problems/array-partition-i/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,17 @@ Given an array of <b>2n</b> integers, your task is to group these integers into
3939
<summary>Hint 1</summary>
4040
Obviously, brute force won't help here. Think of something else, take some example like 1,2,3,4.
4141
</details>
42+
4243
<details>
4344
<summary>Hint 2</summary>
4445
How will you make pairs to get the result? There must be some pattern.
4546
</details>
47+
4648
<details>
4749
<summary>Hint 3</summary>
4850
Did you observe that- Minimum element gets add into the result in sacrifice of maximum element.
4951
</details>
52+
5053
<details>
5154
<summary>Hint 4</summary>
5255
Still won't able to find pairs? Sort the array and try to find the pattern.

Diff for: problems/binary-tree-tilt/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,17 @@ Tilt of binary tree : 0 + 0 + 1 = 1
4747
<summary>Hint 1</summary>
4848
Don't think too much, this is an easy problem. Take some small tree as an example.
4949
</details>
50+
5051
<details>
5152
<summary>Hint 2</summary>
5253
Can a parent node use the values of its child nodes? How will you implement it?
5354
</details>
55+
5456
<details>
5557
<summary>Hint 3</summary>
5658
May be recursion and tree traversal can help you in implementing.
5759
</details>
60+
5861
<details>
5962
<summary>Hint 4</summary>
6063
What about postorder traversal, using values of left and right childs?

Diff for: problems/binary-watch/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<summary>Hint 1</summary>
4444
Simplify by seeking for solutions that involve comparing bit counts.
4545
</details>
46+
4647
<details>
4748
<summary>Hint 2</summary>
4849
Consider calculating all possible times for comparison purposes.

Diff for: problems/camelcase-matching/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,12 @@
6666
<summary>Hint 1</summary>
6767
Given a single pattern and word, how can we solve it?
6868
</details>
69+
6970
<details>
7071
<summary>Hint 2</summary>
7172
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.
7273
</details>
74+
7375
<details>
7476
<summary>Hint 3</summary>
7577
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)

Diff for: problems/closest-binary-search-tree-value-ii/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,17 @@ Consider implement these two helper functions:
2929
<li><code>getSuccessor(N)</code>, which returns the next larger node to N.</li>
3030
</ol>
3131
</details>
32+
3233
<details>
3334
<summary>Hint 2</summary>
3435
Try to assume that each node has a parent pointer, it makes the problem much easier.
3536
</details>
37+
3638
<details>
3739
<summary>Hint 3</summary>
3840
Without parent pointer we just need to keep track of the path from the root to the current node using a stack.
3941
</details>
42+
4043
<details>
4144
<summary>Hint 4</summary>
4245
You would need two stacks to track the path in finding predecessor and successor node separately.

Diff for: problems/contains-duplicate-iii/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<summary>Hint 1</summary>
5454
Time complexity O(n logk) - This will give an indication that sorting is involved for k elements.
5555
</details>
56+
5657
<details>
5758
<summary>Hint 2</summary>
5859
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.

Diff for: problems/copy-list-with-random-pointer/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ Node 2&#39;s value is 2, its next pointer points to null and its random pointer
5050
<summary>Hint 1</summary>
5151
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.
5252
</details>
53+
5354
<details>
5455
<summary>Hint 2</summary>
5556
You may want to use extra space to keep <b>old node ---> new node</b> mapping to prevent creating multiples copies of same node.
5657
</details>
58+
5759
<details>
5860
<summary>Hint 3</summary>
5961
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.
@@ -63,6 +65,7 @@ Old List: A --> B --> C --> D
6365
InterWeaved List: A --> A' --> B --> B' --> C --> C' --> D --> D'
6466
</pre>
6567
</details>
68+
6669
<details>
6770
<summary>Hint 4</summary>
6871
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.

Diff for: problems/count-and-say/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ The following are the terms from n=1 to n=10 of the count-and-say sequence:
6868
10. 13211311123113112211
6969
</pre>
7070
</details>
71+
7172
<details>
7273
<summary>Hint 2</summary>
7374
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.

Diff for: problems/count-numbers-with-unique-digits/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,22 @@
3434
<summary>Hint 1</summary>
3535
A direct way is to use the backtracking approach.
3636
</details>
37+
3738
<details>
3839
<summary>Hint 2</summary>
3940
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>.
4041
</details>
42+
4143
<details>
4244
<summary>Hint 3</summary>
4345
This problem can also be solved using a dynamic programming approach and some knowledge of combinatorics.
4446
</details>
47+
4548
<details>
4649
<summary>Hint 4</summary>
4750
Let f(k) = count of numbers with unique digits with length equals k.
4851
</details>
52+
4953
<details>
5054
<summary>Hint 5</summary>
5155
f(1) = 10, ..., f(k) = 9 * 9 * 8 * ... (9 - k + 2) [The first factor is 9 because a number cannot start with 0].

Diff for: problems/count-primes/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535
<summary>Hint 1</summary>
3636
<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>
3737
</details>
38+
3839
<details>
3940
<summary>Hint 2</summary>
4041
<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>
4142
</details>
43+
4244
<details>
4345
<summary>Hint 3</summary>
4446
<p>Let's write down all of 12's factors:</p>
@@ -73,6 +75,7 @@ private boolean isPrime(int num) {
7375
}
7476
</pre>
7577
</details>
78+
7679
<details>
7780
<summary>Hint 4</summary>
7881
<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>
@@ -84,18 +87,22 @@ private boolean isPrime(int num) {
8487

8588
<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>
8689
</details>
90+
8791
<details>
8892
<summary>Hint 5</summary>
8993
<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>
9094
</details>
95+
9196
<details>
9297
<summary>Hint 6</summary>
9398
<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>
9499
</details>
100+
95101
<details>
96102
<summary>Hint 7</summary>
97103
<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>
98104
</details>
105+
99106
<details>
100107
<summary>Hint 8</summary>
101108
<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>

Diff for: problems/count-student-number-in-departments/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<summary>Hint 1</summary>
7878
Still remember the difference between 'INNER JOIN' and 'OUTTER JOIN' in SQL?
7979
</details>
80+
8081
<details>
8182
<summary>Hint 2</summary>
8283
Do you know other expressions using the 'COUNT' function besides 'COUNT(*)'?

Diff for: problems/counting-bits/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@
4646
<summary>Hint 1</summary>
4747
You should make use of what you have produced already.
4848
</details>
49+
4950
<details>
5051
<summary>Hint 2</summary>
5152
Divide the numbers in ranges like [2-3], [4-7], [8-15] and so on. And try to generate new range from previous.
5253
</details>
54+
5355
<details>
5456
<summary>Hint 3</summary>
5557
Or does the odd/even status of the number help you in calculating the number of 1s?

Diff for: problems/course-schedule-ii/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@
6161
<summary>Hint 1</summary>
6262
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.
6363
</details>
64+
6465
<details>
6566
<summary>Hint 2</summary>
6667
<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.
6768
</details>
69+
6870
<details>
6971
<summary>Hint 3</summary>
7072
Topological sort could also be done via <a href="http://en.wikipedia.org/wiki/Topological_sorting#Algorithms" target="_blank">BFS</a>.

Diff for: problems/course-schedule-iii/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ During iteration, say I want to add the current course, currentTotalTime being t
5353

5454
1. If it doesn’t, then I have added one new course. Increment the currentTotalTime with duration of current course.
5555
</details>
56+
5657
<details>
5758
<summary>Hint 2</summary>
5859
2. If it exceeds deadline, I can swap current course with current courses that has biggest duration.</br>

Diff for: problems/course-schedule/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@
5959
<summary>Hint 1</summary>
6060
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.
6161
</details>
62+
6263
<details>
6364
<summary>Hint 2</summary>
6465
<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.
6566
</details>
67+
6668
<details>
6769
<summary>Hint 3</summary>
6870
Topological sort could also be done via <a href="http://en.wikipedia.org/wiki/Topological_sorting#Algorithms" target="_blank">BFS</a>.

Diff for: problems/design-tic-tac-toe/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<summary>Hint 1</summary>
2525
Could you trade extra space such that <code>move()</code> operation can be done in O(1)?
2626
</details>
27+
2728
<details>
2829
<summary>Hint 2</summary>
2930
You need two arrays: int rows[n], int cols[n], plus two variables: diagonal, anti_diagonal.

Diff for: problems/distribute-candies/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,17 @@ The sister has two different kinds of candies, the brother has only one kind of
4848
<summary>Hint 1</summary>
4949
To maximize the number of kinds of candies, we should try to distribute candies such that sister will gain all kinds.
5050
</details>
51+
5152
<details>
5253
<summary>Hint 2</summary>
5354
What is the upper limit of the number of kinds of candies sister will gain? Remember candies are to distributed equally.
5455
</details>
56+
5557
<details>
5658
<summary>Hint 3</summary>
5759
Which data structure is the most suitable for finding the number of kinds of candies?
5860
</details>
61+
5962
<details>
6063
<summary>Hint 4</summary>
6164
Will hashset solves the problem? Inserting all candies kind in the hashset and then checking its size with upper limit.

Diff for: problems/employee-bonus/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ empId is the primary key column for this table.
5959
<summary>Hint 1</summary>
6060
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.
6161
</details>
62+
6263
<details>
6364
<summary>Hint 2</summary>
6465
Inner join is the default join, we can solve the mismatching problem by using outer join.

Diff for: problems/escape-a-large-maze/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Because there are no blocked cells, it&#39;s possible to reach the target square
5858
<summary>Hint 1</summary>
5959
If we become stuck, there's either a loop around the source or around the target.
6060
</details>
61+
6162
<details>
6263
<summary>Hint 2</summary>
6364
If there is a loop around say, the source, what is the maximum number of squares it can have?

Diff for: problems/expression-add-operators/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,17 @@
6161
<summary>Hint 1</summary>
6262
Note that a number can contain multiple digits.
6363
</details>
64+
6465
<details>
6566
<summary>Hint 2</summary>
6667
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!)
6768
</details>
69+
6870
<details>
6971
<summary>Hint 3</summary>
7072
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?
7173
</details>
74+
7275
<details>
7376
<summary>Hint 4</summary>
7477
Think carefully about the multiply operator. It has a higher precedence than the addition and subtraction operators.
@@ -78,6 +81,7 @@ Think carefully about the multiply operator. It has a higher precedence than the
7881
1 + 2 - 4 * 12 --> -1 * 12 --> -12 (WRONG!) <br>
7982
1 + 2 - 4 * 12 --> -1 - (-4) + (-4 * 12) --> 3 + (-48) --> -45 (CORRECT!)
8083
</details>
84+
8185
<details>
8286
<summary>Hint 5</summary>
8387
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.

Diff for: problems/find-cumulative-salary-of-an-employee/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,12 @@ Employ &#39;3&#39; has two salary records except its most recent pay month &#39;
8686
Seem hard at first glance? Try to divide this problem into some sub-problems.
8787
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.
8888
</details>
89+
8990
<details>
9091
<summary>Hint 2</summary>
9192
Use the technique of self-join if you have only one table but to write a complex query.
9293
</details>
94+
9395
<details>
9496
<summary>Hint 3</summary>
9597
Still remember how to use the function `sum` and `max`?

Diff for: problems/find-minimum-in-rotated-sorted-array/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@
4646
<summary>Hint 1</summary>
4747
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].
4848
</details>
49+
4950
<details>
5051
<summary>Hint 2</summary>
5152
You can divide the search space into two and see which direction to go.
5253
Can you think of an algorithm which has O(logN) search complexity?
5354
</details>
55+
5456
<details>
5557
<summary>Hint 3</summary>
5658
<ol>

Diff for: problems/find-the-closest-palindrome/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,17 @@
3737
<summary>Hint 1</summary>
3838
Will brute force work for this problem? Think of something else.
3939
</details>
40+
4041
<details>
4142
<summary>Hint 2</summary>
4243
Take some examples like 1234, 999,1000, etc and check their closest palindromes. How many different cases are possible?
4344
</details>
45+
4446
<details>
4547
<summary>Hint 3</summary>
4648
Do we have to consider only left half or right half of the string or both?
4749
</details>
50+
4851
<details>
4952
<summary>Hint 4</summary>
5053
Try to find the closest palindrome of these numbers- 12932, 99800, 12120. Did you observe something?

Diff for: problems/first-missing-positive/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ Output: 1
5252
<summary>Hint 1</summary>
5353
Think about how you would solve the problem in non-constant space. Can you apply that logic to the existing space?
5454
</details>
55+
5556
<details>
5657
<summary>Hint 2</summary>
5758
We don't care about duplicates or non-positive integers
5859
</details>
60+
5961
<details>
6062
<summary>Hint 3</summary>
6163
Remember that O(2n) = O(n)

0 commit comments

Comments
 (0)