[ SWEA ] 1204, 1206, 9611
1204 : 1000명의 학생들의 점수를 보고 가장 많이 등장한 점수 꺼내기
-> HashMap 사용해서 풀었다.
학생의 점수 + 빈도 수를 key, value 값으로 넣어두기 / map은 같은 key값이 들어오면 나중에 들어온 value로 덮어씌운다.
기존에 있던 빈도 수 scores.get() + 1해서 빈도수 + 1
이때 Map을 정렬해야 하는데, 문제 조건은 2가지가 있다.
1. 가장 많이 등장한 점수
2. 빈도수가 같은 점수가 있을 경우, 더 높은쪽을 선택.
Map 자료구조에서 map.entrySet() : map의 모든 Key, Value 꺼낸다.
ArrayList로 꺼내도 되고 LinkedList로 꺼내도 된다. 돌려보니 ArrayList가 메모리를 더 쓰고 시간을 덜 쓰는 것 같다. LinkedList는 메모리를 덜 쓰고 시간을 조금 더 씀. 이건 둘의 특징 차이 때문이긴 하지만 ... 이 둘의 자료구조 상 특징은 나중에 따로 정리해야지
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int test = sc.nextInt();
for (int i = 0; i < test; i++) {
Map<Integer, Integer> scores = new HashMap<>();
for (int j = 0; j <= 100; j++) {
scores.put(j, 0);
}
int num = sc.nextInt(); // 테스트케이스 번호
for (int j = 0; j < 1000; j++) {
int temp = sc.nextInt();
scores.put(temp, scores.get(temp) + 1);
}
List<Map.Entry<Integer, Integer>> entries = new LinkedList<>(scores.entrySet());
entries.sort((o1, o2) -> {
if (o1.getValue() == o2.getValue()) {
return o2.getKey() - o1.getKey();
}
return o2.getValue() - o1.getValue();
});
System.out.println("#" + num + " " + entries.get(0).getKey());
}
}
}
1206 : View
- 배열에 각 빌딩 정보들을 저장해두고, 자기 기준 양 옆 2칸씩 보니까 5칸짜리 슬라이딩 윈도우 방식으로 탐색
- 시작 2칸과 끝 2칸이 0이므로, 2부터 빌딩개수 - 3까지 탐색 (빌딩번호 0 ~ 빌딩개수-1이라고 생각할 때)
- 자기 자신 양 옆 2칸씩 본다. 1 3 9 5 6 이런식으로 되어있으면 9(자기자신)이 가장 max이므로 조망권 확보 됨. 다음으로 높은 빌딩이 6층짜리이므로 9-6 = 3, 총 3칸의 조망권 확보
- 1 4 2 1 1 이런식이면 2(자기자신)이 max가 아니므로 조망권 확보 불가.
- 5칸짜리에 자기자신 + 양옆 2칸 넣어두고, 내가 max라면 그 다음으로 큰 애 빼주면 된다.
- int[]를 Collections.reverseOrder()로 역순 정렬 하려면 Integer[] 로 변경해줘야 함. 근데 어차피 오름차순 정렬하고 끝에서 2개빼면 되는거라 그냥 Arrays.sort 사용함.
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for (int t = 1; t <= 10; t++) {
int answer = 0;
StringTokenizer st = new StringTokenizer(br.readLine());
int building = Integer.parseInt(st.nextToken());
int[] arr = new int[building];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < building; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
for (int i = 2; i < building-2; i++) {
int[] temp = Arrays.copyOfRange(arr, i-2, i+3);
Arrays.sort(temp);
if (temp[4] == arr[i]) {
answer += (temp[4] - temp[3]);
}
}
System.out.println("#" + t + " "+answer);
}
}
}
9611 : 명진이와 동휘의 숫자 맞추기
아이디어가 생각이 안나서 고민하고 있었는데 문제 댓글에 달려있던 아이디어 보고 해결...
방문 처리를 따로 해야하나 그 생각하고 있었는데... 이렇게 풀게 되었다.
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int test = Integer.parseInt(st.nextToken());
for (int t = 1; t <= test; t++) {
st = new StringTokenizer(br.readLine());
int query = Integer.parseInt(st.nextToken());
int[] answer = new int[10];
Arrays.fill(answer, 1);
for (int i = 0; i < query; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
int d = Integer.parseInt(st.nextToken());
String included = st.nextToken();
if (included.equals("YES")) {
answer[a] *= 2;
answer[b] *= 2;
answer[c] *= 2;
answer[d] *= 2;
}
else {
answer[a] = answer[b] = answer[c] = answer[d] = 0;
}
}
int index = 0;
int max = -111;
for (int i = 0; i < 10; i++) {
if (answer[i] > max) {
max = answer[i];
index = i;
}
}
System.out.println("#" + t + " " + index);
}
}
}