๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 2548_๋Œ€ํ‘œ ์ž์—ฐ์ˆ˜

๋ฟŒ์•ผ._. 2023. 10. 17. 16:35

Silver III

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/2548)

< ๋Œ€ํ‘œ ์ž์—ฐ์ˆ˜ >

 

๋ฌธ์ œ ํ’€์ด 

 

์ž…๋ ฅ๋ฐ›์€ ์ž์—ฐ์ˆ˜๋ฅผ ์˜ค๋ฆ„ ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ ํ›„ ๊ฐ€์šด๋ฐ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

 

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class _2548_ { // ๋Œ€ํ‘œ ์ž์—ฐ์ˆ˜

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;

		int n = Integer.parseInt(bf.readLine());

		int arr[] = new int[n];
		st = new StringTokenizer(bf.readLine());
		for (int i = 0; i < n; i++) {
			arr[i] = Integer.parseInt(st.nextToken());
		}
		Arrays.sort(arr);

		if (n % 2 == 0)
			System.out.println(arr[n / 2 - 1]);
		else
			System.out.println(arr[n / 2]);

	}
}

 

Main

๋ณ€์ˆ˜)
n : ์ž์—ฐ์ˆ˜ ๊ฐœ์ˆ˜
arr : ์ž์—ฐ์ˆ˜ ์ €์žฅ

 

- ์ž์—ฐ์ˆ˜ ๊ฐœ์ˆ˜(n) ์ž…๋ ฅ

- ์ž์—ฐ์ˆ˜ ๊ฐœ์ˆ˜(n)๋งŒํผ ์ž์—ฐ์ˆ˜ ์ž…๋ ฅ๋ฐ›์•„ arr์— ์ €์žฅ

- arr ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ

- ์ž์—ฐ์ˆ˜ ๊ฐœ์ˆ˜(n)๊ฐ€ ํ™€์ˆ˜๋ผ๋ฉด arr [n/2] ์ถœ๋ ฅ, ์ง์ˆ˜๋ผ๋ฉด ์ž‘์€ ๊ฐ’์„ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๋ฏ€๋กœ arr [n/2-1] ์ถœ๋ ฅ