Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7b39dcc

Browse files
committed
Add solution #2732
1 parent385540f commit7b39dcc

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,6 +2014,7 @@
20142014
2726|[Calculator with Method Chaining](./solutions/2726-calculator-with-method-chaining.js)|Easy|
20152015
2727|[Is Object Empty](./solutions/2727-is-object-empty.js)|Easy|
20162016
2729|[Check if The Number is Fascinating](./solutions/2729-check-if-the-number-is-fascinating.js)|Easy|
2017+
2732|[Find a Good Subset of the Matrix](./solutions/2732-find-a-good-subset-of-the-matrix.js)|Hard|
20172018
2780|[Minimum Index of a Valid Split](./solutions/2780-minimum-index-of-a-valid-split.js)|Medium|
20182019
2799|[Count Complete Subarrays in an Array](./solutions/2799-count-complete-subarrays-in-an-array.js)|Medium|
20192020
2818|[Apply Operations to Maximize Score](./solutions/2818-apply-operations-to-maximize-score.js)|Hard|
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* 2732. Find a Good Subset of the Matrix
3+
* https://leetcode.com/problems/find-a-good-subset-of-the-matrix/
4+
* Difficulty: Hard
5+
*
6+
* You are given a 0-indexed m x n binary matrix grid.
7+
*
8+
* Let us call a non-empty subset of rows good if the sum of each column of the subset is at
9+
* most half of the length of the subset.
10+
*
11+
* More formally, if the length of the chosen subset of rows is k, then the sum of each column
12+
* should be at most floor(k / 2).
13+
*
14+
* Return an integer array that contains row indices of a good subset sorted in ascending order.
15+
*
16+
* If there are multiple good subsets, you can return any of them. If there are no good subsets,
17+
* return an empty array.
18+
*
19+
* A subset of rows of the matrix grid is any matrix that can be obtained by deleting some (possibly
20+
* none or all) rows from grid.
21+
*/
22+
23+
/**
24+
*@param {number[][]} grid
25+
*@return {number[]}
26+
*/
27+
vargoodSubsetofBinaryMatrix=function(grid){
28+
constrows=grid.length;
29+
constcols=grid[0].length;
30+
constrowMasks=newMap();
31+
32+
for(leti=0;i<rows;i++){
33+
letmask=0;
34+
for(letj=0;j<cols;j++){
35+
mask|=grid[i][j]<<j;
36+
}
37+
if(mask===0)return[i];
38+
rowMasks.set(mask,i);
39+
}
40+
41+
for(const[mask1,i]ofrowMasks){
42+
for(const[mask2,j]ofrowMasks){
43+
if((mask1&mask2)===0)return[Math.min(i,j),Math.max(i,j)];
44+
}
45+
}
46+
47+
return[];
48+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp