Skip to content

Latest commit

 

History

History
30 lines (26 loc) · 772 Bytes

2023-01-04.md

File metadata and controls

30 lines (26 loc) · 772 Bytes

题目

  1. Minimum Rounds to Complete All Tasks

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.

思路

代码

var minimumRounds = function(tasks) {
  const count = {}
  for (const task of tasks) {
    if (count[task] == undefined) {
      count[task] = 1
    } else {
      count[task]++
    }
  }
  let rounds = 0
  for (const level in count) {
    let c = count[level]
    if (c == 1) return -1
    rounds += Math.floor(c / 3)
    if (c % 3 != 0) rounds++
  }
  return rounds
}