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] 김재원 : 트리의 부모 찾기, 회의실 배정, 주차 요금 계산 #24

Merged
merged 3 commits into from
Nov 6, 2022
Merged
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
42 changes: 42 additions & 0 deletions src/3week/jaewon/주차 요금 계산.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class jaewon_Solution {
fun solution(fees: IntArray, records: Array<String>): IntArray {
var answer: IntArray = intArrayOf()
val keys = records.groupBy{it.split(" ")[1]}
val blank = keys.keys.associateWith{0}.toMutableMap()

keys.values.forEach{ key ->
val times = key.map{(it.split(" ")[0].split(":"))}.flatten().toMutableList()
if(times.size%4 != 0){
times.add("23")
times.add("59")
}
var i = 0
var total = 0
repeat(times.size/4){
// 입출차 셋트
var h = (times[i+2].toInt()-times[i].toInt()) * 60
var m = times[i+3].toInt()-times[i+1].toInt()
total += h+m
i+=4
}
blank[key[0].split(" ")[1]] = total
}

answer = blank.mapValues{
val v = it.value
var temp = 0
if(fees[0] < v){
val add = if((v-fees[0])%fees[2] != 0) (v-fees[0])/fees[2]+1 else (v-fees[0])/fees[2]
temp = add*fees[3]
}
fees[1] + temp
}.toList().sortedBy{it.first}.map{it.second}.toIntArray()
return answer
}
}

fun main(){
jaewon_Solution().solution(
intArrayOf(180,5000,10,600), arrayOf("05:34 5961 IN", "06:00 0000 IN", "06:34 0000 OUT", "07:59 5961 OUT", "07:59 0148 IN", "18:59 0000 IN", "19:09 0148 OUT", "22:59 5961 IN", "23:00 5961 OUT")
)
}
32 changes: 32 additions & 0 deletions src/3week/jaewon/트리의 부모 찾기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.lang.StringBuilder
import java.util.LinkedList

fun main(){
val n = readln().toInt()
val tree = Array(n+1){ LinkedList<Int>()}
val visited = BooleanArray(n+1){ false }
val parents = IntArray(n+1){-1}
//인접리스트 만들기
repeat(n-1){
val (a,b) = readln().split(" ").map { it.toInt() }
tree[a].add(b)
tree[b].add(a)
}

fun dfs(n: Int){
for(num in tree[n]){
if(!visited[num]) {
visited[num] = true
parents[num] = n
dfs(num)
}
}
}

dfs(1)
val answer = StringBuilder()
for (i in 2..n){
answer.append("${parents[i]}\n")
}
println(answer.toString())
}
33 changes: 33 additions & 0 deletions src/3week/jaewon/회의실 배정.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

class jaewon회의실배정 {

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

fun solution(){
val n = readln().toInt()
val timeTable = mutableListOf<TimeTable>()
var endTime = 0
var count = 0

repeat(n){
val (start,end) = readln().split(" ").map { it.toInt() }
timeTable.add(TimeTable(start, end))
}

timeTable
.sortedWith( compareBy<TimeTable> { it.end }.thenBy { -(it.end - it.start) })
.forEach {
if (it.start >= endTime){
count++
endTime = it.end
}
}

println(count)
}
}

fun main(){
jaewon회의실배정().solution()
}