Skip to content

Commit

Permalink
leetcode Problem 01 first try
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhargulati committed Mar 2, 2016
1 parent b260f1c commit 3a0fbe3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
33 changes: 26 additions & 7 deletions java8/src/main/java/com/shekhargulati/leetcode/Problem01.java
Expand Up @@ -12,25 +12,42 @@
*/ */
public class Problem01 { public class Problem01 {


/*
This solution is O(n)
*/
public static int[] twoSum(int[] numbers, int target) { public static int[] twoSum(int[] numbers, int target) {
/* /*
Algorithm: Algorithm:
1. Create a new array with filtering all the numbers less than the target. 1. Store all the number and their indexes in a Map
2. Iterate over the filtered array to find a pair which sum equal to target 2. Create a new array with filtering all the numbers less than the target.
3. Iterate over the filtered array.
4. For each number find the second number by subtracting it from the target
5. If Map contains the second number then we have found the pair
6. Short circuit the loop and return the result.
*/ */
Map<Integer, Integer> numberAndIndex = new HashMap<>();
for (int i = 0; i < numbers.length; i++) {
numberAndIndex.put(numbers[i], i);
}

int[] numbersLessThanTarget = Arrays.stream(numbers).filter(i -> i < target).toArray(); int[] numbersLessThanTarget = Arrays.stream(numbers).filter(i -> i < target).toArray();


for (int i = 0; i < numbersLessThanTarget.length - 1; i++) { for (int i = 0; i < numbersLessThanTarget.length - 1; i++) {
for (int j = i + 1; j < numbersLessThanTarget.length; j++) { int first = numbersLessThanTarget[i];
if ((numbersLessThanTarget[i] + numbersLessThanTarget[j]) == target) { int second = target - first;
return new int[]{numbersLessThanTarget[i], numbersLessThanTarget[j]}; if (numberAndIndex.containsKey(second)) {
} return new int[]{i, numberAndIndex.get(second)};
} }

} }
return new int[0]; return new int[0];
} }


public static int[] twoSumIndexes(int[] numbers, int target) {
/*
This solution in O(n^2)
*/
public static int[] twoSum_2(int[] numbers, int target) {
/* /*
Algorithm: Algorithm:
1. Create a new array with filtering all the numbers less than the target. 1. Create a new array with filtering all the numbers less than the target.
Expand All @@ -53,4 +70,6 @@ public static int[] twoSumIndexes(int[] numbers, int target) {
} }
return new int[0]; return new int[0];
} }


} }
16 changes: 2 additions & 14 deletions java8/src/test/java/com/shekhargulati/leetcode/Problem01Test.java
Expand Up @@ -14,28 +14,16 @@ public class Problem01Test {
@Test @Test
public void shouldFindTwoNumberWhoseSumIsEqualToTarget() throws Exception { public void shouldFindTwoNumberWhoseSumIsEqualToTarget() throws Exception {
int[] pairs = Problem01.twoSum(new int[]{2, 7, 11, 15}, 9); int[] pairs = Problem01.twoSum(new int[]{2, 7, 11, 15}, 9);
assertTrue(Arrays.equals(pairs, new int[]{2, 7})); assertTrue(Arrays.equals(pairs, new int[]{0, 1}));
} }


@Test @Test
public void shouldFindTwoNumberWhoseSumIsEqualToTarget2() throws Exception { public void shouldFindTwoNumberWhoseSumIsEqualToTarget2() throws Exception {
int[] pairs = Problem01.twoSum(new int[]{10, 17, 11, 30}, 21); int[] pairs = Problem01.twoSum(new int[]{10, 17, 11, 30}, 21);
assertTrue(Arrays.equals(pairs, new int[]{10, 11}));
}


@Test
public void shouldFindTwoNumberWhoseSumIsEqualToTarget_index() throws Exception {
int[] pairs = Problem01.twoSumIndexes(new int[]{2, 7, 11, 15}, 9);
assertTrue(Arrays.equals(pairs, new int[]{0, 1}));
}

@Test
public void shouldFindTwoNumberWhoseSumIsEqualToTarget2_index() throws Exception {
int[] pairs = Problem01.twoSumIndexes(new int[]{10, 17, 11, 30}, 21);
assertTrue(Arrays.equals(pairs, new int[]{0, 2})); assertTrue(Arrays.equals(pairs, new int[]{0, 2}));
} }



@Test @Test
public void shouldWorkOnARandomArray() throws Exception { public void shouldWorkOnARandomArray() throws Exception {
int[] numbers = new Random().ints(100000, 1, 100000).toArray(); int[] numbers = new Random().ints(100000, 1, 100000).toArray();
Expand Down

0 comments on commit 3a0fbe3

Please sign in to comment.