标签:etc turn ide ima list oss 集合 优化 mic
题目描述:
自己的提交:
class Solution: def isPossibleDivide(self, nums: List[int], k: int) -> bool: c = collections.Counter(nums) n = len(nums) m = n/k if m%1 != 0: return False while m != 0: x = k pre = min(i for i,v in c.items() if v > 0) -1 while x: if c[pre+1] <= 0: return False else: c[pre+1] -= 1 pre += 1 x -= 1 m -= 1 return True
优化:O(N)
class Solution: def isPossibleDivide(self, nums: List[int], k: int) -> bool: n=len(nums) if n%k: return False cnt=collections.Counter(nums) st=min(cnt) while len(cnt)>0: ntime=cnt[st] nst=-1 for num in range(st,st+k): if cnt[num]<ntime: return False elif cnt[num]==ntime: cnt.pop(num) else: cnt[num]-=ntime if nst<0: nst=num if nst<0 and len(cnt)>0: nst=min(cnt) st=nst return True
leetcode-168周赛-1296-划分数字为连续数字的集合
标签:etc turn ide ima list oss 集合 优化 mic
原文地址:https://www.cnblogs.com/oldby/p/12098455.html