1121. 将数组分成几个递增序列
Posted cznczai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
以上是关于1121. 将数组分成几个递增序列的主要内容,如果未能解决你的问题,请参考以下文章