返回介绍

solution / 2200-2299 / 2244.Minimum Rounds to Complete All Tasks / README_EN

发布于 2024-06-17 01:03:08 字数 3799 浏览 0 评论 0 收藏 0

2244. Minimum Rounds to Complete All Tasks

中文文档

Description

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._

 

Example 1:

Input: tasks = [2,2,3,3,2,4,4,4,4,4]
Output: 4
Explanation: To complete all the tasks, a possible plan is:
- In the first round, you complete 3 tasks of difficulty level 2. 
- In the second round, you complete 2 tasks of difficulty level 3. 
- In the third round, you complete 3 tasks of difficulty level 4. 
- In the fourth round, you complete 2 tasks of difficulty level 4.  
It can be shown that all the tasks cannot be completed in fewer than 4 rounds, so the answer is 4.

Example 2:

Input: tasks = [2,3,3]
Output: -1
Explanation: There is only 1 task of difficulty level 2, but in each round, you can only complete either 2 or 3 tasks of the same difficulty level. Hence, you cannot complete all the tasks, and the answer is -1.

 

Constraints:

  • 1 <= tasks.length <= 105
  • 1 <= tasks[i] <= 109

Solutions

Solution 1

class Solution:
  def minimumRounds(self, tasks: List[int]) -> int:
    cnt = Counter(tasks)
    ans = 0
    for v in cnt.values():
      if v == 1:
        return -1
      ans += v // 3 + (v % 3 != 0)
    return ans
class Solution {
  public int minimumRounds(int[] tasks) {
    Map<Integer, Integer> cnt = new HashMap<>();
    for (int t : tasks) {
      cnt.merge(t, 1, Integer::sum);
    }
    int ans = 0;
    for (int v : cnt.values()) {
      if (v == 1) {
        return -1;
      }
      ans += v / 3 + (v % 3 == 0 ? 0 : 1);
    }
    return ans;
  }
}
class Solution {
public:
  int minimumRounds(vector<int>& tasks) {
    unordered_map<int, int> cnt;
    for (auto& t : tasks) {
      ++cnt[t];
    }
    int ans = 0;
    for (auto& [_, v] : cnt) {
      if (v == 1) {
        return -1;
      }
      ans += v / 3 + (v % 3 != 0);
    }
    return ans;
  }
};
func minimumRounds(tasks []int) int {
  cnt := map[int]int{}
  for _, t := range tasks {
    cnt[t]++
  }
  ans := 0
  for _, v := range cnt {
    if v == 1 {
      return -1
    }
    ans += v / 3
    if v%3 != 0 {
      ans++
    }
  }
  return ans
}
function minimumRounds(tasks: number[]): number {
  const cnt = new Map();
  for (const t of tasks) {
    cnt.set(t, (cnt.get(t) || 0) + 1);
  }
  let ans = 0;
  for (const v of cnt.values()) {
    if (v == 1) {
      return -1;
    }
    ans += Math.floor(v / 3) + (v % 3 === 0 ? 0 : 1);
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文