Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions src/main/java/com/leetcode/arrays/ShortestWordDistanceIII.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,15 @@ public static int findShortestDistance(String[] words, String word1, String word
int indexWord1 = -1;
int indexWord2 = -1;
int minDistance = Integer.MAX_VALUE;
boolean isSameWord = word1.equals(word2);

for (int i = 0; i < words.length; i++) {
if (words[i].equals(word1)) {
// if both words are same and the first index is already set then do nothing
if (word1.equals(word2) && indexWord1 != -1) {

} else {
indexWord1 = i;
}
// Update indexWord1 only if: words are different OR this is the first occurrence
if (words[i].equals(word1) && (!isSameWord || indexWord1 == -1)) {
indexWord1 = i;
}
if (words[i].equals(word2)) {
// if both words are same and i is same as first index then it implies its the
// first occurrence, skip and continue look for the second occurrence
if (word1.equals(word2) && i == indexWord1) {
continue;
}
// Update indexWord2 only if: words are different OR this is not the first occurrence
if (words[i].equals(word2) && (!isSameWord || i != indexWord1)) {
indexWord2 = i;
}
if (indexWord1 != -1 && indexWord2 != -1) {
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/BuySellStocksTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@


import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class BuySellStocksTest {
@Test
void testEmptyArray() {
int[] prices = {};
int result = BuySellStocks.maxProfit(prices);

assertEquals(0, result);
}

@Test
void testUpdateBuyPrice() {
int[] prices = {8, 5, 10};
int result = BuySellStocks.maxProfit(prices);

assertEquals(5, result);
}

@Test
void testUpdateProfit() {
int[] prices = {1, 5, 10};
int result = BuySellStocks.maxProfit(prices);

assertEquals(9, result);
}

@Test
void testNoUpdateProfit() {
int[] prices = {1, 10, 5};
int result = BuySellStocks.maxProfit(prices);

assertEquals(9, result);
}

@Test
void testMixedUpdates() {
int[] prices = {10, 2, 7, 3, 8, 4};
int result = BuySellStocks.maxProfit(prices);

assertEquals(6, result);
}
}
70 changes: 70 additions & 0 deletions src/test/java/ClosestBinarySearchTreeValueTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class ClosestBinarySearchTreeValueTest {
@Test
void testNullRoot() {
assertEquals(-1, ClosestBinarySearchTreeValue.closestValue(null, 10.0) );
}

@Test
void testSingleNode() {
TreeNode root = new TreeNode(10);

assertEquals(10, ClosestBinarySearchTreeValue.closestValue(root, 5.0));
}

@Test
void testCloserToLeftChild() {
// 10
// /
// 7
TreeNode root = new TreeNode(10);
root.left = new TreeNode(7);

assertEquals(7, ClosestBinarySearchTreeValue.closestValue(root, 8.0));
}

@Test
void testCloserToRightChild() {
// 10
// \
// 13
TreeNode root = new TreeNode(10);
root.right = new TreeNode(13);

assertEquals(13, ClosestBinarySearchTreeValue.closestValue(root, 12.0));
}

@Test
void testTieBreaking() {
// 10
// \
// 13
TreeNode root = new TreeNode(10);
root.right = new TreeNode(13);

assertEquals(10, ClosestBinarySearchTreeValue.closestValue(root, 11.0));
}

@Test
void testComplexTree() {
// Let's stick to a valid BST construction.
// 10
// / \
// 7 13
// / \ / \
// 5 9 13 20

TreeNode root = new TreeNode(10);
root.left = new TreeNode(7);
root.right = new TreeNode(13);
root.left.left = new TreeNode(5);
root.left.right = new TreeNode(9);
root.right.left = new TreeNode(13);
root.right.right = new TreeNode(20);

assertEquals(7, ClosestBinarySearchTreeValue.closestValue(root, 8.0));
assertEquals(9, ClosestBinarySearchTreeValue.closestValue(root, 8.1));
}
}
40 changes: 40 additions & 0 deletions src/test/java/RansomNoteTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class RansomNoteTest {
@Test
void testBothEmpty() {
boolean result = RansomNote.canConstruct("", "");
assertTrue(result);
}

@Test
void testEmptyMagazine() {
boolean result = RansomNote.canConstruct("a", "");
assertFalse(result);
}

@Test
void testEmptyRansomeNote() {
boolean result = RansomNote.canConstruct("", "abc");
assertTrue(result);
}

@Test
void testCharCountZero() {
boolean result = RansomNote.canConstruct("aa", "a");
assertFalse(result);
}

@Test
void testExtraUnusedChars() {
boolean result = RansomNote.canConstruct("ab", "abcdxyz");
assertTrue(result);
}

@Test
void testMissingOneChar() {
boolean result = RansomNote.canConstruct("abcdxyz", "bcdxyz");
assertFalse(result);
}
}
39 changes: 39 additions & 0 deletions src/test/java/ShortestWordDistanceIIITest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class ShortestWordDistanceIIITest {
@Test
void testDifferentWordsAdjacent() {
String[] words = {"practice", "makes", "perfect", "coding", "makes"};

assertEquals(1, ShortestWordDistanceIII.findShortestDistance(words, "makes", "coding"));
}

@Test
void testDifferentWordsNotAdjacent() {
String[] words = {"practice", "makes", "perfect", "coding", "makes"};

assertEquals(3, ShortestWordDistanceIII.findShortestDistance(words, "practice", "coding"));
}

@Test
void testSameWords() {
String[] words = {"practice", "makes", "perfect", "coding", "makes"};

assertEquals(3, ShortestWordDistanceIII.findShortestDistance(words, "makes", "makes"));
}

@Test
void testWord1BeforeWord2() {
String[] words = {"practice", "makes", "perfect", "coding", "makes"};

assertEquals(1, ShortestWordDistanceIII.findShortestDistance(words, "makes", "perfect"));
}

@Test
void testWord2BeforeWord1() {
String[] words = {"practice", "makes", "perfect", "coding", "makes"};

assertEquals(1, ShortestWordDistanceIII.findShortestDistance(words, "perfect", "makes"));
}
}