Skip to content

Commit f7d95f2

Browse files
add 944
1 parent 8df1dc0 commit f7d95f2

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Your ideas/fixes/algorithms are more than welcome!
2929

3030
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
3131
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
32+
|944|[Delete Columns to Make Sorted](https://leetcode.com/problems/delete-columns-to-make-sorted/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_944.java) | O(n) | O(1) | |Easy|
3233
|941|[Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_941.java) | O(n) | O(1) | |Easy|
3334
|933|[Number of Recent Calls](https://leetcode.com/problems/number-of-recent-calls/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_933.java) | O(n) | O(n) | |Easy|
3435
|929|[Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_929.java) | O(n) | O(n) | |Easy|
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 944. Delete Columns to Make Sorted
5+
*
6+
* We are given an array A of N lowercase letter strings, all of the same length.
7+
* Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices.
8+
* For example, if we have an array A = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3},
9+
* then the final array after deletions is ["bef", "vyz"], and the remaining columns of A are ["b","v"], ["e","y"],
10+
* and ["f","z"]. (Formally, the c-th column is [A[0][c], A[1][c], ..., A[A.length-1][c]].)
11+
*
12+
* Suppose we chose a set of deletion indices D such that after deletions, each remaining column in A is in non-decreasing sorted order.
13+
*
14+
* Return the minimum possible value of D.length.
15+
*
16+
* Example 1:
17+
*
18+
* Input: ["cba","daf","ghi"]
19+
* Output: 1
20+
* Explanation:
21+
* After choosing D = {1}, each column ["c","d","g"] and ["a","f","i"] are in non-decreasing sorted order.
22+
* If we chose D = {}, then a column ["b","a","h"] would not be in non-decreasing sorted order.
23+
* Example 2:
24+
*
25+
* Input: ["a","b"]
26+
* Output: 0
27+
* Explanation: D = {}
28+
* Example 3:
29+
*
30+
* Input: ["zyx","wvu","tsr"]
31+
* Output: 3
32+
* Explanation: D = {0, 1, 2}
33+
*
34+
*
35+
* Note:
36+
*
37+
* 1 <= A.length <= 100
38+
* 1 <= A[i].length <= 1000
39+
* */
40+
public class _944 {
41+
public static class Solution1 {
42+
public int minDeletionSize(String[] A) {
43+
if (A == null || A.length == 0) {
44+
return 0;
45+
}
46+
int deletion = 0;
47+
for (int i = 0; i < A[0].length(); i++) {
48+
for (int j = 0; j < A.length - 1; j++) {
49+
if (A[j].charAt(i) > A[j + 1].charAt(i)) {
50+
deletion++;
51+
break;
52+
}
53+
}
54+
}
55+
return deletion;
56+
}
57+
}
58+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._944;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.Assert.assertEquals;
8+
9+
public class _944Test {
10+
private static _944.Solution1 solution1;
11+
private static String[] A;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _944.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
A = new String[] {"cba", "daf", "ghi"};
21+
assertEquals(1, solution1.minDeletionSize(A));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
A = new String[] {"a", "b"};
27+
assertEquals(0, solution1.minDeletionSize(A));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
A = new String[] {"zyx", "wvu", "tsr"};
33+
assertEquals(3, solution1.minDeletionSize(A));
34+
}
35+
}

0 commit comments

Comments
 (0)