做网站做一个什么主题的,小型电子商务企业网站建设,怎么用vs2008做网站,广西南宁时空网首页给你一个下标从 0 开始的整数数组 tasks #xff0c;其中 tasks[i] 表示任务的难度级别。在每一轮中#xff0c;你可以完成 2 个或者 3 个 相同难度级别 的任务。 返回完成所有任务需要的 最少 轮数#xff0c;如果无法完成所有任务#xff0c;返回 -1 。 英文原题#xf… 给你一个下标从 0 开始的整数数组 tasks 其中 tasks[i] 表示任务的难度级别。在每一轮中你可以完成 2 个或者 3 个 相同难度级别 的任务。 返回完成所有任务需要的 最少 轮数如果无法完成所有任务返回 -1 。 英文原题 You are given a 0-indexed integer array tasks, where tasks[i] represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the same difficulty level. Return the minimum rounds required to complete all the tasks, or -1 if it is not possible to complete all the tasks. 解题思路
很明显的2a3b形式只要遍历一遍统计然后用O1即可解决然而做的时候傻不拉几地想着2和3互质所以在那边写了个7长度的数组脑子没转过来导致写了个巨丑的解题答案。可能是最开始想到质数分解脑子秀逗了。。。
AC代码
class Solution:def minimumRounds(self, tasks: List[int]) - int:task_dict {}need_time_list [0, 0, 1, 1, 2, 2, 2, 3]for t in tasks:if t in task_dict.keys():task_dict[t] 1else:task_dict[t] 1res 0for i in task_dict.keys():if task_dict[i] 1:return -1else:res (task_dict[i] - 4) // 3 need_time_list[task_dict[i] - (task_dict[i] - 4) // 3 * 3] if task_dict[i] 7 else need_time_list[task_dict[i]]return res官方代码
class Solution:def minimumRounds(self, tasks: List[int]) - int:cnt Counter(tasks)ans 0for v in cnt.values():if v 1:return -1ans v // 3 (v % 3 ! 0)return ans