Skip to content

Commit daf777b

Browse files
authored
feat: add solutions to lc problem: No.3071 (doocs#2407)
No.3071.Minimum Operations to Write the Letter Y on a Grid
1 parent 9fb8d56 commit daf777b

File tree

7 files changed

+392
-8
lines changed

7 files changed

+392
-8
lines changed

solution/3000-3099/3071.Minimum Operations to Write the Letter Y on a Grid/README.md

+133-4
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,153 @@
6161

6262
## 解法
6363

64-
### 方法一
64+
### 方法一:计数
65+
66+
我们用两个长度为 $3$ 的数组 `cnt1``cnt2` 分别记录属于 `Y` 的单元格和不属于 `Y` 的单元格的值的个数。然后我们枚举 `i``j`,分别表示属于 `Y` 的单元格和不属于 `Y` 的单元格的值,计算出最少操作次数。
67+
68+
时间复杂度 $O(n^2)$,其中 $n$ 是矩阵的大小。空间复杂度 $O(1)$。
6569

6670
<!-- tabs:start -->
6771

6872
```python
69-
73+
class Solution:
74+
def minimumOperationsToWriteY(self, grid: List[List[int]]) -> int:
75+
n = len(grid)
76+
cnt1 = Counter()
77+
cnt2 = Counter()
78+
for i, row in enumerate(grid):
79+
for j, x in enumerate(row):
80+
a = i == j and i <= n // 2
81+
b = i + j == n - 1 and i <= n // 2
82+
c = j == n // 2 and i >= n // 2
83+
if a or b or c:
84+
cnt1[x] += 1
85+
else:
86+
cnt2[x] += 1
87+
return min(
88+
n * n - cnt1[i] - cnt2[j] for i in range(3) for j in range(3) if i != j
89+
)
7090
```
7191

7292
```java
73-
93+
class Solution {
94+
public int minimumOperationsToWriteY(int[][] grid) {
95+
int n = grid.length;
96+
int[] cnt1 = new int[3];
97+
int[] cnt2 = new int[3];
98+
for (int i = 0; i < n; ++i) {
99+
for (int j = 0; j < n; ++j) {
100+
boolean a = i == j && i <= n / 2;
101+
boolean b = i + j == n - 1 && i <= n / 2;
102+
boolean c = j == n / 2 && i >= n / 2;
103+
if (a || b || c) {
104+
++cnt1[grid[i][j]];
105+
} else {
106+
++cnt2[grid[i][j]];
107+
}
108+
}
109+
}
110+
int ans = n * n;
111+
for (int i = 0; i < 3; ++i) {
112+
for (int j = 0; j < 3; ++j) {
113+
if (i != j) {
114+
ans = Math.min(ans, n * n - cnt1[i] - cnt2[j]);
115+
}
116+
}
117+
}
118+
return ans;
119+
}
120+
}
74121
```
75122

76123
```cpp
77-
124+
class Solution {
125+
public:
126+
int minimumOperationsToWriteY(vector<vector<int>>& grid) {
127+
int n = grid.size();
128+
int cnt1[3]{};
129+
int cnt2[3]{};
130+
for (int i = 0; i < n; ++i) {
131+
for (int j = 0; j < n; ++j) {
132+
bool a = i == j && i <= n / 2;
133+
bool b = i + j == n - 1 && i <= n / 2;
134+
bool c = j == n / 2 && i >= n / 2;
135+
if (a || b || c) {
136+
++cnt1[grid[i][j]];
137+
} else {
138+
++cnt2[grid[i][j]];
139+
}
140+
}
141+
}
142+
int ans = n * n;
143+
for (int i = 0; i < 3; ++i) {
144+
for (int j = 0; j < 3; ++j) {
145+
if (i != j) {
146+
ans = min(ans, n * n - cnt1[i] - cnt2[j]);
147+
}
148+
}
149+
}
150+
return ans;
151+
}
152+
};
78153
```
79154
80155
```go
156+
func minimumOperationsToWriteY(grid [][]int) int {
157+
n := len(grid)
158+
cnt1 := [3]int{}
159+
cnt2 := [3]int{}
160+
for i, row := range grid {
161+
for j, x := range row {
162+
a := i == j && i <= n/2
163+
b := i+j == n-1 && i <= n/2
164+
c := j == n/2 && i >= n/2
165+
if a || b || c {
166+
cnt1[x]++
167+
} else {
168+
cnt2[x]++
169+
}
170+
}
171+
}
172+
ans := n * n
173+
for i := 0; i < 3; i++ {
174+
for j := 0; j < 3; j++ {
175+
if i != j {
176+
ans = min(ans, n*n-cnt1[i]-cnt2[j])
177+
}
178+
}
179+
}
180+
return ans
181+
}
182+
```
81183

184+
```ts
185+
function minimumOperationsToWriteY(grid: number[][]): number {
186+
const n = grid.length;
187+
const cnt1: number[] = Array(3).fill(0);
188+
const cnt2: number[] = Array(3).fill(0);
189+
for (let i = 0; i < n; ++i) {
190+
for (let j = 0; j < n; ++j) {
191+
const a = i === j && i <= n >> 1;
192+
const b = i + j === n - 1 && i <= n >> 1;
193+
const c = j === n >> 1 && i >= n >> 1;
194+
if (a || b || c) {
195+
++cnt1[grid[i][j]];
196+
} else {
197+
++cnt2[grid[i][j]];
198+
}
199+
}
200+
}
201+
let ans = n * n;
202+
for (let i = 0; i < 3; ++i) {
203+
for (let j = 0; j < 3; ++j) {
204+
if (i !== j) {
205+
ans = Math.min(ans, n * n - cnt1[i] - cnt2[j]);
206+
}
207+
}
208+
}
209+
return ans;
210+
}
82211
```
83212

84213
<!-- tabs:end -->

solution/3000-3099/3071.Minimum Operations to Write the Letter Y on a Grid/README_EN.md

+133-4
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,153 @@ It can be shown that 12 is the minimum number of operations needed to write Y on
5656

5757
## Solutions
5858

59-
### Solution 1
59+
### Solution 1: Counting
60+
61+
We use two arrays of length 3, `cnt1` and `cnt2`, to record the counts of cell values that belong to `Y` and do not belong to `Y`, respectively. Then we enumerate `i` and `j`, which represent the values of cells that belong to `Y` and do not belong to `Y`, respectively, to calculate the minimum number of operations.
62+
63+
The time complexity is $O(n^2)$, where $n$ is the size of the matrix. The space complexity is $O(1)$.
6064

6165
<!-- tabs:start -->
6266

6367
```python
64-
68+
class Solution:
69+
def minimumOperationsToWriteY(self, grid: List[List[int]]) -> int:
70+
n = len(grid)
71+
cnt1 = Counter()
72+
cnt2 = Counter()
73+
for i, row in enumerate(grid):
74+
for j, x in enumerate(row):
75+
a = i == j and i <= n // 2
76+
b = i + j == n - 1 and i <= n // 2
77+
c = j == n // 2 and i >= n // 2
78+
if a or b or c:
79+
cnt1[x] += 1
80+
else:
81+
cnt2[x] += 1
82+
return min(
83+
n * n - cnt1[i] - cnt2[j] for i in range(3) for j in range(3) if i != j
84+
)
6585
```
6686

6787
```java
68-
88+
class Solution {
89+
public int minimumOperationsToWriteY(int[][] grid) {
90+
int n = grid.length;
91+
int[] cnt1 = new int[3];
92+
int[] cnt2 = new int[3];
93+
for (int i = 0; i < n; ++i) {
94+
for (int j = 0; j < n; ++j) {
95+
boolean a = i == j && i <= n / 2;
96+
boolean b = i + j == n - 1 && i <= n / 2;
97+
boolean c = j == n / 2 && i >= n / 2;
98+
if (a || b || c) {
99+
++cnt1[grid[i][j]];
100+
} else {
101+
++cnt2[grid[i][j]];
102+
}
103+
}
104+
}
105+
int ans = n * n;
106+
for (int i = 0; i < 3; ++i) {
107+
for (int j = 0; j < 3; ++j) {
108+
if (i != j) {
109+
ans = Math.min(ans, n * n - cnt1[i] - cnt2[j]);
110+
}
111+
}
112+
}
113+
return ans;
114+
}
115+
}
69116
```
70117

71118
```cpp
72-
119+
class Solution {
120+
public:
121+
int minimumOperationsToWriteY(vector<vector<int>>& grid) {
122+
int n = grid.size();
123+
int cnt1[3]{};
124+
int cnt2[3]{};
125+
for (int i = 0; i < n; ++i) {
126+
for (int j = 0; j < n; ++j) {
127+
bool a = i == j && i <= n / 2;
128+
bool b = i + j == n - 1 && i <= n / 2;
129+
bool c = j == n / 2 && i >= n / 2;
130+
if (a || b || c) {
131+
++cnt1[grid[i][j]];
132+
} else {
133+
++cnt2[grid[i][j]];
134+
}
135+
}
136+
}
137+
int ans = n * n;
138+
for (int i = 0; i < 3; ++i) {
139+
for (int j = 0; j < 3; ++j) {
140+
if (i != j) {
141+
ans = min(ans, n * n - cnt1[i] - cnt2[j]);
142+
}
143+
}
144+
}
145+
return ans;
146+
}
147+
};
73148
```
74149
75150
```go
151+
func minimumOperationsToWriteY(grid [][]int) int {
152+
n := len(grid)
153+
cnt1 := [3]int{}
154+
cnt2 := [3]int{}
155+
for i, row := range grid {
156+
for j, x := range row {
157+
a := i == j && i <= n/2
158+
b := i+j == n-1 && i <= n/2
159+
c := j == n/2 && i >= n/2
160+
if a || b || c {
161+
cnt1[x]++
162+
} else {
163+
cnt2[x]++
164+
}
165+
}
166+
}
167+
ans := n * n
168+
for i := 0; i < 3; i++ {
169+
for j := 0; j < 3; j++ {
170+
if i != j {
171+
ans = min(ans, n*n-cnt1[i]-cnt2[j])
172+
}
173+
}
174+
}
175+
return ans
176+
}
177+
```
76178

179+
```ts
180+
function minimumOperationsToWriteY(grid: number[][]): number {
181+
const n = grid.length;
182+
const cnt1: number[] = Array(3).fill(0);
183+
const cnt2: number[] = Array(3).fill(0);
184+
for (let i = 0; i < n; ++i) {
185+
for (let j = 0; j < n; ++j) {
186+
const a = i === j && i <= n >> 1;
187+
const b = i + j === n - 1 && i <= n >> 1;
188+
const c = j === n >> 1 && i >= n >> 1;
189+
if (a || b || c) {
190+
++cnt1[grid[i][j]];
191+
} else {
192+
++cnt2[grid[i][j]];
193+
}
194+
}
195+
}
196+
let ans = n * n;
197+
for (let i = 0; i < 3; ++i) {
198+
for (let j = 0; j < 3; ++j) {
199+
if (i !== j) {
200+
ans = Math.min(ans, n * n - cnt1[i] - cnt2[j]);
201+
}
202+
}
203+
}
204+
return ans;
205+
}
77206
```
78207

79208
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
int minimumOperationsToWriteY(vector<vector<int>>& grid) {
4+
int n = grid.size();
5+
int cnt1[3]{};
6+
int cnt2[3]{};
7+
for (int i = 0; i < n; ++i) {
8+
for (int j = 0; j < n; ++j) {
9+
bool a = i == j && i <= n / 2;
10+
bool b = i + j == n - 1 && i <= n / 2;
11+
bool c = j == n / 2 && i >= n / 2;
12+
if (a || b || c) {
13+
++cnt1[grid[i][j]];
14+
} else {
15+
++cnt2[grid[i][j]];
16+
}
17+
}
18+
}
19+
int ans = n * n;
20+
for (int i = 0; i < 3; ++i) {
21+
for (int j = 0; j < 3; ++j) {
22+
if (i != j) {
23+
ans = min(ans, n * n - cnt1[i] - cnt2[j]);
24+
}
25+
}
26+
}
27+
return ans;
28+
}
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func minimumOperationsToWriteY(grid [][]int) int {
2+
n := len(grid)
3+
cnt1 := [3]int{}
4+
cnt2 := [3]int{}
5+
for i, row := range grid {
6+
for j, x := range row {
7+
a := i == j && i <= n/2
8+
b := i+j == n-1 && i <= n/2
9+
c := j == n/2 && i >= n/2
10+
if a || b || c {
11+
cnt1[x]++
12+
} else {
13+
cnt2[x]++
14+
}
15+
}
16+
}
17+
ans := n * n
18+
for i := 0; i < 3; i++ {
19+
for j := 0; j < 3; j++ {
20+
if i != j {
21+
ans = min(ans, n*n-cnt1[i]-cnt2[j])
22+
}
23+
}
24+
}
25+
return ans
26+
}

0 commit comments

Comments
 (0)