-
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
Showing
6 changed files
with
69 additions
and
17 deletions.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,36 @@ | ||
// 找出叠涂元素 | ||
// https://leetcode.cn/problems/first-completely-painted-row-or-column | ||
// INLINE ../../images/array/first_completely_painted_row_or_column.jpeg | ||
|
||
#include <headers.hpp> | ||
|
||
class Solution { | ||
public: | ||
int firstCompleteIndex(vector<int> &arr, vector<vector<int>> &mat) { | ||
int n = mat.size(); | ||
int m = mat[0].size(); | ||
unordered_map<int, pair<int, int>> mp; | ||
vector<int> rowCnt(n, 0); | ||
vector<int> colCnt(m, 0); | ||
int completeIndex = -1; | ||
for (int i = 0; i < n; ++i) { | ||
for (int j = 0; j < m; ++j) { | ||
mp[mat[i][j]] = {i, j}; | ||
} | ||
} | ||
for (int i = 0; i < arr.size(); ++i) { | ||
auto &v = mp[arr[i]]; | ||
++rowCnt[v.first]; | ||
if (rowCnt[v.first] == m) { | ||
completeIndex = i; | ||
break; | ||
} | ||
++colCnt[v.second]; | ||
if (colCnt[v.second] == n) { | ||
completeIndex = i; | ||
break; | ||
} | ||
} | ||
return completeIndex; | ||
} | ||
}; |
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
20 changes: 20 additions & 0 deletions
20
test/array/first_completely_painted_row_or_column_test.cpp
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,20 @@ | ||
#include <array/first_completely_painted_row_or_column.cpp> | ||
|
||
TEST(找出叠涂元素, firstCompleteIndex) { | ||
Solution solution; | ||
// 示例 1: | ||
// 输入:arr = [1,3,4,2], mat = [[1,4],[2,3]] | ||
// 输出:2 | ||
// 解释:遍历如上图所示,arr[2] 在矩阵中的第一行或第二列上都被涂色。 | ||
vector<int> arr1 = {1, 3, 4, 2}; | ||
vector<vector<int>> mat1 = {{1, 4}, {2, 3}}; | ||
EXPECT_EQ(solution.firstCompleteIndex(arr1, mat1), 2); | ||
|
||
// 示例 2: | ||
// 输入:arr = [2,8,7,4,1,3,5,6,9], mat = [[3,2,5],[1,4,6],[8,7,9]] | ||
// 输出:3 | ||
// 解释:遍历如上图所示,arr[3] 在矩阵中的第二列上都被涂色。 | ||
vector<int> arr2 = {2, 8, 7, 4, 1, 3, 5, 6, 9}; | ||
vector<vector<int>> mat2 = {{3, 2, 5}, {1, 4, 6}, {8, 7, 9}}; | ||
EXPECT_EQ(solution.firstCompleteIndex(arr2, mat2), 3); | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// 执行编译时间:2023-09-17 15:52:26 | ||
// 执行编译时间:2023-12-01 16:35:53 | ||
#include <gtest/gtest.h> | ||
#include <lib.hpp> | ||
|
||
|