Skip to content

Commit 2c81bcc

Browse files
committed
Add: algorithm of programmers
1 parent 3649725 commit 2c81bcc

File tree

3 files changed

+87
-16
lines changed

3 files changed

+87
-16
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package io.github.imsejin.study.programmers.lv1;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
/**
8+
* <a href="https://school.programmers.co.kr/learn/courses/30/lessons/160586">대충 만든 자판</a>
9+
*/
10+
public class L160586 {
11+
12+
static int[] solve(String[] keymap, String[] targets) {
13+
int maxLength = Arrays.stream(keymap).mapToInt(String::length).max().getAsInt();
14+
for (int i = 0; i < keymap.length; i++) {
15+
keymap[i] = String.format("%-" + maxLength + "s", keymap[i]);
16+
}
17+
18+
Map<Character, Integer> alphabetMap = new HashMap<>();
19+
for (int i = 0; i < maxLength; i++) {
20+
for (int j = 0; j < keymap.length; j++) {
21+
char c = keymap[j].charAt(i);
22+
alphabetMap.putIfAbsent(c, i + 1);
23+
}
24+
}
25+
26+
int[] counts = new int[targets.length];
27+
for (int i = 0; i < targets.length; i++) {
28+
String target = targets[i];
29+
int length = target.length();
30+
31+
int count = 0;
32+
for (int j = 0; j < length; j++) {
33+
char c = target.charAt(j);
34+
Integer number = alphabetMap.get(c);
35+
36+
if (number != null) {
37+
count += number;
38+
} else {
39+
count = -1;
40+
break;
41+
}
42+
}
43+
44+
counts[i] = count;
45+
}
46+
47+
return counts;
48+
}
49+
50+
}

src/main/java/io/github/imsejin/study/programmers/lv1/L64061.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,23 @@ static int solve(int[][] board, int[] moves) {
1818
for (int i = 0; i < row.length; i++) {
1919
int cell = board[i][move - 1];
2020

21-
if (cell > 0) {
22-
Integer prev = null;
23-
if (!stack.isEmpty()) {
24-
prev = stack.peekLast();
25-
}
26-
27-
stack.offerLast(cell);
28-
board[i][move - 1] = 0;
29-
30-
if (Objects.equals(prev, cell)) {
31-
stack.pollLast();
32-
stack.pollLast();
33-
count++;
34-
}
35-
36-
break;
21+
if (cell == 0) continue;
22+
23+
Integer prev = null;
24+
if (!stack.isEmpty()) {
25+
prev = stack.peekLast();
3726
}
27+
28+
stack.offerLast(cell);
29+
board[i][move - 1] = 0;
30+
31+
if (Objects.equals(prev, cell)) {
32+
stack.pollLast();
33+
stack.pollLast();
34+
count++;
35+
}
36+
37+
break;
3838
}
3939
}
4040

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.github.imsejin.study.programmers.lv1
2+
3+
import spock.lang.Specification
4+
5+
class L160586Spec extends Specification {
6+
7+
def "test"() {
8+
when:
9+
def actual = L160586.solve(keymap as String[], targets as String[])
10+
11+
then:
12+
actual == expected as int[]
13+
14+
where:
15+
keymap | targets || expected
16+
["ABACD", "BCEFD"] | ["ABCD", "AABB"] || [9, 4]
17+
["AA"] | ["B"] || [-1]
18+
["AGZ", "BSSS"] | ["ASA", "BGZ"] || [4, 6]
19+
}
20+
21+
}

0 commit comments

Comments
 (0)