Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

22-SeongHoonC #80

Merged
merged 1 commit into from
May 10, 2024
Merged

22-SeongHoonC #80

merged 1 commit into from
May 10, 2024

Conversation

SeongHoonC
Copy link
Collaborator

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

30๋ถ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๋ณด์ž๋งˆ์ž ๊ฐ€์žฅ ๋จผ์ € ๋– ์˜ฌ๋ฆฐ ์ƒ๊ฐ์€ ์ด์ „์— ๊ฐ’๋“ค์„ ์ „๋ถ€ ๋ณด๊ณ  ์ž๊ธฐ๋ณด๋‹ค ์ž‘์œผ๋ฉด + 1 ํ•ด์„œ ์—…๋ฐ์ดํŠธ ํ•˜๋ฉด ๋˜์ง€ ์•Š์„๊นŒ? ์˜€๋‹ค.

์ฒซ ๋ฒˆ์งธ ์‹œ๋„

fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val n = br.readLine().toInt()
    val arr = br.readLine().split(" ").map { it.toInt() }

    val answer = Array(n) { 0 }

    // ์ฒ˜์Œ์—๋งŒ 1 ๋„ฃ์–ด์คŒ
    answer[0] = 1

    // 1 ~ n-1 ๊นŒ์ง€ ๋ณธ์ธ ์ด์ „ ๊ฐ’๋“ค๊ณผ ๋น„๊ตํ•ด์„œ ์ž๊ธฐ๋ณด๋‹ค ์ž‘์œผ๋ฉด, +1 ํ–ˆ์„ ๋•Œ ์ตœ๋Œ€ ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.
    for (now in 1 until n) {
        for (target in 0 until now) {
            if (arr[target] < arr[now]) {
                answer[now] = max(answer[now], answer[target] + 1)
            }
        }
    }
    println(answer[n - 1])
}

๊ทธ๋Ÿฌ๋‚˜ ์ฒซ ๋ฒˆ์งธ ์‹œ๋„์—์„œ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋‹ค.

fun main() {
    // ...
    // 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๋ฉด ์ตœ๋Œ€ ๊ฐœ์ˆ˜๊ฐ€ 1๊ฐœ ์ ๊ฒŒ ๊ณ„์‚ฐ๋จ
    val answer = Array(n) { 0 }

    // ์ฒ˜์Œ์—๋งŒ 1 ์„ ๋„ฃ์–ด์คฌ๋”๋‹ˆ ์ฆ๊ฐ€ ์ˆ˜์—ด์˜ ์‹œ์ž‘์ด ๊ผญ 0๋ถ€ํ„ฐ๋Š” ์•„๋‹ˆ๋ผ์„œ ํ‹€๋ฆผ
    answer[0] = 1

    for (now in 1 until n) {
        for (target in 0 until now) {
            if (arr[target] < arr[now]) {
                answer[now] = max(answer[now], answer[target] + 1)
            }
        }
    }

    // ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ n ๋ฒˆ์งธ๊ฐ€ ๊ผญ ๋‹ต์€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ํ‹€๋ฆผ
    println(answer[n - 1])
}

๋‘ ๋ฒˆ์งธ ์‹œ๋„

fun main() {
    // ...
    // 1 ์„ ๋„ฃ๊ณ  ์ดˆ๊ธฐํ™”
    val answer = Array(n) { 1 }

    // 1 ~ n-1 ๊นŒ์ง€ ๋ณธ์ธ ์ด์ „ ๊ฐ’๋“ค๊ณผ ๋น„๊ตํ•ด์„œ ์ž๊ธฐ๋ณด๋‹ค ์ž‘์œผ๋ฉด, +1 ํ–ˆ์„ ๋•Œ ์ตœ๋Œ€ ๊ฐ’์„ ์—…๋ฐ์ดํŠธ
    for (now in 1 until n) {
        for (target in 0 until now) {
            if (arr[target] < arr[now]) {
                answer[now] = max(answer[now], answer[target] + 1)
            }
        }
    }
    // ๊ฒฐ๊ณผ ์ค‘์— ์ตœ๋Œ€๊ฐ’์„ ์ถœ๋ ฅ
    println(answer.max())
}

์Œ ์ด๋ ‡๊ฒŒ ํ’€๊ณ  ๋‚˜์„œ ๋ณด๋‹ˆ ์žฅ๋ฅด๊ฐ€ DP ์˜€๋‹ค.
์™œ์ง€...?

์ตœ์ ์˜ ํ•ด๋ฅผ ์ฐพ๋Š” ๊ฒƒ ๋•Œ๋ฌธ์— ๊ทธ๋Ÿฐ ๊ฒƒ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐ๋œ๋‹ค.
๊ธฐ์กด์— ํ’€์—ˆ๋˜ DP ๋“ค์€ ์ด๋ ‡๊ฒŒ ์ด์ „ ๊ฐ’์„ ์ „๋ถ€ ํƒ์ƒ‰ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์ž˜ ์—†์—ˆ๋Š”๋ฐ..
DP ๋ผ๋ฉด ๋” ๊น”์‚ผํ•˜๊ฒŒ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์ง€ ์•Š์„๊นŒ?

์˜๋„์น˜ ์•Š๊ฒŒ DP ๋ฅผ 3 ์—ฐ์† ํ’€์–ด์„œ ๋‹น๋ถ„๊ฐ„ DP ๋Š” ๊ธˆ์ง€ํ•ด์•ผ๊ฒ ๋‹ค..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants