Skip to content

Commit 908096e

Browse files
committed
refactor : dongho님 review 반영
1 parent 41f21c9 commit 908096e

File tree

5 files changed

+42
-43
lines changed

5 files changed

+42
-43
lines changed

src/4week/acw/근손실.kt

+2-6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ class `acw근손실` {
88

99
fun makePermutation(arr: MutableList<Int>, power: Int, day: Int, N: Int, K: Int) {
1010
//순열구성하기
11-
var powerNow = power
12-
if (arr.isNotEmpty()) {
13-
powerNow = power + arr.last() - K
14-
}
11+
val powerNow = power + (arr.takeIf { it.isNotEmpty() }?.let { it.last() - K } ?: 0)
1512

1613
if (powerNow < 500) {
1714
return
@@ -50,5 +47,4 @@ class `acw근손실` {
5047
fun main() {
5148
val sol = `acw근손실`()
5249
sol.solution()
53-
}
54-
50+
}

src/4week/acw/주차요금계산.kt

+22-8
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,48 @@ class Solution {
55
val recordsArr = Array(10000) { LinkedList<String>() }
66
val feeArr = Array(10000) { 0 }
77

8+
data class Time(var hour: Int, var min: Int) {
9+
10+
constructor(hourAndMin: List<Int>) : this(hourAndMin[0], hourAndMin[1]) {
11+
this.hour = hourAndMin[0]
12+
this.min = hourAndMin[1]
13+
}
14+
15+
companion object {
16+
fun calcUsingTime(inTime: Time, outTime: Time): Int {
17+
return (outTime.hour * 60 + outTime.min - inTime.hour * 60 - inTime.min)
18+
}
19+
}
20+
}
21+
22+
fun String.timeSplit() = split(":").map { it.toInt() }
23+
824
fun solution(fees: IntArray, records: Array<String>): IntArray {
925
var answer: IntArray = intArrayOf()
26+
1027
for (i in records) {
1128
val (time, num, inout) = i.split(" ")
1229
if (inout == "IN") {
1330
recordsArr[num.toInt()].add(time)
1431
} else {
1532
//out이 나올경우에는 바로 해당번호에 대해 처리를 해준다.
16-
val inTime = recordsArr[num.toInt()].removeLast()
33+
val inTime = Time((recordsArr[num.toInt()].removeLast().timeSplit()))
34+
val outTime = Time(time.timeSplit())
1735

18-
val inHM = inTime.split(":").map { it.toInt() }
19-
val outHM = time.split(":").map { it.toInt() }
20-
21-
val usingTime = outHM[0] * 60 + outHM[1] - inHM[0] * 60 - inHM[1]
36+
val usingTime = Time.calcUsingTime(inTime, outTime)
2237
feeArr[num.toInt()] += usingTime
2338
}
2439
}
2540

2641
for (i in 0 until 10000) {
2742
while (recordsArr[i].isNotEmpty()) {
28-
val now = recordsArr[i].removeFirst().split(":").map { it.toInt() }
29-
val usingTime = 23 * 60 + 59 - now[0] * 60 - now[1]
43+
val now = Time(recordsArr[i].removeFirst().timeSplit())
44+
val usingTime = Time.calcUsingTime(Time(23, 59), now)
3045
feeArr[i] += usingTime
3146
}
3247
}//In은 했는데 Out은 안한경우 위에서 처리가 안되므로 처음부터 돌면서 23:59에서 빼준다.
3348

3449

35-
3650
//요금 계산하기
3751
val arr = mutableListOf<Int>()
3852
for (i in 0 until 10000) {

src/4week/acw/톱니바퀴.kt

+9-9
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ class `acw톱니바퀴` {
1414
var turn = arrayListOf<Pair<Int, Int>>()
1515

1616

17-
fun addTurn(w: Int, turnD: Int) {
17+
private fun addTurn(w: Int, turnD: Int) {
1818
//dfs로 들어가면서 회전해야할 것들 turn에 추가해주기
1919

2020

21-
var wheelNowRight = if (wheel[w] and (1 shl RIGHT) != 0) 1 else 0
22-
var wheelNowLeft = if (wheel[w] and (1 shl LEFT) != 0) 1 else 0
21+
val wheelNowRight = if (wheel[w] and (1 shl RIGHT) != 0) 1 else 0
22+
val wheelNowLeft = if (wheel[w] and (1 shl LEFT) != 0) 1 else 0
2323

2424
if (w - 1 >= 0) {
2525
val leftWheelsRight = if (wheel[w - 1] and (1 shl RIGHT) != 0) 1 else 0
26-
2726
if (wheelNowLeft != leftWheelsRight && !visit[w - 1]) {
2827
visit[w - 1] = true
2928
turn.add(Pair(w - 1, turnD * -1))
@@ -43,7 +42,7 @@ class `acw톱니바퀴` {
4342

4443
}
4544

46-
fun turnWheel() {
45+
private fun turnWheel() {
4746

4847
while (turn.isNotEmpty()) {
4948
val (w, d) = turn.removeFirst()
@@ -100,10 +99,11 @@ class `acw톱니바퀴` {
10099
}
101100

102101

103-
var answer = 0
104-
for (i in 0 until 4) {
105-
if ((wheel[i] and (1 shl 7)) != 0) {
106-
answer += (2.0).pow(i).toInt()
102+
val answer = wheel.withIndex().sumOf { (idx, value) ->
103+
if (value and (1 shl 7) != 0) {
104+
(2.0).pow(idx).toInt()
105+
} else {
106+
0
107107
}
108108
}
109109

src/4week/acw/트리의부모찾기.kt

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package `4week`.acw
22

33
class `acw트리의부모찾기` {
4-
lateinit var treeMap: Array<MutableList<Int>>
4+
lateinit var treeMap: Array<MutableSet<Int>>
55
lateinit var parent: Array<Int>
66
var N = 0
77

@@ -18,7 +18,7 @@ class `acw트리의부모찾기` {
1818

1919
fun solution() {
2020
N = readln().toInt()
21-
treeMap = Array(N + 1) { mutableListOf() }
21+
treeMap = Array(N + 1) { mutableSetOf() }
2222
parent = Array(N + 1) { 0 }
2323

2424
for (i in 1 until N) {
@@ -29,9 +29,7 @@ class `acw트리의부모찾기` {
2929
}
3030

3131
dfs(1)
32-
for (i in 2..N) {
33-
println(parent[i])
34-
}
32+
println(parent.drop(2).joinToString("\n"))
3533

3634
}
3735
}

src/4week/acw/회의실배정.kt

+6-15
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
package `4week`.acw
22

3-
43
import java.util.PriorityQueue
54

6-
class `acw회의실배정` {
7-
val conferance = PriorityQueue<Pair<Int, Int>>(
8-
Comparator { a, b ->
9-
if (a.second - b.second == 0) {
10-
a.first - b.first
11-
} else {
12-
a.second - b.second
13-
}
14-
}
155

6+
class `acw회의실배정` {
7+
val conferance = PriorityQueue(
8+
compareBy<Pair<Int, Int>> {it.second}.thenBy { it.first }
169
)
1710
// 종료시간이 빠른것을 기준으로 정렬한다.
1811
// 종료시간이 같을 경우 시작시간이 더 빠른것을 위주로 정렬해야한다. -> 시작시간과 종료시간이 같은 회의가 있을 수 있기 때문
1912
// ex) 5,6 / 6,6 이 있는데 6,6이 먼저 뽑힐 경우 56 /66이 모두 사용될 수 있음에도 불구하고 56은 선택되지 못한다.
2013

2114
fun solution() {
2215
val N = readln().toInt()
23-
var answer = 0
16+
var answer = 1
2417
var lastConferance = Pair(0, 0)
2518

2619
repeat(N) {
@@ -29,14 +22,11 @@ class `acw회의실배정` {
2922

3023
}
3124
lastConferance = conferance.poll()
32-
answer++
3325

3426

3527
while (conferance.isNotEmpty()) {
3628
val nowConferance = conferance.poll()
37-
if (nowConferance.first < lastConferance.second) {
38-
continue
39-
} else {
29+
if (nowConferance.first >= lastConferance.second) {
4030
lastConferance = nowConferance
4131
answer++
4232
}
@@ -53,3 +43,4 @@ fun main() {
5343
val sol = `acw회의실배정`()
5444
sol.solution()
5545
}
46+

0 commit comments

Comments
 (0)