Skip to content

Commit b25f0e8

Browse files
add 628 and clean up
1 parent 2214f62 commit b25f0e8

15 files changed

+105
-138
lines changed

README.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Your ideas/fixes/algorithms are more than welcome!
2020

2121
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2222
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
23+
|628|[Maximum Product of Three Numbers](https://leetcode.com/problems/maximum-product-of-three-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_628.java) | O(nlogn) |O(1) | Easy |
2324
|625|[Minimum Factorization](https://leetcode.com/problems/minimum-factorization/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_625.java) | O(?) |O(?) | Medium |
2425
|624|[Maximum Distance in Arrays](https://leetcode.com/problems/maximum-distance-in-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_624.java) | O(nlogn) |O(1) | Easy | Sort, Array
2526
|623|[Add One Row to Tree](https://leetcode.com/problems/add-one-row-to-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_623.java) | O(n) |O(h) | Medium | Tree
@@ -58,15 +59,15 @@ Your ideas/fixes/algorithms are more than welcome!
5859
|557|[Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/ReverseWordsinaStringIII.java) | O(n) |O(n) | Easy | String
5960
|556|[Next Greater Element III](https://leetcode.com/problems/next-greater-element-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/NextGreaterElementIII.java) | O(n)|O(1)| Medium | String
6061
|555|[Split Concatenated Strings](https://leetcode.com/problems/split-concatenated-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_555.java) | O(n^2) |O(n) | Medium | String
61-
|554|[Brick Wall](https://leetcode.com/problems/brick-wall/)|[Solution](../master/src/main/java/com/fishercoder/solutions/BrickWall.java) | O(n) (n is total number of bricks in the wall) |O(m) (m is width of the wall) | Medium | HashMap
62+
|554|[Brick Wall](https://leetcode.com/problems/brick-wall/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_554.java) | O(n) (n is total number of bricks in the wall) |O(m) (m is width of the wall) | Medium | HashMap
6263
|553|[Optimal Division](https://leetcode.com/problems/optimal-division/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_553.java) | O(n) | O(n) | Medium | String
6364
|552|[Student Attendance Record II](https://leetcode.com/problems/student-attendance-record-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_552.java) | O(n)| O(1) | Hard| DP
6465
|551|[Student Attendance Record I](https://leetcode.com/problems/student-attendance-record-i/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_551.java) | O(n)| O(1) | Easy| String
6566
|549|[Binary Tree Longest Consecutive Sequence II](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/BinaryTreeLongestConsecutiveSequenceII.java) | O(n) |O(n) | Medium | Tree
6667
|548|[Split Array with Equal Sum](https://leetcode.com/problems/split-array-with-equal-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_548.java) | O(n^2) |O(1) | Medium | Array
6768
|547|[Friend Circles](https://leetcode.com/problems/friend-circles/)|[Solution](../master/src/main/java/com/fishercoder/solutions/FriendCircles.java) | O(n^2) |O(n) | Medium | Union Find
6869
|546|[Remove Boxes](https://leetcode.com/problems/remove-boxes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_546.java) | O(n^3) |O(n^3) | Hard| DFS, DP
69-
|545|[Boundary of Binary Tree](https://leetcode.com/problems/boundary-of-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/BoundaryofBinaryTree.java) | O(n) |O(n) | Medium | Recursion
70+
|545|[Boundary of Binary Tree](https://leetcode.com/problems/boundary-of-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_545.java) | O(n) |O(n) | Medium | Recursion
7071
|544|[Output Contest Matches](https://leetcode.com/problems/output-contest-matches/)|[Solution](../master/src/main/java/com/fishercoder/solutions/OutputContestMatches.java) | O(n) |O(n) | Medium | Recursion
7172
|543|[Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/DiameterofBinaryTree.java) | O(n) |O(h) | Easy | Tree/DFS/Recursion
7273
|542|[01 Matrix](https://leetcode.com/problems/01-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_542.java) | O(m*n) |O(n) | Medium | BFS
@@ -157,7 +158,7 @@ Your ideas/fixes/algorithms are more than welcome!
157158
|438|[Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/FindAllAnagramsinaString.java)| O(n)|O(1) | Easy|
158159
|437|[Path Sum III](https://leetcode.com/problems/path-sum-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/PathSumIII.java) | O(n^2) |O(n) | Easy| DFS, recursion
159160
|436|[Find Right Interval](https://leetcode.com/problems/find-right-interval/)|[Solution](../master/src/main/java/com/fishercoder/solutions/FindRightInterval.java) | O(nlogn) |O(n) | Medium| Binary Search
160-
|435|[Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/)|[Solution](../master/src/main/java/com/fishercoder/solutions/NonOverlappingIntervals.java) | O(nlogn) |O(1) | Medium| Greedy
161+
|435|[Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_435.java) | O(nlogn) |O(1) | Medium| Greedy
161162
|434|[Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/NumberofSegmentsinaString.java)| O(n)|O(1) | Easy|
162163
|432|[All O`one Data Structure](https://leetcode.com/problems/all-oone-data-structure/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_432.java)| O(1)|O(n) | Hard| Design
163164
|425|[Word Squares](https://leetcode.com/problems/word-squares/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_425.java)| O(n!)|O(n) | Hard| Trie, Backtracking, Recursion
@@ -213,7 +214,7 @@ Your ideas/fixes/algorithms are more than welcome!
213214
|365|[Water and Jug Problem](https://leetcode.com/problems/water-and-jug-problem/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_365.java)| O(n)|O(1) | Medium| Math
214215
|364|[Nested List Weight Sum II](https://leetcode.com/problems/nested-list-weight-sum-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/NestedListWeightSumII.java)| O(n)|O(h) | Medium| DFS
215216
|362|[Design Hit Counter](https://leetcode.com/problems/design-hit-counter/)|[Solution](../master/src/main/java/com/fishercoder/solutions/DesignHitCounter.java)| O(1) amortized|O(k) | Medium| Design
216-
|361|[Bomb Enemy](https://leetcode.com/problems/bomb-enemy/)|[Solution](../master/src/main/java/com/fishercoder/solutions/BombEnemies.java)| O(?)|O(?) | Medium|
217+
|361|[Bomb Enemy](https://leetcode.com/problems/bomb-enemy/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_361.java)| O(?)|O(?) | Medium|
217218
|360|[Sort Transformed Array](https://leetcode.com/problems/sort-transformed-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_360.java)| O(n)|O(1) | Medium| Two Pointers, Math
218219
|359|[Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter/)|[Solution](../master/src/main/java/com/fishercoder/solutions/LoggerRateLimiter.java)| amortized O(1)|O(k) | Easy| HashMap
219220
|358|[Rearrange String k Distance Apart](https://leetcode.com/problems/rearrange-string-k-distance-apart/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_358.java)| O(n)|O(n) | Hard| HashMap, Heap, Greedy
@@ -231,7 +232,7 @@ Your ideas/fixes/algorithms are more than welcome!
231232
|343|[Integer Break](https://leetcode.com/problems/integer-break/)|[Solution](../master/src/main/java/com/fishercoder/solutions/IntegerBreak.java)| O(1)|O(1) | Medium| Math
232233
|341|[Flatten Nested List Iterator](https://leetcode.com/problems/flatten-nested-list-iterator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_341.java)| O(n)|O(n) | Medium| Stack
233234
|340|[Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_340.java)| O(n)|O(1) | Hard| Sliding Window
234-
|339|[Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/NestedListWeightSum.java)| O(n)|O(h)) | Easy| DFS
235+
|339|[Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_339.java)| O(n)|O(h)) | Easy| DFS
235236
|338|[Counting Bits](https://leetcode.com/problems/counting-bits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_338.java)| O(nlogn)|O(h) | Medium|
236237
|337|[House Robber III](https://leetcode.com/problems/house-robber-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_337.java)| O(n)|O(n)| Medium | DP
237238
|336|[Palindrome Pairs](https://leetcode.com/problems/palindrome-pairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/PalindromePairs.java)| O(n^2)|O(n) | Hard|
@@ -269,13 +270,13 @@ Your ideas/fixes/algorithms are more than welcome!
269270
|301|[Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/RemoveInvalidParentheses.java)| ? | ? | Hard| BFS
270271
|300|[Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_300.java)| O(logn)|O(n) | Medium| DP
271272
|299|[Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_299.java)| O(n)|O(1) | Easy|
272-
|298|[Binary Tree Longest Consecutive Sequence](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/BinaryTreeLongestConsecutiveSequence.java)| O(n)|O(n) | Medium | Tree
273+
|298|[Binary Tree Longest Consecutive Sequence](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_298.java)| O(n)|O(n) | Medium | Tree
273274
|297|[Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_297.java)| O(n) | O(h) | Hard| BFS
274275
|296|[Best Meeting Point](https://leetcode.com/problems/best-meeting-point/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_296.java)| ?|? | Hard|
275276
|295|[Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_295.java)| O(nlogn) | O(n) | Hard| Heap
276277
|294|[Flip Game II](https://leetcode.com/problems/flip-game-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_294.java)| O(?) | O(?)| Medium| Backtracking
277278
|293|[Flip Game](https://leetcode.com/problems/flip-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/FlipGame.java)| O(n) | O(1)| Easy|
278-
|292|[Nim Game](https://leetcode.com/problems/nim-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/NimGame.java)| O(1)|O(1) | Easy|
279+
|292|[Nim Game](https://leetcode.com/problems/nim-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_292.java)| O(1)|O(1) | Easy|
279280
|291|[Word Pattern II](https://leetcode.com/problems/word-pattern-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_291.java)| O(n)|O(n) | Hard|
280281
|290|[Word Pattern](https://leetcode.com/problems/word-pattern/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_290.java)| O(n)|O(n) | Easy|
281282
|289|[Game of Life](https://leetcode.com/problems/game-of-life/)|[Solution](../master/src/main/java/com/fishercoder/solutions/GameOfLife.java)| O(m*n)|O(m*n), could be optimized to O(1) | Medium|
@@ -308,7 +309,7 @@ Your ideas/fixes/algorithms are more than welcome!
308309
|261|[Graph Valid Tree](https://leetcode.com/problems/graph-valid-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/GraphValidTree.java)| O(V+E)|O(V+E) | Medium|
309310
|259|[3Sum Smaller](https://leetcode.com/problems/3sum-smaller/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_259.java)| O(n^2)|O(1) | Medium|
310311
|258|[Add Digits](https://leetcode.com/problems/add-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_258.java)| O(1)|O(1) | Easy|
311-
|257|[Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/)|[Solution](../master/src/main/java/com/fishercoder/solutions/BinaryTreePaths.java) | O(n*h) | O(h) | DFS/Recursion
312+
|257|[Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_257.java) | O(n*h) | O(h) | DFS/Recursion
312313
|256|[Paint House](https://leetcode.com/problems/paint-house/)|[Solution](../master/src/main/java/com/fishercoder/solutions/PaintHouse.java) | O(n) | O(1) | Medium| DP
313314
|255|[Verify Preorder Sequence in Binary Search Tree](https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_255.java) | O(n) | O(h) | Medium| Tree
314315
|254|[Factor Combinations](https://leetcode.com/problems/factor-combinations/)|[Solution](../master/src/main/java/com/fishercoder/solutions/FactorCombinations.java) | O(nlogn) | O(nlogn) | Medium| Backtracking

src/main/java/com/fishercoder/solutions/BinaryTreeLevelOrderTraversalII.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
[9,20],
2525
[3]
2626
]
27-
2827
*/
28+
2929
public class BinaryTreeLevelOrderTraversalII {
3030
public List<List<Integer>> levelOrder(TreeNode root) {
3131
List<List<Integer>> result = new ArrayList<List<Integer>>();

src/main/java/com/fishercoder/solutions/NimGame.java

-22
This file was deleted.

src/main/java/com/fishercoder/solutions/BinaryTreePaths.java renamed to src/main/java/com/fishercoder/solutions/_257.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
2222
["1->2->5", "1->3"]
2323
*/
24-
public class BinaryTreePaths {
24+
25+
public class _257 {
2526
//a very typical/good question to test your recursion/dfs understanding.
2627
public List<String> binaryTreePaths_more_concise(TreeNode root) {
2728
List<String> paths = new ArrayList<String>();
@@ -41,7 +42,7 @@ private void dfs(TreeNode root, List<String> paths, String path) {
4142
}
4243

4344
public static void main(String...strings){
44-
BinaryTreePaths test = new BinaryTreePaths();
45+
_257 test = new _257();
4546
TreeNode root = new TreeNode(1);
4647
root.left = new TreeNode(2);
4748
root.left.right = new TreeNode(5);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.fishercoder.solutions;
2+
3+
/**You are playing the following Nim Game with your friend:
4+
* There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones.
5+
* The one who removes the last stone will be the winner. You will take the first turn to remove the stones.
6+
* Both of you are very clever and have optimal strategies for the game.
7+
* Write a function to determine whether you can win the game given the number of stones in the heap.
8+
* For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove,
9+
* the last stone will always be removed by your friend.
10+
11+
Hint:
12+
If there are 5 stones in the heap, could you figure out a way to remove the stones such that you will always be the winner?*/
13+
14+
public class _292 {
15+
16+
/**1. If there are only 1 or 2 or 3 stones, you could always win by taking 1 or 2 or 3 stones;
17+
* 2. If there are 4 stones, you could never win because no matter you tak 1 or 2 or 3 stones, you could never take the 4th one;
18+
* 3. If there are 5 or 6 or 7 stones, you could always win because no matter how your opponent works, you'll always get the last one;
19+
* 4. Then we could deduce that as long as the number is not divisible by 4, you could always win.*/
20+
21+
public boolean canWinNim(int n) {
22+
return n%4 != 0;
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ The longest consecutive path need to be from parent to child (cannot be the reve
2727
1
2828
Longest consecutive sequence path is 2-3,not3-2-1, so return 2.
2929
*/
30-
public class BinaryTreeLongestConsecutiveSequence {
30+
public class _298 {
3131

3232
private int max = 1;
3333
public int longestConsecutive(TreeNode root) {

src/main/java/com/fishercoder/solutions/NestedListWeightSum.java renamed to src/main/java/com/fishercoder/solutions/_339.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import java.util.List;
66

7-
public class NestedListWeightSum {
7+
public class _339 {
88
class Solution_with_global_sum {
99
private int sum = 0;
1010

src/main/java/com/fishercoder/solutions/BombEnemies.java renamed to src/main/java/com/fishercoder/solutions/_361.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
1515
return 3. (Placing a bomb at (1,1) kills 3 enemies)
1616
*/
17-
public class BombEnemies {
17+
public class _361 {
1818

1919
public int maxKilledEnemies(char[][] grid) {
2020
int m = grid.length;

src/main/java/com/fishercoder/solutions/NonOverlappingIntervals.java renamed to src/main/java/com/fishercoder/solutions/_435.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@
2929
Output: 0
3030
3131
Explanation: You don't need to remove any of the intervals since they're already non-overlapping.*/
32-
public class NonOverlappingIntervals {
33-
/**Looked at these two posts: https://discuss.leetcode.com/topic/65828/java-solution-with-clear-explain
34-
* and https://discuss.leetcode.com/topic/65594/java-least-is-most
35-
* Sort the intervals by their end time, if equal, then sort by their start time.*/
32+
33+
public class _435 {
34+
35+
/**
36+
* References:: https://discuss.leetcode.com/topic/65828/java-solution-with-clear-explain
37+
* and https://discuss.leetcode.com/topic/65594/java-least-is-most
38+
* Sort the intervals by their end time, if equal, then sort by their start time.
39+
*/
3640
public static int eraseOverlapIntervals(Interval[] intervals) {
3741
Collections.sort(Arrays.asList(intervals), new Comparator<Interval>(){
3842
@Override
@@ -57,8 +61,6 @@ public static void main(String...args){
5761
Interval interval3 = new Interval(1,11);
5862
Interval interval4 = new Interval(2,12);
5963
Interval[] intervals = new Interval[]{interval1, interval2, interval3, interval4};
60-
61-
6264
System.out.println(eraseOverlapIntervals(intervals));
6365
}
6466

src/main/java/com/fishercoder/solutions/BoundaryofBinaryTree.java renamed to src/main/java/com/fishercoder/solutions/_545.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
So order them in anti-clockwise without duplicate nodes we have [1,2,4,7,8,9,10,6,3].
5555
5656
*/
57-
public class BoundaryofBinaryTree {
57+
public class _545 {
5858
public List<Integer> boundaryOfBinaryTree(TreeNode root) {
5959
List<Integer> nodes = new ArrayList<>(1000);
6060
if(root == null) return nodes;

src/main/java/com/fishercoder/solutions/BrickWall.java renamed to src/main/java/com/fishercoder/solutions/_554.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
The width sum of bricks in different rows are the same and won't exceed INT_MAX.
2929
The number of bricks in each row is in range [1,10,000]. The height of wall is in range [1,10,000]. Total number of bricks of the wall won't exceed 20,000.
3030
*/
31-
public class BrickWall {
31+
public class _554 {
3232
//credit to: https://leetcode.com/articles/brick-wall/
3333

3434
/**we make use of a HashMap

0 commit comments

Comments
 (0)