diff --git a/src/1week/seoyoon/FindingPrimeNum.kt b/src/1week/seoyoon/FindingPrimeNum.kt index ab0de4d..8f44a8d 100644 --- a/src/1week/seoyoon/FindingPrimeNum.kt +++ b/src/1week/seoyoon/FindingPrimeNum.kt @@ -1,6 +1,6 @@ import kotlin.math.sqrt -class Solution { +class Week1SYSolution { var visited = BooleanArray(7, {false}) val set: MutableSet = mutableSetOf() var depth: Int = 0 diff --git a/src/1week/seoyoon/MaximizeDiff.kt b/src/1week/seoyoon/MaximizeDiff.kt index 509b32e..3204dc3 100644 --- a/src/1week/seoyoon/MaximizeDiff.kt +++ b/src/1week/seoyoon/MaximizeDiff.kt @@ -1,15 +1,15 @@ -val n = readln().toInt() -val arr = readln().split(" ").map{it.toInt()}.toIntArray() -var newArr = IntArray(n) -var visited = BooleanArray(n, {false}) -var max: Int = Int.MIN_VALUE - fun main() { permutation(0) - println(max) } -fun permutation(depth: Int) { +fun permutation(depth: Int): Int { + val n = readln().toInt() + val arr = readln().split(" ").map{it.toInt()}.toIntArray() + var newArr = IntArray(n) + var visited = BooleanArray(n, {false}) + var max: Int = Int.MIN_VALUE + + if (depth == n) { var sum = 0; for (i in 0 until n-1) { @@ -29,4 +29,5 @@ fun permutation(depth: Int) { visited[i] = false } } -} \ No newline at end of file + return max +} diff --git a/src/1week/seoyoon/Minecraft.kt b/src/1week/seoyoon/Minecraft.kt index e425004..1a5b98c 100644 --- a/src/1week/seoyoon/Minecraft.kt +++ b/src/1week/seoyoon/Minecraft.kt @@ -46,4 +46,4 @@ fun solution(n: Int, m: Int, b: Int, ground: Array, high: Int, low: In } } print("$ansSec $ansHigh") -} \ No newline at end of file +} diff --git a/src/1week/seoyoon/PressKeypad.kt b/src/1week/seoyoon/PressKeypad.kt index 5e06ca9..f8d905a 100644 --- a/src/1week/seoyoon/PressKeypad.kt +++ b/src/1week/seoyoon/PressKeypad.kt @@ -1,45 +1,43 @@ import java.lang.Math.abs -class MySolution { - fun solution(numbers: IntArray, hand: String): String { - var answer = "" - var LCur: Int = 10 - var RCur: Int = 12 +fun solution(numbers: IntArray, hand: String): String { + var answer = "" + var LCur: Int = 10 + var RCur: Int = 12 - for (i in 0 until numbers.size) { - if (numbers[i] == 0) numbers[i] = 11 + for (i in 0 until numbers.size) { + if (numbers[i] == 0) numbers[i] = 11 - if (numbers[i] == 1 || numbers[i] == 4 || numbers[i] == 7) { + if (numbers[i] == 1 || numbers[i] == 4 || numbers[i] == 7) { + answer += "L" + LCur = numbers[i] + } + else if (numbers[i] == 3 || numbers[i] == 6 || numbers[i] == 9) { + answer += "R" + RCur = numbers[i] + } + else { + var LDis = abs(numbers[i] - LCur)/3 + abs(numbers[i] - LCur)%3 + var RDis = abs(numbers[i] - RCur)/3 + abs(numbers[i] - RCur)%3 + + if (LDis < RDis) { answer += "L" LCur = numbers[i] } - else if (numbers[i] == 3 || numbers[i] == 6 || numbers[i] == 9) { + else if (RDis < LDis) { answer += "R" RCur = numbers[i] } else { - var LDis = abs(numbers[i] - LCur)/3 + abs(numbers[i] - LCur)%3 - var RDis = abs(numbers[i] - RCur)/3 + abs(numbers[i] - RCur)%3 - - if (LDis < RDis) { - answer += "L" - LCur = numbers[i] - } - else if (RDis < LDis) { + if (hand == "right") { answer += "R" RCur = numbers[i] - } - else { - if (hand == "right") { - answer += "R" - RCur = numbers[i] - } else { - answer += "L" - LCur = numbers[i] - } + } else { + answer += "L" + LCur = numbers[i] } } } - return answer } -} \ No newline at end of file + return answer +} diff --git a/src/1week/seoyoon/QuitTheJob.kt b/src/1week/seoyoon/QuitTheJob.kt index 817339e..11f4ef0 100644 --- a/src/1week/seoyoon/QuitTheJob.kt +++ b/src/1week/seoyoon/QuitTheJob.kt @@ -6,17 +6,17 @@ fun main() { val P = IntArray(n + 1) val arr = IntArray(n + 2) - for (i in 1 .. n) { - val input = readln().split(" ").map{it.toInt()} + for (i in 1..n) { + val input = readln().split(" ").map { it.toInt() } T[i] = input[0] P[i] = input[1] } - for (i in 1 .. n) { + for (i in 1..n) { if (i + T[i] <= n + 1) { arr[i + T[i]] = max(arr[i + T[i]], arr[i] + P[i]) } arr[i + 1] = max(arr[i + 1], arr[i]) } println(arr[n + 1]) -} \ No newline at end of file +} diff --git a/src/1week/seoyoon/SumOfSubsequences.kt b/src/1week/seoyoon/SumOfSubsequences.kt index 0afcdc6..e7a4a97 100644 --- a/src/1week/seoyoon/SumOfSubsequences.kt +++ b/src/1week/seoyoon/SumOfSubsequences.kt @@ -1,10 +1,10 @@ -var ans:Int = 0 +var ans: Int = 0 fun main() { - val (n, s) = readln().split(" ").map{it.toInt()} - val arr = readln().split(" ").map{it.toInt()}.toIntArray() + val (n, s) = readln().split(" ").map { it.toInt() } + val arr = readln().split(" ").map { it.toInt() }.toIntArray() - subSum(n, s, arr,0, 0, 0) + subSum(n, s, arr, 0, 0, 0) if (s == 0) { ans-- diff --git a/src/2week/seoyoon/ATM.kt b/src/2week/seoyoon/ATM.kt new file mode 100644 index 0000000..d94b56c --- /dev/null +++ b/src/2week/seoyoon/ATM.kt @@ -0,0 +1,32 @@ +package `2week`.seoyoon + +class ATM { + companion object { + fun getSolution(): Solution { + return Solution() + } + } + + class Solution { + fun solution(n: Int, arr: IntArray): Int { + var ans = 0 + arr.sort() + + for (i in 0 until n) { + for (j in 0..i) { + ans += arr[j] + } + } + return ans + } + } +} + + +fun main() { + val solution = ATM.getSolution() + val n = Integer.parseInt(readln()) + val arr = readln().split(" ").map { it.toInt() }.toIntArray() + println(solution.solution(n, arr)) +} + diff --git a/src/2week/seoyoon/H-Index.kt b/src/2week/seoyoon/H-Index.kt new file mode 100644 index 0000000..78d93f9 --- /dev/null +++ b/src/2week/seoyoon/H-Index.kt @@ -0,0 +1,25 @@ +package `2week`.seoyoon + +import java.lang.Integer.max +import java.lang.Integer.min + +class `H-Index` { + companion object { + fun getSolution(): Solution { + return Solution() + } + } + + class Solution { + fun solution(citations: IntArray): Int { + var answer = 0 + citations.sort() + + for (i in 0 until citations.size) { + var min = min(citations[i], citations.size - i) + answer = max(min, answer) + } + return answer + } + } +} \ No newline at end of file diff --git a/src/2week/seoyoon/Non-Overlapping Intervals.kt b/src/2week/seoyoon/Non-Overlapping Intervals.kt new file mode 100644 index 0000000..19a3abd --- /dev/null +++ b/src/2week/seoyoon/Non-Overlapping Intervals.kt @@ -0,0 +1,30 @@ +package `2week`.seoyoon + +class `Non-Overlapping Intervals` { + companion object { + fun getSolution(): Solution { + return Solution() + } + } + class Solution { + fun eraseOverlapIntervals(intervals: Array): Int { + var count = 1 + + intervals.sortBy { it[1] } + var prevEnd = intervals.first().get(1) + for (i in 1 until intervals.size) { + val (start, end) = intervals.get(1) + if (prevEnd <= start) { + prevEnd = end + count++ + } + } + return intervals.size - count + } + } +} + +fun main() { + val solution = `Non-Overlapping Intervals`.getSolution() + println(solution.eraseOverlapIntervals(arrayOf(intArrayOf(1, 2), intArrayOf(2, 3), intArrayOf(3, 4), intArrayOf(1, 3)))) +} \ No newline at end of file diff --git "a/src/2week/seoyoon/\352\260\200\354\236\245 \355\201\260 \354\210\230.kt" "b/src/2week/seoyoon/\352\260\200\354\236\245 \355\201\260 \354\210\230.kt" new file mode 100644 index 0000000..073a217 --- /dev/null +++ "b/src/2week/seoyoon/\352\260\200\354\236\245 \355\201\260 \354\210\230.kt" @@ -0,0 +1,11 @@ +package `2week`.seoyoon + +fun main() { + val numbers = intArrayOf(6, 10, 2) + var answer = "" + numbers.sortedWith(Comparator { o1, o2 -> "$o2$o1".compareTo("$o1$o2") }) + .forEach { answer += it } + + if (answer.startsWith("0")) "0" else answer + println(answer) +} \ No newline at end of file diff --git "a/src/2week/seoyoon/\354\235\264\352\261\264 \352\274\255 \355\222\200\354\226\264\354\225\274 \355\225\264!.kt" "b/src/2week/seoyoon/\354\235\264\352\261\264 \352\274\255 \355\222\200\354\226\264\354\225\274 \355\225\264!.kt" new file mode 100644 index 0000000..8f83e0f --- /dev/null +++ "b/src/2week/seoyoon/\354\235\264\352\261\264 \352\274\255 \355\222\200\354\226\264\354\225\274 \355\225\264!.kt" @@ -0,0 +1,32 @@ +package `2week`.seoyoon + +import java.util.* + +fun main() { + val (n, q) = readln().split(" ").map{it.toInt()} + val arrA = readln().split(" ").map { it.toInt() }.toIntArray() + val arrB = Array(q, {IntArray(2)}) + + for (i in 0 until q) { + val st = StringTokenizer(readln()) + arrB[i][0] = Integer.parseInt(st.nextToken()) + arrB[i][1] = Integer.parseInt(st.nextToken()) + } + + val sumArr = IntArray(n) + arrA.sort() + sumArr[0] = arrA[0] + + for (i in 1 until n) { + sumArr[i] = sumArr[i - 1] + arrA[i] + } + + for (i in 0 until q) { + if (arrB[i][0] == 1 || arrB[i][1] == 1) { + println(sumArr[arrB[i][1] - 1]) + } + else { + println(sumArr[arrB[i][1] - 1] - sumArr[arrB[i][0] - 1 - 1]) + } + } +} \ No newline at end of file diff --git "a/src/2week/seoyoon/\354\244\204 \354\204\270\354\232\260\352\270\260.kt" "b/src/2week/seoyoon/\354\244\204 \354\204\270\354\232\260\352\270\260.kt" new file mode 100644 index 0000000..1d83738 --- /dev/null +++ "b/src/2week/seoyoon/\354\244\204 \354\204\270\354\232\260\352\270\260.kt" @@ -0,0 +1,46 @@ +package `2week`.seoyoon + +import java.util.* + +fun main() { + val (n, m) = readln().split(" ").map{it.toInt()} + var degree = IntArray(n+1) + + val graph : Array> = Array(n+1){ ArrayList() } + for (i in 1..n) { + graph[i] = ArrayList() + } + + for (i in 1 .. m) { + val (A, B) = readln().split(" ").map{it.toInt()} + + graph[A].add(B) + degree[B]++ + } + + val queue: Queue = LinkedList() + val result: Queue = LinkedList() + + for (i in 1..n) { + if (degree[i] == 0) { + queue.offer(i) + } + } + + while (!queue.isEmpty()) { + val node = queue.poll() + result.offer(node) + + for (i in graph[node]) { + degree[i]-- + + if (degree[i] == 0) { + queue.offer(i) + } + } + } + + for (i in result) { + print("$i ") + } +} \ No newline at end of file