알고리즘 문제 풀이
-
프로그래머스 - 모의고사알고리즘 문제 풀이 2020. 8. 24. 21:53
/* * 2020-08-24 * https://programmers.co.kr/learn/courses/30/lessons/42840 */ class PracticeTest { fun solution(answers: IntArray): IntArray { val first = listOf(1, 2, 3, 4, 5) val second = listOf(2, 1, 2, 3, 2, 4, 2, 5) val third = listOf(3, 3, 1, 1, 2, 2, 4, 4, 5, 5) var firstCnt = 0 var secondCnt = 0 var thirdCnt = 0 answers.forEachIndexed { i, answer -> if (answer == first[i % first.count()]..
-
프로그래머스 - 프린터알고리즘 문제 풀이 2020. 8. 23. 12:33
import java.util.LinkedList /* * 2020-08-23 * https://programmers.co.kr/learn/courses/30/lessons/42587 */ class Printer { fun solution(priorities: IntArray, location: Int): Int { val queue = LinkedList(priorities.toList()) var index = location var answer = 1 while (!queue.isEmpty()) { val priority = queue.pop() val checkPriority = queue.find { it > priority } if (checkPriority !== null) { // 가장 ..
-
프로그래머스 - 다리를 지나는 트럭알고리즘 문제 풀이 2020. 8. 8. 20:37
/* * 2020-08-08 * https://programmers.co.kr/learn/courses/30/lessons/42583 */ class TruckPassingTheBridge { fun solution(bridge_length: Int, weight: Int, truck_weights: IntArray): Int { var passingList = ArrayList() var time = 0 val truckList = truck_weights.toMutableList() var truck = truckList.removeAt(0) do { time++ passingList = passingList.filter { truckPair -> truckPair.second < bridge_len..
-
프로그래머스 - 기능개발알고리즘 문제 풀이 2020. 8. 5. 23:09
import kotlin.math.ceil /* * 2020-08-05 * https://programmers.co.kr/learn/courses/30/lessons/42586?language=kotlin */ class FunctionDevelopment { fun solution(progresses: IntArray, speeds: IntArray): IntArray { var answer = ArrayList() val workingDays = progresses.mapIndexed { i, progress -> ceil((100-progress).div(speeds[i].toDouble())).toInt() } var prevWork = workingDays.first() var cnt = 1 w..
-
프로그래머스 - 쇠막대알고리즘 문제 풀이 2020. 7. 28. 23:04
/* * 2020-07-28 * https://programmers.co.kr/learn/courses/30/lessons/42585 */ class IronBar { fun solution(arrangement: String): Int { /* val ironBarList = ArrayList() var answer = 0 var prev:Char? = null arrangement.forEach { if(it == '('){ ironBarList.add(1) } if(it == ')'){ if(prev == '('){ val openCount = ironBarList.removeAt(ironBarList.count()-1) - 1 if(openCount > ..
-
프로그래머스 - 탑알고리즘 문제 풀이 2020. 7. 27. 22:42
/* * 2020-07-27 * https://programmers.co.kr/learn/courses/30/lessons/42588?language=kotlin */ class Tower { fun solution(heights: IntArray): IntArray { val reversedHeights = heights.reversed() return reversedHeights.mapIndexed { i, h -> val index = reversedHeights.slice(0 + i + 1 until heights.count()).indexOfFirst { it > h } if (index > -1) { heights.count() - index - i - 1 } else { 0 } }.rever..
-
프로그래머스 - H-Index알고리즘 문제 풀이 2020. 7. 26. 23:34
아래와 같은 테스트케이스를 추가하고 실행해보는 것을 강력하게 추천한다. input output 5,5,5,5 4 class HIndex { fun solution(citations: IntArray): Int { var result = 0 val sortedList = citations.sortedDescending() val max = citations.max() ?: 0 for (i in 0..max) { val index = sortedList.indexOfLast { it >= i } + 1 if (index > result) { result = i } } return result } } fun main() { val result = HIndex().solution( // intArrayOf(3,..