Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Leetcode task solution: https://leetcode.com/problems/minimum-rounds-to-complete-all-tasks/description/
- class Solution {
- fun minimumRounds(tasks: IntArray): Int {
- val map = tasks.toCountedMap()
- var rounds = 0
- map.forEach { entry ->
- val minRounds = minRounds(entry.value)
- if (minRounds == -1) {
- return -1
- } else {
- rounds += minRounds
- }
- }
- return rounds
- }
- private fun IntArray.toCountedMap(): Map<Int, Int> {
- val map = mutableMapOf<Int, Int>()
- for (task in this) {
- map[task] = (map[task] ?: 0) + 1
- }
- return map
- }
- private fun minRounds(tasksNumber: Int): Int {
- val rounds3 = tasksNumber / 3
- val residue = tasksNumber % 3
- return when {
- residue == 1 && rounds3 == 0 -> -1
- residue == 1 ||
- residue == 2 -> rounds3 + 1
- else -> rounds3
- }
- }
- }
Advertisement
Advertisement