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 067c0af

Browse files
committed
shortest word distance 1,2,3 done
1 parent 40c3eb1 commit 067c0af

File tree

4 files changed

+158
-20
lines changed

4 files changed

+158
-20
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/**
66
* Level: Easy
7-
* Problem Link:
7+
* Problem Link: https://leetcode.com/problems/shortest-word-distance/
88
* Problem Description:
99
* Given a list of words and two words word1 and word2, return the shortest distance between these two words in the
1010
* list of words.
@@ -34,18 +34,20 @@ public class ShortestWordDistance {
3434
* @return
3535
*/
3636
public static int findShortestDistance(String[] words, String word1, String word2) {
37-
int startWord1 = Integer.MIN_VALUE;
38-
int startWord2 = Integer.MAX_VALUE;
37+
int indexWord1 = -1;
38+
int indexWord2 = -1;
3939
int minDistance = Integer.MAX_VALUE;
4040

4141
for (int i = 0; i < words.length; i++) {
4242
if (words[i].equals(word1)) {
43-
startWord1 = i;
43+
indexWord1 = i;
4444
}
4545
if (words[i].equals(word2)) {
46-
startWord2 = i;
46+
indexWord2 = i;
47+
}
48+
if (indexWord1 != -1 && indexWord2 != -1) {
49+
minDistance = Math.min(minDistance, Math.abs(indexWord1 - indexWord2));
4750
}
48-
minDistance = Math.min(minDistance, Math.abs(startWord1 - startWord2));
4951
}
5052

5153
return minDistance;
@@ -54,8 +56,12 @@ public static int findShortestDistance(String[] words, String word1, String word
5456
public static void main(String[] args) {
5557
assertEquals(3, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
5658
"practice", "coding"));
59+
assertEquals(3, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
60+
"coding", "practice"));
5761
assertEquals(1, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
5862
"makes", "coding"));
63+
assertEquals(1, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
64+
"makes", "perfect"));
5965
assertEquals(0, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
6066
"perfect", "perfect"));
6167
assertEquals(0, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.leetcode.arrays;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
/**
6+
* Level: Easy
7+
* Problem Link: https://leetcode.com/problems/shortest-word-distance-iii/
8+
* Problem Description:
9+
* This is a follow-up problem of {@link ShortestWordDistance}. The only difference is that now word1 could be the
10+
* same as word2.
11+
* <p>
12+
* Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
13+
* word1 and word2 may be the same and they represent two individual words in the list.
14+
* <p>
15+
* For example,
16+
* Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
17+
* Given word1 = "makes", word2 = "coding", return 1.
18+
* Given word1 = "makes", word2 = "makes", return 3.
19+
* <p>
20+
* Note: You may assume word1 and word2 are both in the list. If they are same then it's guaranteed that there are
21+
* two occurrences of the same.
22+
*
23+
* @author rampatra
24+
* @since 2019年07月31日
25+
*/
26+
public class ShortestWordDistanceIII {
27+
28+
/**
29+
* Time Complexity:
30+
* Space Complexity:
31+
* TODO
32+
*
33+
* @param words
34+
* @param word1
35+
* @param word2
36+
* @return
37+
*/
38+
public static int findShortestDistance(String[] words, String word1, String word2) {
39+
int indexWord1 = -1;
40+
int indexWord2 = -1;
41+
int minDistance = Integer.MAX_VALUE;
42+
43+
for (int i = 0; i < words.length; i++) {
44+
if (words[i].equals(word1)) {
45+
// if both words are same and the first index is already set then do nothing
46+
if (word1.equals(word2) && indexWord1 != -1) {
47+
48+
} else {
49+
indexWord1 = i;
50+
}
51+
}
52+
if (words[i].equals(word2)) {
53+
// if both words are same and i is same as first index then it implies its the
54+
// first occurrence, skip and continue look for the second occurrence
55+
if (word1.equals(word2) && i == indexWord1) {
56+
continue;
57+
}
58+
indexWord2 = i;
59+
}
60+
if (indexWord1 != -1 && indexWord2 != -1) {
61+
minDistance = Math.min(minDistance, Math.abs(indexWord1 - indexWord2));
62+
}
63+
}
64+
65+
return minDistance;
66+
}
67+
68+
public static void main(String[] args) {
69+
assertEquals(3, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
70+
"makes", "makes"));
71+
assertEquals(3, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
72+
"coding", "practice"));
73+
assertEquals(3, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
74+
"practice", "coding"));
75+
assertEquals(1, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
76+
"makes", "coding"));
77+
assertEquals(1, findShortestDistance(new String[]{"practice", "makes", "perfect", "coding", "makes"},
78+
"makes", "perfect"));
79+
}
80+
}
Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,82 @@
11
package com.leetcode.maps;
22

