-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: 258 ms (65.43%) | Memory: 84.2 MB (56.79%) - LeetSync
- Loading branch information
1 parent
734337f
commit 73cb9a4
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* @param {number} n | ||
* @param {number[][]} meetings | ||
* @return {number} | ||
*/ | ||
var mostBooked = function(n, meetings) { | ||
let solution = new Solution(); | ||
return solution.mostBooked(n, meetings); | ||
}; | ||
|
||
class Solution { | ||
mostBooked(n, meetings) { | ||
const ans = new Array(n).fill(0); | ||
const times = new Array(n).fill(0); | ||
meetings.sort((a, b) => a[0] - b[0]); | ||
|
||
for (let i = 0; i < meetings.length; i++) { | ||
const [start, end] = meetings[i]; | ||
let flag = false; | ||
let minind = -1; | ||
let val = Number.MAX_SAFE_INTEGER; | ||
for (let j = 0; j < n; j++) { | ||
if (times[j] < val) { | ||
val = times[j]; | ||
minind = j; | ||
} | ||
if (times[j] <= start) { | ||
flag = true; | ||
ans[j]++; | ||
times[j] = end; | ||
break; | ||
} | ||
} | ||
if (!flag) { | ||
ans[minind]++; | ||
times[minind] += (end - start); | ||
} | ||
} | ||
|
||
let maxi = -1; | ||
let id = -1; | ||
for (let i = 0; i < n; i++) { | ||
if (ans[i] > maxi) { | ||
maxi = ans[i]; | ||
id = i; | ||
} | ||
} | ||
return id; | ||
} | ||
} |