Skip to content

Commit 37f0909

Browse files
solves delete columns to make sorted
1 parent aaffb4f commit 37f0909

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@
256256
| 938 | [Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst) | [![Java](assets/java.png)](src/RangeSumOfBST.java) |
257257
| 941 | [Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array) | [![Java](assets/java.png)](src/ValidMountainArray.java) [![Python](assets/python.png)](python/valid_mountain_array.py) |
258258
| 942 | [DI String Match](https://leetcode.com/problems/di-string-match) | [![Java](assets/java.png)](src/DIStringMatch.java) |
259-
| 944 | [Delete Columns to Make Sorted](https://leetcode.com/problems/delete-columns-to-make-sorted) | |
259+
| 944 | [Delete Columns to Make Sorted](https://leetcode.com/problems/delete-columns-to-make-sorted) | [![Java](assets/java.png)](src/DeleteColumnsToMakeSorted.java) |
260260
| 949 | [Largest Time for Given Digits](https://leetcode.com/problems/largest-time-for-given-digits) | |
261261
| 953 | [Verifying an Alien Dictionary](https://leetcode.com/problems/verifying-an-alien-dictionary) | |
262262
| 961 | [N-Repeated Elements in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array) | |

src/DeleteColumnsToMakeSorted.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class DeleteColumnsToMakeSorted {
2+
public int minDeletionSize(String[] strings) {
3+
final int columns = strings[0].length();
4+
int deletions = 0;
5+
for (int column = 0 ; column < columns ; column++) {
6+
if (!isSorted(strings, column)) deletions++;
7+
}
8+
return deletions;
9+
}
10+
11+
private boolean isSorted(String[] strings, int column) {
12+
for (int row = 1 ; row < strings.length ; row++) {
13+
if (strings[row].charAt(column) < strings[row - 1].charAt(column)) return false;
14+
}
15+
return true;
16+
}
17+
}

0 commit comments

Comments
 (0)