You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0900-0999/0944.Delete Columns to Make Sorted/README_EN.md
+42-18
Original file line number
Diff line number
Diff line change
@@ -87,7 +87,15 @@ All 3 columns are not sorted, so you will delete all 3.
87
87
88
88
<!-- solution:start -->
89
89
90
-
### Solution 1
90
+
### Solution 1: Compare Column by Column
91
+
92
+
We denote the number of rows in the string array $\textit{strs}$ as $n$, and the number of columns as $m$.
93
+
94
+
We traverse each column, starting from the second row, and compare the character of the current row with that of the previous row column by column. If the character of the current row is less than that of the previous row, it indicates that the current column is not arranged in non-strictly increasing lexicographical order, and we need to delete it, incrementing the result by one, then break out of the inner loop.
95
+
96
+
Finally, we return the result.
97
+
98
+
The time complexity is $O(L)$, where $L$ is the total length of the strings in the array $\textit{strs}$. The space complexity is $O(1)$.
91
99
92
100
<!-- tabs:start -->
93
101
@@ -132,28 +140,26 @@ class Solution {
132
140
classSolution {
133
141
public:
134
142
int minDeletionSize(vector<string>& strs) {
135
-
int n = strs.size();
136
-
int m = strs[0].size();
137
-
int res = 0;
138
-
for (int i = 0; i < m; ++i) {
139
-
for (int j = 0; j < n - 1; ++j) {
140
-
if (strs[j][i] > strs[j + 1][i]) {
141
-
res++;
143
+
int m = strs[0].size(), n = strs.size();
144
+
int ans = 0;
145
+
for (int j = 0; j < m; ++j) {
146
+
for (int i = 1; i < n; ++i) {
147
+
if (strs[i][j] < strs[i - 1][j]) {
148
+
++ans;
142
149
break;
143
150
}
144
151
}
145
152
}
146
-
return res;
153
+
return ans;
147
154
}
148
155
};
149
156
```
150
157
151
158
#### Go
152
159
153
160
```go
154
-
func minDeletionSize(strs []string) int {
161
+
func minDeletionSize(strs []string) (ans int) {
155
162
m, n := len(strs[0]), len(strs)
156
-
ans := 0
157
163
for j := 0; j < m; j++ {
158
164
for i := 1; i < n; i++ {
159
165
if strs[i][j] < strs[i-1][j] {
@@ -162,7 +168,25 @@ func minDeletionSize(strs []string) int {
0 commit comments