ํฐ์คํ ๋ฆฌ ๋ทฐ
dev/algorithm
BOJ / 11722๋ฒ / ๊ฐ์ฅ ๊ธด ๊ฐ์ํ๋ ๋ถ๋ถ ์์ด [Go][Python3]
crscnt 2021. 3. 30. 21:00๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/11722
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n int
fmt.Fscanln(reader, &n)
sequence := make([]int, n)
for i := 0; i < n; i++ {
fmt.Fscanf(reader, "%d ", &sequence[i])
}
fmt.Fprintln(writer, longestDecreasingSequenceCount(sequence))
}
func longestDecreasingSequenceCount(sequence []int) (longestCount int) {
dp := make([]int, len(sequence)) // ๊ฐ ์์๋ก๋ถํฐ ์ต์ฅ ๊ฐ์ ๋ถ๋ถ ์์ด์ ๊ธธ์ด๋ฅผ ์ ์ฅํ๋ค
for i := len(sequence) - 1; i >= 0; i-- { // ๋น๊ต๋์์ ์์ด์ ๋ง์ง๋ง ์์๋ถํฐ ์ ํ์ฌ ๊ณ์ฐํ๋ค
dp[i] = 1
length := 0
for j := i + 1; j < len(sequence); j++ {
if sequence[i] > sequence[j] { // i, j ์์๊ฐ ๊ฐ์ํ๋ ๋ถ๋ถ ์์ด์ ํด๋นํ ๊ฒฝ์ฐ
if length < dp[j] {
length = dp[j] // j ์์์ ์ต์ฅ ๊ฐ์ ๋ถ๋ถ ์์ด์ ๊ธธ์ด๊ฐ ๋ ํฐ ๊ฒฝ์ฐ length๊ฐ์ ๊ฐฑ์ ํด์ค๋ค
}
}
}
dp[i] += length // i ์์์ ์ต์ฅ ๊ฐ์ ๋ถ๋ถ ์์ด์ ๊ธธ์ด์ length๋ฅผ ๋ํ๋ค
if longestCount < dp[i] {
longestCount = dp[i]
}
}
return longestCount
}
๐จ Python3
# https://www.acmicpc.net/problem/11722
import sys
def longest_descreasing_sequence_count(sequence):
dp = [0 for _ in range(len(sequence))]
longest_count = 0
for i in range(len(sequence)-1, -1, -1):
dp[i] = 1
length = 0
for j in range(i+1, len(sequence)):
if sequence[i] > sequence[j]:
length = max(length, dp[j])
dp[i] += length
longest_count = max(longest_count, dp[i])
return longest_count
if __name__ == "__main__":
n = int(sys.stdin.readline())
sequence = list(map(int, sys.stdin.readline().split()))
print(longest_descreasing_sequence_count(sequence))
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 2343๋ฒ / ๊ธฐํ ๋ ์จ [Go][Python3] (0) | 2021.04.01 |
---|---|
BOJ / 11055๋ฒ / ๊ฐ์ฅ ํฐ ์ฆ๊ฐํ๋ ๋ถ๋ถ ์์ด [Go][Python3] (0) | 2021.03.31 |
BOJ / 10974๋ฒ / ๋ชจ๋ ์์ด [Go][Python3] (0) | 2021.03.29 |
BOJ / 2960๋ฒ / ์๋ผํ ์คํ ๋ค์ค์ ์ฒด [Go][Python3] (1) | 2021.03.28 |
BOJ / 1057๋ฒ / ํ ๋๋จผํธ [Go][Python3] (0) | 2021.03.27 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- ๋งฅ๋ถํ๋ก
- MongoDB
- dp
- java
- ๋ธ๋ฃจํธํฌ์ค
- ํ
- ์๊ฐ๊ต์ฒด
- ๋ชฝ๊ณ ๋๋น
- ํ๋ก์ด๋์์ฌ
- Algorithm
- BOJ
- ๋ถํ ์ ๋ณต
- AWS
- dfs
- ๋ฐฑ์ค
- ballet
- python3
- ํด์๋งต
- ์๊ณ ๋ฆฌ์ฆ
- BFS
- ์คํ
- Macbook pro 2012 mid 13
- go
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- ์ด๋ถํ์
- Golang
- ๋งฅ๋ถ
- baekjoon
- ๋ฐ๋
- ์๋ฐ
- Total
- Today
- Yesterday