Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Similar to my comment on previous problem, I am unable to understand why we need...

Sukumar

Jun 30, 2022

Similar to my comment on previous problem, I am unable to understand why we need to use Map and also reduce is it while loop? what input will not work if we simply remove it? so something like below:

public static int findLength(char[] arr) { int windowStart = 0, maxLength = 0; Map fruitFrequencyMap = new HashMap(); // try to extend the range [windowStart, windowEnd] for (int windowEnd = 0; windowEnd < arr.length; windowEnd++) { fruitFrequencyMap.put(arr[windowEnd], fruitFrequencyMap.getOrDefault(arr[windowEnd], 0) + 1); // shrink the sliding window, until we're left with '2' fruits in the frequency map while (fruitFrequencyMap.size() > 2) { fruitFrequencyMap.remove(arr[windowStart]); windowStart++; // shrink the window } maxLength = Math.max(maxLength, windowEnd - windowStart + 1); } return maxLength; }

0

0

Comments
Comments

On this page