Skip to content

Commit aa061d4

Browse files
authored
Create 2151-maximum-good-people-based-on-statements.js
1 parent aaf409b commit aa061d4

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @param {number[][]} statements
3+
* @return {number}
4+
*/
5+
const maximumGood = function (statements) {
6+
const n = statements.length
7+
let res = 0,
8+
c = (1 << n) - 1
9+
for (let i = 0; i < c + 1; i++) {
10+
let s = dec2bin(i)
11+
s = '0'.repeat(n - s.length) + s
12+
let arr = [],
13+
f = 1
14+
for (let i = 0; i < n; i++) {
15+
if (s[i] === '1') arr.push(i)
16+
}
17+
for (let i of arr) {
18+
for (let j = 0; j < n; j++) {
19+
if (statements[i][j] !== 2 && statements[i][j] !== +s[j]) {
20+
f = 0
21+
break
22+
}
23+
}
24+
if (!f) break
25+
}
26+
if (f) res = Math.max(res, cnt(s, '1'))
27+
}
28+
29+
return res
30+
}
31+
function cnt(s, ch) {
32+
let res = 0
33+
for (let e of s) {
34+
if (e === ch) res++
35+
}
36+
return res
37+
}
38+
function dec2bin(dec) {
39+
return (dec >>> 0).toString(2)
40+
}

0 commit comments

Comments
 (0)