๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ”Ž Algorithm/๐Ÿ’ป ๋ฌธ์ œ ํ’€์ด

[๋ฐฑ์ค€] 11053. ๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด

by Seongpyo Hong 2021. 2. 10.

๋ฌธ์ œ

 

ํ’€์ด

์ฒ˜์Œ์œผ๋กœ ์ ‘๊ทผํ–ˆ๋˜ ๋ฐฉ๋ฒ•์€ index๋ฅผ ํ†ตํ•ด ๋ฉ”๋ชจ์ด์ œ์ด์…˜์„ ์ˆ˜ํ–‰ํ•˜๋ ค๊ณ  ํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฐ™์€ ์ˆ˜๊ฐ€ ์กด์žฌํ•  ๊ฒฝ์šฐ ๊ฐฑ์‹ ํ•˜๋Š” ๊ณผ์ •์—์„œ ์ผ๊ด€๋œ ๊ทœ์น™์„ ์ฐพ์„ ์ˆ˜ ์—†์—ˆ๊ณ  ์ด๋ฅผ ์ปค๋ฒ„ํ•˜๊ธฐ ์œ„ํ•ด index๊ฐ€ ์•„๋‹Œ ์‹ค์ œ ๊ฐ’์„ ํ†ตํ•ด ๋ฉ”๋ชจ์ด์ œ์ด์…˜์„ ์ˆ˜ํ–‰ํ–ˆ๋‹ค.

์•ž์—์„œ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ํ•ด๋‹น ๊ฐ’ A๋ฅผ ์ฐพ๊ณ  1 ~ A-1๊นŒ์ง€์˜ ์ตœ๋Œ€ ๊ธธ์ด + 1๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ์ €์žฅํ–ˆ๋‹ค.

import java.util.Scanner;

public class Solution11053 {
	static Scanner sc = new Scanner(System.in);
	static int[] cache = new int[1001];

	public static void main(String[] args) {
		int N = sc.nextInt();
		int[] input = new int[N];

		for (int i = 0; i < input.length; i++) {
			input[i] = sc.nextInt();
		}

		for (int i = 0; i < input.length; i++) {
			int current = input[i];
			int maxCnt = 0;
			for (int j = current - 1; j > 0; j--) {
				if (cache[j] == 0)
					continue;
				maxCnt = (maxCnt < cache[j]) ? cache[j] : maxCnt;
			}
			cache[current] = maxCnt + 1;
		}

		int maxCnt = 0;
		for (int i = 1; i < 1001; i++) {
			if (maxCnt < cache[i]) {
				maxCnt = cache[i];
			}
		}

		System.out.println(maxCnt);
	}
}

๋Œ“๊ธ€