diff --git a/src/2week/dongho/ATM.kt b/src/2week/dongho/ATM.kt new file mode 100644 index 0000000..4217fae --- /dev/null +++ b/src/2week/dongho/ATM.kt @@ -0,0 +1,17 @@ +package `2week`.dongho + +import java.io.* +import java.util.* + +fun main() { + val br = BufferedReader(InputStreamReader(System.`in`)) + br.readLine() + val pq = PriorityQueue(br.readLine().split(" ").map { it.toInt() }.toList()) + var temp = 0 + var answer = 0 + while (!pq.isEmpty()) { + temp += pq.poll() + answer += temp + } + println(answer) +} \ No newline at end of file diff --git a/src/2week/dongho/H-index.kt b/src/2week/dongho/H-index.kt new file mode 100644 index 0000000..1e5431e --- /dev/null +++ b/src/2week/dongho/H-index.kt @@ -0,0 +1,31 @@ +package `2week`.dongho + +class `H-index` { + companion object { + fun getSolution(): Solution { + return Solution() + } + } + + class Solution { + fun solution(citations: IntArray): Int { + var answer = 0 + citations.sort() + val length = citations.size + for (i in 0 until length) { + val diff = length - i + val v = citations[i] + if (diff <= v) { + answer = diff + break + } + } + return answer + } + } +} + +fun main() { + val solution = `H-index`.getSolution() + println(solution.solution(intArrayOf(3, 0, 6, 1, 5))) +} \ No newline at end of file diff --git a/src/2week/dongho/Non-overlappingIntervals.kt b/src/2week/dongho/Non-overlappingIntervals.kt new file mode 100644 index 0000000..38129da --- /dev/null +++ b/src/2week/dongho/Non-overlappingIntervals.kt @@ -0,0 +1,52 @@ +package `2week`.dongho + +class `Non-overlappingIntervals` { + companion object { + fun getSolution(): Solution { + return Solution() + } + } + + class Solution { + fun eraseOverlapIntervals(intervals: Array): Int { + var connectedCount = 1 + val length = intervals.size + intervals.sortBy { it[1] } +// intervals.sortWith(Comparator { a: IntArray, b: IntArray -> a[1] - b[1] }) + var prevEnd = intervals.first().get(1) + for (i in 1 until length) { + val (start, end) = intervals.get(i) + if (prevEnd <= start) { + prevEnd = end + connectedCount++ + } + } + return length - connectedCount + } + } +} + +fun main() { + val solution = `Non-overlappingIntervals`.getSolution() + println(solution.eraseOverlapIntervals(arrayOf( + intArrayOf(1, 2), + intArrayOf(2, 3), + intArrayOf(3, 4), + intArrayOf(1, 3) + ))) + println(solution.eraseOverlapIntervals(arrayOf( + intArrayOf(1, 2), + intArrayOf(1, 2), + intArrayOf(1, 2) + ))) + println(solution.eraseOverlapIntervals(arrayOf( + intArrayOf(1, 2), + intArrayOf(2, 3), + ))) + println(solution.eraseOverlapIntervals(arrayOf( + intArrayOf(1, 100), + intArrayOf(11, 22), + intArrayOf(1, 11), + intArrayOf(2, 12), + ))) +} \ No newline at end of file diff --git "a/src/2week/dongho/\352\260\200\354\236\245 \355\201\260 \354\210\230.kt" "b/src/2week/dongho/\352\260\200\354\236\245 \355\201\260 \354\210\230.kt" new file mode 100644 index 0000000..8f9bdcb --- /dev/null +++ "b/src/2week/dongho/\352\260\200\354\236\245 \355\201\260 \354\210\230.kt" @@ -0,0 +1,24 @@ +package `2week`.dongho + +class `가장 큰 수` { + companion object { + fun getSolution(): Solution { + return Solution() + } + } + + class Solution { + + fun solution(numbers: IntArray): String { + val answer = numbers.sortedWith { a, b -> ("$b$a").compareTo("$a$b") }.joinToString("") + return if (answer.first() === '0') "0" else answer + } + } + +} + +fun main() { + val solution = `가장 큰 수`.getSolution() + println(solution.solution(intArrayOf(6, 10, 2))) + println(solution.solution(intArrayOf(3, 30, 34, 5, 9))) +} \ No newline at end of file diff --git "a/src/2week/dongho/\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/dongho/\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..576e48d --- /dev/null +++ "b/src/2week/dongho/\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,20 @@ +package `2week`.dongho + +import java.io.* + +fun main() { + val br = BufferedReader(InputStreamReader(System.`in`)) + val bw = BufferedWriter(OutputStreamWriter(System.out)) + val (_, Q) = br.readLine().split(" ").map { it.toInt() } + val B = br.readLine().split(" ").map { it.toInt() }.sorted() + val Bsum = B.fold(mutableListOf(0)) { prev, next -> + prev.add((prev.lastOrNull() ?: 0) + next) + prev + } + repeat(Q) { + val (L, R) = br.readLine().split(" ").map { it.toInt() } + bw.write("${Bsum.get(R) - Bsum.get(L - 1)}\n") + } + bw.flush() + bw.close() +} \ No newline at end of file diff --git "a/src/2week/dongho/\354\244\204 \354\204\270\354\232\260\352\270\260.kt" "b/src/2week/dongho/\354\244\204 \354\204\270\354\232\260\352\270\260.kt" new file mode 100644 index 0000000..2c18176 --- /dev/null +++ "b/src/2week/dongho/\354\244\204 \354\204\270\354\232\260\352\270\260.kt" @@ -0,0 +1,44 @@ +package `2week`.dongho + +import java.io.* +import java.util.* + +// 이쪽 풀이 참고하시면 좋을것 같아요 설명 잘되어있음! +// https://blog.naver.com/PostView.nhn?blogId=sweetgirl0111&logNo=222227941751&parentCategoryNo=&categoryNo=&viewDate=&isShowPopularPosts=false&from=postView +fun main() { + val br = BufferedReader(InputStreamReader(System.`in`)) + val (N, M) = br.readLine().split(" ").map { it.toInt() } + + val indegree = IntArray(N + 1) + val graph: MutableList> = ArrayList() + for (i in 0 until N + 1) { + graph.add(ArrayList()) + } + for (i in 0 until M) { + val (a, b) = br.readLine().split(" ").map { it.toInt() } + graph[a].add(b) + indegree[b]++ + } + + val q: Queue = LinkedList() + val result: Queue = LinkedList() + for (i in 1 until indegree.size) { + if (indegree[i] == 0) { + q.offer(i) + } + } + + var current: Int + while (q.isNotEmpty()) { + current = q.poll() + result.offer(current) + for (i in graph[current]) { + indegree[i]-- + if (indegree[i] == 0) { + q.offer(i) + } + } + } + + println(result.joinToString(" ")) +} \ No newline at end of file