๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 10972_๋‹ค์Œ ์ˆœ์—ด

๋ฟŒ์•ผ._. 2024. 4. 17. 12:16

Silver III

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

< ๋‹ค์Œ ์ˆœ์—ด >

 

๋ฌธ์ œ ํ’€์ด 

 

๋งŒ์•ฝ 1 2 3 5 4๊ฐ€ ์ฃผ์–ด์กŒ๋‹ค๊ณ  ์ƒ๊ฐํ•ด ๋ณด์ž.

๋’ค์—์„œ๋ถ€ํ„ฐ ๊ฐ’์„ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ํ˜„์žฌ ๊ฐ’์—์„œ ๋’ค์— ๊ฐ’์„ ํƒ์ƒ‰ํ•œ๋‹ค. 

4๋Š” ๋งˆ์ง€๋ง‰ ๊ฐ’์ด๋ฏ€๋กœ ๋„˜์–ด๊ฐ„๋‹ค.

5๋ฅผ ํ™•์ธํ–ˆ์„ ๋•Œ ๋’ค์— 4๋ฐ–์— ์—†์œผ๋ฏ€๋กœ 5๋ณด๋‹ค ์ž‘์€ ์ˆ˜ ์ด๋ฏ€๋กœ ๋„˜์–ด๊ฐ„๋‹ค.

3์„ ํ™•์ธํ–ˆ์„ ๋•Œ 5,4์™€ ๊ฐ™์ด 3๋ณด๋‹ค ํฐ ๊ฐ’์ด ์žˆ์œผ๋ฏ€๋กœ ๋‹ค์Œ ์ˆœ์—ด์„ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด 4๋กœ ๊ต์ฒดํ•œ๋‹ค. ๊ทธ ํ›„์— ๋‚จ์€ ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ํ›„ ์ถœ๋ ฅํ•œ๋‹ค.

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

public class _10972_ { // ๋‹ค์Œ ์ˆœ์—ด

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		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());
		}

		ArrayList<Integer> list = new ArrayList<>();
		int num = -1;
		int idx = -1;

		for (int i = n - 1; i >= 0; i--) {
			list.add(arr[i]);

			Collections.sort(list, Collections.reverseOrder());

			if (list.get(0) != arr[i]) {
				for (int j = list.size() - 1; j >= 0; j--) {
					if (list.get(j) > arr[i]) {
						num = list.get(j);
						break;
					}
				}
				idx = i;
				break;
			}
		}

		if (num == -1) {
			System.out.println(-1);
		} else {
			boolean flag = false;
			for (int i = 0; i < idx; i++) {
				bw.write(arr[i] + " ");
			}
			bw.write(num + " ");
			for (int i = list.size() - 1; i >= 0; i--) {
				if (!flag && list.get(i) == num) {
					flag = true;
					continue;
				}
				bw.write(list.get(i) + " ");
			}
			bw.flush();
		}
	}
}
๋ณ€์ˆ˜)
n : ์ˆœ์—ด ์ˆ˜
arr : ์ˆœ์—ด ๊ฐ’
list : ํ˜„์žฌ ๊ฐ’๋ณด๋‹ค ํฐ ์ˆ˜๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•œ ๋ฆฌ์ŠคํŠธ
num, idx : ํ˜„์žฌ ๊ฐ’๋ณด๋‹ค ํฐ ๊ฐ’, ๊ต์ฒดํ•  ์ธ๋ฑ์Šค

 

์ˆœ์—ด ๊ฐœ์ˆ˜ n์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ๋ฐฐ์—ด์— ์ˆœ์—ด ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค. ๋’ค์—์„œ๋ถ€ํ„ฐ ๊ฐ’์„ ํ™•์ธํ•˜๋ฉด์„œ  ๋‹ค์Œ ๊ณผ์ •์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

1) list์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

2) list๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ํ›„ 0๋ฒˆ์งธ ๊ฐ’๊ณผ ํ˜„์žฌ ๊ฐ’์ด ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

3) ์ผ์น˜ํ•œ๋‹ค๋ฉด ํ˜„์žฌ ๊ฐ’๋ณด๋‹ค ํฐ ๊ฐ’์ด ์—†์œผ๋ฏ€๋กœ ๊ณ„์†ํ•ด์„œ ๋ฐ˜๋ณตํ•œ๋‹ค. ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ํ˜„์žฌ ๊ฐ’๋ณด๋‹ค ํฐ ๊ฐ’์ด ์žˆ๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ list์— ์žˆ๋Š” ๊ฐ’์„ ํƒ์ƒ‰ํ•˜์—ฌ ํ˜„์žฌ ๊ฐ’๋ณด๋‹ค ํฐ ๊ฐ’ ์ค‘์—์„œ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ์ฐพ์•„ num์— ์ €์žฅํ•œ๋‹ค. idx๋ฅผ ํ˜„์žฌ ์œ„์น˜๋กœ ์ €์žฅํ•œ ํ›„ ์ข…๋ฃŒํ•œ๋‹ค.

 

๋งŒ์•ฝ num์ด -1์ด๋ผ๋ฉด ๋‹ค์Œ ์ˆœ์—ด์ด ์—†๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ -1์„ ์ถœ๋ ฅํ•œ๋‹ค. num ๊ฐ’์ด ์žˆ๋‹ค๋ฉด arr์˜ 0๋ฒˆ์งธ๋ถ€ํ„ฐ idx์ „๊นŒ์ง€ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค. ๊ทธ ํ›„์— num ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค. list๋ฅผ ๋’ค์—์„œ๋ถ€ํ„ฐ num ๊ฐ’์„ ์ œ์™ธํ•œ ๊ฐ’์„ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•œ๋‹ค.