[Week4] 전현수: 근손실, 톱니바퀴, 트리의 부모 찾기, 회의실 배정, 지름길 - #18
Conversation
There was a problem hiding this comment.
이거 왜 안지우는거에요? Iterator는 그냥 자동으로 destructure 지원되서 사용 안해도 되요!
There was a problem hiding this comment.
@soopeach 이건 어떠세요?
val (gear1, gear2, gear3, gear4) = gearListThere was a problem hiding this comment.
이부분 exerciseKitList.forEachIndexed 했으면 어땠을까요?
gongdongho12
left a comment
There was a problem hiding this comment.
4주차 하나 드디어 끝냈네요
늦어져서 죄송합니다 ㅠ
There was a problem hiding this comment.
이부분 코틀린의 groupBy를 활용해보면 쓸데없는 데이터는 안 만들 수 있지 않을까요?
val shortCutGraph: Map<Int, List<ShortCutData>> = (0 until shortCutCnt).mapNotNull {
val (start, end, shortCutDistance) = readln().split(" ").map { it.toInt() }
if (end <= highWayLength && (end - start) > shortCutDistance) {
end to ShortCutData(start, shortCutDistance)
} else null
}.groupBy({ it.first }, { it.second })There was a problem hiding this comment.
이부분도 minOf를 써서 잘 활용해서
(1..highWayLength).forEach { end ->
dp[end] = dp[end - 1] + 1
dp[end] = shortCutGraph[end]?.minOf {
dp[it.start] + it.distance
}?.takeIf { it < dp[end] } ?: dp[end]
}minOf 한 값이 dp[end] 보다 작으면 진짜 최솟값인거고 아니면 null 리턴해서 dp[end]를 리턴하는거에요!
There was a problem hiding this comment.
class Gear(val state: IntArray) {
constructor(str: String): this(str.map { it.digitToInt() }.toIntArray())There was a problem hiding this comment.
val gearList = Array(4) {
Gear(readln())
}.toList()
val affectedInfo = Array(4) {
mutableListOf<Int>()
}
fun solution() {
val rotateCnt = readln().toInt()
val rotateInfoList = Array(rotateCnt) {
val (gearNum, dir) = readln().split(" ").map { it.toInt() }
RotateInfo(gearNum, dir)
}
rotateInfoList.forEach { rotateInfo ->
val (gearNum, dir) = rotateInfo
gearList.windowed(2, 1).forEachIndexed { index, gears ->
val (prev, next) = gears.map { it.getPole() }
if (prev.second != next.first) {
affectedInfo[index].add(index + 1)
affectedInfo[index + 1].add(index)
}
}어... 이런거 있을거같은데? 찾아보면 있더라구요!
머리를 쓰지 않으면 고생합니다 ㅠ
저도
windowed같은게 있는지 몰랐어요
There was a problem hiding this comment.
이건 이제 안할때 된거같지만
val score = gearList.mapIndexed { index, gear ->
gear.getTopState() * twoToPowOfExponent(index)
}.sum()택 1
val score = (0 until 4).sumOf { gearList[it].getTopState() * twoToPowOfExponent(it) }There was a problem hiding this comment.
중첩되지 않고 사실 순서보다는 실제 Collection의 값을 삭제하는 데에 초점을 맞췄게 때문에
mutableList 보다는 mutableSet 사용하시는게 이 로직에서는 훨씬 효율적이에요!
There was a problem hiding this comment.
MutableSet이면 더 효율적으로 삭제될 수 있는 이유
There was a problem hiding this comment.
소팅을 통해 추가해야 하는 경우에는 PriorityQueue를 이용해봐요
이럴때 쓰라고 만들어진 친구입니다!
val pq = PriorityQueue(compareBy<MeetingData> { it.end }.thenBy { it.start })There was a problem hiding this comment.
pq를 사용하면 drop 처리 안하고 그냥 뽑아주면 되겠죠?
endTime = pq.poll().end
canMeetingCnt++
while (pq.isNotEmpty()) {
val meetingData = pq.poll()
if (endTime <= meetingData.start) {
endTime = meetingData.end
canMeetingCnt++
}
}There was a problem hiding this comment.
항상 전역변수를 선언해줘야한다는 부분을 사용하는 부분에만 적용한다 생각해보시면 아래 endTime을 사용하는 부분에서 var 선언해주면 되지 않았을까 싶어요!
Issue Number
Issue #17
💎 문제 해결
🔥 특이사항/질문
자유롭게 작성해주세요!
오호... 별건 아니지만... dp문제 거의 처음으로 혼자 풀어봐서 기쁘네요,,,