Skip to content

Commit e566521

Browse files
[LEET-0000] clean up
1 parent 1cdaece commit e566521

9 files changed

+416
-3
lines changed

leetcode-algorithms/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
|362|[Design Hit Counter](https://leetcode.com/problems/design-hit-counter/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/DesignHitCounter.java)| O(1) amortized|O(k) | Medium| Design
6161
|361|[Bomb Enemy](https://leetcode.com/problems/bomb-enemy/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BombEnemies.java)| O(?)|O(?) | Medium|
6262
|359|[Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/LoggerRateLimiter.java)| amortized O(1)|O(k) | Easy| HashMap
63+
|357|[Count Numbers with Unique Digits](https://leetcode.com/problems/count-numbers-with-unique-digits/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/CountNumbersWithUniqueDigits.java)| O(?)|O(?) | Medium|
64+
|353|[Design Snake Game](https://leetcode.com/problems/design-snake-game/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/DesignSnakeGame.java)| O(?)|O(?) | Medium|
6365
|351|[Android Unlock Patterns](https://leetcode.com/problems/android-unlock-patterns/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/AndroidUnlockPatterns.java)| O(?)|O(?) | Medium|
6466
|350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/IntersectionOfTwoArraysII.java)| O(m+n)|O((m+n)) could be optimized | Easy| HashMap, Binary Search
6567
|349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/IntersectionOfTwoArrays.java)| O(m+n)|O(min(m,n)) | Easy| Two Pointers, Binary Search
@@ -75,6 +77,7 @@
7577
|325|[Maximum Size Subarray Sum Equals k](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/)|[Solution] | O(n)|O(n) | Medium| HashMap
7678
|323|[Number of Connected Components in an Undirected Graph](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/NumberOfConnectedComponentsInAnUndirectedGraph.java)| O(?)|O(?)| Medium|
7779
|322|[Coin Change](https://leetcode.com/problems/coin-change/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/CoinChange.java)| O(?)|O(?) | Medium|
80+
|321|[Create Maximum Number](https://leetcode.com/problems/create-maximum-number/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/CreateMaximumNumber.java)| O(?)|O(?) | Hard
7881
|317|[Shortest Distance from All Buildings](https://leetcode.com/problems/shortest-distance-from-all-buildings/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ShortestDistanceFromAllBuildings.java)| O(?)|O(?) | Hard|
7982
|315|[Count of Smaller Numbers After Self](https://leetcode.com/problems/count-of-smaller-numbers-after-self/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/CountOfSmallerNumbersAfterSelf.java)| O(?)|O(?)| Hard| Tree
8083
|314|[Binary Tree Vertical Order Traversal](https://leetcode.com/problems/binary-tree-vertical-order-traversal/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BinaryTreeVerticalOrderTraversal.java)| O(n)|O(n) | Medium| HashMap, BFS
@@ -201,13 +204,15 @@
201204
|141|[Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/LinkedListCycle.java)| O(n)|O(1) | Easy| Linked List
202205
|140|[Word Break II](https://leetcode.com/problems/word-break-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/WordBreakII.java)| ? |O(n^2) | Hard| Backtracking/DFS
203206
|139|[Word Break](https://leetcode.com/problems/word-break/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/WordBreak.java)| O(n^2)|O(n) | Medium| DP
207+
|138|[Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/CopyListWithRandomPointer.java)| O(?)|O(?) | Medium|
204208
|137|[Single Number II](https://leetcode.com/problems/single-number-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/SingleNumberII.java)| O(n)|O(n) | Medium|
205209
|135|[Candy](https://leetcode.com/problems/candy/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/Candy.java)| O(n)|O(1) | Hard| Greedy
206210
|133|[Clone Graph](https://leetcode.com/problems/clone-graph/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/CloneGraph.java)| O(n)|O(n) | Medium| HashMap, BFS
207211
|132|[Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ImplementQueueUsingStacks.java)| O(n)|O(n) | Easy| Stack, Queue
208212
|129|[Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/SumRootToLeafNumbers.java)| O(n)|O(h) | Medium| DFS
209213
|125|[Valid Palindrome](https://leetcode.com/problems/valid-palindrome/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ValidPalindrome.java)| O(n)|O(1) | Easy| Two Pointers
210214
|124|[Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BinaryTreeMaximumPathSum.java)| O(n)|O(h) | Hard | Tree
215+
|123|[Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BestTimeToBuyAndSellStockIII.java)| O(?)|O(?) | Hard |
211216
|122|[Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BestTimeToBuyAndSellStockII.java)| O(n)|O(1) | Medium | Greedy
212217
|121|[Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BestTimeToBuyAndSellStock.java)| O(n)|O(1) | Easy| DP
213218
|120|[Triangle](https://leetcode.com/problems/triangle/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/Triangle.java)| O(m*n)|O(n) | Medium| DP
@@ -223,12 +228,15 @@
223228
|109|[Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ConvertSortedListtoBinarySearchTree.java)| O(n)|O(h) | Medium | DFS, Recursion
224229
|108|[Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ConvertSortedArraytoBinarySearchTree.java)| O(n)|O(h) | Medium| Tree
225230
|107|[Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BinaryTreeLevelOrderTraversalII.java)| O(nlogn)|O(h) | Easy| BFS
231+
|105|[Construct Binary Tree from Preorder and Inorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ConstructBinaryTreefromPreorderandInorderTraversal.java)| O(?)|O(?) | Medium|
226232
|104|[Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/MaximumDepthOfBinaryTree.java)| O(n)|O(h) | Easy| DFS
227233
|103|[Binary Tree Zigzag Level Order Traversal](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BinaryTreeZigzagLevelOrderTraversal.java)| O(n)|O(h) | Medium| BFS,DFS
228234
|102|[Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BinaryTreeLevelOrderTraversal.java)| O(n)|O(h) | Easy| BFS
235+
|99|[Recover Binary Search Tree](https://leetcode.com/problems/recover-binary-search-tree/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/RecoverBinarySearchTree.java) | O(?) | O(?) | Hard |
229236
|98|[Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ValidateBinarySearchTree.java) | O(n) | O(h) | Medium | DFS/Recursion
230237
|97|[Interleaving String](https://leetcode.com/problems/interleaving-string/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/InterleavingString.java)| O(?)|O(?) | Hard| DP
231238
|96|[Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/UniqueBinarySearchTrees.java) | O(n^2) | O(n) | Medium | Recursion, DP
239+
|95|[Unique Binary Search Trees II](https://leetcode.com/problems/unique-binary-search-trees-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/UniqueBinarySearchTreesII.java) | O(?) | O(?) | Medium | Recursion
232240
|94|[Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BinaryTreeInorderTraversal.java)| O(n)|O(h) | Medium| Binary Tree
233241
|92|[Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ReverseLinkedListII.java)| O(n)|O(1) | Medium
234242
|91|[Decode Ways](https://leetcode.com/problems/decode-ways/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/DecodeWays.java)| O(n)|O(n) | Medium| DP

leetcode-algorithms/src/main/java/com/stevesun/solutions/BestTimeToBuyAndSellStockIII.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33

44
/**123. Best Time to Buy and Sell Stock III QuestionEditorial Solution My Submissions
5-
Total Accepted: 62880
6-
Total Submissions: 231914
7-
Difficulty: Hard
85
Say you have an array for which the ith element is the price of a given stock on day i.
96
107
Design an algorithm to find the maximum profit. You may complete at most two transactions.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.stevesun.solutions;
2+
3+
import com.stevesun.common.classes.TreeNode;
4+
5+
/**Given preorder and inorder traversal of a tree, construct the binary tree.
6+
7+
Note:
8+
You may assume that duplicates do not exist in the tree.*/
9+
public class ConstructBinaryTreefromPreorderandInorderTraversal {
10+
11+
public static TreeNode buildTree(int[] preorder, int[] inorder) {
12+
if (preorder.length == 0)
13+
return null;
14+
return buildTree(preorder, inorder, 0, 0, inorder.length - 1);
15+
}
16+
17+
private static TreeNode buildTree(int[] preorder, int[] inorder,
18+
int preorderIndex, int start, int end) {
19+
if (start > end)
20+
return null;
21+
TreeNode node = new TreeNode(preorder[preorderIndex]);
22+
int inorderIndex = findInorderIndex(inorder, start, end,
23+
preorder[preorderIndex]);
24+
int leftTreeSize = inorderIndex - start;
25+
int rightTreeSize = end - inorderIndex;
26+
node.left = buildTree(preorder, inorder, preorderIndex + 1, start,
27+
inorderIndex - 1);
28+
node.right = buildTree(preorder, inorder, preorderIndex + leftTreeSize
29+
+ 1, inorderIndex + 1, end);
30+
return node;
31+
}
32+
33+
private static int findInorderIndex(int[] inorder, int start, int end,
34+
int key) {
35+
for (int i = start; i <= end; i++) {
36+
if (inorder[i] == key)
37+
return i;
38+
}
39+
return -1;
40+
}
41+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.stevesun.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
7+
8+
Return a deep copy of the list.*/
9+
10+
public class CopyListWithRandomPointer {
11+
12+
public RandomListNode copyRandomList(RandomListNode head) {
13+
Map<RandomListNode, RandomListNode> map = new HashMap();
14+
RandomListNode node = head;
15+
16+
//loop for the first time: copy the node themselves
17+
while(node != null){
18+
map.put(node, new RandomListNode(node.label));
19+
node = node.next;
20+
}
21+
22+
//loop for the second time: copy random and next pointers
23+
node = head;
24+
while(node != null){
25+
map.get(node).next = map.get(node.next);
26+
map.get(node).random = map.get(node.random);
27+
node = node.next;
28+
}
29+
30+
return map.get(head);
31+
}
32+
}
33+
34+
// Definition for singly-linked list with a random pointer.
35+
class RandomListNode {
36+
int label;
37+
RandomListNode next, random;
38+
RandomListNode(int x) { this.label = x; }
39+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.stevesun.solutions;
2+
3+
/**
4+
* Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.
5+
6+
Example:
7+
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])
8+
9+
Hint:
10+
11+
A direct way is to use the backtracking approach.
12+
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 10n.
13+
This problem can also be solved using a dynamic programming approach and some knowledge of combinatorics.
14+
Let f(k) = count of numbers with unique digits with length equals k.
15+
f(1) = 10, ..., f(k) = 9 * 9 * 8 * ... (9 - k + 2) [The first factor is 9 because a number cannot start with 0].
16+
*/
17+
public class CountNumbersWithUniqueDigits {
18+
19+
public int countNumbersWithUniqueDigits(int n) {
20+
if (n == 0) return 1;
21+
int res = 10;
22+
int uniqueDigits = 9;
23+
int availableNumber = 9;
24+
while (n-- > 1 && availableNumber > 0) {
25+
uniqueDigits = uniqueDigits * availableNumber;
26+
res += uniqueDigits;
27+
availableNumber--;
28+
}
29+
return res;
30+
}
31+
32+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.stevesun.solutions;
2+
3+
/**
4+
* Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum number of length k <= m + n from digits of the two. The relative order of the digits from the same array must be preserved. Return an array of the k digits. You should try to optimize your time and space complexity.
5+
6+
Example 1:
7+
nums1 = [3, 4, 6, 5]
8+
nums2 = [9, 1, 2, 5, 8, 3]
9+
k = 5
10+
return [9, 8, 6, 5, 3]
11+
12+
Example 2:
13+
nums1 = [6, 7]
14+
nums2 = [6, 0, 4]
15+
k = 5
16+
return [6, 7, 6, 0, 4]
17+
18+
Example 3:
19+
nums1 = [3, 9]
20+
nums2 = [8, 9]
21+
k = 3
22+
return [9, 8, 9]
23+
*/
24+
public class CreateMaximumNumber {
25+
public int[] maxNumber(int[] nums1, int[] nums2, int k) {
26+
int n = nums1.length;
27+
int m = nums2.length;
28+
int[] ans = new int[k];
29+
for(int i = Math.max(0, k-m); i <= k && i <= n; ++i){//what is this and why?
30+
int[] candidate = merge(maxArray(nums1, i), maxArray(nums2, k-i), k);
31+
if(greater(candidate, 0, ans, 0)) ans = candidate;
32+
}
33+
return ans;
34+
}
35+
36+
private boolean greater(int[] nums1, int i, int[] nums2, int j) {
37+
while(i < nums1.length && j < nums2.length && nums1[i] == nums2[j]){
38+
i++;
39+
j++;
40+
}
41+
return j == nums2.length || (i < nums1.length && nums1[i] > nums2[j]);
42+
}
43+
44+
private int[] merge(int[] nums1, int[] nums2, int k) {
45+
int[] ans = new int[k];
46+
for(int i = 0, j = 0, r = 0; r < k; r++){
47+
ans[r] = greater(nums1, i, nums2, j) ? nums1[i++] : nums2[j++];
48+
}
49+
return ans;
50+
}
51+
52+
private int[] maxArray(int[] nums, int k) {
53+
int n = nums.length;
54+
int[] ans = new int[k];
55+
for(int i = 0, j = 0; i < n; i++){
56+
while(n - i + j > k && j > 0 && ans[j-1] < nums[i]) j--;
57+
if(j < k) ans[j++] = nums[i];
58+
}
59+
return ans;
60+
}
61+
}

0 commit comments

Comments
 (0)