diff --git a/leetcode/2601-2700/2661.First-Completely-Painted-Row-or-Column/1.jpg b/leetcode/2601-2700/2661.First-Completely-Painted-Row-or-Column/1.jpg new file mode 100644 index 000000000..1606a3a45 Binary files /dev/null and b/leetcode/2601-2700/2661.First-Completely-Painted-Row-or-Column/1.jpg differ diff --git a/leetcode/2601-2700/2661.First-Completely-Painted-Row-or-Column/2.jpg b/leetcode/2601-2700/2661.First-Completely-Painted-Row-or-Column/2.jpg new file mode 100644 index 000000000..b2e43193c Binary files /dev/null and b/leetcode/2601-2700/2661.First-Completely-Painted-Row-or-Column/2.jpg differ diff --git a/leetcode/2601-2700/2661.First-Completely-Painted-Row-or-Column/README.md b/leetcode/2601-2700/2661.First-Completely-Painted-Row-or-Column/README.md new file mode 100644 index 000000000..169163cb8 --- /dev/null +++ b/leetcode/2601-2700/2661.First-Completely-Painted-Row-or-Column/README.md @@ -0,0 +1,35 @@ +# [2661.First Completely Painted Row or Column][title] + +## Description +You are given a **0-indexed** integer array `arr`, and an `m x n` integer **matrix** `mat`. `arr` and `mat` both contain **all** the integers in the range `[1, m * n]`. + +Go through each index `i` in `arr` starting from index `0` and paint the cell in `mat` containing the integer `arr[i]`. + +Return the smallest index `i` at which either a row or a column will be completely painted in `mat`. + +**Example 1:** + +![1](./1.jpg) + +``` +Input: arr = [1,3,4,2], mat = [[1,4],[2,3]] +Output: 2 +Explanation: The moves are shown in order, and both the first row and second column of the matrix become fully painted at arr[2]. +``` + +**EXample 2:** + +![2](./2.jpg) + +``` +Input: arr = [2,8,7,4,1,3,5,6,9], mat = [[3,2,5],[1,4,6],[8,7,9]] +Output: 3 +Explanation: The second column becomes fully painted at arr[3]. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/first-completely-painted-row-or-column +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2601-2700/2661.First-Completely-Painted-Row-or-Column/Solution.go b/leetcode/2601-2700/2661.First-Completely-Painted-Row-or-Column/Solution.go index d115ccf5e..572b4598c 100755 --- a/leetcode/2601-2700/2661.First-Completely-Painted-Row-or-Column/Solution.go +++ b/leetcode/2601-2700/2661.First-Completely-Painted-Row-or-Column/Solution.go @@ -1,5 +1,21 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(arr []int, mat [][]int) int { + m, n := len(mat), len(mat[0]) + rows, cols := make([]int, m), make([]int, n) + key := make(map[int][2]int) + for i := range m { + for j := range n { + key[mat[i][j]] = [2]int{i, j} + } + } + for i := range arr { + pos := key[arr[i]] + rows[pos[0]]++ + cols[pos[1]]++ + if rows[pos[0]] == n || cols[pos[1]] == m { + return i + } + } + return len(arr) - 1 } diff --git a/leetcode/2601-2700/2661.First-Completely-Painted-Row-or-Column/Solution_test.go b/leetcode/2601-2700/2661.First-Completely-Painted-Row-or-Column/Solution_test.go index 14ff50eb4..0f23ccc98 100755 --- a/leetcode/2601-2700/2661.First-Completely-Painted-Row-or-Column/Solution_test.go +++ b/leetcode/2601-2700/2661.First-Completely-Painted-Row-or-Column/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + arr []int + mat [][]int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 3, 4, 2}, [][]int{{1, 4}, {2, 3}}, 2}, + {"TestCase2", []int{2, 8, 7, 4, 1, 3, 5, 6, 9}, [][]int{{3, 2, 5}, {1, 4, 6}, {8, 7, 9}}, 3}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.arr, c.mat) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.arr, c.mat) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }