๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 3231_์นด๋“œ๋†€์ด

๋ฟŒ์•ผ._. 2024. 7. 22. 17:16
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/3231)

< ์นด๋“œ๋†€์ด >

 

๋ฌธ์ œ ํ’€์ด 

 

1๋ถ€ํ„ฐ N์„ ์ฐพ์„ ๋•Œ๊นŒ์ง€ ์•ž์—์„œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด์„œ ํ™•์ธํ•œ๋‹ค.

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class _3231_ { // ์นด๋“œ๋†€์ด

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

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

		int arr[] = new int[N];
		for (int i = 0; i < N; i++) {
			arr[i] = Integer.parseInt(bf.readLine());
		}

		int idx = 1, result = 0;
		while (true) {
			for (int i = 0; i < N; i++) {
				if (arr[i] == idx) {
					idx++;
				}
				if (idx == N + 1) {
					break;
				}
			}
			if (idx == N + 1) {
				break;
			}
			result += 1;
		}
		System.out.println(result);
	}
}
๋ณ€์ˆ˜)
N : ์นด๋“œ์˜ ์ˆ˜
arr : ์นด๋“œ ์ •๋ณด
idx : ์ฐพ๋Š” ์ˆซ์ž
result : ๋ฐ•์ˆ˜ ํšŸ์ˆ˜

 

์นด๋“œ์˜ ์ˆ˜ N์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ์นด๋“œ ์ˆ˜๋งŒํผ ์นด๋“œ ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ arr์— ์ €์žฅํ•œ๋‹ค. arr์„ ์•ž์—์„œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ํƒ์ƒ‰ํ•˜๋ฉด์„œ 1๋ถ€ํ„ฐ ์ˆซ์ž๋ฅผ ์ฐพ๋Š”๋‹ค. ์ด๋•Œ ๋์— ๋„๋‹ฌํ•˜๋ฉด result๋ฅผ 1 ๋”ํ•˜๊ณ  ์•ž์—์„œ๋ถ€ํ„ฐ ๋‹ค์‹œ ์ฐพ๋Š”๋‹ค. N๊นŒ์ง€ ๋‹ค ์ฐพ์•˜๋‹ค๋ฉด ์ข…๋ฃŒ ํ›„ result๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.