Skip to content

Commit 8592991

Browse files
committed
Add solution #955
1 parent 8c12bc8 commit 8592991

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,040 LeetCode solutions in JavaScript
1+
# 1,041 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -764,6 +764,7 @@
764764
952|[Largest Component Size by Common Factor](./solutions/0952-largest-component-size-by-common-factor.js)|Hard|
765765
953|[Verifying an Alien Dictionary](./solutions/0953-verifying-an-alien-dictionary.js)|Easy|
766766
954|[Array of Doubled Pairs](./solutions/0954-array-of-doubled-pairs.js)|Medium|
767+
955|[Delete Columns to Make Sorted II](./solutions/0955-delete-columns-to-make-sorted-ii.js)|Medium|
767768
966|[Vowel Spellchecker](./solutions/0966-vowel-spellchecker.js)|Medium|
768769
970|[Powerful Integers](./solutions/0970-powerful-integers.js)|Easy|
769770
976|[Largest Perimeter Triangle](./solutions/0976-largest-perimeter-triangle.js)|Easy|
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* 955. Delete Columns to Make Sorted II
3+
* https://leetcode.com/problems/delete-columns-to-make-sorted-ii/
4+
* Difficulty: Medium
5+
*
6+
* You are given an array of n strings strs, all of the same length.
7+
*
8+
* We may choose any deletion indices, and we delete all the characters in those indices
9+
* for each string.
10+
*
11+
* For example, if we have strs = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then
12+
* the final array after deletions is ["bef", "vyz"].
13+
*
14+
* Suppose we chose a set of deletion indices answer such that after deletions, the final
15+
* array has its elements in lexicographic order (i.e., strs[0] <= strs[1] <= strs[2]
16+
* <= ... <= strs[n - 1]). Return the minimum possible value of answer.length.
17+
*/
18+
19+
/**
20+
* @param {string[]} strs
21+
* @return {number}
22+
*/
23+
var minDeletionSize = function(strs) {
24+
let previousSorted = new Array(strs.length).fill('');
25+
let result = 0;
26+
27+
for (let col = 0; col < strs[0].length; col++) {
28+
const currentSorted = previousSorted.slice();
29+
let isValid = true;
30+
31+
for (let row = 0; row < strs.length; row++) {
32+
currentSorted[row] += strs[row][col];
33+
if (row > 0 && currentSorted[row] < currentSorted[row - 1]) {
34+
isValid = false;
35+
break;
36+
}
37+
}
38+
39+
if (isValid) {
40+
previousSorted = currentSorted;
41+
} else {
42+
result++;
43+
}
44+
}
45+
46+
return result;
47+
};

0 commit comments

Comments
 (0)