Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 85429f1

Browse files
feat: add solutions to lc problem: No.0960 (#4294)
No.0960.Delete Columns to Make Sorted III
1 parent c4712ae commit 85429f1

File tree

10 files changed

+259
-152
lines changed

10 files changed

+259
-152
lines changed

‎solution/0900-0999/0960.Delete Columns to Make Sorted III/README.md‎

Lines changed: 87 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,15 @@ tags:
7575

7676
<!-- solution:start -->
7777

78-
### 方法一
78+
### 方法一:动态规划
79+
80+
我们定义 $f[i]$ 表示以第 $i$ 列结尾的最长不下降子序列的长度,初始时 $f[i] = 1,ドル答案即为 $n - \max(f)$。
81+
82+
考虑计算 $f[i],ドル我们可以枚举 $j < i,ドル如果对于所有的字符串 $s,ドル有 $s[j] \le s[i],ドル那么 $f[i] = \max(f[i], f[j] + 1)$。
83+
84+
最后,我们返回 $n - \max(f)$。
85+
86+
时间复杂度 $O(n^2 \times m),ドル空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是数组 $\textit{strs}$ 每个字符串的长度和数组的长度。
7987

8088
<!-- tabs:start -->
8189

@@ -85,12 +93,12 @@ tags:
8593
class Solution:
8694
def minDeletionSize(self, strs: List[str]) -> int:
8795
n = len(strs[0])
88-
dp = [1] * n
89-
for i in range(1, n):
96+
f = [1] * n
97+
for i in range(n):
9098
for j in range(i):
9199
if all(s[j] <= s[i] for s in strs):
92-
dp[i] = max(dp[i], dp[j] + 1)
93-
return n - max(dp)
100+
f[i] = max(f[i], f[j] + 1)
101+
return n - max(f)
94102
```
95103

96104
#### Java
@@ -99,27 +107,23 @@ class Solution:
99107
class Solution {
100108
public int minDeletionSize(String[] strs) {
101109
int n = strs[0].length();
102-
int[] dp = new int[n];
103-
Arrays.fill(dp, 1);
104-
int mx = 1;
110+
int[] f = new int[n];
111+
Arrays.fill(f, 1);
105112
for (int i = 1; i < n; ++i) {
106113
for (int j = 0; j < i; ++j) {
107-
if (check(i, j, strs)) {
108-
dp[i] = Math.max(dp[i], dp[j] + 1);
114+
boolean ok = true;
115+
for (String s : strs) {
116+
if (s.charAt(j) > s.charAt(i)) {
117+
ok = false;
118+
break;
119+
}
120+
}
121+
if (ok) {
122+
f[i] = Math.max(f[i], f[j] + 1);
109123
}
110-
}
111-
mx = Math.max(mx, dp[i]);
112-
}
113-
return n - mx;
114-
}
115-
116-
private boolean check(int i, int j, String[] strs) {
117-
for (String s : strs) {
118-
if (s.charAt(i) < s.charAt(j)) {
119-
return false;
120124
}
121125
}
122-
return true;
126+
return n -Arrays.stream(f).max().getAsInt();
123127
}
124128
}
125129
```
@@ -131,24 +135,15 @@ class Solution {
131135
public:
132136
int minDeletionSize(vector<string>& strs) {
133137
int n = strs[0].size();
134-
vector<int> dp(n, 1);
135-
int mx = 1;
138+
vector<int> f(n, 1);
136139
for (int i = 1; i < n; ++i) {
137140
for (int j = 0; j < i; ++j) {
138-
if (check(i, j, strs)) {
139-
dp[i] = max(dp[i], dp[j] + 1);
141+
if (ranges::all_of(strs, [&](const string& s) { return s[j] <= s[i]; })) {
142+
f[i] = max(f[i], f[j] + 1);
140143
}
141144
}
142-
mx = max(mx, dp[i]);
143145
}
144-
return n - mx;
145-
}
146-
147-
bool check(int i, int j, vector<string>& strs) {
148-
for (string& s : strs)
149-
if (s[i] < s[j])
150-
return false;
151-
return true;
146+
return n - ranges::max(f);
152147
}
153148
};
154149
```
@@ -158,27 +153,70 @@ public:
158153
```go
159154
func minDeletionSize(strs []string) int {
160155
n := len(strs[0])
161-
dp := make([]int, n)
162-
mx := 1
163-
dp[0] = 1
164-
check := func(i, j int) bool {
165-
for _, s := range strs {
166-
if s[i] < s[j] {
167-
return false
168-
}
169-
}
170-
return true
156+
f := make([]int, n)
157+
for i := range f {
158+
f[i] = 1
171159
}
172160
for i := 1; i < n; i++ {
173-
dp[i] = 1
174161
for j := 0; j < i; j++ {
175-
if check(i, j) {
176-
dp[i] = max(dp[i], dp[j]+1)
162+
ok := true
163+
for _, s := range strs {
164+
if s[j] > s[i] {
165+
ok = false
166+
break
167+
}
168+
}
169+
if ok {
170+
f[i] = max(f[i], f[j]+1)
177171
}
178172
}
179-
mx = max(mx, dp[i])
180173
}
181-
return n - mx
174+
return n - slices.Max(f)
175+
}
176+
```
177+
178+
#### TypeScript
179+
180+
```ts
181+
function minDeletionSize(strs: string[]): number {
182+
const n = strs[0].length;
183+
const f: number[] = Array(n).fill(1);
184+
for (let i = 1; i < n; i++) {
185+
for (let j = 0; j < i; j++) {
186+
let ok = true;
187+
for (const s of strs) {
188+
if (s[j] > s[i]) {
189+
ok = false;
190+
break;
191+
}
192+
}
193+
if (ok) {
194+
f[i] = Math.max(f[i], f[j] + 1);
195+
}
196+
}
197+
}
198+
return n - Math.max(...f);
199+
}
200+
```
201+
202+
#### Rust
203+
204+
```rust
205+
impl Solution {
206+
pub fn min_deletion_size(strs: Vec<String>) -> i32 {
207+
let n = strs[0].len();
208+
let mut f = vec![1; n];
209+
210+
for i in 1..n {
211+
for j in 0..i {
212+
if strs.iter().all(|s| s.as_bytes()[j] <= s.as_bytes()[i]) {
213+
f[i] = f[i].max(f[j] + 1);
214+
}
215+
}
216+
}
217+
218+
(n - *f.iter().max().unwrap()) as i32
219+
}
182220
}
183221
```
184222

‎solution/0900-0999/0960.Delete Columns to Make Sorted III/README_EN.md‎

Lines changed: 88 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,16 @@ Note that strs[0] &gt; strs[1] - the array strs is not necessarily in lexicograp
7272

7373
<!-- solution:start -->
7474

75-
### Solution 1
75+
### Solution 1: Dynamic Programming
76+
77+
We define $f[i]$ as the length of the longest non-decreasing subsequence ending at column $i$. Initially, $f[i] = 1,ドル and the final answer is $n - \max(f)$.
78+
79+
To compute $f[i],ドル we iterate over all $j < i$. If for all strings $s,ドル we have $s[j] \leq s[i],ドル then we update $f[i]$ as follows:
80+
$$ f[i] = \max(f[i], f[j] + 1) $$
81+
82+
Finally, we return $n - \max(f)$.
83+
84+
The time complexity is $O(n^2 \times m),ドル and the space complexity is $O(n),ドル where $n$ is the length of each string in the array $\textit{strs},ドル and $m$ is the number of strings in the array.
7685

7786
<!-- tabs:start -->
7887

@@ -82,12 +91,12 @@ Note that strs[0] &gt; strs[1] - the array strs is not necessarily in lexicograp
8291
class Solution:
8392
def minDeletionSize(self, strs: List[str]) -> int:
8493
n = len(strs[0])
85-
dp = [1] * n
86-
for i in range(1, n):
94+
f = [1] * n
95+
for i in range(n):
8796
for j in range(i):
8897
if all(s[j] <= s[i] for s in strs):
89-
dp[i] = max(dp[i], dp[j] + 1)
90-
return n - max(dp)
98+
f[i] = max(f[i], f[j] + 1)
99+
return n - max(f)
91100
```
92101

93102
#### Java
@@ -96,27 +105,23 @@ class Solution:
96105
class Solution {
97106
public int minDeletionSize(String[] strs) {
98107
int n = strs[0].length();
99-
int[] dp = new int[n];
100-
Arrays.fill(dp, 1);
101-
int mx = 1;
108+
int[] f = new int[n];
109+
Arrays.fill(f, 1);
102110
for (int i = 1; i < n; ++i) {
103111
for (int j = 0; j < i; ++j) {
104-
if (check(i, j, strs)) {
105-
dp[i] = Math.max(dp[i], dp[j] + 1);
112+
boolean ok = true;
113+
for (String s : strs) {
114+
if (s.charAt(j) > s.charAt(i)) {
115+
ok = false;
116+
break;
117+
}
118+
}
119+
if (ok) {
120+
f[i] = Math.max(f[i], f[j] + 1);
106121
}
107-
}
108-
mx = Math.max(mx, dp[i]);
109-
}
110-
return n - mx;
111-
}
112-
113-
private boolean check(int i, int j, String[] strs) {
114-
for (String s : strs) {
115-
if (s.charAt(i) < s.charAt(j)) {
116-
return false;
117122
}
118123
}
119-
return true;
124+
return n -Arrays.stream(f).max().getAsInt();
120125
}
121126
}
122127
```
@@ -128,24 +133,15 @@ class Solution {
128133
public:
129134
int minDeletionSize(vector<string>& strs) {
130135
int n = strs[0].size();
131-
vector<int> dp(n, 1);
132-
int mx = 1;
136+
vector<int> f(n, 1);
133137
for (int i = 1; i < n; ++i) {
134138
for (int j = 0; j < i; ++j) {
135-
if (check(i, j, strs)) {
136-
dp[i] = max(dp[i], dp[j] + 1);
139+
if (ranges::all_of(strs, [&](const string& s) { return s[j] <= s[i]; })) {
140+
f[i] = max(f[i], f[j] + 1);
137141
}
138142
}
139-
mx = max(mx, dp[i]);
140143
}
141-
return n - mx;
142-
}
143-
144-
bool check(int i, int j, vector<string>& strs) {
145-
for (string& s : strs)
146-
if (s[i] < s[j])
147-
return false;
148-
return true;
144+
return n - ranges::max(f);
149145
}
150146
};
151147
```
@@ -155,27 +151,70 @@ public:
155151
```go
156152
func minDeletionSize(strs []string) int {
157153
n := len(strs[0])
158-
dp := make([]int, n)
159-
mx := 1
160-
dp[0] = 1
161-
check := func(i, j int) bool {
162-
for _, s := range strs {
163-
if s[i] < s[j] {
164-
return false
165-
}
166-
}
167-
return true
154+
f := make([]int, n)
155+
for i := range f {
156+
f[i] = 1
168157
}
169158
for i := 1; i < n; i++ {
170-
dp[i] = 1
171159
for j := 0; j < i; j++ {
172-
if check(i, j) {
173-
dp[i] = max(dp[i], dp[j]+1)
160+
ok := true
161+
for _, s := range strs {
162+
if s[j] > s[i] {
163+
ok = false
164+
break
165+
}
166+
}
167+
if ok {
168+
f[i] = max(f[i], f[j]+1)
174169
}
175170
}
176-
mx = max(mx, dp[i])
177171
}
178-
return n - mx
172+
return n - slices.Max(f)
173+
}
174+
```
175+
176+
#### TypeScript
177+
178+
```ts
179+
function minDeletionSize(strs: string[]): number {
180+
const n = strs[0].length;
181+
const f: number[] = Array(n).fill(1);
182+
for (let i = 1; i < n; i++) {
183+
for (let j = 0; j < i; j++) {
184+
let ok = true;
185+
for (const s of strs) {
186+
if (s[j] > s[i]) {
187+
ok = false;
188+
break;
189+
}
190+
}
191+
if (ok) {
192+
f[i] = Math.max(f[i], f[j] + 1);
193+
}
194+
}
195+
}
196+
return n - Math.max(...f);
197+
}
198+
```
199+
200+
#### Rust
201+
202+
```rust
203+
impl Solution {
204+
pub fn min_deletion_size(strs: Vec<String>) -> i32 {
205+
let n = strs[0].len();
206+
let mut f = vec![1; n];
207+
208+
for i in 1..n {
209+
for j in 0..i {
210+
if strs.iter().all(|s| s.as_bytes()[j] <= s.as_bytes()[i]) {
211+
f[i] = f[i].max(f[j] + 1);
212+
}
213+
}
214+
}
215+
216+
(n - *f.iter().max().unwrap()) as i32
217+
}
179218
}
180219
```
181220

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /