diff --git a/src/main/kotlin/hyunsoo/13week/DNA.kt b/src/main/kotlin/hyunsoo/13week/DNA.kt new file mode 100644 index 00000000..db53bb09 --- /dev/null +++ b/src/main/kotlin/hyunsoo/13week/DNA.kt @@ -0,0 +1,48 @@ +package hyunsoo.`13week` + +/** + * + * <문제> + * [DNA](https://www.acmicpc.net/problem/1969) + * + * Hamming Distance는 길이가 같은 DNA에서 각 위치의 문자가 다른 것 + * 길이가 M인 N개의 DNA 중 Hamming Distance이 가장 작은 DNA를 구하기 + * + * 문제 이해를 못해서 고생했습니다... + */ + +fun main() { + + val alphabetMap = hashMapOf() + val dnaList = mutableListOf() + val (dnaCnt, dnaLength) = readln().split(" ").map { it.toInt() } + val dnaBuilder = StringBuilder() + var hammingDistance = 0 + + repeat(dnaCnt) { + dnaList.add(readln()) + } + + for (i in 0 until dnaLength) { + for (j in 0 until dnaCnt) { + val curAlphabet = dnaList[j][i] + alphabetMap[curAlphabet] = + alphabetMap.getOrDefault(curAlphabet, 0) + 1 + } + + val mostFrequentAlphabet = alphabetMap + .toSortedMap() + .maxBy { it.value }.key + + hammingDistance += + alphabetMap.filter { it.key != mostFrequentAlphabet } + .map { it.value }.sum() + + dnaBuilder.append(mostFrequentAlphabet) + alphabetMap.clear() + } + + println(dnaBuilder) + println(hammingDistance) + +} diff --git "a/src/main/kotlin/hyunsoo/13week/\352\270\260\354\240\201\354\235\230 \353\247\244\353\247\244\353\262\225.kt" "b/src/main/kotlin/hyunsoo/13week/\352\270\260\354\240\201\354\235\230 \353\247\244\353\247\244\353\262\225.kt" new file mode 100644 index 00000000..0f79550e --- /dev/null +++ "b/src/main/kotlin/hyunsoo/13week/\352\270\260\354\240\201\354\235\230 \353\247\244\353\247\244\353\262\225.kt" @@ -0,0 +1,88 @@ +package hyunsoo.`13week` + +/** + * + * <문제> + * [기적의 매매법](https://www.acmicpc.net/problem/20546) + * + * - 준현이는 살 수 있을 때 최대한 산다. + * + * - 성민이는 33 매매법을 사용한다. + * - 전량 매수, 전량 매도 + * - 3일 연속 가격이 전일 대비 상승하면 전량 매도 + * - 3일 연속 가격이 전일 대히 하락하면 전량 매수 + */ + +data class Wallet( + var remainedCash: Int = 0, + var ownedStockCnt: Int = 0, +) { + + fun sellStock(price: Int) { + remainedCash += ownedStockCnt * price + ownedStockCnt = 0 + } + + fun buyStock(price: Int) { + ownedStockCnt += remainedCash / price + remainedCash %= price + } + + fun getStockWorth(price: Int) = ownedStockCnt * price + remainedCash +} + +fun main() { + + val juneWallet = Wallet() + val sungWallet = Wallet() + + readln().toInt().apply { + juneWallet.remainedCash = this + sungWallet.remainedCash = this + } + + val stockList = readln().split(" ").map { it.toInt() } + var lastStockPrice = stockList.first().apply { + if (this <= juneWallet.remainedCash) { + juneWallet.buyStock(this) + } + } + + var continuouslyIncreasedCnt = 0 + var continuouslyDecreasedCnt = 0 + + stockList.drop(1).forEach { todayStockPrice -> + + // 준은 그냥 무지성 풀매수 + if (todayStockPrice <= juneWallet.remainedCash) { + juneWallet.buyStock(todayStockPrice) + } + + if (lastStockPrice < todayStockPrice) { + continuouslyIncreasedCnt++ + continuouslyDecreasedCnt = 0 + } else if (todayStockPrice < lastStockPrice) { + continuouslyIncreasedCnt = 0 + continuouslyDecreasedCnt++ + } + + if (3 <= continuouslyIncreasedCnt) { + sungWallet.sellStock(todayStockPrice) + } else if (3 <= continuouslyDecreasedCnt) { + sungWallet.buyStock(todayStockPrice) + } + + lastStockPrice = todayStockPrice + + } + + if (sungWallet.getStockWorth(stockList.last()) < juneWallet.getStockWorth(stockList.last())) { + println("BNP") + } else if (juneWallet.getStockWorth(stockList.last()) < sungWallet.getStockWorth(stockList.last())) { + println("TIMING") + } else { + println("SAMESAME") + } + + +} \ No newline at end of file diff --git "a/src/main/kotlin/hyunsoo/13week/\353\221\220 \354\210\230\354\235\230 \355\225\251.kt" "b/src/main/kotlin/hyunsoo/13week/\353\221\220 \354\210\230\354\235\230 \355\225\251.kt" new file mode 100644 index 00000000..f5944021 --- /dev/null +++ "b/src/main/kotlin/hyunsoo/13week/\353\221\220 \354\210\230\354\235\230 \355\225\251.kt" @@ -0,0 +1,35 @@ +package hyunsoo.`13week` + +/** + * <문제> + * + * [두 수의 합](https://www.acmicpc.net/problem/3273) + */ + +fun main() { + + val arraySize = readln().toInt() + val numList = readln().split(" ").map { it.toInt() }.sorted() + val targetNum = readln().toInt() + + var start = 0 + var end = numList.lastIndex + var count = 0 + + while (start < end) { + + val sumOfPair = numList[start] + numList[end] + + if (sumOfPair == targetNum) { + count++ + start++ + } else if (sumOfPair < targetNum) { + start++ + } else { + end-- + } + + } + + println(count) +} \ No newline at end of file diff --git "a/src/main/kotlin/hyunsoo/13week/\353\221\220 \354\232\251\354\225\241.kt" "b/src/main/kotlin/hyunsoo/13week/\353\221\220 \354\232\251\354\225\241.kt" new file mode 100644 index 00000000..12111606 --- /dev/null +++ "b/src/main/kotlin/hyunsoo/13week/\353\221\220 \354\232\251\354\225\241.kt" @@ -0,0 +1,53 @@ +package hyunsoo.`13week` + +import kotlin.math.absoluteValue + +/** + * + * <문제> + * + * [두 용액](https://www.acmicpc.net/problem/2470) + */ + +data class Solution(val firSolution: Int, val secSolution: Int) { + override fun toString(): String { + return if (firSolution < secSolution) "$firSolution $secSolution" else "$secSolution $firSolution" + } +} + +val solutionCnt = readln() +val solutionList = readln().split(" ").map { it.toInt() }.sorted() + +var numThatCloseToZero = Int.MAX_VALUE +var start = 0 +var end = solutionList.lastIndex +var ans = Solution(0, 0) + +fun main() { + + while (start < end) { + + val sumOfSolution = solutionList[start] + solutionList[end] + + if (sumOfSolution < 0) { + renewCloserToZeroWord(sumOfSolution) + start++ + } else if (sumOfSolution > 0) { + renewCloserToZeroWord(sumOfSolution) + end-- + } else { + println(Solution(solutionList[start], solutionList[end])) + return + } + + } + println(ans) + +} + +fun renewCloserToZeroWord(sumOfSolution: Int) { + if (sumOfSolution.absoluteValue < numThatCloseToZero.absoluteValue) { + numThatCloseToZero = sumOfSolution + ans = Solution(solutionList[start], solutionList[end]) + } +} \ No newline at end of file diff --git "a/src/main/kotlin/hyunsoo/13week/\353\223\261\354\210\230 \352\265\254\355\225\230\352\270\260.kt" "b/src/main/kotlin/hyunsoo/13week/\353\223\261\354\210\230 \352\265\254\355\225\230\352\270\260.kt" new file mode 100644 index 00000000..c0ec46a7 --- /dev/null +++ "b/src/main/kotlin/hyunsoo/13week/\353\223\261\354\210\230 \352\265\254\355\225\230\352\270\260.kt" @@ -0,0 +1,37 @@ +package hyunsoo.`13week` + + +/** + * + * <문제> + * [등수 구하기](https://www.acmicpc.net/problem/1205) + * + * + */ +fun main() { + + val (scoreCnt, newScore, canRankCnt) = + readln().split(" ") + .map { it.toInt() } + + if (scoreCnt == 0) { + println(1) + return + } + + val newList = (listOf(newScore) + readln().split(" ").map { it.toInt() }) + .sortedByDescending { it } + + newList + .lastIndexOf(newScore).apply { + if (canRankCnt < this + 1) { + println(-1) + } else { + newList.indexOf(newScore).apply { + println(this + 1) + } + } + } + + +} \ No newline at end of file diff --git "a/src/main/kotlin/hyunsoo/13week/\353\260\260\354\227\264 \355\225\251\354\271\230\352\270\260.kt" "b/src/main/kotlin/hyunsoo/13week/\353\260\260\354\227\264 \355\225\251\354\271\230\352\270\260.kt" new file mode 100644 index 00000000..80f3b492 --- /dev/null +++ "b/src/main/kotlin/hyunsoo/13week/\353\260\260\354\227\264 \355\225\251\354\271\230\352\270\260.kt" @@ -0,0 +1,52 @@ +package hyunsoo.`13week` + +import java.io.BufferedReader +import java.io.BufferedWriter +import java.io.InputStreamReader +import java.io.OutputStreamWriter + +/** + * <문제> + * + * [배열 합치기](https://www.acmicpc.net/problem/11728) + */ + +fun main() { + + val br = BufferedReader(InputStreamReader(System.`in`)) + val bw = BufferedWriter(OutputStreamWriter(System.out)) + + val (aSize, bSize) = br.readLine().split(" ").map { it.toInt() } + val arrayA = br.readLine().split(" ").map { it.toInt() } + val arrayB = br.readLine().split(" ").map { it.toInt() } + val sortedArray = IntArray(aSize + bSize) + + var aPointer = 0 + var bPointer = 0 + var sortedPointer = 0 + + while (aPointer <= arrayA.lastIndex && bPointer <= arrayB.lastIndex) { + + if (arrayA[aPointer] < arrayB[bPointer]) { + sortedArray[sortedPointer++] = arrayA[aPointer++] + } else { + sortedArray[sortedPointer++] = arrayB[bPointer++] + } + } + + while (aPointer <= arrayA.lastIndex) { + sortedArray[sortedPointer++] = arrayA[aPointer++] + } + + while (bPointer <= arrayB.lastIndex) { + sortedArray[sortedPointer++] = arrayB[bPointer++] + } + + + sortedArray.forEach { + bw.write("$it ") + } + + bw.flush() + bw.close() +} \ No newline at end of file diff --git "a/src/main/kotlin/hyunsoo/13week/\353\266\200\353\266\204\355\225\251.kt" "b/src/main/kotlin/hyunsoo/13week/\353\266\200\353\266\204\355\225\251.kt" new file mode 100644 index 00000000..9335ab64 --- /dev/null +++ "b/src/main/kotlin/hyunsoo/13week/\353\266\200\353\266\204\355\225\251.kt" @@ -0,0 +1,42 @@ +package hyunsoo.`13week` + +import java.io.BufferedReader +import java.io.InputStreamReader + +/** + * <문제> + * + * [부분합](https://www.acmicpc.net/problem/1806) + */ + +fun main() { + + val br = BufferedReader(InputStreamReader(System.`in`)) + + val (sequenceLength, target) = br.readLine().split(" ").map { it.toInt() } + val prefixSumSequence = mutableListOf(0) + val sequence = br.readLine().split(" ").map { it.toInt() }.fold(0) { total, next -> + prefixSumSequence.add(total + next) + total + next + } + + var start = 0 + var end = 0 + var minLength = 100000 + + while (start <= sequenceLength && end <= sequenceLength) { + + val sumOfSubsequence = prefixSumSequence[end] - prefixSumSequence[start] + + if (sumOfSubsequence >= target) { + start++ + val subsequenceLength = end - start + 1 + if (subsequenceLength < minLength) minLength = subsequenceLength + } else { + end++ + } + } + + println(if (minLength == 100000) 0 else minLength) + +} \ No newline at end of file diff --git "a/src/main/kotlin/hyunsoo/13week/\354\206\214\354\210\230\354\235\230 \354\227\260\354\206\215\355\225\251.kt" "b/src/main/kotlin/hyunsoo/13week/\354\206\214\354\210\230\354\235\230 \354\227\260\354\206\215\355\225\251.kt" new file mode 100644 index 00000000..1a258834 --- /dev/null +++ "b/src/main/kotlin/hyunsoo/13week/\354\206\214\354\210\230\354\235\230 \354\227\260\354\206\215\355\225\251.kt" @@ -0,0 +1,56 @@ +package hyunsoo.`13week` + +import kotlin.math.sqrt + + +/** + * <문제> + * + * [소수의 연속합](https://www.acmicpc.net/problem/1644) + */ + +fun main() { + + val target = readln().toInt() + val primeList = getPrimeList(target) + + var count = 0 + var start = 0 + var end = 0 + + while (start <= primeList.lastIndex && end <= primeList.lastIndex) { + + val sumOfSubsequence = primeList.subList(start, end + 1).sum() + + if (sumOfSubsequence == target) { + count++ + start++ + } else if (sumOfSubsequence < target) { + end++ + } else { + start++ + } + + } + + println(count) +} + +fun getPrimeList(target: Int): List { + val numArray = (0..target).toMutableList() + val primeList = mutableListOf() + + for (prime in 2..sqrt(target.toDouble()).toInt()) { + if (numArray[prime] != 0) { + for (num in prime + 1..target) { + if (num % prime == 0) numArray[num] = 0 + } + } + } + numArray.drop(2).forEachIndexed { index, num -> + if (num != 0) primeList.add(index + 2) + } + + return primeList + +} \ No newline at end of file diff --git "a/src/main/kotlin/hyunsoo/13week/\354\210\230 \354\235\264\354\226\264 \354\223\260\352\270\260 1.kt" "b/src/main/kotlin/hyunsoo/13week/\354\210\230 \354\235\264\354\226\264 \354\223\260\352\270\260 1.kt" new file mode 100644 index 00000000..1c2060fe --- /dev/null +++ "b/src/main/kotlin/hyunsoo/13week/\354\210\230 \354\235\264\354\226\264 \354\223\260\352\270\260 1.kt" @@ -0,0 +1,46 @@ +package hyunsoo.`13week` + +/** + * + * <문제> + * [수 이어 쓰기 1](https://www.acmicpc.net/problem/1748) + * + * 이슈 + * - 그냥 StringBuilder로 풀었더니 메모리 초과가... + * - num 을 toString().length 로 계산했더니 메모리 초과가... + * + * [아니 과거에 교훈을 얻었다면서 변한게 없냐...](https://soopeach.tistory.com/6) + * + * 1 ~ 9 -> 9 개 + * 10 ~ 99 -> 90 개 + * 100 ~ 999 -> 900 개 + * 1000 ~ 9999 -> 9000 개 + * ... 자릿수를 먼저 계산해보자. + * + * 자릿수 구하기 + * 9 * 자릿수만큼의 0 / * 자릿수 + * - 한 자릿수는 0, digit = 0 + * - 두 자릿수는 9 * 1, digit = 1 + * - 세 자릿수는 9 * 1 , 90 * 2, .. digit = 2 + * - 네 자릿수는 9 * 1, 90 * 2, 900 * 3 .. digit = 3 + */ +fun main() { + + var answer = 0 + val target = readln() + val targetLength = target.length + val digit = targetLength - 1 + + if (digit == 0) { + answer = target.toInt() + } else { + repeat(digit) { zeroCnt -> + answer += if (zeroCnt == 0) 9 + else ("9" + "0".repeat(zeroCnt)).toInt() * (zeroCnt + 1) + } + + answer += (target.toInt() - "9".repeat(digit).toInt()) * targetLength + } + + println(answer) +} \ No newline at end of file diff --git "a/src/main/kotlin/hyunsoo/13week/\354\210\230\353\223\244\354\235\230 \355\225\251 2.kt" "b/src/main/kotlin/hyunsoo/13week/\354\210\230\353\223\244\354\235\230 \355\225\251 2.kt" new file mode 100644 index 00000000..7b9ef8dd --- /dev/null +++ "b/src/main/kotlin/hyunsoo/13week/\354\210\230\353\223\244\354\235\230 \355\225\251 2.kt" @@ -0,0 +1,37 @@ +package hyunsoo.`13week` + +/** + * <문제> + * + * [수들의 합 2](https://www.acmicpc.net/problem/2003) + * + * 투 포인터 연습해볼라고요 + */ + +fun main() { + + val (numCount, target) = readln().split(" ").map { it.toInt() } + val numList = readln().split(" ").map { it.toInt() } + + var start = 0 + var end = 0 + var count = 0 + + while(start <= numCount && end <= numCount) { + + val sumOfSubsequence = numList.subList(start, end).sum() + + if(sumOfSubsequence < target) { + end++ + } else if (sumOfSubsequence == target) { + count++ + start++ + } else { + start++ + } + + } + + println(count) +} + diff --git "a/src/main/kotlin/hyunsoo/13week/\354\210\230\353\223\244\354\235\230 \355\225\251 5.kt" "b/src/main/kotlin/hyunsoo/13week/\354\210\230\353\223\244\354\235\230 \355\225\251 5.kt" new file mode 100644 index 00000000..bd32c722 --- /dev/null +++ "b/src/main/kotlin/hyunsoo/13week/\354\210\230\353\223\244\354\235\230 \355\225\251 5.kt" @@ -0,0 +1,40 @@ +package hyunsoo.`13week` + +/** + * <문제> + * + * [수들의 합 5](https://www.acmicpc.net/problem/2018) + * + */ +fun main() { + + val target = readln().toInt() + + var start = 1 + var end = 1 + var count = 1 + + while (start <= target - 1 && end <= target - 1) { + + val sumOfSubsequence = sumOfRange(start, end + 1) + + if (sumOfSubsequence == target) { + count++ + start++ + } else if (sumOfSubsequence < target) { + end++ + } else { + start++ + } + } + + println(count) +} + +fun sumOfRange(start: Int, end: Int): Int { + var sum = 0 + for (num in start..end) { + sum += num + } + return sum +} \ No newline at end of file diff --git "a/src/main/kotlin/hyunsoo/13week/\354\210\230\354\227\264.kt" "b/src/main/kotlin/hyunsoo/13week/\354\210\230\354\227\264.kt" new file mode 100644 index 00000000..91ab77d2 --- /dev/null +++ "b/src/main/kotlin/hyunsoo/13week/\354\210\230\354\227\264.kt" @@ -0,0 +1,23 @@ +package hyunsoo.`13week` + +/** + * <문제> + * + * [수열](https://www.acmicpc.net/problem/2559) + * + */ + +fun main() { + + val (dayCount, sequenceSize) = readln().split(" ").map { it.toInt() } + val temperatureDataList = readln().split(" ").map { it.toInt() } + var maxOfSum = -100 * 100_000 + + for (startIndex in 0..dayCount - sequenceSize) { + val sumOfSubsequence = temperatureDataList.subList(startIndex, startIndex + sequenceSize).sum() + if (maxOfSum < sumOfSubsequence) maxOfSum = sumOfSubsequence + } + + println(maxOfSum) + +} diff --git "a/src/main/kotlin/hyunsoo/13week/\354\243\274\353\252\275.kt" "b/src/main/kotlin/hyunsoo/13week/\354\243\274\353\252\275.kt" new file mode 100644 index 00000000..bb4879fc --- /dev/null +++ "b/src/main/kotlin/hyunsoo/13week/\354\243\274\353\252\275.kt" @@ -0,0 +1,33 @@ +package hyunsoo.`13week` + +/** + * <문제> + * + * [주몽](https://www.acmicpc.net/problem/1940) + */ +fun main() { + + val ingredientCnt = readln().toInt() + val target = readln().toInt() + val ingredientList = readln().split(" ").map { it.toInt() }.sorted() + + var count = 0 + var start = 0 + var end = ingredientList.lastIndex + + while (start < end) { + + val sumOfIngredient = ingredientList[start] + ingredientList[end] + + if (sumOfIngredient == target) { + count++ + start++ + } else if (sumOfIngredient < target) { + start++ + } else { + end-- + } + } + + println(count) +} \ No newline at end of file