Skip to content

Commit

Permalink
Added solution to Leetcode problem Task Scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
nehalabhasetwar committed Oct 21, 2020
1 parent 9b0b6e0 commit d269686
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Greedy/(LEETCODE) Task-Scheduler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Solution to Task Scheduler at Leetcode in Java
* Ref : https://leetcode.com/problems/task-scheduler
* Problem Statement:
* Given a characters array tasks, representing the tasks a CPU needs to do, where each letter represents a different task. Tasks could be done in any order. Each task is done in one unit of time.
* For each unit of time, the CPU could complete either one task or just be idle.
* However, there is a non-negative integer n that represents the cooldown period between two same tasks (the same letter in the array), that is that there must be at least n units of time between any two same tasks.
* Return the least number of units of times that the CPU will take to finish all the given tasks.
Example 1:
Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8
Explanation:
A -> B -> idle -> A -> B -> idle -> A -> B
There is at least 2 units of time between any two same tasks.
**/

public class Solution {
public int leastInterval(char[] tasks, int n) {
int[] storage = new int[26];
for (char c : tasks) {
storage[(c - 'A')]++;
}
int max = 0;
int count = 1;
for (int num : storage) {
if (num == 0) {
continue;
}
if (max < num) {
max = num;
count = 1;
} else if (max == num) {
count++;
}
}
int space = (n + 1) * (max - 1) + count;
return (space < tasks.length) ? tasks.length : space;
}
}

0 comments on commit d269686

Please sign in to comment.