Skip to content

Commit 3a0fbe3

Browse files
committed
leetcode Problem 01 first try
1 parent b260f1c commit 3a0fbe3

2 files changed

Lines changed: 28 additions & 21 deletions

File tree

java8/src/main/java/com/shekhargulati/leetcode/Problem01.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,42 @@
1212
*/
1313
public class Problem01 {
1414

15+
/*
16+
This solution is O(n)
17+
*/
1518
public static int[] twoSum(int[] numbers, int target) {
1619
/*
1720
Algorithm:
18-
1. Create a new array with filtering all the numbers less than the target.
19-
2. Iterate over the filtered array to find a pair which sum equal to target
21+
1. Store all the number and their indexes in a Map
22+
2. Create a new array with filtering all the numbers less than the target.
23+
3. Iterate over the filtered array.
24+
4. For each number find the second number by subtracting it from the target
25+
5. If Map contains the second number then we have found the pair
26+
6. Short circuit the loop and return the result.
2027
*/
28+
Map<Integer, Integer> numberAndIndex = new HashMap<>();
29+
for (int i = 0; i < numbers.length; i++) {
30+
numberAndIndex.put(numbers[i], i);
31+
}
32+
2133
int[] numbersLessThanTarget = Arrays.stream(numbers).filter(i -> i < target).toArray();
2234

2335
for (int i = 0; i < numbersLessThanTarget.length - 1; i++) {
24-
for (int j = i + 1; j < numbersLessThanTarget.length; j++) {
25-
if ((numbersLessThanTarget[i] + numbersLessThanTarget[j]) == target) {
26-
return new int[]{numbersLessThanTarget[i], numbersLessThanTarget[j]};
27-
}
36+
int first = numbersLessThanTarget[i];
37+
int second = target - first;
38+
if (numberAndIndex.containsKey(second)) {
39+
return new int[]{i, numberAndIndex.get(second)};
2840
}
41+
2942
}
3043
return new int[0];
3144
}
3245

33-
public static int[] twoSumIndexes(int[] numbers, int target) {
46+
47+
/*
48+
This solution in O(n^2)
49+
*/
50+
public static int[] twoSum_2(int[] numbers, int target) {
3451
/*
3552
Algorithm:
3653
1. Create a new array with filtering all the numbers less than the target.
@@ -53,4 +70,6 @@ public static int[] twoSumIndexes(int[] numbers, int target) {
5370
}
5471
return new int[0];
5572
}
73+
74+
5675
}

java8/src/test/java/com/shekhargulati/leetcode/Problem01Test.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,16 @@ public class Problem01Test {
1414
@Test
1515
public void shouldFindTwoNumberWhoseSumIsEqualToTarget() throws Exception {
1616
int[] pairs = Problem01.twoSum(new int[]{2, 7, 11, 15}, 9);
17-
assertTrue(Arrays.equals(pairs, new int[]{2, 7}));
17+
assertTrue(Arrays.equals(pairs, new int[]{0, 1}));
1818
}
1919

2020
@Test
2121
public void shouldFindTwoNumberWhoseSumIsEqualToTarget2() throws Exception {
2222
int[] pairs = Problem01.twoSum(new int[]{10, 17, 11, 30}, 21);
23-
assertTrue(Arrays.equals(pairs, new int[]{10, 11}));
24-
}
25-
26-
27-
@Test
28-
public void shouldFindTwoNumberWhoseSumIsEqualToTarget_index() throws Exception {
29-
int[] pairs = Problem01.twoSumIndexes(new int[]{2, 7, 11, 15}, 9);
30-
assertTrue(Arrays.equals(pairs, new int[]{0, 1}));
31-
}
32-
33-
@Test
34-
public void shouldFindTwoNumberWhoseSumIsEqualToTarget2_index() throws Exception {
35-
int[] pairs = Problem01.twoSumIndexes(new int[]{10, 17, 11, 30}, 21);
3623
assertTrue(Arrays.equals(pairs, new int[]{0, 2}));
3724
}
3825

26+
3927
@Test
4028
public void shouldWorkOnARandomArray() throws Exception {
4129
int[] numbers = new Random().ints(100000, 1, 100000).toArray();

0 commit comments

Comments
 (0)