[D2] [S/W 문제해결 기본] 1일차 - 최빈수 구하기 - 1204
성능 요약
메모리: 42,880 KB, 시간: 210 ms, 코드길이: 973 Bytes
제출 일자
2023-10-19 13:34
출처: SW Expert Academy, https://swexpertacademy.com/main/code/problem/problemList.do
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
import java.util.*;
import java.io.*;
class Solution
{
public static void main(String args[]) throws Exception
{
Scanner sc = new Scanner(System.in);
Map<Integer, Integer> map;
int T = sc.nextInt();
for(int tc = 1; tc <= T; tc++)
{
int why = sc.nextInt();
map = new HashMap<>();
int maxKey = 0;
for(int i = 0; i < 1000; i++){
int N = sc.nextInt();
map.put(N, map.getOrDefault(N,0)+1);
// N의 빈도수
int currentValue = map.get(N);
// 현재 가장 높은 빈도수
int maxValue = map.getOrDefault(maxKey,0);
if(currentValue > maxValue){
maxKey = N;
}else if(currentValue == maxValue){
maxKey = Math.max(N, maxKey);
}
}
System.out.printf("#%d %d\n",tc,maxKey);
}
}
}
Map을 이용한 풀이
import java.util.*;
import java.io.*;
class Solution
{
public static void main(String args[]) throws Exception
{
Scanner sc = new Scanner(System.in);
int[] nums;
int T = sc.nextInt();
for(int tc = 1; tc <= T; tc++)
{
int why = sc.nextInt();
nums = new int[101];
int maxIdx = 0;
for(int i = 0; i < 1000; i++){
int N = sc.nextInt();
nums[N]++;
int currentValue = nums[N];
int maxValue = nums[maxIdx];
if(currentValue > maxValue){
maxIdx = N;
}else if(currentValue == maxValue){
maxIdx = Math.max(N, maxIdx);
}
}
System.out.printf("#%d %d\n",tc,maxIdx);
}
}
}
배열을 이용한 풀이
'알고리즘 - SWEA > D2' 카테고리의 다른 글
[SW expert Academy] SWEA 1284번 수도 요금 경쟁 자바(Java) (1) | 2023.10.19 |
---|---|
[SW expert Academy] SWEA 1940번 가랏! RC카! 자바(Java) (0) | 2023.10.19 |
[SW expert Academy] SWEA 1288번 불면증 치료법 자바(Java) (1) | 2023.10.19 |
[SW expert Academy] SWEA 1928번 Base64 Decoder 자바(Java) (1) | 2023.10.19 |
[SW expert Academy] SWEA 1945번 간단한 소인수분해 자바(Java) (0) | 2023.10.18 |