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

[소병희] 겹치는 건 싫어, 쉬운 최단거리, 회전 초밥 #169

Merged
merged 3 commits into from
Sep 3, 2023
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
37 changes: 37 additions & 0 deletions src/main/kotlin/byeonghee/week43/겹치는 건 싫어.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package byeonghee.week43

import java.io.BufferedReader
import java.io.InputStreamReader

class 소병희_겹치는건싫어 {

companion object {
fun solve(): Unit = with(BufferedReader(InputStreamReader(System.`in`))) {
val (n, k) = readLine().split(" ").map { it.toInt() }
val nums = IntArray(n)
val count = IntArray(100001)
var p1 = 0
var p2 = 0
Comment on lines +13 to +14
Copy link
Member

Choose a reason for hiding this comment

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

투 포인터!👍

var ans = 0

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

while(p2 < n) {
count[nums[p2]]++
while(count[nums[p2]] > k && p1 < p2)
count[nums[p1++]]--
Comment on lines +23 to +24
Copy link
Member

Choose a reason for hiding this comment

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

오호 이렇게도 할 수 있군요


p2++
ans = ans.coerceAtLeast(p2 - p1)
}
Comment on lines +21 to +28
Copy link
Member

Choose a reason for hiding this comment

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

엄청 깔끔하게 로직을 작성하셨네요


println(ans)
}
}
}

fun main() {
소병희_겹치는건싫어.solve()
}
69 changes: 69 additions & 0 deletions src/main/kotlin/byeonghee/week43/쉬운 최단거리.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package byeonghee.week43

import java.io.BufferedReader
import java.io.InputStreamReader

class 소병희_쉬운최단거리 {

companion object {
val dr = intArrayOf(1, 0, -1, 0)
val dc = intArrayOf(0, 1, 0, -1)

fun solve(): Unit = with(BufferedReader(InputStreamReader(System.`in`))) {
val (n, m) = readLine().split(" ").map { it.toInt() }
val ground = Array(n) { IntArray(m) { -1 } }
val sb = StringBuilder()

val q = ArrayDeque<IntArray>()

repeat(n) { i ->
readLine().split(" ").forEachIndexed { j, v ->
if (v.toInt() == 2) {
q.add(intArrayOf(i, j, 0))
Copy link
Member

Choose a reason for hiding this comment

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

미리 queue에 넣는게 좋네요

ground[i][j] = 0
}
else if (v.toInt() == 0) {
Comment on lines +21 to +25
Copy link
Member

Choose a reason for hiding this comment

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

2, 0 판단하는 부분 그냥 문자열로 검사하셨으면 사소하지만 연산이 한 번 줄었을 것 같아요!

Copy link
Member

Choose a reason for hiding this comment

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

근데 저도 똑같이 했어요 ㅋㅋㅋㅋㅋ

ground[i][j] = 0
}
}
}

var nr = 0
var nc = 0
var r = 0
var c = 0
var v = 0

while(q.isNotEmpty()) {
q.removeFirst().let {
r = it[0]
c = it[1]
v = it[2]
}

for(d in 0 until 4) {
nr = r + dr[d]
nc = c + dc[d]
if (nr !in 0 until n || nc !in 0 until m) continue
if (ground[nr][nc] == 0) continue
if (ground[nr][nc] != -1) continue
Comment on lines +47 to +49
Copy link
Member

Choose a reason for hiding this comment

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

묶어두는 것 보다는 관심사(?)가 다르면 떨어트려놓는 것도 괜찮은 방법인 것 같군요!

ground[nr][nc] = v + 1
q.add(intArrayOf(nr, nc, v + 1))
}
}

repeat(n) { i ->
repeat(m) { j ->
sb.append("${ground[i][j]} ")
}
sb.appendLine()
}

print(sb)
}
}
}

fun main() {
소병희_쉬운최단거리.solve()
}
35 changes: 35 additions & 0 deletions src/main/kotlin/byeonghee/week43/회전 초밥.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package byeonghee.week43

import java.io.BufferedReader
import java.io.InputStreamReader

class 소병희_회전초밥 {

companion object {
fun solve(): Unit = with(BufferedReader(InputStreamReader(System.`in`))) {
val (n, d, k, c) = readLine().split(" ").map { it.toInt() }
val sushi = IntArray(n) { readLine().toInt() }
val eaten = IntArray(3001)

var kind = 1
var ans = 0
var p1 = 0
eaten[c]++

for(p2 in 0 until n + k - 1) {
if (eaten[sushi[p2%n]]++ == 0) kind++
if (p2 - p1 + 1 < k) continue

ans = ans.coerceAtLeast(kind)
if (--eaten[sushi[p1%n]] == 0) kind--
p1++
}

println(ans)
}
}
}

fun main() {
소병희_회전초밥.solve()
}