diff --git a/src/week01/inti0/PG_160586.java b/src/week01/inti0/PG_160586.java new file mode 100644 index 0000000..eb85db0 --- /dev/null +++ b/src/week01/inti0/PG_160586.java @@ -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 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 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; + } +} \ No newline at end of file diff --git a/src/week01/inti0/README.md b/src/week01/inti0/README.md new file mode 100644 index 0000000..626d323 --- /dev/null +++ b/src/week01/inti0/README.md @@ -0,0 +1,85 @@ +## 과거 풀이법 및 주요 메소드 +### map.merge +merge(key, 값이 없었을 때 put할 Value, 충돌시 매핑전략) + +```java + default V merge(K key, V value, + BiFunction 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 + Collector> toMap(Function keyMapper, + Function valueMapper, + BinaryOperator 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 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(); + } +} + +```