@@ -28,3 +28,105 @@ const possible = (dis, res, K) => {
28
28
}
29
29
return need <= K
30
30
}
31
+
32
+ // another
33
+
34
+ /**
35
+ * @param {number[] } stations
36
+ * @param {number } k
37
+ * @return {number }
38
+ */
39
+ const minmaxGasDist = function ( stations , k ) {
40
+ const pq = new PriorityQueue ( ( a , b ) => a [ 0 ] > b [ 0 ] )
41
+ for ( let i = 1 , n = stations . length ; i < n ; i ++ ) {
42
+ const delta = stations [ i ] - stations [ i - 1 ]
43
+ pq . push ( [ delta , delta , 1 ] )
44
+ }
45
+ const limit = ( stations [ stations . length - 1 ] - stations [ 0 ] ) / k
46
+ // console.log(pq.heap)
47
+ while ( k > 0 ) {
48
+ let [ delta , gap , num ] = pq . pop ( )
49
+
50
+ let v = gap / ( num + 1 )
51
+ while ( k > 0 && gap / ( num + 1 ) > limit ) {
52
+ k --
53
+ num ++
54
+ }
55
+
56
+ v = gap / ++ num
57
+ k --
58
+
59
+ pq . push ( [ v , gap , num ] )
60
+ }
61
+
62
+ return pq . peek ( ) [ 0 ]
63
+ } ;
64
+
65
+
66
+ class PriorityQueue {
67
+ constructor ( comparator = ( a , b ) => a > b ) {
68
+ this . heap = [ ]
69
+ this . top = 0
70
+ this . comparator = comparator
71
+ }
72
+ size ( ) {
73
+ return this . heap . length
74
+ }
75
+ isEmpty ( ) {
76
+ return this . size ( ) === 0
77
+ }
78
+ peek ( ) {
79
+ return this . heap [ this . top ]
80
+ }
81
+ push ( ...values ) {
82
+ values . forEach ( ( value ) => {
83
+ this . heap . push ( value )
84
+ this . siftUp ( )
85
+ } )
86
+ return this . size ( )
87
+ }
88
+ pop ( ) {
89
+ const poppedValue = this . peek ( )
90
+ const bottom = this . size ( ) - 1
91
+ if ( bottom > this . top ) {
92
+ this . swap ( this . top , bottom )
93
+ }
94
+ this . heap . pop ( )
95
+ this . siftDown ( )
96
+ return poppedValue
97
+ }
98
+ replace ( value ) {
99
+ const replacedValue = this . peek ( )
100
+ this . heap [ this . top ] = value
101
+ this . siftDown ( )
102
+ return replacedValue
103
+ }
104
+
105
+ parent = ( i ) => ( ( i + 1 ) >>> 1 ) - 1
106
+ left = ( i ) => ( i << 1 ) + 1
107
+ right = ( i ) => ( i + 1 ) << 1
108
+ greater = ( i , j ) => this . comparator ( this . heap [ i ] , this . heap [ j ] )
109
+ swap = ( i , j ) => ( [ this . heap [ i ] , this . heap [ j ] ] = [ this . heap [ j ] , this . heap [ i ] ] )
110
+ siftUp = ( ) => {
111
+ let node = this . size ( ) - 1
112
+ while ( node > this . top && this . greater ( node , this . parent ( node ) ) ) {
113
+ this . swap ( node , this . parent ( node ) )
114
+ node = this . parent ( node )
115
+ }
116
+ }
117
+ siftDown = ( ) => {
118
+ let node = this . top
119
+ while (
120
+ ( this . left ( node ) < this . size ( ) && this . greater ( this . left ( node ) , node ) ) ||
121
+ ( this . right ( node ) < this . size ( ) && this . greater ( this . right ( node ) , node ) )
122
+ ) {
123
+ let maxChild =
124
+ this . right ( node ) < this . size ( ) &&
125
+ this . greater ( this . right ( node ) , this . left ( node ) )
126
+ ? this . right ( node )
127
+ : this . left ( node )
128
+ this . swap ( node , maxChild )
129
+ node = maxChild
130
+ }
131
+ }
132
+ }
0 commit comments