Skip to content

Commit

Permalink
add: 找出叠涂元素
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Dec 1, 2023
1 parent b0b8a3a commit 0569f33
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 17 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ C++标准库提供的数据结构实在是太多了,参考[C++标准库头文

### 数组/队列/集合/映射

- [找出叠涂元素](src/array/first_completely_painted_row_or_column.cpp) [数组, 哈希表, 矩阵]

- LeetCode 2661. 找出叠涂元素 <https://leetcode.cn/problems/first-completely-painted-row-or-column>

- [打家劫舍](src/array/house_robber.cpp) [数组, 动态规划]

- LeetCode 198. 打家劫舍 <https://leetcode.cn/problems/house-robber>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions src/array/first_completely_painted_row_or_column.cpp
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;
}
};
24 changes: 8 additions & 16 deletions src/lib/list_node.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
#include <list_node.hpp>

// 创建链表,输入数组和数组长度,返回链表头指针
ListNode *createLinkedList(int arr[], int n)
{
ListNode *createLinkedList(int arr[], int n) {
if (n == 0)
return nullptr;

ListNode *head = new ListNode(arr[0]);

ListNode *curNode = head;
for (int i = 1; i < n; i++)
{
for (int i = 1; i < n; i++) {
curNode->next = new ListNode(arr[i]);
curNode = curNode->next;
}
return head;
}

// 将链表转换为字符串输出
string listNodeToString(ListNode *head)
{
string listNodeToString(ListNode *head) {
string str = "";
ListNode *curNode = head;
while (curNode != nullptr)
{
while (curNode != nullptr) {
str.append(to_string(curNode->val));
str.append(" -> ");
curNode = curNode->next;
Expand All @@ -33,23 +29,19 @@ string listNodeToString(ListNode *head)
}

// 输出链表
void printLinkedList(ListNode *head)
{
void printLinkedList(ListNode *head) {
ListNode *curNode = head;
while (curNode != nullptr)
{
while (curNode != nullptr) {
cout << curNode->val << " -> ";
curNode = curNode->next;
}
cout << "NULL" << endl;
}

// 删除链表
void deleteLinkedList(ListNode *head)
{
void deleteLinkedList(ListNode *head) {
ListNode *curNode = head;
while (curNode != nullptr)
{
while (curNode != nullptr) {
ListNode *delNode = curNode;
curNode = curNode->next;
delete delNode;
Expand Down
20 changes: 20 additions & 0 deletions test/array/first_completely_painted_row_or_column_test.cpp
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);
}
2 changes: 1 addition & 1 deletion test/lib/lib_test.cpp
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>

Expand Down

0 comments on commit 0569f33

Please sign in to comment.