Skip to content

Commit 5e7a814

Browse files
committed
Added some more comments and minor refactorings
1 parent 63a6b37 commit 5e7a814

File tree

5 files changed

+224
-3
lines changed

5 files changed

+224
-3
lines changed

src/main/java/com/leetcode/arrays/NestedListWeightSumI.java renamed to src/main/java/com/leetcode/arrays/NestedListWeightSum.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* @author rampatra
2121
* @since 2019-07-27
2222
*/
23-
public class NestedListWeightSumI {
23+
public class NestedListWeightSum {
2424

2525
/**
2626
* Time Complexity: The algorithm takes O(N) time, where N is the total number of nested elements in the input
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.leetcode.arrays;
2+
3+
import java.util.*;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
/**
8+
* Level: Medium
9+
* Problem Link: https://leetcode.com/problems/nested-list-weight-sum-ii/ (premium)
10+
* Problem Description:
11+
* Given a nested list of integers, return the sum of all integers in the list weighted by their depth. Each element
12+
* is either an integer, or a list – whose elements may also be integers or other lists.
13+
* <p>
14+
* Different from {@link NestedListWeightSum} where weight is increasing from root to leaf, now the weight is defined
15+
* from bottom up, i.e., the leaf level integers have weight 1, and the root level integers have the largest weight.
16+
* <p>
17+
* Example 1:
18+
* Given the list [[1,1],2,[1,1]], return 8. (four 1’s at depth 1, one 2 at depth 2)
19+
* <p>
20+
* Example 2:
21+
* Given the list [1,[4,[6]]], return 17. (one 1 at depth 3, one 4 at depth 2, and one 6 at depth 1; 13 + 42 + 6*1 = 17)
22+
* <p>
23+
* Note: For a simpler variant, see {@link NestedListWeightSum}.
24+
*
25+
* @author rampatra
26+
* @since 2019-07-27
27+
*/
28+
public class NestedListWeightSumII {
29+
30+
/**
31+
* @param nestedList
32+
* @return
33+
*/
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+
}
51+
}
52+
unweightedSum += unweightedSum;
53+
weightedSum = unweightedSum;
54+
}
55+
}
56+
57+
return weightedSum;
58+
}
59+
60+
public static void main(String[] args) {
61+
assertEquals(0, nestedSum(Collections.singletonList(new NestedInteger())));
62+
63+
assertEquals(0, nestedSum(Collections.singletonList(new NestedInteger().add(new NestedInteger()))));
64+
65+
NestedInteger ni = new NestedInteger(2);
66+
ni.add(new NestedInteger().add(new NestedInteger(1)).add(new NestedInteger(1)));
67+
ni.add(new NestedInteger().add(new NestedInteger(1)).add(new NestedInteger(1)));
68+
69+
assertEquals(10, nestedSum(Collections.singletonList(ni)));
70+
71+
ni = new NestedInteger(1);
72+
ni.add(new NestedInteger(4).add(new NestedInteger(6)));
73+
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}}}));*/
78+
}
79+
}
80+
81+
class NestedInteger {
82+
83+
private Integer integer;
84+
private List<NestedInteger> list;
85+
86+
public NestedInteger() {
87+
this.list = new ArrayList<>();
88+
}
89+
90+
public NestedInteger(int integer) {
91+
this.integer = integer;
92+
this.list = new ArrayList<>();
93+
}
94+
95+
public boolean isInteger() {
96+
return this.integer != null;
97+
}
98+
99+
public Integer getInteger() {
100+
return integer;
101+
}
102+
103+
public void setInteger(Integer integer) {
104+
this.integer = integer;
105+
}
106+
107+
public List<NestedInteger> getList() {
108+
return list;
109+
}
110+
111+
public NestedInteger add(NestedInteger nestedInteger) {
112+
this.list.add(nestedInteger);
113+
return this;
114+
}
115+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.leetcode.arrays;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
/**
6+
* Level: Easy
7+
* Problem Link:
8+
* Problem Description:
9+
* Given a list of words and two words word1 and word2, return the shortest distance between these two words in the
10+
* list of words.
11+
*
12+
* Example 1:
13+
* Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
14+
* Given word1 = "coding", word2 = "practice", return 3.
15+
* Given word1 = "makes", word2 = "coding", return 1.
16+
*
17+
* Note: You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
18+
*
19+
* Lastly, for a more complex variant, see {@link com.leetcode.maps.ShortestWordDistanceII}.
20+
*
21+
* @author rampatra
22+
* @since 2019-07-31
23+
*/
24+
public class ShortestWordDistance {
25+
26+
/**
27+
* Time Complexity:
28+
* Space Complexity:
29+
* TODO
30+
*
31+
* @param words
32+
* @param word1
33+
* @param word2
34+
* @return
35+
*/
36+
public static int findShortestDistance(String[] words, String word1, String word2) {
37+
int startWord1 = Integer.MIN_VALUE;
38+
int startWord2 = Integer.MAX_VALUE;
39+
int minDistance = Integer.MAX_VALUE;
40+
41+
for (int i = 0; i < words.length; i++) {
42+
if (words[i].equals(word1)) {
43+
startWord1 = i;
44+
}
45+
if (words[i].equals(word2)) {
46+
startWord2 = i;
47+
}
48+
minDistance = Math.min(minDistance, Math.abs(startWord1 - startWord2));
49+
}
50+
51+
return minDistance;
52+
}
53+
54+
public static void main(String[] args) {
55+
assertEquals(3, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
56+
"practice", "coding"));
57+
assertEquals(1, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
58+
"makes", "coding"));
59+
assertEquals(0, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
60+
"perfect", "perfect"));
61+
assertEquals(0, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
62+
"makes", "makes"));
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.leetcode.maps;
2+
3+
/**
4+
* Level: Medium
5+
* Problem Link: https://leetcode.com/problems/shortest-word-distance-ii/ (premium)
6+
* Problem Description:
7+
* This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and
8+
* your method will be called repeatedly many times with different parameters. How would you optimize it?
9+
* <p>
10+
* Design a class which receives a list of words in the constructor, and implements a method that takes two words
11+
* word1 and word2 and return the shortest distance between these two words in the list.
12+
* <p>
13+
* Example 1:
14+
* Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
15+
* Given word1 = "coding”, word2 = "practice”, return 3.
16+
* Given word1 = "makes", word2 = "coding", return 1.
17+
* <p>
18+
* Note: You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
19+
* <p>
20+
* Lastly, for a simpler variant, see {@link com.leetcode.arrays.ShortestWordDistance}.
21+
*
22+
* @author rampatra
23+
* @since 2019-07-31
24+
*/
25+
public class ShortestWordDistanceII {
26+
27+
public static int findShortestDistance(String[] words, String word1, String word2) {
28+
return -1;
29+
}
30+
31+
public static void main(String[] args) {
32+
33+
}
34+
}

src/main/java/com/leetcode/trees/ClosestBinarySearchTreeValueI.java renamed to src/main/java/com/leetcode/trees/ClosestBinarySearchTreeValue.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@
44

55
/**
66
* Level: Easy
7-
* Problem Link: https://leetcode.com/problems/closest-binary-search-tree-value/
7+
* Problem Link: https://leetcode.com/problems/closest-binary-search-tree-value/ (premium)
88
* Problem Description:
99
*
1010
* @author rampatra
1111
* @since 2019-07-31
1212
*/
13-
public class ClosestBinarySearchTreeValueI {
13+
public class ClosestBinarySearchTreeValue {
1414

15+
/**
16+
*
17+
* @param node
18+
* @param parentNode
19+
* @param val
20+
* @param diff
21+
* @return
22+
*/
1523
public static TreeNode findNodeWithClosestValue(TreeNode node, TreeNode parentNode, int val, int diff) {
1624
if (node == null) return parentNode;
1725

0 commit comments

Comments
 (0)