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

[Week4] 전현수: 근손실, 톱니바퀴, 트리의 부모 찾기, 회의실 배정, 지름길 #18

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Feat: 회의실 배정 추가
  • Loading branch information
soopeach committed Oct 23, 2022
commit 960965705c56dfb0d017b9a70397cbb669f2d279
52 changes: 52 additions & 0 deletions src/4week/hyunsoo/회의실 배정.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package `4week`.hyunsoo

/**
* <문제>
* [회의실 배정](https://www.acmicpc.net/problem/1931)
*
* 한 개의 회의실이 있는데 이를 사용하고자 하는 N개의 회으ㅔㅇ 대하여 회의실 사용표를 만드려고 함.
* 각 회의 I에 대해 시작시간과 끝나는 시간이 주어져있고, 각 회의가 겹치지 않게 하면서 회의실을 사용할 수 있는 회의의 최대 개수 찾기
*
* 아이디어
* - 회의의 수가 100,000개라 완탐은 불가능
*/

class 전현수_회의실_배정{

data class MeetingData(val start: Int, val end: Int)

fun solution() {

val meetingCnt = readln().toInt()
val meetingList = mutableListOf<MeetingData>()
var canMeetingCnt = 0
var endTime = 0
Copy link
Member

@gongdongho12 gongdongho12 Nov 3, 2022

Choose a reason for hiding this comment

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

항상 전역변수를 선언해줘야한다는 부분을 사용하는 부분에만 적용한다 생각해보시면 아래 endTime을 사용하는 부분에서 var 선언해주면 되지 않았을까 싶어요!


repeat(meetingCnt) {
val (start, end) = readln().split(" ").map { it.toInt() }
meetingList.add(MeetingData(start, end))
}

// 정렬
meetingList.sortWith(compareBy<MeetingData> { it.end }.thenBy { it.start })
Comment on lines +21 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.

소팅을 통해 추가해야 하는 경우에는 PriorityQueue를 이용해봐요
이럴때 쓰라고 만들어진 친구입니다!

val pq = PriorityQueue(compareBy<MeetingData> { it.end }.thenBy { it.start })


// 정렬된 첫 미팅은 반드시 해야지!
endTime = meetingList.first().end
canMeetingCnt++

meetingList.drop(1).forEach { meetingData ->
if (endTime <= meetingData.start) {
endTime = meetingData.end
canMeetingCnt++
}
}
Comment on lines +34 to +42
Copy link
Member

Choose a reason for hiding this comment

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

pq를 사용하면 drop 처리 안하고 그냥 뽑아주면 되겠죠?

endTime = pq.poll().end
canMeetingCnt++

while (pq.isNotEmpty()) {
    val meetingData = pq.poll()
    if (endTime <= meetingData.start) {
        endTime = meetingData.end
        canMeetingCnt++
    }
}


println(canMeetingCnt)
}

}

fun main(){
val myClass = 전현수_회의실_배정()
myClass.solution()
}