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

[장희직] - 겹치는 건 싫어, 쉬운 최단거리, 회전 초밥, 기타리스트 #168

Merged
merged 4 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
50 changes: 50 additions & 0 deletions src/main/kotlin/heejik/43week/겹치는 건 싫어.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package heejik.`43week`

import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.StreamTokenizer
import kotlin.math.max
import kotlin.properties.Delegates

class `겹치는 건 싫어` {
var n by Delegates.notNull<Int>()
var k by Delegates.notNull<Int>()
lateinit var numbers: List<Int>
fun setting() = with(BufferedReader(InputStreamReader(System.`in`))) {
readLine().split(' ').map { it.toInt() }.run {
n = this[0]
k = this[1]
}
numbers = readLine().split(' ').map { it.toInt() }
}


fun solve() {
setting()

var answer = 0
val stored = ArrayDeque<Int>()
val counts = MutableList(100001) { 0 }

for (number in numbers) {
val numberCount = counts[number]
if (numberCount == k) {
answer = max(answer, stored.size)
while (true) {
val removeNumber = stored.removeFirst()
Copy link
Contributor

Choose a reason for hiding this comment

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

오 큐로 처리하는 거 못지네요

counts[removeNumber]--
if (removeNumber == number) break
}
Comment on lines +33 to +37
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 +33 to +37
Copy link
Member

Choose a reason for hiding this comment

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

while문으로 미리 갱신해버리는거 좋네용

}
stored.add(number)
counts[number]++
}
answer = max(answer, stored.size)

println(answer)
}
}

fun main() {
`겹치는 건 싫어`().solve()
}
52 changes: 52 additions & 0 deletions src/main/kotlin/heejik/43week/기타리스트.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package heejik.`43week`

import kotlin.math.max
import kotlin.properties.Delegates

class 기타리스트 {

lateinit var differences: MutableList<Int>
lateinit var dp: MutableList<MutableList<Boolean>>
var n by Delegates.notNull<Int>()
var s by Delegates.notNull<Int>()
var m by Delegates.notNull<Int>()
var answer = -1

fun solve() {
readln().split(' ').map { it.toInt() }.run {
n = this[0]
s = this[1]
m = this[2]
}
differences = readln().split(' ').map { it.toInt() }.toMutableList()
dp = MutableList(n + 1) { MutableList(m + 1) { false } }
Copy link
Contributor

@bngsh bngsh Sep 3, 2023

Choose a reason for hiding this comment

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

아마 범위가 0에서m으로 제한되니까 매 곡마다 0-m범위만 확인하면 되서 2^n까지 안 가게 되는 거 아닐까요...! 앞서서 범위를 초과하는 경우의수를 차단해버리면 뒤에선 그 경우의 수를 탐색하지 않아도 돼서...!? 이게 아니었군요 ㅋㅋㅋ 재귀면 특정 깊이에서 같은 값인 경우가 중복돼서인가봐요

Copy link
Member Author

Choose a reason for hiding this comment

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

명강의 감사합니다 크 👍👍👍👍👍

dp[0][s] = true

play().also {
println(it)
}
}

private fun play(): Int {
repeat(n) first@{ songIdx ->
repeat(m + 1) second@{ volume ->
if (dp[songIdx][volume].not()) return@second
val plusDiff = volume + differences[songIdx]
val minusDiff = volume - differences[songIdx]

if (plusDiff in 0..m) {
dp[songIdx + 1][plusDiff] = true
}
if (minusDiff in 0..m) {
dp[songIdx + 1][minusDiff] = true
}
}
}

return dp[n].lastIndexOf(true)
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()
}
74 changes: 74 additions & 0 deletions src/main/kotlin/heejik/43week/쉬운 최단거리.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package heejik.`43week`

import kotlin.properties.Delegates

class `쉬운 최단거리` {

val dx = listOf(1, -1, 0, 0)
val dy = listOf(0, 0, 1, -1)

data class Pos(
val x: Int,
val y: Int
)

lateinit var target: Pos
var n by Delegates.notNull<Int>()
var m by Delegates.notNull<Int>()
val board = mutableListOf<MutableList<Int>>()

fun solve() = with(System.`in`.bufferedReader()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

헐 훨씬 짧아지네요 바로 따라해

Copy link
Member

Choose a reason for hiding this comment

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

와 좋다


readLine().split(' ').map { it.toInt() }.run {
n = this[0]
m = this[1]
}

repeat(n) {
val row = readLine().split(' ').map {
if (it == "1") -1
else it.toInt()
}
Comment on lines +28 to +31
Copy link
Member

Choose a reason for hiding this comment

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

완전 예리하시네요

if (row.contains(2)) {
target = Pos(x = it, y = row.indexOf(2))
}
Comment on lines +32 to +34
Copy link
Member

Choose a reason for hiding this comment

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

요거 항상 참인 조건문 아닌가요?

board.add(row.toMutableList())
}

bfs(target.x, target.y)

val sb = StringBuilder()
repeat(n) { x ->
repeat(m) { y ->
sb.append("${board[x][y]} ")
}
sb.appendLine()
}

print(sb)
}

private fun bfs(_x: Int, _y: Int) {
val queue = ArrayDeque<Pos>()
queue.add(Pos(_x, _y))
board[_x][_y] = 0

while (queue.isNotEmpty()) {
val (x, y) = queue.removeFirst()
for (i in dx.indices) {
val nx = x + dx[i]
val ny = y + dy[i]
if (nx in 0 until n && ny in 0 until m) {
if (board[nx][ny] == -1) {
queue.add(Pos(nx, ny))
board[nx][ny] = board[x][y] + 1
}
}
}
}
}
}

fun main() {
`쉬운 최단거리`().solve()
}
39 changes: 39 additions & 0 deletions src/main/kotlin/heejik/43week/회전 초밥.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package heejik.`43week`

import kotlin.math.max

class `회전 초밥` {

fun solve() = with(System.`in`.bufferedReader()) {
var answer = 0

val belt = mutableListOf<Int>()

val (n, d, k, c) = readLine().split(' ').map { it.toInt() }

repeat(n) {
val sushi = readLine().toInt()
belt.add(sushi)
}
// 맨 뒤에 k - 1 만큼 앞에 놈들 추가
repeat(k - 1) {
belt.add(belt[it])
}

repeat(n) { i ->
val partBeltSet = belt.subList(i, i + k).toSet()
Copy link
Contributor

Choose a reason for hiding this comment

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

toSet 똑똑하네요...set.add로 하려고 했었는데!!

answer = if (partBeltSet.contains(c)) {
max(answer, partBeltSet.size)
} else {
max(answer, partBeltSet.size + 1)
}
}

println(answer)
}
}


fun main() {
`회전 초밥`().solve()
}