All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2301_2400.s2365_task_scheduler_ii.Solution Maven / Gradle / Ivy

There is a newer version: 1.35
Show newest version
package g2301_2400.s2365_task_scheduler_ii;

// #Medium #Array #Hash_Table #Simulation #2022_08_14_Time_70_ms_(55.56%)_Space_123.3_MB_(11.11%)

import java.util.HashMap;

public class Solution {
    public long taskSchedulerII(int[] tasks, int space) {
        long days = 0;
        space++;
        HashMap lastOccurence = new HashMap<>();
        for (int i = 0; i < tasks.length; i++) {
            if (lastOccurence.containsKey(tasks[i])) {
                long lastTimeOccurred = lastOccurence.get(tasks[i]);
                long daysDifference = days - lastTimeOccurred;
                if (daysDifference < space) {
                    days += (space - daysDifference);
                }
            }
            lastOccurence.put(tasks[i], days);
            days++;
        }
        return days;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy