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

[전현수] 기적의 매매법, 수 이어 쓰기 1, 등수 구하기, DNA #62

Merged
merged 14 commits into from
Jan 1, 2023
48 changes: 48 additions & 0 deletions src/main/kotlin/hyunsoo/13week/DNA.kt
Original file line number Diff line number Diff line change
@@ -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<Char, Int>()
val dnaList = mutableListOf<String>()
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
Copy link
Member

Choose a reason for hiding this comment

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

getOrDefault 사용 배우고 갑니다!

}

val mostFrequentAlphabet = alphabetMap
.toSortedMap()
.maxBy { it.value }.key
Copy link
Contributor

Choose a reason for hiding this comment

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

고차함수란 어렵네요...


hammingDistance +=
alphabetMap.filter { it.key != mostFrequentAlphabet }
.map { it.value }.sum()
Comment on lines +37 to +39
Copy link
Member

Choose a reason for hiding this comment

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

sum으로 다른걸 더하는 것보다 max인 value를 dnaCnt에서 빼도 될 것 같습니다!

Copy link
Member Author

Choose a reason for hiding this comment

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

@jeeminimini
좋은 방법 감사합니다!!


dnaBuilder.append(mostFrequentAlphabet)
alphabetMap.clear()
}

println(dnaBuilder)
println(hammingDistance)

}
88 changes: 88 additions & 0 deletions src/main/kotlin/hyunsoo/13week/기적의 매매법.kt
Original file line number Diff line number Diff line change
@@ -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
}
Comment on lines +26 to +29
Copy link
Contributor

Choose a reason for hiding this comment

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

저는 파는 부분은 성민이만 필요하길래 따로 함수로 안 뺐는데 둘다 빼주는게 추상화 레벨(?)이 맞아서 더 깔끔했겠다는 생각을 했습니다

Comment on lines +21 to +29
Copy link
Member

Choose a reason for hiding this comment

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

미리 함수로 빼둔 게 좋네요


fun getStockWorth(price: Int) = ownedStockCnt * price + remainedCash
}
Comment on lines +16 to +32
Copy link
Member

Choose a reason for hiding this comment

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

굉장히 깔끔합니다 👍


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


}
35 changes: 35 additions & 0 deletions src/main/kotlin/hyunsoo/13week/두 수의 합.kt
Original file line number Diff line number Diff line change
@@ -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)
}
53 changes: 53 additions & 0 deletions src/main/kotlin/hyunsoo/13week/두 용액.kt
Original file line number Diff line number Diff line change
@@ -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])
}
}
37 changes: 37 additions & 0 deletions src/main/kotlin/hyunsoo/13week/등수 구하기.kt
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

고차함수를 쓰니 이렇게 깔꼼하게 풀리는 문제였군요...

if (canRankCnt < this + 1) {
println(-1)
} else {
newList.indexOf(newScore).apply {
println(this + 1)
Comment on lines +30 to +31
Copy link
Member

Choose a reason for hiding this comment

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

lastIndexOf 랑 indexOf 나눈 게 좋습니다!!

}
}
}


}
52 changes: 52 additions & 0 deletions src/main/kotlin/hyunsoo/13week/배열 합치기.kt
Original file line number Diff line number Diff line change
@@ -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()
}
42 changes: 42 additions & 0 deletions src/main/kotlin/hyunsoo/13week/부분합.kt
Original file line number Diff line number Diff line change
@@ -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<Int>(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)

}
Loading