dengxinyi 6 vuotta sitten
vanhempi
commit
33fc0d77eb
1 muutettua tiedostoa jossa 9 lisäystä ja 4 poistoa
  1. 9 4
      medium/621.task-scheduler.go

+ 9 - 4
medium/621.task-scheduler.go

@@ -3,12 +3,17 @@ func leastInterval(tasks []byte, n int) int {
 	for _, t := range tasks {
 		freq[t-'A']++
 	}
-	sort.Ints(freq)
-	max, cnt := freq[25], 0
+	max, cnt := freq[0], 0
 	for _, i := range freq {
-		if i == max {
+		if max < i {
+			max, cnt = i, 1
+		} else if max == i {
 			cnt++
 		}
 	}
-	return (max-1)*n + cnt
+	res := (max-1)*(n+1) + cnt
+	if l := len(tasks); res < l {
+		return l // If n is small enough, all tasks can be processed one by one
+	}
+	return res
 }