From ad4abce9f66ff86383a3f71b12b261f6b90e1e53 Mon Sep 17 00:00:00 2001 From: Shuo Date: Wed, 15 Jan 2020 16:34:16 +0800 Subject: [PATCH] Add: Minimum Depth of Binary Tree --- problems/3sum/README.md | 16 +++++++++ problems/bulb-switcher/README.md | 2 +- problems/container-with-most-water/README.md | 23 +++++++++++++ problems/count-and-say/README.md | 7 ++-- .../README.md | 4 +-- problems/create-maximum-number/README.md | 2 +- problems/generalized-abbreviation/README.md | 2 +- problems/length-of-last-word/README.md | 4 +-- problems/merge-sorted-array/README.md | 7 +++- .../minimum_depth_of_binary_tree.go | 28 +++++++++++++++ .../minimum_depth_of_binary_tree_test.go | 34 +++++++++++++++++++ .../README.md | 19 +++++++++++ problems/remove-element/README.md | 7 ++-- .../README.md | 4 ++- 14 files changed, 145 insertions(+), 14 deletions(-) diff --git a/problems/3sum/README.md b/problems/3sum/README.md index 3bdb80943..f9061e54a 100644 --- a/problems/3sum/README.md +++ b/problems/3sum/README.md @@ -38,3 +38,19 @@ A solution set is: 1. [3Sum Closest](../3sum-closest) (Medium) 1. [4Sum](../4sum) (Medium) 1. [3Sum Smaller](../3sum-smaller) (Medium) + +### Hints +
+Hint 1 +So, we essentially need to find three numbers x, y, and z such that they add up to the given value. If we fix one of the numbers say x, we are left with the two-sum problem at hand! +
+ +
+Hint 2 +For the two-sum problem, if we fix one of the numbers, say
x
, we have to scan the entire array to find the next number
y
which is
value - x
where value is the input parameter. Can we change our array somehow so that this search becomes faster? +
+ +
+Hint 3 +The second train of thought for two-sum is, without changing the array, can we use additional space somehow? Like maybe a hash map to speed up the search? +
diff --git a/problems/bulb-switcher/README.md b/problems/bulb-switcher/README.md index b8cdb6bab..51dd1e362 100644 --- a/problems/bulb-switcher/README.md +++ b/problems/bulb-switcher/README.md @@ -28,8 +28,8 @@ So you should return 1, because there is only one bulb is on. ### Related Topics - [[Brainteaser](../../tag/brainteaser/README.md)] [[Math](../../tag/math/README.md)] + [[Brainteaser](../../tag/brainteaser/README.md)] ### Similar Questions 1. [Bulb Switcher II](../bulb-switcher-ii) (Medium) diff --git a/problems/container-with-most-water/README.md b/problems/container-with-most-water/README.md index c56097f9e..6a5accfe4 100644 --- a/problems/container-with-most-water/README.md +++ b/problems/container-with-most-water/README.md @@ -35,3 +35,26 @@ ### Similar Questions 1. [Trapping Rain Water](../trapping-rain-water) (Hard) + +### Hints +
+Hint 1 +The aim is to maximize the area formed between the vertical lines. The area of any container is calculated using the shorter line as length and the distance between the lines as the width of the rectangle. + +
+Area = length of shorter vertical line * distance between lines
+
+ +We can definitely get the maximum width container as the outermost lines have the maximum distance between them. However, this container might not be the maximum in size as one of the vertical lines of this container could be really short. + +
+ + +
+ +
+ +
+Hint 2 +Start with the maximum width container and go to a shorter width container if there is a vertical line longer than the current containers shorter line. This way we are compromising on the width but we are looking forward to a longer length container. +
diff --git a/problems/count-and-say/README.md b/problems/count-and-say/README.md index 9d287b488..ff64e1143 100644 --- a/problems/count-and-say/README.md +++ b/problems/count-and-say/README.md @@ -25,7 +25,7 @@ 11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.

-

Given an integer n where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence.

+

Given an integer n where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence. You can do so recursively, in other words from the previous member read off the digits, counting the number of digits in groups of the same digit.

Note: Each term of the sequence of integers will be represented as a string.

@@ -36,13 +36,16 @@
 Input: 1
 Output: "1"
+Explanation: This is the base case.
 

Example 2:

 Input: 4
