Develop / Leetcode March 28, 2024

Daily Question – 2958. Length of Longest Subarray With at Most K Frequency

Description: https://leetcode.com/problems/length-of-longest-subarray-with-at-most-k-frequency/description/

Notes:

The first solution that came to my mind was brute force: scanning each subarray, checking the frequency of each number, and then returning the result. However, this would undoubtedly exceed the time limit. To reduce time complexity, I utilized a HashMap to store the frequency of each number and implemented a sliding window algorithm for the check.

Code:

class Solution {
    public int maxSubarrayLength(int[] nums, int k) {
        if (nums.length <= k) return nums.length;
        int left = 0,right = 0,max = 0,result = 0, maxKey = 0;
        HashMap<Integer,Integer> map = new HashMap<>();
        // step 1: find the first slide window
        while (max <= k && right<nums.length) {
            map.put( nums[right], map.getOrDefault(nums[right],0)+1);
            if (map.get(nums[right]) > max ) {
                max = map.get(nums[right]);
                maxKey = nums[right];
            }
            if (max > k) break;
            right++;
        }
        if (right-left > result) result = right - left;
        // step 2: start sliding
        for (left = 1; left < nums.length;left++) {
            map.put( nums[left-1], map.get(nums[left-1])-1);
            if (nums[left-1] == maxKey) max--;
            while (max <= k && right<nums.length) {
                right++;
                if (right == nums.length) break;
                map.put( nums[right], map.getOrDefault(nums[right],0)+1);
                if (map.get(nums[right]) > max ) {
                    max = map.get(nums[right]);
                    maxKey = nums[right];
                }
                if (max > k) break;
            }
            if (right-left > result) result = right - left;
        }
        return result;
    }
}

Simplify by chatGPT:

We can merge two loop to make the code more clear.

class Solution {
    public int maxSubarrayLength(int[] nums, int k) {
        if (nums.length <= k) return nums.length;
        
        int left = 0, right = 0, max = 0, result = 0, maxKey = 0;
        HashMap<Integer,Integer> map = new HashMap<>();
        
        while (right < nums.length) {
            map.put(nums[right], map.getOrDefault(nums[right], 0) + 1);
            if (map.get(nums[right]) > max) {
                max = map.get(nums[right]);
                maxKey = nums[right];
            }
            if (max > k) {
                map.put(nums[left], map.get(nums[left]) - 1);
                if (nums[left] == maxKey) max--;
                left++;
            }
            result = Math.max(result, right - left + 1);
            right++;
        }
        
        return result;
    }
}

You may also like...

May
07
2023
0

2023-Northern Europe Part1 – Copenhagen 哥本哈根

时隔三年,至于可以踏出国门,一口气去了三个北欧国家,Hello World~~~

Apr
10
2024
0

Daily Question – 950. Reveal Cards In Increasing Order

Post Views: 37 Description: https://leetcode.com/problems/reveal-cards-in-increasing-order/description/?envType=daily-question&envId=2024-04-10 Code: Time Complexity O(n2) Improvement: This approach is genius. https://leetcode.com/problems/reveal-cards-in-increasing-order/solutions/5001220/beats-94-easy-approach-using-deque-with-explanation Deque...

Mar
06
2024
0

Daily Question – 0141. Linked List Cycle

This problem also employs the “Two Pointers” technique, which is rather ingenious.

Feb
17
2024
0

19. Remove Nth Node From End of List

Post Views: 33 Description: https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ Notes: