File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } points
3+ * @param {number } m
4+ * @return {number }
5+ */
6+ var maxScore = function ( points , m ) {
7+ const n = points . length
8+ if ( m < n ) return 0
9+
10+ const can = ( val ) => {
11+ let total = 0 ,
12+ transfer = 0 ,
13+ skipAdd = 0
14+ for ( let i = 0 ; i < n && total <= m ; i ++ ) {
15+ const point = points [ i ]
16+ const necessary = Math . floor ( ( val + point - 1 ) / point )
17+ if ( transfer >= necessary ) {
18+ transfer = 0
19+ skipAdd ++
20+ } else {
21+ const p = transfer * point
22+ const ops = Math . floor ( ( val - p + point - 1 ) / point )
23+ total += 2 * ops - 1
24+ total += skipAdd
25+
26+ transfer = Math . max ( ops - 1 , 0 )
27+ skipAdd = 0
28+ }
29+ }
30+ return total <= m
31+ }
32+
33+ let l = 1n ,
34+ r = 10n ** 15n ,
35+ res = 0n
36+ while ( l <= r ) {
37+ const mid = l + ( r - l ) / 2n
38+ if ( can ( Number ( mid ) ) ) {
39+ res = mid
40+ l = mid + 1n
41+ } else {
42+ r = mid - 1n
43+ }
44+ }
45+ return Number ( res )
46+ }
You can’t perform that action at this time.
0 commit comments