-Output: "1211"
+Output: "1211" +Explanation: For n = 3 the term was "21" in which we have two groups "2" and "1", "2" can be read as "12" which means frequency = 1 and value = 2, the same way "1" is read as "11", so the answer is the concatenation of "12" and "11" which is "1211". + ### Related Topics [[String](../../tag/string/README.md)] diff --git a/problems/count-of-smaller-numbers-after-self/README.md b/problems/count-of-smaller-numbers-after-self/README.md index ceae78a02..a298f932d 100644 --- a/problems/count-of-smaller-numbers-after-self/README.md +++ b/problems/count-of-smaller-numbers-after-self/README.md @@ -26,11 +26,11 @@ To the right of 1 there is 0 smaller element. ### Related Topics + [[Binary Search](../../tag/binary-search/README.md)] + [[Divide and Conquer](../../tag/divide-and-conquer/README.md)] [[Sort](../../tag/sort/README.md)] [[Binary Indexed Tree](../../tag/binary-indexed-tree/README.md)] [[Segment Tree](../../tag/segment-tree/README.md)] - [[Binary Search](../../tag/binary-search/README.md)] - [[Divide and Conquer](../../tag/divide-and-conquer/README.md)] ### Similar Questions 1. [Count of Range Sum](../count-of-range-sum) (Hard) diff --git a/problems/create-maximum-number/README.md b/problems/create-maximum-number/README.md index 56b92324f..44e118849 100644 --- a/problems/create-maximum-number/README.md +++ b/problems/create-maximum-number/README.md @@ -47,8 +47,8 @@ k = 3 ### Related Topics - [[Greedy](../../tag/greedy/README.md)] [[Dynamic Programming](../../tag/dynamic-programming/README.md)] + [[Greedy](../../tag/greedy/README.md)] ### Similar Questions 1. [Remove K Digits](../remove-k-digits) (Medium) diff --git a/problems/generalized-abbreviation/README.md b/problems/generalized-abbreviation/README.md index 2756ddefe..be8286812 100644 --- a/problems/generalized-abbreviation/README.md +++ b/problems/generalized-abbreviation/README.md @@ -26,8 +26,8 @@

 

### Related Topics - [[Bit Manipulation](../../tag/bit-manipulation/README.md)] [[Backtracking](../../tag/backtracking/README.md)] + [[Bit Manipulation](../../tag/bit-manipulation/README.md)] ### Similar Questions 1. [Subsets](../subsets) (Medium) diff --git a/problems/length-of-last-word/README.md b/problems/length-of-last-word/README.md index 3273ade81..3ac31cbac 100644 --- a/problems/length-of-last-word/README.md +++ b/problems/length-of-last-word/README.md @@ -11,11 +11,11 @@ ## [58. Length of Last Word (Easy)](https://leetcode.com/problems/length-of-last-word "最后一个单词的长度") -

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.

+

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word (last word means the last appearing word if we loop from left to right) in the string.

If the last word does not exist, return 0.

-

Note: A word is defined as a character sequence consists of non-space characters only.

+

Note: A word is defined as a maximal substring consisting of non-space characters only.

Example:

