-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
78def55
commit 6489d31
Showing
3 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/page-22/2352. Equal Row and Column Pairs/equalPairs.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { equalPairs } from './equalPairs'; | ||
|
||
describe('2352. Equal Row and Column Pairs', () => { | ||
test('equalPairs', () => { | ||
expect( | ||
equalPairs([ | ||
[3, 2, 1], | ||
[1, 7, 6], | ||
[2, 7, 7], | ||
]), | ||
).toBe(1); | ||
|
||
expect( | ||
equalPairs([ | ||
[3, 1, 2, 2], | ||
[1, 4, 4, 5], | ||
[2, 4, 2, 2], | ||
[2, 4, 2, 2], | ||
]), | ||
).toBe(3); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
src/page-22/2352. Equal Row and Column Pairs/equalPairs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
type EqualPairs = (grid: number[][]) => number; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const equalPairs: EqualPairs = (grid) => { | ||
const rowMap = new Map<string, number>(); | ||
|
||
// Store each row in the map | ||
for (let i = 0; i < grid.length; i++) { | ||
const row = grid[i].join(); | ||
rowMap.set(row, (rowMap.get(row) || 0) + 1); | ||
} | ||
|
||
let count = 0; | ||
|
||
// Check each column against the stored rows | ||
for (let j = 0; j < grid.length; j++) { | ||
const col = grid.map((row) => row[j]).join(); | ||
count += rowMap.get(col) || 0; | ||
} | ||
|
||
return count; | ||
}; |