๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Coding Test/Algorithms

[์†Œํ”„ํ‹ฐ์–ด] ์ง•๊ฒ€๋‹ค๋ฆฌ/Python - Lv.3

by The Future Engineer, Lucy 2024. 10. 30.
728x90
๋ฐ˜์‘ํ˜•

โ“๋ฌธ์ œ

https://softeer.ai/practice/6293

 

Softeer - ํ˜„๋Œ€์ž๋™์ฐจ๊ทธ๋ฃน SW์ธ์žฌํ™•๋ณดํ”Œ๋žซํผ

 

softeer.ai

์–ธ์–ด๋ณ„ ์‹œ๊ฐ„/๋ฉ”๋ชจ๋ฆฌ

์–ธ์–ด ์‹œ๊ฐ„ ๋ฉ”๋ชจ๋ฆฌ
JavaScript 2์ดˆ 256MB
C 1์ดˆ 256MB
C++ 1์ดˆ 256MB
Java 2์ดˆ 256MB
Python 2์ดˆ 256MB

์ด ์ง•๊ฒ€๋‹ค๋ฆฌ์˜ ๋Œ์€ ๋“ค์‘ฅ๋‚ ์‘ฅํ•˜์—ฌ ๋†’์ด๊ฐ€ ๋ชจ๋‘ ๋‹ค๋ฅด๋‹ค.
์„œ์ชฝ์—์„œ ๋™์ชฝ์œผ๋กœ ๋†’์ด๊ฐ€ ์ ์  ๋†’์€ ๋Œ์„ ๋ฐŸ์œผ๋ฉด์„œ ๊ฐœ์šธ์„ ์ง€๋‚˜๊ฐ€๋ ค๊ณ  ํ•œ๋‹ค.
์„œ์ชฝ์—์„œ ๋™์ชฝ์œผ๋กœ ๊ฐˆ ๋•Œ ๋ฐŸ์„ ์ˆ˜ ์žˆ๋Š” ๋Œ์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋Š”?

์ œ์•ฝ์กฐ๊ฑด

1 ≤ N ≤ 3×10³ ์ธ ์ •์ˆ˜
1 ≤ Ai ≤ 10โธ ์ธ ์ •์ˆ˜

โœ๐Ÿปํ’€์ด

DP๋ฌธ์ œ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ชจ๋“  ๋Œ์€ 1๋ฒˆ ๋ฐŸ์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ 1๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
๋†’์ด๊ฐ€ ๊ฐ 3, 2, 1, 4, 5 ๋ฅผ ๊ฐ€์ง„ 5๊ฐœ์˜ ๋Œ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ,
(3, 4, 5), (2, 4, 5), (1, 4, 5)๋กœ ๋†’์ด๊ฐ€ 5์ธ ๋Œ์— ๋„์ฐฉํ–ˆ์„ ๋•Œ ์ตœ๋Œ€ 3๊ฐœ์˜ ๋Œ์„ ๋ฐŸ์„ ์ˆ˜ ์žˆ๋‹ค.
์ด์ค‘๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ 0~1, 0~2, 0~3 ์ด๋Ÿฌํ•œ ์‹์œผ๋กœ ํ•˜๋‚˜์”ฉ ๋Š˜๋ ค๊ฐ€๋ฉฐ ์ตœ๋Œ€๋กœ ๋ฐŸ์„ ์ˆ˜ ์žˆ๋Š” ๋Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•œ๋‹ค.

๐Ÿ’ป์ฝ”๋“œ

import sys

input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
dp = [1] * n

for i in range(1, n):
    for j in range(i):
        if a[i] > a[j]:
            dp[i] = max(dp[i], dp[j] + 1)

print(max(dp))

๐Ÿ“ํ›„๊ธฐ

์ด ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์ „์— DP๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋”๋‹ˆ ํ˜น์‹œ DP์ธ๊ฐ€ ์ƒ๊ฐํ•ด๋ณด๊ณ  ๊ทธ๋ƒฅ ๋ง‰ ํ’€์—ˆ๋‹ค.
๊ทธ๋Ÿฐ๋ฐ ์ด ๋•Œ ๋ฐ”๋กœ ์•ž์˜ ๋Œ์˜ ๋†’์ด๊ฐ€ ํฐ์ง€๋งŒ ํ™•์ธํ•˜์—ฌ ์ฒ˜์Œ์— (3, 4, 5), (2, 4, 5), (1, 4, 5)๋กœ ๊ฑด๋„ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐ๋งŒ ํ•˜๊ณ  ์ฝ”๋“œ๋กœ๋Š” ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•˜์—ˆ๋‹ค. ์ด๋ฅผ ๊นจ๋‹ซ๊ณ  ์ด์ค‘๋ฌธ์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค.
์ œ๋ฐœ ๋ฌธ์ œ ํ’€ ๋•Œ ์ผ€์ด์Šค๋ฅผ ์ž˜ ์ƒ๊ฐํ•ด์„œ ํ’€์ž..

728x90
๋ฐ˜์‘ํ˜•