Skip to content

Commit 8287d98

Browse files
committed
leetcode
1 parent 7a40c4d commit 8287d98

File tree

4 files changed

+264
-0
lines changed

4 files changed

+264
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
3+
4+
5+
-* 944. Delete Columns to Make Sorted *-
6+
7+
You are given an array of n strings strs, all of the same length.
8+
9+
The strings can be arranged such that there is one on each line, making a grid. For example, strs = ["abc", "bce", "cae"] can be arranged as:
10+
11+
abc
12+
bce
13+
cae
14+
You want to delete the columns that are not sorted lexicographically. In the above example (0-indexed), columns 0 ('a', 'b', 'c') and 2 ('c', 'e', 'e') are sorted while column 1 ('b', 'c', 'a') is not, so you would delete column 1.
15+
16+
Return the number of columns that you will delete.
17+
18+
19+
20+
Example 1:
21+
22+
Input: strs = ["cba","daf","ghi"]
23+
Output: 1
24+
Explanation: The grid looks as follows:
25+
cba
26+
daf
27+
ghi
28+
Columns 0 and 2 are sorted, but column 1 is not, so you only need to delete 1 column.
29+
Example 2:
30+
31+
Input: strs = ["a","b"]
32+
Output: 0
33+
Explanation: The grid looks as follows:
34+
a
35+
b
36+
Column 0 is the only column and is sorted, so you will not delete any columns.
37+
Example 3:
38+
39+
Input: strs = ["zyx","wvu","tsr"]
40+
Output: 3
41+
Explanation: The grid looks as follows:
42+
zyx
43+
wvu
44+
tsr
45+
All 3 columns are not sorted, so you will delete all 3.
46+
47+
48+
Constraints:
49+
50+
n == strs.length
51+
1 <= n <= 100
52+
1 <= strs[i].length <= 1000
53+
strs[i] consists of lowercase English letters.
54+
55+
*/
56+
57+
import 'dart:collection';
58+
59+
class A {
60+
int minDeletionSize(List<String> strs) {
61+
int answer = 0;
62+
for (int i = 0; i < strs[0].length; i++) {
63+
for (int j = 0; j < strs.length - 1; j++) {
64+
if (strs[j + 1].codeUnitAt(i) < strs[j].codeUnitAt(i)) {
65+
answer++;
66+
break;
67+
}
68+
}
69+
}
70+
return answer;
71+
}
72+
}
73+
74+
class B {
75+
// O(n*m)
76+
int minDeletionSize(List<String> strs) {
77+
int deleted = 0;
78+
if (strs.length == 0) return deleted;
79+
int rowSize = strs[0].length;
80+
81+
for (int col = 0; col < rowSize; col++) {
82+
if (!isSorted(strs, col)) deleted++;
83+
}
84+
85+
return deleted;
86+
}
87+
88+
bool isSorted(List<String> strs, int col) {
89+
int N = strs.length;
90+
int prev = 0;
91+
for (int i = 0; i < N; i++) {
92+
int c = strs[i].codeUnitAt(col);
93+
//If out of order
94+
if (c < prev) return false;
95+
//update prev
96+
prev = c;
97+
}
98+
99+
return true; //In order the whole time
100+
}
101+
}
102+
103+
class C {
104+
int minDeletionSize(List<String> strs) {
105+
HashSet<int> hashSET = HashSet();
106+
for (int j = 0; j < strs[0].length; j++) hashSET.add(j);
107+
for (int i = 1; i < strs.length; i++) {
108+
HashSet<int> tmp = HashSet();
109+
tmp.addAll(hashSET);
110+
for (int k in hashSET)
111+
if (strs[i].codeUnitAt(k) < strs[i - 1].codeUnitAt(k)) tmp.remove(k);
112+
hashSET = tmp;
113+
}
114+
return strs[0].length - hashSET.length;
115+
}
116+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package main
2+
3+
// func minDeletionSize(strs []string) int {
4+
// var count int = 0
5+
// if len(strs) <= 1 {
6+
// return 0
7+
// }
8+
// for i := 0; i < len(strs[0]); i++ {
9+
// for j := 0; j < len(strs)-1; j++ {
10+
// if strs[j+1][i] < strs[j][i] {
11+
// count++
12+
// break
13+
// }
14+
// }
15+
// }
16+
// return count
17+
// }
18+
19+
func minDeletionSize(strs []string) int {
20+
var deleted int = 0
21+
if len(strs) == 0 {
22+
return deleted
23+
}
24+
var rowSize int = len(strs[0])
25+
for col := 0; col < rowSize; col++ {
26+
if !isSorted(strs, col) {
27+
deleted++
28+
}
29+
}
30+
return deleted
31+
}
32+
33+
func isSorted(strs []string, col int) bool {
34+
var size int = len(strs)
35+
var previous byte = 0
36+
for i := 0; i < size; i++ {
37+
var c byte = strs[i][col]
38+
if c < previous {
39+
return false
40+
}
41+
previous = c
42+
43+
}
44+
return true
45+
}
46+
47+
/*
48+
49+
int minDeletionSize(List<String> strs) {
50+
int deleted = 0;
51+
if (strs.length == 0) return deleted;
52+
int rowSize = strs[0].length;
53+
54+
for (int col = 0; col < rowSize; col++) {
55+
if (!isSorted(strs, col)) deleted++;
56+
}
57+
58+
return deleted;
59+
}
60+
61+
bool isSorted(List<String> strs, int col) {
62+
int N = strs.length;
63+
int prev = 0;
64+
for (int i = 0; i < N; i++) {
65+
int c = strs[i].codeUnitAt(col);
66+
//If out of order
67+
if (c < prev) return false;
68+
//update prev
69+
prev = c;
70+
}
71+
72+
return true; //In order the whole time
73+
}
74+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# 🔥 3 Approaches 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1 BRUTE FORCE
4+
5+
```dart
6+
class Solution {
7+
int minDeletionSize(List<String> strs) {
8+
int answer = 0;
9+
for (int i = 0; i < strs[0].length; i++) {
10+
for (int j = 0; j < strs.length - 1; j++) {
11+
if (strs[j + 1].codeUnitAt(i) < strs[j].codeUnitAt(i)) {
12+
answer++;
13+
break;
14+
}
15+
}
16+
}
17+
return answer;
18+
}
19+
}
20+
```
21+
22+
## Solution - 2 O(n * m)
23+
24+
```dart
25+
class Solution {
26+
int minDeletionSize(List<String> strs) {
27+
int deleted = 0;
28+
if (strs.length == 0) return deleted;
29+
int rowSize = strs[0].length;
30+
31+
for (int col = 0; col < rowSize; col++) {
32+
if (!isSorted(strs, col)) deleted++;
33+
}
34+
35+
return deleted;
36+
}
37+
38+
bool isSorted(List<String> strs, int col) {
39+
int N = strs.length;
40+
int prev = 0;
41+
for (int i = 0; i < N; i++) {
42+
int c = strs[i].codeUnitAt(col);
43+
//If out of order
44+
if (c < prev) return false;
45+
//update prev
46+
prev = c;
47+
}
48+
49+
return true; //In order the whole time
50+
}
51+
}
52+
```
53+
54+
## Solution - 3
55+
56+
```dart
57+
import 'dart:collection';
58+
59+
class Solution {
60+
int minDeletionSize(List<String> strs) {
61+
HashSet<int> hashSET = HashSet();
62+
for (int j = 0; j < strs[0].length; j++) hashSET.add(j);
63+
for (int i = 1; i < strs.length; i++) {
64+
HashSet<int> tmp = HashSet();
65+
tmp.addAll(hashSET);
66+
for (int k in hashSET)
67+
if (strs[i].codeUnitAt(k) < strs[i - 1].codeUnitAt(k)) tmp.remove(k);
68+
hashSET = tmp;
69+
}
70+
return strs[0].length - hashSET.length;
71+
}
72+
}
73+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
175175
- [**886.** Possible Bipartition](PossibleBipartition/possible_bipartition.dart)
176176
- [**980.** Unique Paths III](UniquePathsIII/unique_paths_iii.dart)
177177
- [**520.** Detect Capital](DetectCapital/detect_capital.dart)
178+
- [**944.** Delete Columns to Make Sorted](DeleteColumnsToMakeSorted/delete_columns_to_make_sorted.dart)
178179

179180
## Reach me via
180181

0 commit comments

Comments
 (0)