Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/week01/inti0/PG_160586.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

// https://school.programmers.co.kr/learn/courses/30/lessons/160586
// 프로그래머스 키패드 누르기

class Solution {
public int[] solution(String[] keymap, String[] targets) {
Map<Character, Integer> map = new HashMap<>();

for (int i = 0; i < keymap.length; i++) {
for (int j = 0; j < keymap[i].length(); j++) {
//최소 횟수 버튼값 추가
Character button = keymap[i].charAt(j);
int count = j + 1;
map.merge(button, count, (v1, v2) -> Math.min(v1, v2));
}
}
// System.out.println(map);

return Arrays.stream(targets)
.mapToInt(target -> targetToCount(map, target))
.toArray();
}

private int targetToCount(Map<Character, Integer> map, String target) {
int sum = 0;

for (int i = 0; i < target.length(); i++) {
char ch = target.charAt(i);
if (!map.containsKey(ch)) {
return -1;
} else {
sum += map.get(ch);
}
}

return sum;
}
}
85 changes: 85 additions & 0 deletions src/week01/inti0/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
## 과거 풀이법 및 주요 메소드
### map.merge
merge(key, 값이 없었을 때 put할 Value, 충돌시 매핑전략)

```java
default V merge(K key, V value,
BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
Objects.requireNonNull(value);
V oldValue = get(key);
V newValue = (oldValue == null) ? value :
remappingFunction.apply(oldValue, value);
if (newValue == null) {
remove(key);
} else {
put(key, newValue);
}
return newValue;
}
```

### Collectors.toMap(매개변수 3개짜리)
(
키전달방식,
밸류전달방식,
충돌시 매핑전략
)


```java
public static <T, K, U>
Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper,
BinaryOperator<U> mergeFunction) {
return toMap(keyMapper, valueMapper, mergeFunction, HashMap::new);
}
```


```java
import java.util.*;
import java.util.stream.*;

class Solution {
public int[] solution(String[] keymap, String[] targets) {
Map<String, Integer> map = IntStream.range(0, keymap.length)
.mapToObj(i -> IntStream.range(0, keymap[i].length())
.boxed()
.collect(Collectors.toMap(
j -> keymap[i].charAt(j) + "",
j -> j + 1,
(v1, v2) -> Math.min(v1, v2))
))
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(
e -> e.getKey(),
e -> e.getValue(),
(v1, v2) -> Math.min(v1, v2)));

// return IntStream.range(0, targets.length)
// .map(i -> Arrays.stream(targets[i].split(""))
// .map(s -> map.getOrDefault(s, -1))
// .reduce(0, (a, b) -> {
// if (a == -1 || b == -1) {
// return -1;
// }
// return a + b;
// }))
// .toArray();

return IntStream.range(0, targets.length)
.mapToObj(i -> Arrays.stream(targets[i].split(""))
.map(key -> map.getOrDefault(key, -1)))
.mapToInt(stream -> stream.reduce(
0, (a, b) -> {
if (a == -1 || b == -1) {
return -1;
}
return a + b;
}))
.toArray();
}
}

```