Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Solution for facebook-LongestSequenceWithTwoUniqueNumbers #8

Merged
merged 1 commit into from
Oct 31, 2020
Merged
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
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));
}
}