3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
310
/**
411
* Level: Medium
512
* Problem Link: https://leetcode.com/problems/shortest-word-distance-ii/ (premium)
613
* 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>
1014
* 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.
15+
* word1 and word2 and return the shortest distance between these two words in the list. Your method will be called
16+
* repeatedly many times with different parameters. For a simpler variant, see {@link com.leetcode.arrays.ShortestWordDistance}.
1217
* <p>
13-
* Example 1:
18+
* Examples:
1419
* Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
15-
* Given word1 = "coding", word2 = "practice", return 3.
16-
* Given word1 = "makes", word2 = "coding", return 1.
1720
* <p>
18-
* Note: You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
21+
* Input1: word1 = "coding", word2 = "practice"
22+
* Output1: 3
23+
* <p>
24+
* Input2: word1 = "makes", word2 = "coding"
25+
* Output2: 1
1926
* <p>
20-
* Lastly, for a simpler variant, see {@link com.leetcode.arrays.ShortestWordDistance}.
27+
* Note: You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
2128
*
2229
* @author rampatra
2330
* @since 2019年07月31日
2431
*/
2532
public class ShortestWordDistanceII {
2633

27-
public static int findShortestDistance(String[] words, String word1, String word2) {
28-
return -1;
34+
private String[] words;
35+
private Map<String, List<Integer>> wordsToIndexesMap;
36+
37+
ShortestWordDistanceII(String[] words) {
38+
this.words = words;
39+
this.wordsToIndexesMap = getWordsToIndexesMap();
2940
}
3041

31-
public static void main(String[] args) {
42+
public int findShortestDistance(String word1, String word2) {
43+
return findShortestDistance(wordsToIndexesMap.get(word1), wordsToIndexesMap.get(word2));
44+
}
3245

46+
private int findShortestDistance(List<Integer> indexes1, List<Integer> indexes2) {
47+
int minDistance = Integer.MAX_VALUE;
48+
49+
for (int i = 0, j = 0; i < indexes1.size() && j < indexes2.size(); ) {
50+
if (indexes1.get(i) <= indexes2.get(j)) {
51+
minDistance = Math.min(minDistance, Math.abs(indexes1.get(i) - indexes2.get(j)));
52+
i++;
53+
} else if (indexes1.get(i) > indexes2.get(j)) {
54+
minDistance = Math.min(minDistance, Math.abs(indexes1.get(i) - indexes2.get(j)));
55+
j++;
56+
}
57+
}
58+
59+
return minDistance;
60+
}
61+
62+
private Map<String, List<Integer>> getWordsToIndexesMap() {
63+
Map<String, List<Integer>> wordsToIndexesMap = new HashMap<>();
64+
65+
for (int i = 0; i < words.length; i++) {
66+
wordsToIndexesMap.putIfAbsent(words[i], new ArrayList<>());
67+
wordsToIndexesMap.get(words[i]).add(i);
68+
}
69+
return wordsToIndexesMap;
70+
}
71+
72+
public static void main(String[] args) {
73+
ShortestWordDistanceII shortestWordDist = new ShortestWordDistanceII(new String[]{"practice", "makes", "perfect", "coding", "makes"});
74+
assertEquals(1, shortestWordDist.findShortestDistance("coding", "makes"));
75+
assertEquals(1, shortestWordDist.findShortestDistance("perfect", "makes"));
76+
assertEquals(1, shortestWordDist.findShortestDistance("practice", "makes"));
77+
assertEquals(1, shortestWordDist.findShortestDistance("makes", "practice"));
78+
assertEquals(3, shortestWordDist.findShortestDistance("coding", "practice"));
79+
assertEquals(0, shortestWordDist.findShortestDistance("coding", "coding"));
80+
assertEquals(0, shortestWordDist.findShortestDistance("makes", "makes"));
3381
}
34-
}
82+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
* Level: Easy
77
* Problem Link: https://leetcode.com/problems/closest-binary-search-tree-value/ (premium)
88
* Problem Description:
9+
* Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.
10+
* <p>
11+
* Note:
12+
* - Given target value is a floating point.
13+
* - You are guaranteed to have only one unique value in the BST that is closest to the target.
914
*
1015
* @author rampatra
1116
* @since 2019年07月31日
1217
*/
1318
public class ClosestBinarySearchTreeValue {
1419

1520
/**
16-
*
1721
* @param node
1822
* @param parentNode
1923
* @param val

0 commit comments

Comments
(0)

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