Skip to content

Commit

Permalink
Merge pull request #8 from ratanboddu/facebook-sequence
Browse files Browse the repository at this point in the history
Adding Solution for facebook-LongestSequenceWithTwoUniqueNumbers
  • Loading branch information
emafazillah authored Oct 31, 2020
2 parents 24192ce + bb270a6 commit 3f92034
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Number of Ways to Climb Stairs | LinkedIn | [ClimbingStairs.java](https://github
Find Pythagorean Triplets | Uber | [FindPythagoreanTriplets.java](https://github.com/emafazillah/DailyInterviewProJava/blob/master/src/main/java/com/dailyinterviewprojava/uber/FindPythagoreanTriplets.java) | Done |
Edit Distance | Airbnb | [EditDistance.java](https://github.com/emafazillah/DailyInterviewProJava/blob/master/src/main/java/com/dailyinterviewprojava/airbnb/EditDistance.java) | To Do |
Create a Simple Calculator | Google | [CreateASimpleCalculator.java](https://github.com/emafazillah/DailyInterviewProJava/blob/master/src/main/java/com/dailyinterviewprojava/google/CreateASimpleCalculator.java) | Done |
Longest Sequence with Two Unique Numbers | Facebook | [LongestSequenceWithTwoUniqueNumbers.java](https://github.com/emafazillah/DailyInterviewProJava/blob/master/src/main/java/com/dailyinterviewprojava/facebook/LongestSequenceWithTwoUniqueNumbers.java) | To Do |
Longest Sequence with Two Unique Numbers | Facebook | [LongestSequenceWithTwoUniqueNumbers.java](https://github.com/emafazillah/DailyInterviewProJava/blob/master/src/main/java/com/dailyinterviewprojava/facebook/LongestSequenceWithTwoUniqueNumbers.java) | Done |
Find Cycles in a Graph | Facebook | [FindCyclesInAGraph.java](https://github.com/emafazillah/DailyInterviewProJava/blob/master/src/main/java/com/dailyinterviewprojava/facebook/FindCyclesInAGraph.java) | To Do |
Word Search | Amazon | [WordSearch.java](https://github.com/emafazillah/DailyInterviewProJava/blob/master/src/main/java/com/dailyinterviewprojava/amazon/WordSearch.java) | Done |
Minimum Size Subarray Sum | Amazon | [MinimumSizeSubarraySum.java](https://github.com/emafazillah/DailyInterviewProJava/blob/master/src/main/java/com/dailyinterviewprojava/amazon/MinimumSizeSubarraySum.java) | Done |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,62 @@
* The longest sequence that contains just 2 unique numbers is [3, 1, 3, 1]
*
*/

import java.util.Arrays;
import java.util.Scanner;

import com.dailyinterviewprojava.util.InputUtil;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class LongestSequenceWithTwoUniqueNumbers {

static int findSequence(int[] numbers) {
if (numbers.length < 3) {
return numbers.length;
}


Map<Integer, Integer> unique = new HashMap<>();
int startIndex = 0;
int max = 0;

for (int i = 0; i < numbers.length; i++) {
int number = numbers[i];
if (!unique.containsKey(number)) {
unique.put(number, 0);
}
unique.put(number, unique.get(number) + 1);

if (unique.size() > 2) {
// shift "window"
while (unique.size() > 2) {
int numberToDelete = numbers[startIndex];
unique.replace(numberToDelete, unique.get(numberToDelete) - 1);
if (unique.get(numberToDelete) == 0) {
unique.remove(numberToDelete);
}
startIndex++;
}
} else {
// update max
max = Math.max(max, i - startIndex + 1);
}
}

return max;
}

public static void main(String[] args) {
// Input
Scanner scanner = new Scanner(System.in);
String[] inputs = InputUtil.inputArr(scanner.next());
int[] nums = InputUtil.integerArr(inputs);

// Calculate output
System.out.println(findSequence(nums));
}
}

0 comments on commit 3f92034

Please sign in to comment.