From 0ceaec0cc9e0f293e3bf49d3fb97ec3c9aa3b9bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Khoa=20Hu=E1=BB=B3nh=20=28Daniel=29?= Date: Tue, 19 Mar 2024 23:17:14 +0700 Subject: [PATCH] Create main.go --- solution/621/main.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 solution/621/main.go diff --git a/solution/621/main.go b/solution/621/main.go new file mode 100644 index 0000000..c9ae1da --- /dev/null +++ b/solution/621/main.go @@ -0,0 +1,29 @@ +package main + +func leastInterval(tasks []byte, n int) int { + if n == 0 { + return len(tasks) + } + + cnt := make([]int, 26) + for _, task := range tasks { + cnt[task - 'A']++ + } + + var maxCount, sameMaxCount int + for _, c := range cnt { + if c > maxCount { + maxCount = c + sameMaxCount = 1 + } else if c == maxCount { + sameMaxCount++ + } + } + + res := (n + 1) * (maxCount - 1) + sameMaxCount + if (res > len(tasks)) { + return res + } else { + return len(tasks) + } +}