Skip to content

feat: add solutions to lc problem: No.0944 #3211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 42 additions & 18 deletions solution/0900-0999/0944.Delete Columns to Make Sorted/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,15 @@ cae</pre>

<!-- solution:start -->

### 方法一
### 方法一:逐列比较

我们记字符串数组 $\textit{strs}$ 的行数为 $n$,列数为 $m$。

遍历每一列,从第二行开始,逐列比较当前行和上一行的字符,如果当前行的字符小于上一行的字符,说明当前列不是按字典序非严格递增排列的,需要删除,结果加一,然后跳出内层循环。

最后返回结果即可。

时间复杂度 $O(L)$,其中 $L$ 为字符串数组 $\textit{strs}$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -129,28 +137,26 @@ class Solution {
class Solution {
public:
int minDeletionSize(vector<string>& strs) {
int n = strs.size();
int m = strs[0].size();
int res = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n - 1; ++j) {
if (strs[j][i] > strs[j + 1][i]) {
res++;
int m = strs[0].size(), n = strs.size();
int ans = 0;
for (int j = 0; j < m; ++j) {
for (int i = 1; i < n; ++i) {
if (strs[i][j] < strs[i - 1][j]) {
++ans;
break;
}
}
}
return res;
return ans;
}
};
```

#### Go

```go
func minDeletionSize(strs []string) int {
func minDeletionSize(strs []string) (ans int) {
m, n := len(strs[0]), len(strs)
ans := 0
for j := 0; j < m; j++ {
for i := 1; i < n; i++ {
if strs[i][j] < strs[i-1][j] {
Expand All @@ -159,7 +165,25 @@ func minDeletionSize(strs []string) int {
}
}
}
return ans
return
}
```

#### TypeScript

```ts
function minDeletionSize(strs: string[]): number {
const [m, n] = [strs[0].length, strs.length];
let ans = 0;
for (let j = 0; j < m; ++j) {
for (let i = 1; i < n; ++i) {
if (strs[i][j] < strs[i - 1][j]) {
++ans;
break;
}
}
}
return ans;
}
```

Expand All @@ -170,16 +194,16 @@ impl Solution {
pub fn min_deletion_size(strs: Vec<String>) -> i32 {
let n = strs.len();
let m = strs[0].len();
let mut res = 0;
for i in 0..m {
for j in 1..n {
if strs[j - 1].as_bytes()[i] > strs[j].as_bytes()[i] {
res += 1;
let mut ans = 0;
for j in 0..m {
for i in 1..n {
if strs[i].as_bytes()[j] < strs[i - 1].as_bytes()[j] {
ans += 1;
break;
}
}
}
res
ans
}
}
```
Expand Down
60 changes: 42 additions & 18 deletions solution/0900-0999/0944.Delete Columns to Make Sorted/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,15 @@ All 3 columns are not sorted, so you will delete all 3.

<!-- solution:start -->

### Solution 1
### Solution 1: Compare Column by Column

We denote the number of rows in the string array $\textit{strs}$ as $n$, and the number of columns as $m$.

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.

Finally, we return the result.

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)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -132,28 +140,26 @@ class Solution {
class Solution {
public:
int minDeletionSize(vector<string>& strs) {
int n = strs.size();
int m = strs[0].size();
int res = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n - 1; ++j) {
if (strs[j][i] > strs[j + 1][i]) {
res++;
int m = strs[0].size(), n = strs.size();
int ans = 0;
for (int j = 0; j < m; ++j) {
for (int i = 1; i < n; ++i) {
if (strs[i][j] < strs[i - 1][j]) {
++ans;
break;
}
}
}
return res;
return ans;
}
};
```

#### Go

```go
func minDeletionSize(strs []string) int {
func minDeletionSize(strs []string) (ans int) {
m, n := len(strs[0]), len(strs)
ans := 0
for j := 0; j < m; j++ {
for i := 1; i < n; i++ {
if strs[i][j] < strs[i-1][j] {
Expand All @@ -162,7 +168,25 @@ func minDeletionSize(strs []string) int {
}
}
}
return ans
return
}
```

#### TypeScript

```ts
function minDeletionSize(strs: string[]): number {
const [m, n] = [strs[0].length, strs.length];
let ans = 0;
for (let j = 0; j < m; ++j) {
for (let i = 1; i < n; ++i) {
if (strs[i][j] < strs[i - 1][j]) {
++ans;
break;
}
}
}
return ans;
}
```

Expand All @@ -173,16 +197,16 @@ impl Solution {
pub fn min_deletion_size(strs: Vec<String>) -> i32 {
let n = strs.len();
let m = strs[0].len();
let mut res = 0;
for i in 0..m {
for j in 1..n {
if strs[j - 1].as_bytes()[i] > strs[j].as_bytes()[i] {
res += 1;
let mut ans = 0;
for j in 0..m {
for i in 1..n {
if strs[i].as_bytes()[j] < strs[i - 1].as_bytes()[j] {
ans += 1;
break;
}
}
}
res
ans
}
}
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
class Solution {
public:
int minDeletionSize(vector<string>& strs) {
int n = strs.size();
int m = strs[0].size();
int res = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n - 1; ++j) {
if (strs[j][i] > strs[j + 1][i]) {
res++;
int m = strs[0].size(), n = strs.size();
int ans = 0;
for (int j = 0; j < m; ++j) {
for (int i = 1; i < n; ++i) {
if (strs[i][j] < strs[i - 1][j]) {
++ans;
break;
}
}
}
return res;
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
func minDeletionSize(strs []string) int {
func minDeletionSize(strs []string) (ans int) {
m, n := len(strs[0]), len(strs)
ans := 0
for j := 0; j < m; j++ {
for i := 1; i < n; i++ {
if strs[i][j] < strs[i-1][j] {
Expand All @@ -9,5 +8,5 @@ func minDeletionSize(strs []string) int {
}
}
}
return ans
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ impl Solution {
pub fn min_deletion_size(strs: Vec<String>) -> i32 {
let n = strs.len();
let m = strs[0].len();
let mut res = 0;
for i in 0..m {
for j in 1..n {
if strs[j - 1].as_bytes()[i] > strs[j].as_bytes()[i] {
res += 1;
let mut ans = 0;
for j in 0..m {
for i in 1..n {
if strs[i].as_bytes()[j] < strs[i - 1].as_bytes()[j] {
ans += 1;
break;
}
}
}
res
ans
}
}
13 changes: 13 additions & 0 deletions solution/0900-0999/0944.Delete Columns to Make Sorted/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function minDeletionSize(strs: string[]): number {
const [m, n] = [strs[0].length, strs.length];
let ans = 0;
for (let j = 0; j < m; ++j) {
for (let i = 1; i < n; ++i) {
if (strs[i][j] < strs[i - 1][j]) {
++ans;
break;
}
}
}
return ans;
}
Loading