Skip to content

Commit f4e4894

Browse files
committed
Updates
1 parent 06f6800 commit f4e4894

3 files changed

Lines changed: 65 additions & 2 deletions

File tree

pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,39 @@
1616
</properties>
1717

1818
<dependencies>
19+
<dependency>
20+
<groupId>org.apache.commons</groupId>
21+
<artifactId>commons-lang3</artifactId>
22+
<version>3.17.0</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>com.google.guava</groupId>
26+
<artifactId>guava</artifactId>
27+
<version>33.4.8-jre</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.apache.commons</groupId>
31+
<artifactId>commons-math3</artifactId>
32+
<version>3.6.1</version>
33+
</dependency>
1934
<dependency>
2035
<groupId>org.apache.commons</groupId>
2136
<artifactId>commons-numbers-complex</artifactId>
2237
<version>1.2</version>
2338
</dependency>
2439

40+
<!-- Unit Tests-->
2541
<dependency>
2642
<groupId>org.junit.jupiter</groupId>
2743
<artifactId>junit-jupiter</artifactId>
2844
<version>5.12.2</version>
2945
<scope>test</scope>
3046
</dependency>
47+
<dependency>
48+
<groupId>org.assertj</groupId>
49+
<artifactId>assertj-core</artifactId>
50+
<version>3.27.3</version>
51+
</dependency>
3152

3253
<!-- Java Microbenchmark Harness-->
3354
<dependency>

src/main/java/org/dojo/algorithms/HashTables.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package org.dojo.algorithms;
22

3+
import org.apache.commons.lang3.tuple.Pair;
4+
5+
import java.util.ArrayList;
36
import java.util.HashMap;
7+
import java.util.List;
48
import java.util.Map;
59
import java.util.TreeMap;
610

@@ -31,4 +35,14 @@ public int[] twoSumAlt(int[] nums, int target) {
3135
}
3236
return new int[] {};
3337
}
38+
39+
public List<Pair<Integer, Integer>> twoSumAlt2(int[] nums, int target) {
40+
List<Pair<Integer, Integer>> pairs = new ArrayList<>();
41+
Map<Integer, Integer> lookbehind = new HashMap<>();
42+
for (int i = 0; i < nums.length; i++) {
43+
if (lookbehind.containsKey(target - nums[i])) pairs.add(Pair.of(lookbehind.get(target - nums[i]), i));
44+
lookbehind.put(nums[i], i);
45+
}
46+
return pairs;
47+
}
3448
}

src/test/java/org/dojo/algorithms/HashTablesTests.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package org.dojo.algorithms;
22

3+
import org.apache.commons.lang3.tuple.Pair;
34
import org.junit.jupiter.api.BeforeEach;
45
import org.junit.jupiter.params.ParameterizedTest;
56
import org.junit.jupiter.params.provider.Arguments;
67
import org.junit.jupiter.params.provider.MethodSource;
78

89
import java.util.Arrays;
10+
import java.util.List;
911
import java.util.stream.Stream;
1012

13+
import static org.assertj.core.api.Assertions.assertThat;
1114
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
1215

1316
class HashTablesTests {
@@ -32,7 +35,7 @@ void countFrequencies(int[] input, int[] expected) {
3235
assertArrayEquals(expected, actual);
3336
}
3437

35-
private static Stream<Arguments> testData() {
38+
private static Stream<Arguments> testTwoSumData() {
3639
return Stream.of(
3740
Arguments.of(new int[] {2, 7, 11, 15}, 9, new int[] {0, 1}),
3841
Arguments.of(new int[] {3, 2, 4}, 6, new int[] {1, 2}),
@@ -46,11 +49,36 @@ private static Stream<Arguments> testData() {
4649
}
4750

4851
@ParameterizedTest
49-
@MethodSource("testData")
52+
@MethodSource("testTwoSumData")
5053
public void testTwoSum(int[] inputArray, int inputTarget, int[] expected) {
5154
int[] actual = sut.twoSum(inputArray, inputTarget);
5255
System.out.printf("%s%n", Arrays.toString(expected));
5356
System.out.printf("%s%n", Arrays.toString(actual));
5457
assertArrayEquals(expected, actual);
5558
}
59+
60+
private static Stream<Arguments> testTwoSumAlt2Data() {
61+
return Stream.of(
62+
Arguments.of(new int[] {2, 7, 11, 15}, 9, List.of(Pair.of(0, 1))),
63+
Arguments.of(new int[] {2, 7, 4, 5, 11, 15}, 9, List.of(Pair.of(0, 1), Pair.of(2, 3))),
64+
Arguments.of(new int[] {3, 2, 4}, 6, List.of(Pair.of(1, 2))),
65+
Arguments.of(new int[] {3, 3}, 6, List.of(Pair.of(0, 1))),
66+
Arguments.of(new int[] {3, 2, 3}, 6, List.of(Pair.of(0, 2))),
67+
Arguments.of(new int[] {0, 4, 3, 0}, 0, List.of(Pair.of(0, 3))),
68+
Arguments.of(new int[] {-3, 4, 3, 90}, 0, List.of(Pair.of(0, 2))),
69+
Arguments.of(new int[] {-1, -2, -3, -4, -5}, -8, List.of(Pair.of(2, 4))),
70+
Arguments.of(new int[] {-10, -1, -18, -19}, -19, List.of(Pair.of(1, 2))),
71+
Arguments.of(new int[] {1, 2, 3, 4, 5, 6}, 7, List.of(Pair.of(0, 5), Pair.of(1, 4), Pair.of(2, 3))),
72+
Arguments.of(new int[] {1, 2, 3}, 100, List.of())
73+
);
74+
}
75+
76+
@ParameterizedTest
77+
@MethodSource("testTwoSumAlt2Data")
78+
public void testTwoSumAlt2(int[] inputArray, int inputTarget, List<Pair<Integer, Integer>> expected) {
79+
List<Pair<Integer, Integer>> actual = sut.twoSumAlt2(inputArray, inputTarget);
80+
assertThat(expected)
81+
.withFailMessage(() -> String.format("Expected pairs: %s, but got: %s", expected, actual))
82+
.containsAll(actual);
83+
}
5684
}

0 commit comments

Comments
 (0)