Formatted question description: https://leetcode.ca/all/1224.html

1224. Maximum Equal Frequency

Level

Hard

Description

Given an array nums of positive integers, return the longest possible length of an array prefix of nums, such that it is possible to remove **exactly one **element from this prefix so that every number that has appeared in it will have the same number of occurrences.

If after removing one element there are no remaining elements, it’s still considered that every appeared number has the same number of ocurrences (0).

Example 1:

Input: nums = [2,2,1,1,5,3,3,5]

Output: 7

Explanation: For the subarray [2,2,1,1,5,3,3] of length 7, if we remove nums[4]=5, we will get [2,2,1,1,3,3], so that each number will appear exactly twice.

Example 2:

Input: nums = [1,1,1,2,2,2,3,3,3,4,4,4,5]

Output: 13

Example 3:

Input: nums = [1,1,1,2,2,2]

Output: 5

Example 4:

Input: nums = [10,2,8,9,3,8,1,5,2,3,7,6]

Output: 8

Constraints:

  • 2 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^5

Solution

For each prefix of nums, it is possible if one of the following cases is met.

  1. All elements in the prefix are different.
  2. All elements in the prefix are the same.
  3. Exactly one element occurs for n + 1 times and all the other elements occur for n times, where n is a positive integer.
  4. Exactly one element occurs once and all the other elements occur for n times, where n is a positive integer.

Use two maps to store each number’s frequency and for each frequency, how many numbers have such a frequency. Loop over nums from left to right and update the two maps. For each prefix, check whether it is possible using the cases. If it is possible, update the maximum length. Finally, return the maximum length.

class Solution {
    public int maxEqualFreq(int[] nums) {
        if (nums == null || nums.length == 0)
            return 0;
        int maxLength = 1;
        Map<Integer, Integer> numFrequencyMap = new HashMap<Integer, Integer>();
        Map<Integer, Integer> frequencyCountMap = new HashMap<Integer, Integer>();
        int length = nums.length;
        for (int i = 0; i < length; i++) {
            int num = nums[i];
            int frequency = numFrequencyMap.getOrDefault(num, 0);
            int newFrequency = frequency + 1;
            numFrequencyMap.put(num, newFrequency);
            if (frequency > 0) {
                int count = frequencyCountMap.get(frequency) - 1;
                if (count > 0)
                    frequencyCountMap.put(frequency, count);
                else
                    frequencyCountMap.remove(frequency);
            }
            int newCount = frequencyCountMap.getOrDefault(newFrequency, 0) + 1;
            frequencyCountMap.put(newFrequency, newCount);
            if (isPossible(frequencyCountMap))
                maxLength = i + 1;
        }
        return maxLength;
    }

    public boolean isPossible(Map<Integer, Integer> frequencyCountMap) {
        if (frequencyCountMap.size() > 2)
            return false;
        List<Integer> list = new ArrayList<Integer>(frequencyCountMap.keySet());
        if (list.size() == 1) {
            int key = list.get(0);
            if (key == 1)
                return true;
            int count = frequencyCountMap.get(key);
            return count == 1;
        } else {
            int min = list.get(0), max = list.get(1);
            if (min > max) {
                int temp = min;
                min = max;
                max = temp;
            }
            if (max - min == 1 && frequencyCountMap.get(max) == 1)
                return true;
            else if (min == 1 && frequencyCountMap.get(min) == 1)
                return true;
            else
                return false;
        }
    }
}

All Problems

All Solutions