Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 57caf4d

Browse files
committed
Added comments, runtime, etc.
1 parent 067c0af commit 57caf4d

File tree

7 files changed

+226
-32
lines changed

7 files changed

+226
-32
lines changed

‎src/main/java/com/leetcode/arrays/NestedListWeightSumII.java

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,31 @@
2828
public class NestedListWeightSumII {
2929

3030
/**
31+
* Time Complexity:
32+
* Space Complexity:
33+
* Runtime: <a href="https://leetcode.com/submissions/detail/248595263/">1 ms</a>.
34+
*
3135
* @param nestedList
3236
* @return
3337
*/
34-
public static long nestedSum(List<NestedInteger> nestedList) {
35-
long weightedSum = 0;
36-
long unweightedSum = 0;
37-
Queue<NestedInteger> queue = new LinkedList<>();
38-
39-
for (NestedInteger nestedInteger : nestedList) {
40-
if (nestedInteger.isInteger()) {
41-
unweightedSum += nestedInteger.getInteger();
42-
} else {
43-
queue.addAll(nestedInteger.getList());
44-
while (!queue.isEmpty()) {
45-
NestedInteger ni = queue.poll();
46-
if (ni.isInteger()) {
47-
unweightedSum += ni.getInteger();
48-
} else {
49-
nestedList.addAll(ni.getList());
50-
}
38+
public static int nestedSum(List<NestedInteger> nestedList) {
39+
int weightedSum = 0;
40+
int unweightedSum = 0;
41+
42+
while (!nestedList.isEmpty()) {
43+
List<NestedInteger> nextLevel = new ArrayList<>();
44+
45+
for (NestedInteger ni : nestedList) {
46+
if (ni.isInteger()) {
47+
unweightedSum += ni.getInteger();
48+
} else {
49+
nextLevel.addAll(ni.getList());
5150
}
52-
unweightedSum += unweightedSum;
53-
weightedSum = unweightedSum;
5451
}
52+
53+
unweightedSum += unweightedSum; // multiplication by repetitive addition
54+
weightedSum = unweightedSum;
55+
nestedList = nextLevel;
5556
}
5657

5758
return weightedSum;
@@ -62,19 +63,20 @@ public static void main(String[] args) {
6263

6364
assertEquals(0, nestedSum(Collections.singletonList(new NestedInteger().add(new NestedInteger()))));
6465

66+
// TODO: fix the test cases
67+
68+
// {2, {1,1}, {1,1}}
6569
NestedInteger ni = new NestedInteger(2);
6670
ni.add(new NestedInteger().add(new NestedInteger(1)).add(new NestedInteger(1)));
6771
ni.add(new NestedInteger().add(new NestedInteger(1)).add(new NestedInteger(1)));
6872

69-
assertEquals(10, nestedSum(Collections.singletonList(ni)));
73+
assertEquals(6, nestedSum(Collections.singletonList(ni)));
7074

75+
// {1, {4, {6}}}
7176
ni = new NestedInteger(1);
7277
ni.add(new NestedInteger(4).add(new NestedInteger(6)));
7378

74-
assertEquals(27, nestedSum(Collections.singletonList(ni)));
75-
76-
/*assertEquals(10, nestedSum(new Object[]{new Object[]{1, 1}, 2, new Object[]{1, 1}}));
77-
assertEquals(27, nestedSum(new Object[]{1, new Object[]{4, new Object[]{6}}}));*/
79+
assertEquals(17, nestedSum(Collections.singletonList(ni)));
7880
}
7981
}
8082

‎src/main/java/com/leetcode/arrays/ShortestWordDistance.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class ShortestWordDistance {
2626
/**
2727
* Time Complexity:
2828
* Space Complexity:
29-
* TODO
29+
* Runtime: <a href="https://leetcode.com/submissions/detail/248572791/">1 ms</a>.
3030
*
3131
* @param words
3232
* @param word1
@@ -41,8 +41,7 @@ public static int findShortestDistance(String[] words, String word1, String word
4141
for (int i = 0; i < words.length; i++) {
4242
if (words[i].equals(word1)) {
4343
indexWord1 = i;
44-
}
45-
if (words[i].equals(word2)) {
44+
} else if (words[i].equals(word2)) {
4645
indexWord2 = i;
4746
}
4847
if (indexWord1 != -1 && indexWord2 != -1) {

‎src/main/java/com/leetcode/dynamicprogramming/PaintHouse.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
public class PaintHouse {
2121

2222
/**
23+
* Runtime: <a href="https://leetcode.com/submissions/detail/248573066/">1 ms</a>.
24+
*
2325
* @param costs of coloring the houses with red, blue, and green respectively. 1st row represents house 1, 2nd row
2426
* house 2 and so on
2527
* @return the minimum cost to paint all houses such that no two adjacent houses are of same color

‎src/main/java/com/leetcode/maps/ShortestWordDistanceII.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ public class ShortestWordDistanceII {
3939
this.wordsToIndexesMap = getWordsToIndexesMap();
4040
}
4141

42+
/**
43+
* Runtime: <a href="https://leetcode.com/submissions/detail/248572352/">65 ms</a>.
44+
*
45+
* @param word1
46+
* @param word2
47+
* @return
48+
*/
4249
public int findShortestDistance(String word1, String word2) {
4350
return findShortestDistance(wordsToIndexesMap.get(word1), wordsToIndexesMap.get(word2));
4451
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.leetcode.trees;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
/**
6+
* Level: Hard
7+
* Problem Link: https://leetcode.com/problems/closest-binary-search-tree-value-ii/ (premium)
8+
* Problem Description:
9+
*
10+
*
11+
* @author rampatra
12+
* @since 2019年07月31日
13+
*/
14+
public class ClosestBinarySearchTreeValueII {
15+
16+
/**
17+
* @param node
18+
* @param parentNode
19+
* @param val
20+
* @param diff
21+
* @return
22+
*/
23+
public static TreeNode findNodeWithClosestValue(TreeNode node, TreeNode parentNode, int val, int diff) {
24+
return null;
25+
}
26+
27+
public static void main(String[] args) {
28+
29+
/*
30+
BST looks like:
31+
32+
9
33+
/ \
34+
7 13
35+
/ \ \
36+
5 8 20
37+
/ \
38+
2 6
39+
*/
40+
TreeNode root = new TreeNode(9);
41+
root.left = new TreeNode(7);
42+
root.right = new TreeNode(13);
43+
root.left.left = new TreeNode(5);
44+
root.left.right = new TreeNode(8);
45+
root.left.left.left = new TreeNode(2);
46+
root.left.left.right = new TreeNode(6);
47+
root.right.right = new TreeNode(20);
48+
49+
assertEquals(13, findNodeWithClosestValue(root, root, 15, Integer.MAX_VALUE).val);
50+
assertEquals(13, findNodeWithClosestValue(root, root, 13, Integer.MAX_VALUE).val);
51+
assertEquals(9, findNodeWithClosestValue(root, root, 9, Integer.MAX_VALUE).val);
52+
assertEquals(2, findNodeWithClosestValue(root, root, 2, Integer.MAX_VALUE).val);
53+
assertEquals(2, findNodeWithClosestValue(root, root, 1, Integer.MAX_VALUE).val);
54+
assertEquals(6, findNodeWithClosestValue(root, root, 6, Integer.MAX_VALUE).val);
55+
assertEquals(13, findNodeWithClosestValue(root, root, 11, Integer.MAX_VALUE).val); // tie b/w 9 and 13
56+
57+
/*
58+
BST looks like:
59+
60+
9
61+
/ \
62+
7 13
63+
/ \ / \
64+
5 8 13 20
65+
*/
66+
root = new TreeNode(9);
67+
root.left = new TreeNode(7);
68+
root.right = new TreeNode(13);
69+
root.left.left = new TreeNode(5);
70+
root.left.right = new TreeNode(8);
71+
root.right.left = new TreeNode(13);
72+
root.right.right = new TreeNode(20);
73+
74+
assertEquals(13, findNodeWithClosestValue(root, root, 15, Integer.MAX_VALUE).val);
75+
}
76+
}

‎src/main/java/com/leetcode/trees/LeavesOfBinaryTree.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,18 @@ public class LeavesOfBinaryTree {
4545
* the node into the list indexed by their heights.
4646
* Time Complexity:
4747
* Space Complexity:
48-
* Runtime: <a href=""></a>.
48+
* Runtime: <a href="https://leetcode.com/submissions/detail/248573435/">1 ms</a>.
4949
*
5050
* @param root
5151
* @return
5252
*/
53-
public static List<List<TreeNode>> findLeavesOfBinaryTree(TreeNode root) {
54-
List<List<TreeNode>> levels = new ArrayList<>();
53+
public static List<List<Integer>> findLeavesOfBinaryTree(TreeNode root) {
54+
List<List<Integer>> levels = new ArrayList<>();
5555
findLeavesOfBinaryTree(root, levels);
5656
return levels;
5757
}
5858

59-
private static int findLeavesOfBinaryTree(TreeNode root, List<List<TreeNode>> levels) {
59+
private static int findLeavesOfBinaryTree(TreeNode root, List<List<Integer>> levels) {
6060
if (root == null) return -1;
6161

6262
int leftHeight = findLeavesOfBinaryTree(root.left, levels);
@@ -66,7 +66,7 @@ private static int findLeavesOfBinaryTree(TreeNode root, List<List<TreeNode>> le
6666
if (height >= levels.size()) {
6767
levels.add(height, new ArrayList<>());
6868
}
69-
levels.get(height).add(root);
69+
levels.get(height).add(root.val);
7070

7171
return height;
7272
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.leetcode.trees;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* Level: Easy
7+
* Problem Link: https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/
8+
* Problem Description:
9+
* Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this
10+
* tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value
11+
* among its two sub-nodes. More formally, the property root.val = min(root.left.val, root.right.val) always holds.
12+
*
13+
* Given such a binary tree, you need to output the second minimum value in the set made of all the nodes' value in
14+
* the whole tree.
15+
*
16+
* If no such second minimum value exists, output -1 instead.
17+
*
18+
* Example 1:
19+
* Input:
20+
* 2
21+
* / \
22+
* 2 5
23+
* / \
24+
* 5 7
25+
*
26+
* Output: 5
27+
* Explanation: The smallest value is 2, the second smallest value is 5.
28+
*
29+
*
30+
* Example 2:
31+
* Input:
32+
* 2
33+
* / \
34+
* 2 2
35+
*
36+
* Output: -1
37+
* Explanation: The smallest value is 2, but there isn't any second smallest value.
38+
*
39+
* @author rampatra
40+
* @since 2019年08月03日
41+
*/
42+
public class SecondMinNodeInBinaryTree {
43+
44+
/**
45+
* Time Complexity: O(n)
46+
* Space Complexity: O(n)
47+
* Runtime: <a href="https://leetcode.com/submissions/detail/248556303/">1 ms</a>.
48+
* @param root
49+
* @return
50+
*/
51+
public static int findSecondMinimumValueIterative(TreeNode root) {
52+
if (root == null || (root.left == null && root.right == null)) return -1;
53+
54+
int min = root.val;
55+
long secondMin = Long.MAX_VALUE;
56+
57+
Stack<TreeNode> stack = new Stack<>();
58+
stack.push(root);
59+
60+
while (!stack.empty()) {
61+
TreeNode node = stack.pop();
62+
if (node == null) continue;
63+
64+
if (node.val > min && node.val < secondMin) {
65+
secondMin = node.val;
66+
}
67+
stack.push(node.left);
68+
stack.push(node.right);
69+
}
70+
71+
return secondMin == Long.MAX_VALUE ? -1 : (int) secondMin;
72+
}
73+
74+
75+
/**
76+
* Time Complexity:
77+
* Space Complexity:
78+
* Runtime: <a href="https://leetcode.com/submissions/detail/248558543/">0 ms</a>.
79+
*
80+
* @param root
81+
* @return
82+
*/
83+
public static int findSecondMinimumValue(TreeNode root) {
84+
// passing a long as secondMin because TreeNode can have Integer.MAX_VALUE as its value
85+
long ans = findSecondMinimumValue(root, root.val, Long.MAX_VALUE);
86+
return ans == Long.MAX_VALUE ? -1 : (int) ans;
87+
}
88+
89+
private static long findSecondMinimumValue(TreeNode root, int min, long secondMin) {
90+
if (root == null) return Long.MAX_VALUE;
91+
92+
if (root.val > min && root.val < secondMin) {
93+
return root.val;
94+
} else {
95+
return Math.min(findSecondMinimumValue(root.left, min, secondMin),
96+
findSecondMinimumValue(root.right, min, secondMin));
97+
}
98+
}
99+
100+
public static void main(String[] args) {
101+
System.out.println((int) 2147483647L);
102+
System.out.println(Integer.MAX_VALUE);
103+
// TODO: A function called buildTree which would take an array like [1,1,3,1,1,3,4,3,1,1,1,3,8,4,8,3,3,1,6,2,1]
104+
// and return a Binary Tree
105+
//assertEquals(2, findSecondMinimumValue(buildTree(new int[]{1,1,3,1,1,3,4,3,1,1,1,3,8,4,8,3,3,1,6,2,1})));
106+
//assertEquals(2147483647, findSecondMinimumValue(buildTree(new int[]{2,2,2147483647})));
107+
}
108+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /