Skip to content

Commit 0689fa6

Browse files
add 942
1 parent d71ebdf commit 0689fa6

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Your ideas/fixes/algorithms are more than welcome!
4343
|961|[N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_961.java) | O(n) | O(1) | |Easy|
4444
|953|[Verifying an Alien Dictionary](https://leetcode.com/problems/verifying-an-alien-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_953.java) | O(1) | O(1) | |Easy|
4545
|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|
46+
|942|[DI String Match](https://leetcode.com/problems/di-string-match/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_942.java) | O(n) | O(n) | |Easy|
4647
|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|
4748
|938|[Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_938.java) | O(n) | O(n) | |Medium| BST, recursion, DFS
4849
|937|[Reorder Log Files](https://leetcode.com/problems/reorder-log-files/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_937.java) | O(n) | O(n) | |Easy|
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Queue;
5+
6+
/**
7+
* 942. DI String Match
8+
*
9+
* Given a string S that only contains "I" (increase) or "D" (decrease), let N = S.length.
10+
*
11+
* Return any permutation A of [0, 1, ..., N] such that for all i = 0, ..., N-1:
12+
*
13+
* If S[i] == "I", then A[i] < A[i+1]
14+
* If S[i] == "D", then A[i] > A[i+1]
15+
*
16+
* Example 1:
17+
* Input: "IDID"
18+
* Output: [0,4,1,3,2]
19+
*
20+
* Example 2:
21+
* Input: "III"
22+
* Output: [0,1,2,3]
23+
*
24+
* Example 3:
25+
* Input: "DDI"
26+
* Output: [3,2,0,1]
27+
*
28+
* Note:
29+
* 1 <= S.length <= 10000
30+
* S only contains characters "I" or "D".
31+
*/
32+
public class _942 {
33+
public static class Solution1 {
34+
public int[] diStringMatch(String S) {
35+
Queue<Integer> deque = new ArrayDeque<>();
36+
for (int i = 0; i <= S.length(); i++) {
37+
deque.add(i);
38+
}
39+
int[] result = new int[S.length() + 1];
40+
for (int i = 0; i <= S.length(); i++) {
41+
if (i == S.length()) {
42+
result[i] = ((ArrayDeque<Integer>) deque).pollLast();
43+
} else if (S.charAt(i) == 'I') {
44+
result[i] = ((ArrayDeque<Integer>) deque).pollFirst();
45+
} else {
46+
result[i] = ((ArrayDeque<Integer>) deque).pollLast();
47+
}
48+
}
49+
return result;
50+
}
51+
}
52+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._942;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
public class _942Test {
9+
private static _942.Solution1 solution1;
10+
11+
@BeforeClass
12+
public static void setup() {
13+
solution1 = new _942.Solution1();
14+
}
15+
16+
@Test
17+
public void test1() {
18+
CommonUtils.printArray(solution1.diStringMatch("IDID"));
19+
}
20+
21+
@Test
22+
public void test2() {
23+
CommonUtils.printArray(solution1.diStringMatch("III"));
24+
}
25+
26+
@Test
27+
public void test3() {
28+
CommonUtils.printArray(solution1.diStringMatch("DDI"));
29+
}
30+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy