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

[소병희] 근손실, 비슷한 단어, 아기상어2, 트리의 부모 찾기, 회의실 배정 #27

Merged
merged 6 commits into from
Oct 23, 2022
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
56 changes: 56 additions & 0 deletions src/main/kotlin/byeonghee/6week/근손실.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package byeonghee.`6week`

import java.io.*

class `소병희_근손실` {
companion object {
fun getSolution(): Solution {
return Solution()
}
}

class Solution {
val br = BufferedReader(InputStreamReader(System.`in`))

var n = 0
var k = 0
var workouts = IntArray(0)

val visited = BooleanArray(8) { false }
var curPerm = 0
var answer = 0

fun solution() {
br.readLine().split(" ").map { it.toInt() }.run {
n = first()
k = last()
}
workouts = br.readLine().split(" ").map { it.toInt() - k }.toIntArray()
Copy link
Member

Choose a reason for hiding this comment

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

미리 k를 뺀게 더 이해하기 좋은 것 같아요! 기본값이 변해도 상관없겠네요!


makePerm(0)
println(answer)
}

fun makePerm(len: Int) {
if (len == n) {
answer++
return
}

for(i in 0 until n) {
if (visited[i]) continue
if (curPerm + workouts[i] < 0) continue
Copy link
Member

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.

계산하는 과정에서 0 미만이면 continue하는거 좋네요!


visited[i] = true
curPerm += workouts[i]
makePerm(len + 1)
curPerm -= workouts[i]
visited[i] = false
}
}
}
}

fun main() {
`소병희_근손실`.getSolution().solution()
}
64 changes: 64 additions & 0 deletions src/main/kotlin/byeonghee/6week/비슷한 단어.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package byeonghee.`6week`

import java.io.*

class `소병희_비슷한 단어` {
companion object {
fun getSolution(): Solution {
return Solution()
}
}

class Solution {
val br = BufferedReader(InputStreamReader(System.`in`))
var answer = 0

fun solution() {
val n = br.readLine().toInt()
val first = br.readLine().toList().sorted().joinToString("")
val words = Array(n-1) { br.readLine().toList().sorted().joinToString("") }

words.forEach {
if (first.length > it.length) compare(it, first) else compare(first, it)
}

println(answer)
}

fun compare(short: String, long: String) {
if ((long.length - short.length) > 1) return

if (long.length > short.length) {
for(i in long.indices) {
if (long.removeRange(i, i + 1) == short) {
answer ++
return
}
}
return
}

if (short == long) {
answer ++
return
}

for(i in long.indices) {
if (long[i] == short[i]) continue

for(j in i until short.length) {
if ((long.drop(i + 1) == short.substring(i, j).plus(short.drop(j+1)))
|| (short.drop(i + 1) == long.substring(i, j).plus(long.drop(j+1)))) {
answer ++
return
}
}
return
}
}
}
}

fun main() {
`소병희_비슷한 단어`.getSolution().solution()
}
84 changes: 84 additions & 0 deletions src/main/kotlin/byeonghee/6week/아기 상어 2.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package byeonghee.`6week`

import java.io.*

class `소병희_아기 상어 2` {
companion object {
fun getSolution(): Solution {
return Solution()
}
}

class Solution {
val br = BufferedReader(InputStreamReader(System.`in`))

data class Pos(val r : Int, val c : Int) {
operator fun plus(p : Pos) : Pos {
return Pos(r + p.r, c + p.c)
}

fun isInBound(h: Int, w: Int) : Boolean {
Copy link
Member

Choose a reason for hiding this comment

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

이 함수가 넘 좋은 듯해요!

return r in 0 until h && c in 0 until w
Copy link
Member

Choose a reason for hiding this comment

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

Pos / isInBound를 데이터 클래스의 메서드로 만드신 점이 인상깊습니다..!!

}
}

val mvs = listOf(
Pos(-1, 0),
Pos(-1, 1),
Pos(0, 1),
Pos(1, 1),
Pos(1, 0),
Pos(1, -1),
Pos(0, -1),
Pos(-1, -1)
)

data class Safe(val p: Pos, val d: Int)

var n = 0
var m = 0
var sea = Array(0) { IntArray(0) }

var sharks = ArrayDeque<Safe>()
var cur = Safe(Pos(0, 0), 0)
var nxt = Pos(0, 0)


fun solution() {
br.readLine().split(" ").map{ it.toInt() }.run {
n = first()
m = last()
}
sea = Array(n) { IntArray(m){ Int.MAX_VALUE } }

repeat(n) { r ->
br.readLine().split(" ").let {
for(c in it.indices) {
if (it[c] == "1") sharks.add(Safe(Pos(r, c), 0))
}
}
}

while(sharks.isNotEmpty()) {
cur = sharks.removeFirst()
if (sea[cur.p.r][cur.p.c] <= cur.d) continue

with(cur) {
sea[p.r][p.c] = d
for (mv in mvs) {
nxt = p + mv
if (nxt.isInBound(n, m).not()) continue
if (sea[nxt.r][nxt.c] <= d + 1) continue
sharks.add(Safe(nxt, d + 1))
}
}
}

println(sea.maxOf { it.maxOf{ it } })
}
}
}

