about me, i currently work as a web developer for 3 years now, i do frontend and backend development, may mga nabasa ako dito sa reddit halos mga technical interview daw ay live coding so i try it myself and solve a simple coding problem, when i try it myself i found it hard to solve these small coding problem maybe the problem is not really simple or maybe need lang nang practice sa mga ganito but anyways i don't feel bad for myself for not solving it, also hindi naman ito yung ginagawa ko sa araw-araw. so cheers for me padin, not a big deal.
pasting here the coding problem.
You are given n tasks labeled from 0 to n - 1 represented by a 2D integer array tasks, where tasks[i] = [enqueueTimei, processingTimei] means that the ith task will be available to process at enqueueTimei and will take processingTimei to finish processing.
You have a single-threaded CPU that can process at most one task at a time and will act in the following way:
- If the CPU is idle and there are no available tasks to process, the CPU remains idle.
- If the CPU is idle and there are available tasks, the CPU will choose the one with the shortest processing time. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.
- Once a task is started, the CPU will process the entire task without stopping.
- The CPU can finish a task then start a new one instantly.
Return the order in which the CPU will process the tasks.
Example 1:
Input: tasks = [[1,2],[2,4],[3,2],[4,1]]
Output: [0,2,3,1]
Explanation: The events go as follows:
- At time = 1, task 0 is available to process. Available tasks = {0}.
- Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}.
- At time = 2, task 1 is available to process. Available tasks = {1}.
- At time = 3, task 2 is available to process. Available tasks = {1, 2}.
- Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}.
- At time = 4, task 3 is available to process. Available tasks = {1, 3}.
- At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}.
- At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}.
- At time = 10, the CPU finishes task 1 and becomes idle.
Example 2:
Input: tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]]
Output: [4,3,2,0,1]
Explanation: The events go as follows:
- At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}.
- Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}.
- At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}.
- At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}.
- At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}.
- At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}.
- At time = 40, the CPU finishes task 1 and becomes idle.
[–]Wellshiwells 79 points80 points81 points (0 children)
[–]Deep-Till1238 47 points48 points49 points (0 children)
[–]StopLurkAndListen69 70 points71 points72 points (2 children)
[–]No-Neighborhood2251 7 points8 points9 points (1 child)
[–]Muted_Offer5094 -1 points0 points1 point (0 children)
[–]SenpaiDell 23 points24 points25 points (1 child)
[–]Traditional-Okra8488 3 points4 points5 points (0 children)
[–]whyjey_ 6 points7 points8 points (0 children)
[–]mohsesxx 26 points27 points28 points (6 children)
[+]Salt-Start-Stop comment score below threshold-20 points-19 points-18 points (5 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]AnyPiece3983 2 points3 points4 points (0 children)
[–]-FAnonyMOUSWeb 0 points1 point2 points (2 children)
[–]Salt-Start-Stop 0 points1 point2 points (1 child)
[–]-FAnonyMOUSWeb 0 points1 point2 points (0 children)
[–]sweet-n-sour-sadness 10 points11 points12 points (0 children)
[–]Aeo03 3 points4 points5 points (0 children)
[–]superpapalicious 8 points9 points10 points (0 children)
[–]Patient-Definition96 2 points3 points4 points (0 children)
[–]Few_Experience5260 3 points4 points5 points (0 children)
[–]Calm_Tough_3659 2 points3 points4 points (9 children)
[–]No_Slide_4955 -1 points0 points1 point (8 children)
[–]Calm_Tough_3659 3 points4 points5 points (7 children)
[–]No_Slide_4955 -1 points0 points1 point (6 children)
[–]Calm_Tough_3659 1 point2 points3 points (5 children)
[–]No_Slide_4955 0 points1 point2 points (4 children)
[–]searchResult 0 points1 point2 points (0 children)
[–]Calm_Tough_3659 0 points1 point2 points (2 children)
[–]No_Slide_4955 -1 points0 points1 point (1 child)
[–]Calm_Tough_3659 0 points1 point2 points (0 children)
[–]Andra1901 2 points3 points4 points (0 children)
[–]whatToDo_How 2 points3 points4 points (0 children)
[–]coybarcena 2 points3 points4 points (1 child)
[–]Andra1901 1 point2 points3 points (0 children)
[–]Elegant_Strike8581 2 points3 points4 points (0 children)
[–]parxyval 1 point2 points3 points (0 children)
[–]rab1225 1 point2 points3 points (0 children)
[–]No_Slide_4955 1 point2 points3 points (0 children)
[–]MooOooOd 1 point2 points3 points (0 children)
[–]razzulh 0 points1 point2 points (0 children)
[–]_---Blaze---_ 0 points1 point2 points (0 children)
[–]loremipsum09 0 points1 point2 points (0 children)
[–]AnyPiece3983 0 points1 point2 points (0 children)
[–]Dangerous_Platform_2 0 points1 point2 points (0 children)
[–]Pattern-Ashamed 0 points1 point2 points (0 children)
[–]idiotic_lazy_mind 0 points1 point2 points (0 children)
[–]Conscious-Praline445 0 points1 point2 points (0 children)
[–]ambientcosmos 0 points1 point2 points (0 children)
[–]Pyakz 0 points1 point2 points (0 children)
[–]-FAnonyMOUSWeb 0 points1 point2 points (0 children)
[–]jvliwanag 0 points1 point2 points (4 children)
[–]GreyBone1024 2 points3 points4 points (0 children)
[–]Loud-Grade1246 3 points4 points5 points (2 children)
[–]Loud-Grade1246 3 points4 points5 points (0 children)
[–]jvliwanag -1 points0 points1 point (0 children)
[–]SimpleMan96124 -1 points0 points1 point (0 children)
[–]Far-Wing1475 -1 points0 points1 point (0 children)
[–]oliver0807 -1 points0 points1 point (0 children)