1121. 将数组分成几个递增序列

这道题被吓到了 ,不知道怎么判断 ,其实是找同一个值的最大出现次数,也就是最大的分组 因为其他的话会分布比较均匀出现

class Solution {

	public boolean canDivideIntoSubsequences(int[] nums, int K) {
		int now = 0;
		int m = 0;
		for (int i = 0; i < nums.length; i++) {
			now++;
			if (i == nums.length - 1 || nums[i] != nums[i + 1]) {          //i==nums.length-1 使得m最小为1
				m = Math.max(now, m);
				now = 0;
			}
		}
		return (nums.length / m) >= K;
	}
}

这道题也是统计有多少个相同的数 然后划分

class Solution {
    
    public boolean canDivideIntoSubsequences(int[] nums, int K) {
        Map<Integer, Integer> cnt = new HashMap<>();
        int mx = 0;
        for (int i = 0; i < nums.length; i++) {
            cnt.put(nums[i], cnt.getOrDefault(nums[i], 0) + 1);
            mx = Math.max(mx, cnt.get(nums[i]));
        }
        
        if ((long) mx * K > nums.length) return false;
        return true;
    }
}
原文地址:https://www.cnblogs.com/cznczai/p/11183078.html