-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
63 lines (49 loc) · 2.17 KB
/
App.java
File metadata and controls
63 lines (49 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* This Java source file was generated by the Gradle 'init' task.
*/
/*
https://programmers.co.kr/learn/courses/30/lessons/42576?language=java
### 1. 이해
- 1명을 제외한 모든 선수가 마라톤 완주를 했음
- 마라톤에 참여한 선수들의 이름이 담긴 배열 participant
- 완주한 선수들의 이름이 담긴 배열 completion
- 완주하지 못한 선수의 이름을 return
- 마라톤 경기에 참여한 선수의 수는 1명 이상 100,000명 이하
- completion의 길이는 participant의 길이보다 1 작음
- 참가자의 이름은 1개 이상 20개 이하의 알파벳 소문자
- 참가자 중에는 동명이인이 있을 수 있음
### 2. 계획
- 단순하게 생각했을 때, participants와 completion을 비교하면서 진행하면 되지않을까 생각한다.
- 이럴경우 O(N^2)의 결과가 자연스레 도출된다.
### 3. 실행
### 4. 반성
Stream의 활용도를 높일 필요가 있다.
너무 단순한 해결책만 먼저 떠오르는 경우가 많다.
*/
package find.prime.numbers;
import java.util.*;
public class App {
public String solution(String[] participant, String[] completion) {
HashMap<String, Integer> participantMap = new HashMap<String, Integer>();
Arrays.stream(participant).forEach(name -> {
int newValue = (participantMap.containsKey(name)) ? participantMap.get(name) + 1 : 1;
participantMap.put(name, newValue);
});
Arrays.stream(completion).forEach(name -> {
if (participantMap.containsKey(name)) {
int oldValue = participantMap.get(name);
if (oldValue == 1) {
participantMap.remove(name);
} else {
participantMap.put(name, oldValue - 1);
}
}
});
return participantMap.entrySet().iterator().next().getKey();
}
public static void main(String[] args) {
String[] participants2 = {"marina", "josipa", "nikola", "vinko", "filipa"};
String[] completion2 = {"josipa", "filipa", "marina", "nikola"};
System.out.println(new App().solution(participants2, completion2));
}
}