File tree 1 file changed +35
-0
lines changed
1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } servers
3
+ * @param {number[] } tasks
4
+ * @return {number[] }
5
+ */
6
+ const assignTasks = function ( servers , tasks ) {
7
+ const freePQ = new PriorityQueue ( ( a , b ) => a . w === b . w ? a . i < b . i : a . w < b . w )
8
+ const runningPQ = new PriorityQueue ( ( a , b ) => a . e === b . e ? ( a . w === b . w ? a . i < b . i : a . w < b . w ) : a . e < b . e )
9
+ const m = servers . length , n = tasks . length
10
+ for ( let i = 0 ; i < m ; i ++ ) freePQ . push ( { w : servers [ i ] , i, e : 0 } )
11
+ const res = [ ]
12
+ for ( let i = 0 ; i < n ; i ++ ) {
13
+ const cur = tasks [ i ]
14
+ while ( ! runningPQ . isEmpty ( ) && runningPQ . peek ( ) . e <= i ) {
15
+ const tmp = runningPQ . pop ( )
16
+ tmp . e = i
17
+ freePQ . push ( tmp )
18
+ }
19
+ if ( freePQ . isEmpty ( ) ) {
20
+ const tmp = runningPQ . pop ( )
21
+ res [ i ] = tmp . i
22
+ tmp . e += cur
23
+ runningPQ . push ( tmp )
24
+ } else {
25
+ const tmp = freePQ . pop ( )
26
+ res [ i ] = tmp . i
27
+ tmp . e = i + cur
28
+ runningPQ . push ( tmp )
29
+ }
30
+ }
31
+ return res
32
+ } ;
33
+
34
+ // another
35
+
1
36
/**
2
37
* @param {number[] } servers
3
38
* @param {number[] } tasks
You can’t perform that action at this time.
0 commit comments