File tree Expand file tree Collapse file tree 3 files changed +68
-0
lines changed
solution/2000-2099/2064.Minimized Maximum of Products Distributed to Any Store Expand file tree Collapse file tree 3 files changed +68
-0
lines changed Original file line number Diff line number Diff line change 66
66
67
67
<!-- 这里可写通用的实现逻辑 -->
68
68
69
+ 二分法
70
+
69
71
<!-- tabs:start -->
70
72
71
73
### ** Python3**
84
86
85
87
```
86
88
89
+ ### ** TypeScript**
90
+
91
+ ``` ts
92
+ function minimizedMaximum(n : number , quantities : number []): number {
93
+ const m = quantities .length ;
94
+ let left = 1 , right = Math .max (... quantities );
95
+ while (left <= right ) {
96
+ let mid = (left + right ) >> 1 ;
97
+ let sum = 0 ;
98
+ for (let num of quantities ) {
99
+ let cur = Math .floor ((num - 1 ) / mid ) + 1 ;
100
+ sum += cur ;
101
+ }
102
+ if (sum > n ) {
103
+ left = mid + 1 ;
104
+ } else {
105
+ right = mid - 1 ;
106
+ }
107
+ }
108
+ return left ;
109
+ };
110
+ ```
111
+
87
112
### ** ...**
88
113
89
114
```
Original file line number Diff line number Diff line change @@ -60,6 +60,8 @@ The maximum number of products given to any store is max(100000) = 100000.
60
60
61
61
## Solutions
62
62
63
+ Binary search.
64
+
63
65
<!-- tabs:start -->
64
66
65
67
### ** Python3**
@@ -74,6 +76,29 @@ The maximum number of products given to any store is max(100000) = 100000.
74
76
75
77
```
76
78
79
+ ### ** TypeScript**
80
+
81
+ ``` ts
82
+ function minimizedMaximum(n : number , quantities : number []): number {
83
+ const m = quantities .length ;
84
+ let left = 1 , right = Math .max (... quantities );
85
+ while (left <= right ) {
86
+ let mid = (left + right ) >> 1 ;
87
+ let sum = 0 ;
88
+ for (let num of quantities ) {
89
+ let cur = Math .floor ((num - 1 ) / mid ) + 1 ;
90
+ sum += cur ;
91
+ }
92
+ if (sum > n ) {
93
+ left = mid + 1 ;
94
+ } else {
95
+ right = mid - 1 ;
96
+ }
97
+ }
98
+ return left ;
99
+ };
100
+ ```
101
+
77
102
### ** ...**
78
103
79
104
```
Original file line number Diff line number Diff line change
1
+ function minimizedMaximum ( n : number , quantities : number [ ] ) : number {
2
+ const m = quantities . length ;
3
+ let left = 1 , right = Math . max ( ...quantities ) ;
4
+ while ( left <= right ) {
5
+ let mid = ( left + right ) >> 1 ;
6
+ let sum = 0 ;
7
+ for ( let num of quantities ) {
8
+ let cur = Math . floor ( ( num - 1 ) / mid ) + 1 ;
9
+ sum += cur ;
10
+ }
11
+ if ( sum > n ) {
12
+ left = mid + 1 ;
13
+ } else {
14
+ right = mid - 1 ;
15
+ }
16
+ }
17
+ return left ;
18
+ } ;
You can’t perform that action at this time.
0 commit comments