diff --git a/README.md b/README.md index 9ddb2c6..8f4931c 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/src/main/java/com/dailyinterviewprojava/facebook/LongestSequenceWithTwoUniqueNumbers.java b/src/main/java/com/dailyinterviewprojava/facebook/LongestSequenceWithTwoUniqueNumbers.java index b43f7fb..5d04464 100644 --- a/src/main/java/com/dailyinterviewprojava/facebook/LongestSequenceWithTwoUniqueNumbers.java +++ b/src/main/java/com/dailyinterviewprojava/facebook/LongestSequenceWithTwoUniqueNumbers.java @@ -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 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)); + } }