---
# System prepended metadata

title: '20200220'

---

# 20200220


Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks. Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle.

However, there is a non-negative cooling interval n that means between two same tasks, there must be at least n intervals that CPU are doing different tasks or just be idle.

You need to return the least number of intervals the CPU will take to finish all the given tasks.

Example:
Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8
Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.

K : V
A : 3
B : 3

Example:
Input: tasks = ["A", "A", "A", "B", "B", "C", "C"], n = 3
Output: 9
A -> B -> C -> idel -> A -> B -> C -> idel -> A


Input: tasks = ["A", "A", "B", "B", "C", "C", "C", "C"], n = 3
Output: 13

A : 2
B : 2
C : 4

C -> A -> B -> idel -> C -> A -> B -> idel -> C -> idel -> idel -> idel -> C

A -> B -> C -> idel -> A -> B -> C -> idel -> idel -> idel -> C -> idel -> idel -> idel -> C

C -> idel -> idel -> idel -> C -> idel -> idel -> idel -> C -> idel -> idel -> idel 
```
class Solution {
    public int leastInterval(char[] tasks, int n) {
        HashMap<Character, Integer> map = new HashMap<>();
        for (char c : tasks) {
            map.put(c, map.getOrDefault(c, 0) + 1);
        }
        
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
        maxHeap.addAll(map.values());
        
        int cycle = 0;
        while (!maxHeap.isEmpty()) {
            List<Integer> temp = new ArrayList<>();
            for (int i = 0; i < n + 1; i++) {
                if (!maxHeap.isEmpty()) {
                    temp.add(maxHeap.remove());
                }
            }
            
            for (int i : temp) {
                i--;
                if (i > 0) {
                    maxHeap.offer(i);
                }
            }
            
            cycle += maxHeap.isEmpty() ? temp.size() : n + 1;
        }
        
        return cycle;
    }
}
```
```
// 0, 0, 0, 0, 0 ...... , 2, 2, 4
// 0, 0, 0, 0, 0 ...... , 2, 2, 4
i = 25
i = 24
task.length = 8
3 * 4 + 25 - 24 = 13
return 13

c xxx c xxx c xxx c
// (c[25] - 1) * (n + 1) + 25 - i  is frame size
// when inserting chars, the frame might be "burst", then tasks.length takes precedence
// when 25 - i > n, the frame is already full at construction, the following is still valid.
public class Solution {
    public int leastInterval(char[] tasks, int n) {

        int[] c = new int[26];
        for(char t : tasks){
            c[t - 'A']++;
        }
        Arrays.sort(c);
        int i = 25;
        while(i >= 0 && c[i] == c[25]) 
            i--;

        return Math.max(tasks.length, (c[25] - 1) * (n + 1) + 25 - i);
    }
}
```

```
(c[25] - 1) * (n + 1) + 25 - i)
3 * 4 + 25 - 24 = 13

(c[25] - 1) * n + c[25] + (25 - i - 1)
3 * 4 + 4 + (25 - 24 - 1) = 16
```

```
  public int leastInterval(char[] tasks, int n) {
        int len = tasks.length;
        int[] count = new int[26];
        int max = 0, maxNum = 0;
        for (char c : tasks) {
            count[c - 'A']++;
            if (count[c - 'A'] > max) {
                max = count[c - 'A'];
                maxNum = 1;
            } else if (count[c - 'A'] == max) {
                maxNum++;
            }
        }
        return Math.max(len, (max - 1) * (n + 1) + maxNum);
    }
```
3 n + 1
[A, A, A] n = 3
A xxx A xxx A = 9
Axxx Axxx Axxx

Cxxx Cxxx Cxxx Cxxx
Input: tasks = ["A", "A", "B", "B", "C", "C", "C", "C"], n = 3
Output: 13
[2, 2, 4, 0, 0, 0 ...... 0]
max = 4
maxNum 1
Math.max(8, 3 * 4 + 1) = 13


CxxxCxxxCxxxC
Input: tasks = ["A", "A", "B", "B", "B", "B", "C", "C", "C", "C"], n = 3
max = 4
maxNum 2
Math.max(10, 3 * 4 + 2) = 14

Axxx, Axxx, Axxx, AB

C -> idel -> idel -> idel -> C -> idel -> idel -> idel -> C -> idel -> idel -> idel

=================================================
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.

Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.

You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
```
Example:
Given n = 5, and version = 4 is the first bad version.

call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true

Then 4 is the first bad version. 
```
n = 100
1, 2, 3, 4, 5 ... 100

1 ~ 23 good 
24 ~ 100 bad
24


```
/* The isBadVersion API is defined in the parent class VersionControl.
      boolean isBadVersion(int version); */

public class Solution extends VersionControl {

    public int firstBadVersion(int n) {
        int left = 1;
        int right = n;
        while(left < right) {
            int mid = right - (right + left) / 2;
            if (isBadVersion(mid)) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return left;
    }
}
```