Skip to content

Commit 0519238

Browse files
authored
Create 1183-maximum-number-of-ones.js
1 parent 05f1544 commit 0519238

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

1183-maximum-number-of-ones.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number} width
3+
* @param {number} height
4+
* @param {number} sideLength
5+
* @param {number} maxOnes
6+
* @return {number}
7+
*/
8+
const maximumNumberOfOnes = function (width, height, sideLength, maxOnes) {
9+
const n1 = (height / sideLength) >> 0,
10+
h = height % sideLength,
11+
n2 = (width / sideLength) >> 0,
12+
w = width % sideLength
13+
if (maxOnes <= w * h) return n1 * n2 * maxOnes + (n1 + n2 + 1) * maxOnes
14+
else {
15+
const a = h * w,
16+
B = (sideLength - w) * h,
17+
C = (sideLength - h) * w
18+
let b = Math.min(B, maxOnes - a)
19+
let c = Math.min(maxOnes - a - b, C)
20+
const res1 = n1 * (a + c) + n2 * (a + b)
21+
c = Math.min(C, maxOnes - a)
22+
b = Math.min(maxOnes - a - c, B)
23+
const res2 = n1 * (a + c) + n2 * (a + b)
24+
return n1 * n2 * maxOnes + Math.max(res1, res2) + a
25+
}
26+
}

0 commit comments

Comments
 (0)