Kotlin
-
프로그래머스 - 단어 변환알고리즘 문제 풀이 2021. 1. 2. 00:14
/* * 2021-01-01 * https://programmers.co.kr/learn/courses/30/lessons/43163 */ fun wordConversion(begin: String, target: String, words: Array): Int { // words 에 target 이 없는 경우 0 반환 if (!words.any { it == target }) { return 0 } // begin 을 포함한 allWords 생성 val allWords = words.plus(begin) // 각 단어별 노드 생성 val nodeMap = allWords.associate { it to WordNode(it) } nodeMap.forEach { (word, node) -> // 한글자만..
-
프로그래머스 - 네트워크알고리즘 문제 풀이 2020. 12. 17. 00:12
index를 set 으로 만들어서 겹치는 부분이 있는 경우 합치는 방식으로 풀었습니다. /* * 2020-12-16 * https://programmers.co.kr/learn/courses/30/lessons/43162 */ fun network(n: Int, computers: Array): Int { // index set 의 set 생성 val networkSet: Set = computers.map { c -> c.toList().mapIndexedNotNull { i, n -> if (n == 1) { i } else { null } }.toSet() }.toSet() println(networkSet) // 네트워크 set 의 set 생성 val networkResultSet: Mutable..
-
프로그래머스 - 타겟 넘버알고리즘 문제 풀이 2020. 12. 9. 00:00
풀이 /* * 2020-12-08 * https://programmers.co.kr/learn/courses/30/lessons/43165 */ var answer = 0 fun targetNumber(numbers: IntArray, target: Int): Int { answer = 0 addNumber(0, numbers, 0, target) return answer } tailrec fun addNumber(sum: Int, numbers: IntArray, index: Int, target: Int) { if (numbers.count() == index) { if (sum == target) answer++ } else { addNumber(sum + numbers[index], numbers..
-
프로그래머스 - 이중우선순위큐알고리즘 문제 풀이 2020. 11. 7. 19:41
풀이 import java.util.* /* * 2020-11-07 * https://programmers.co.kr/learn/courses/30/lessons/42628 */ fun solveDoublePriorityQueue(operations: Array): IntArray { val minQueue = PriorityQueue { n1, n2 -> n1 - n2 } val maxQueue = PriorityQueue { n1, n2 -> n2 - n1 } operations.forEach { val (arg, numStr) = it.split(" ") val num = numStr.toInt() if (arg == "I") { minQueue.add(num) maxQueue.add(num) } ..
-
프로그래머스 - 카펫카테고리 없음 2020. 9. 2. 22:10
import kotlin.test.assertEquals /* * 2020-09-02 * https://programmers.co.kr/learn/courses/30/lessons/42842 */ class Carpet { fun solution(brown: Int, yellow: Int): IntArray { var answer = intArrayOf() for (i in 1..yellow) { for (j in 1..yellow) { if (i >= j && i * j == yellow && (i + 2) * (j + 2) - yellow == brown) { return intArrayOf(i + 2, j + 2) } } } return answer } } fun main(args: Array) {..
-
프로그래머스 - 소수 찾기알고리즘 문제 풀이 2020. 9. 2. 21:30
import kotlin.test.assertEquals /* * 2020-09-02 * https://programmers.co.kr/learn/courses/30/lessons/42839 */ class FindPrimeNumber { fun solution(numbers: String): Int { val numberValues = numbers.map { it.toString() } val numbersGroup = numbers.groupBy { it } val numberList = ArrayList() val newNumberList = ArrayList() for (i in 0 until numbers.count()) { if (numberList.isEmpty()) { numberValu..
-
프로그래머스 - 모의고사알고리즘 문제 풀이 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) { // 가장 ..