Skip to content

Commit

Permalink
Merge pull request #27 from AlgorithmWithMe/byeonghee/6week
Browse files Browse the repository at this point in the history
[소병희] 근손실, 비슷한 단어, 아기상어2, 트리의 부모 찾기, 회의실 배정
  • Loading branch information
bngsh authored Oct 23, 2022
2 parents 982eb63 + 376afb4 commit d9f6710
Show file tree
Hide file tree
Showing 5 changed files with 343 additions and 0 deletions.
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()

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

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 {
return r in 0 until h && c in 0 until w
}
}

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() }
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 })
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()
}

0 comments on commit d9f6710

Please sign in to comment.