diff --git a/problems/merge-sorted-array/README.md b/problems/merge-sorted-array/README.md index 9624f6514..3acd0e9bf 100644 --- a/problems/merge-sorted-array/README.md +++ b/problems/merge-sorted-array/README.md @@ -42,5 +42,10 @@ nums2 = [2,5,6], n = 3 ### Hints
Hint 1 -What if you fill the longer array from the end instead of start ? +You can easily solve this problem if you simply think about two elements at a time rather than two arrays. We know that each of the individual arrays is sorted. What we don't know is how they will intertwine. Can we take a local decision and arrive at an optimal solution? +
+ +
+Hint 2 +If you simply consider one element each at a time from the two arrays and make a decision and proceed accordingly, you will arrive at the optimal solution.
diff --git a/problems/minimum-depth-of-binary-tree/minimum_depth_of_binary_tree.go b/problems/minimum-depth-of-binary-tree/minimum_depth_of_binary_tree.go index 1afb002f3..cb1c6f5f7 100644 --- a/problems/minimum-depth-of-binary-tree/minimum_depth_of_binary_tree.go +++ b/problems/minimum-depth-of-binary-tree/minimum_depth_of_binary_tree.go @@ -1 +1,29 @@ package problem111 + +import "github.com/openset/leetcode/internal/kit" + +// TreeNode - Definition for a binary tree node. +type TreeNode = kit.TreeNode + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func minDepth(root *TreeNode) int { + if root == nil { + return 0 + } + l := minDepth(root.Left) + r := minDepth(root.Right) + if l == 0 || r == 0 { + return l + r + 1 + } + if l < r { + return l + 1 + } + return r + 1 +} diff --git a/problems/minimum-depth-of-binary-tree/minimum_depth_of_binary_tree_test.go b/problems/minimum-depth-of-binary-tree/minimum_depth_of_binary_tree_test.go index 1afb002f3..87367c423 100644 --- a/problems/minimum-depth-of-binary-tree/minimum_depth_of_binary_tree_test.go +++ b/problems/minimum-depth-of-binary-tree/minimum_depth_of_binary_tree_test.go @@ -1 +1,35 @@ package problem111 + +import ( + "testing" + + "github.com/openset/leetcode/internal/kit" +) + +type testType struct { + in []int + want int +} + +func TestMinDepth(t *testing.T) { + tests := [...]testType{ + { + in: []int{3, 9, 20, kit.NULL, kit.NULL, 15, 7}, + want: 2, + }, + { + in: []int{1, 2}, + want: 2, + }, + { + in: nil, + want: 0, + }, + } + for _, tt := range tests { + got := minDepth(kit.SliceInt2TreeNode(tt.in)) + if got != tt.want { + t.Fatalf("in: %v, got: %v, want: %v", tt.in, got, tt.want) + } + } +} diff --git a/problems/remove-duplicates-from-sorted-array/README.md b/problems/remove-duplicates-from-sorted-array/README.md index 0dbb6aa76..24e64268b 100644 --- a/problems/remove-duplicates-from-sorted-array/README.md +++ b/problems/remove-duplicates-from-sorted-array/README.md @@ -59,3 +59,22 @@ for (int i = 0; i < len; i++) { ### Similar Questions 1. [Remove Element](../remove-element) (Easy) 1. [Remove Duplicates from Sorted Array II](../remove-duplicates-from-sorted-array-ii) (Medium) + +### Hints +
+Hint 1 +In this problem, the key point to focus on is the input array being sorted. As far as duplicate elements are concerned, what is their positioning in the array when the given array is sorted? Look at the image above for the answer. If we know the position of one of the elements, do we also know the positioning of all the duplicate elements? + +
+ +
+ +
+Hint 2 +We need to modify the array in-place and the size of the final array would potentially be smaller than the size of the input array. So, we ought to use a two-pointer approach here. One, that would keep track of the current element in the original array and another one for just the unique elements. +
+ +
+Hint 3 +Essentially, once an element is encountered, you simply need to bypass its duplicates and move on to the next unique element. +
diff --git a/problems/remove-element/README.md b/problems/remove-element/README.md index b82b30128..3efd8e8fc 100644 --- a/problems/remove-element/README.md +++ b/problems/remove-element/README.md @@ -68,15 +68,16 @@ for (int i = 0; i < len; i++) { ### Hints
Hint 1 -Try two pointers. +The problem statement clearly asks us to modify the array in-place and it also says that the element beyond the new length of the array can be anything. Given an element, we need to remove all the occurrences of it from the array. We don't technically need to remove that element per-say, right?
Hint 2 -Did you use the property of "the order of elements can be changed"? +We can move all the occurrences of this element to the end of the array. Use two pointers! +
Hint 3 -What happens when the elements to remove are rare? +Yet another direction of thought is to consider the elements to be removed as non-existent. In a single pass, if we keep copying the visible elements in-place, that should also solve this problem for us.
diff --git a/problems/substring-with-concatenation-of-all-words/README.md b/problems/substring-with-concatenation-of-all-words/README.md index 960c8ec8c..717be3254 100644 --- a/problems/substring-with-concatenation-of-all-words/README.md +++ b/problems/substring-with-concatenation-of-all-words/README.md @@ -13,6 +13,8 @@

You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.

+

 

+

Example 1:

@@ -20,7 +22,7 @@
   s = "barfoothefoobarman",
   words = ["foo","bar"]
 Output: [0,9]
-Explanation: Substrings starting at index 0 and 9 are "barfoor" and "foobar" respectively.
+Explanation: Substrings starting at index 0 and 9 are "barfoo" and "foobar" respectively.
 The output order does not matter, returning [9,0] is fine too.