fun main() {
`소병희_아기 상어 2`.getSolution().solution()
}
92 changes: 92 additions & 0 deletions src/main/kotlin/byeonghee/6week/트리의 부모 찾기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package byeonghee.`6week`

import java.io.*

class `소병희_트리의 부모 찾기` {
companion object {
fun getSolution(): Solution {
return Solution()
}
}

class Solution {
val br = BufferedReader(InputStreamReader(System.`in`))

var n = 0
var parents = IntArray(0)
var edges = mutableMapOf<Int, MutableList<Int>>()
var q = ArrayDeque<Pair<Int, Int>>()
lateinit var cur: Pair<Int, Int>

fun solution() {
n = br.readLine().toInt()
parents = IntArray(n + 1)

repeat(n-1) { i ->
br.readLine().split(" ").map{ it.toInt() }.run {
edges.getOrPut(first()) { mutableListOf() }
Copy link
Member

Choose a reason for hiding this comment

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

getOrPut 좋은 것 같습니다..!!

Copy link
Member

Choose a reason for hiding this comment

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

getOrPut 배우고 갑니다!

Copy link
Member

Choose a reason for hiding this comment

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

공감합니다!👍

edges[first()]!!.add(last())
edges.getOrPut(last()) { mutableListOf() }
edges[last()]!!.add(first())
if (first() == 1) q.add(Pair(first(), last()))
else if (last() == 1) q.add(Pair(last(), first()))
}
}

while(q.isNotEmpty()) {
cur = q.removeFirst()
parents[cur.second] = cur.first
for(i in edges[cur.second]!!) {
if (parents[i] == 0) q.add(Pair(cur.second, i))
}
}

for(r in 2..n) {
println(parents[r])
}
}
}
}

fun main() {
`소병희_트리의 부모 찾기`.getSolution().solution()
}

/*var n = 0
var parents = IntArray(0)
var depths = IntArray(0)
var child = 0
var parent = 0
fun main() {
n = br.readLine().toInt()
parents = IntArray(n + 1) { -1 }
parents[1] = 0
depths = IntArray(n + 1) { 100001 }
depths[0] = -1
depths[1] = 0
repeat(n-1) {
br.readLine().trim().split(" ").map{ it.toInt() }.run {
if (depths[first()] > depths[last()]) {
updateParents(first(), last())
}
else {
updateParents(last(), first())
}
}
}
for(r in 2..n) {
println(parents[r])
}
}
fun updateParents(child: Int, parent: Int) {
var tmp = parents[child]
parents[child] = parent
depths[child] = depths[parent] + 1
if (tmp == -1) return
updateParents(tmp, child)
}*/
47 changes: 47 additions & 0 deletions src/main/kotlin/byeonghee/6week/회의실 배정.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package byeonghee.`6week`

import java.io.*
import java.util.PriorityQueue

class `소병희_회의실 배정` {
companion object {
fun getSolution(): Solution {
return Solution()
}
}

class Solution {
val br = BufferedReader(InputStreamReader(System.`in`))

data class Meeting(val s: Int, val e: Int)

var n = 0
val meetings = PriorityQueue(Comparator<Meeting> { a, b -> if (a.e == b.e) a.s - b.s else a.e - b.e })
Copy link
Member

Choose a reason for hiding this comment

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

PriorityQueue 써서 정렬할 수도 있군요!

Copy link
Member

Choose a reason for hiding this comment

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

우선순위 큐 자주 사용하시던데 저도 사용해봐야겠어요..!

var answer = 0
var curLastT = 0
lateinit var nxt : Meeting

fun solution() {
n = br.readLine().toInt()
repeat(n) {
br.readLine().split(" ").map{ it.toInt() }.run{
meetings.add(Meeting(first(), last()))
}
}

while(meetings.isNotEmpty()) {
nxt = meetings.poll()
if (curLastT <= nxt.s) {
curLastT = nxt.e
answer++
}
}

println(answer)
}
}
}

fun main() {
`소병희_회의실 배정`.getSolution()
}