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

[소병희] - 내리막 길, 치즈, 가장 긴 증가하는 부분 수열 4, 삼각 달팽이 #270

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package byeonghee.week64

class `소병희_가장 긴 증가하는 부분 수열 4` {
companion object {
fun solve() = with(System.`in`.bufferedReader()) {
val n = readLine().toInt()
val arr = IntArray(n)
val dp = Array(n) { mutableListOf<Int>() }
var answer = 0

readLine().split(" ").forEachIndexed { i, v -> arr[i] = v.toInt() }

dp[0].add(arr[0])
for(i in 1 until n) {
var idx = i
for(j in i - 1 downTo 0) {
if (dp[j].last() < arr[i] && dp[j].size > dp[idx].size) {
idx = j
}
}
Comment on lines +14 to +20
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앞에서부터 탐색하는 게 가능할까 했는데 단순히 앞뒤 차이였군요,,
배열을 저장하는 것까지!
확실히 다른 풀이 보니깐 좋습니다!!👍

if (idx != i) dp[i].addAll(dp[idx])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 addAll 좋아요

dp[i].add(arr[i])
Comment on lines +21 to +22
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오.. 요런식으로 하면 초기화 처리를 앞에 안해줘도 되는군요!


if (dp[i].size > dp[answer].size) answer = i
}

println(dp[answer].size)
println(dp[answer].joinToString(" "))
}
}
}

fun main() {
`소병희_가장 긴 증가하는 부분 수열 4`.solve()
}
44 changes: 44 additions & 0 deletions src/main/kotlin/byeonghee/week64/내리막 길.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package byeonghee.week64

class 소병희_내리막길 {
companion object {
val dr = intArrayOf(0, 1, 0, -1)
val dc = intArrayOf(1, 0, -1, 0)

fun solve() = with(System.`in`.bufferedReader()) {
val (m, n) = readLine().split(" ").map { it.toInt() }
val map = Array(m) { IntArray(n) }
val visited = Array(m) { IntArray(n) { -1 } }

repeat(m) { i ->
readLine().split(" ").forEachIndexed { j, v ->
map[i][j] = v.toInt()
}
}

visited[m-1][n-1] = 1

fun dfs(r: Int, c: Int): Int {
if (visited[r][c] > -1) return visited[r][c]

visited[r][c] = 0
for(d in 0 until 4) {
val nr = r + dr[d]
val nc = c + dc[d]
if (nr !in 0 until m || nc !in 0 until n) continue
if (map[nr][nc] >= map[r][c]) continue

visited[r][c] += dfs(nr, nc)
}

return visited[r][c]
}

println(dfs(0, 0))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 어차피 최종적으로 반환을 하는군요..

}
}
}

fun main() {
소병희_내리막길.solve()
}
41 changes: 41 additions & 0 deletions src/main/kotlin/byeonghee/week64/삼각 달팽이.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package byeonghee.week64

class `소병희_삼각 달팽이` {
fun solution(n: Int): IntArray {
var answer: IntArray = intArrayOf()

val triangle = Array(n) { ArrayDeque<Int>() }
var side = n
var round = 0
var sl = 0
var count = 1

while(side > 0) {
for(i in sl until sl + side) {
triangle[i].add(round, count++)
}

repeat(side - 1) { i ->
triangle[sl + side - 1].add(round + i + 1, count++)
}

for(i in sl + side-2 downTo sl + 1) {
triangle[i].add(triangle[i].size - round, count++)
}

round++
side -= 3
sl += 2
}
Comment on lines +13 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거를 직접 구현하시다니...

Comment on lines +13 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 중간에 넣어주는 것 시뮬레이션 생각하기 쉽지 않을 것 같은데,, 👍


return triangle.flatMap { it.toList() }.toIntArray()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flatMap 배워갑니다!

}
}

fun main() {
val sol = `소병희_삼각 달팽이`()
println(sol.solution(4).joinToString(" "))
println(sol.solution(5).joinToString(" "))
println(sol.solution(6).joinToString(" "))
println(sol.solution(7).joinToString(" "))
}
60 changes: 60 additions & 0 deletions src/main/kotlin/byeonghee/week64/치즈.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package byeonghee.week64

class 소병희_치즈 {
companion object {
val dr = intArrayOf(0, 1, 0, -1)
val dc = intArrayOf(1, 0, -1, 0)

fun solve() = with(System.`in`.bufferedReader()) {
val (n, m) = readLine().split(" ").map { it.toInt() }
val paper = Array(n) { IntArray(m) }
var airContact = Array(n) { IntArray(m) }
val q = ArrayDeque<IntArray>()
var time = 0
var cheese = 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

미리 치즈 개수를 세어놓는 것도 좋네요!!


repeat(n) { i ->
readLine().split(" ").forEachIndexed { j, v ->
paper[i][j] = v.toInt().also { if (it == 1) cheese++ }
}
}

fun checkAir() {
airContact = Array(n) { IntArray(m) }
airContact[0][0]++
q.add(intArrayOf(0, 0))

while(q.isNotEmpty()) {
val (r, c) = q.removeFirst()
for(d in 0 until 4) {
val nr = r + dr[d]
val nc = c + dc[d]
if (nr !in 0 until n || nc !in 0 until m) continue
if (paper[nr][nc] == 0 && airContact[nr][nc] > 0) continue

airContact[nr][nc]++
if (paper[nr][nc] == 1) continue
q.add(intArrayOf(nr, nc))
}
}
}
Comment on lines +22 to +40
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

바깥의 공기들만 돌면서 치즈와 만나면 맞닿은 공기 개수까지 처리를 하는군요 👍🫡


while(cheese > 0) {
checkAir()
for(i in 0 until n) for(j in 0 until m) {
if (paper[i][j] == 1 && airContact[i][j] >= 2) {
paper[i][j] = 0
cheese--
}
}
time++
}

println(time)
}
}
}

fun main() {
소병희_치즈.solve()
}