01-10 13:07
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[BAEKJOON] ๋ฐฑ์ค€ ์ •๋ ฌ 11652 :: ์นด๋“œ JAVA ๋ณธ๋ฌธ

Algorithm/Baekjoon

[BAEKJOON] ๋ฐฑ์ค€ ์ •๋ ฌ 11652 :: ์นด๋“œ JAVA

miinsun 2022. 3. 14. 16:22

 

๐Ÿ’ฌ  ๋ฌธ์ œ ์„ค๋ช…

์ค€๊ทœ๋Š” ์ˆซ์ž ์นด๋“œ N์žฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
์ˆซ์ž ์นด๋“œ์—๋Š” ์ •์ˆ˜๊ฐ€ ํ•˜๋‚˜ ์ ํ˜€์žˆ๋Š”๋ฐ, ์ ํ˜€์žˆ๋Š” ์ˆ˜๋Š” -262๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 262๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค.

์ค€๊ทœ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์นด๋“œ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ€์žฅ ๋งŽ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ •์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.
๋งŒ์•ฝ, ๊ฐ€์žฅ ๋งŽ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ •์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๋ผ๋ฉด, ์ž‘์€ ๊ฒƒ์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

๐Ÿ”จ  ์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ 

  • ์ฒซ์งธ ์ค„์— ์ค€๊ทœ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ˆซ์ž ์นด๋“œ์˜ ๊ฐœ์ˆ˜ N (1 ≤ N ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค.
  • ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ ์ค„์—๋Š” ์ˆซ์ž ์นด๋“œ์— ์ ํ˜€์žˆ๋Š” ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

 

์ถœ๋ ฅ

  • ์ฒซ์งธ ์ค„์— ์ค€๊ทœ๊ฐ€ ๊ฐ€์žฅ ๋งŽ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ •์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

์˜ˆ์ œ ์ž…๋ ฅ 1)

5
1
2
1
2
1

 

์˜ˆ์ œ ์ถœ๋ ฅ 1)

1

 

์˜ˆ์ œ ์ž…๋ ฅ 2)

6
1
2
1
2
1
2

 

์˜ˆ์ œ ์ถœ๋ ฅ 2)

1

โ€‹

โ€‹

๐Ÿ’ป  Main.java

  • HashMap์„ ์ด์šฉํ•ด ์ตœ๋Œ€ ๊ฐ’์„ ์ฐพ์Œ
  • ์ •๋ ฌ๋ณด๋‹ค๋Š” ์ตœ๋Œ“๊ฐ’ ์ฐพ๊ธฐ๋ฅผ ํ†ตํ•ด ํ’€์ดํ•จ
/* ๋ฐฑ์ค€ ์ •๋ ฌ - 11652 :: ์นด๋“œ */
import java.util.*;
import java.io.*;

public class Main {
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		HashMap <Long, Integer> map = new HashMap<>();
		
		// HashMap์— ์นด๋“œ ์ •๋ณด ์ €์žฅ
		for(int i = 0; i < n; i++) {
			long tmp = sc.nextLong();
			// ์ด๋ฏธ ์žˆ๋Š” ์นด๋“œ ๊ฐ’์ด ๋“ค์–ด์˜ค๋ฉด ์›๋ž˜ ๊ฐ’์—์„œ + 1
			map.put(tmp, map.getOrDefault(tmp, 0) + 1);
		}
		
		// HashMap์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊ฐ€์ง„ Index ๋ฆฌํ„ด 
		int max = Integer.MIN_VALUE;
		long  answer = 0;
		for(long x: map.keySet()) {
			if(map.get(x) > max) {
				max = map.get(x);
				answer = x;
			} else if(map.get(x) == max) {
				answer = Math.min(answer, x);
			}	
		}
		
		System.out.println(answer);
		
		sc.close();
	}
}
Comments