본문 바로가기

코딩테스트 준비(kotlin)/DFS, BFS5

[프로그래머스,kotlin] 네트워크 프로그래머스 네트워크 with kotlin  BFS로 구현 fun solution(n: Int, computers: Array): Int { var answer = 0 val visited = BooleanArray(n) // 노드 방문 여부 체크 배열 fun bfs(start: Int) { val queue: Queue = LinkedList() queue.add(start) visited[start] = true while (queue.isNotEmpty()) { val node = queue.poll() for (i in 0 until n) { if (computers.. 2024. 8. 6.
[프로그래머스 kotlin] 숫자 변환하기 import kotlin.math.*class Solution { fun solution(x: Int, y: Int, n: Int): Int { var answer = -1 var dp = Array (y+1) { 1_000_001 } dp[x] = 0 for(index in x..y) { if(dp[index] == 1_000_001) { continue } if (index + n 2024. 7. 3.
[프로그래머스, kotlin] 타겟넘버 재귀를 이용한 문제 풀이 fun solution(numbers: IntArray, target: Int): Int { var answer=0 fun dfs(index: Int, currentSum: Int) { if (index == numbers.size) { if (currentSum == target) { answer = answer + 1 } } else { val add = dfs(index + 1, currentSum + numbers[index]) val subtract = dfs(index + 1, currentSum - numbers[index]) .. 2024. 6. 28.
[프로그래머스 , kotlin] 피로도 연결리스트를 이용한 풀이 ( 스택과 같은 기능으로)fun solution_ff(k: Int, dungeons: Array): Int { val stack = mutableListOf(Triple(k, 0, BooleanArray(dungeons.size))) var maxCount = 0 while (stack.isNotEmpty()) { val (currentK, count, visited) = stack.removeAt(stack.lastIndex) maxCount = maxOf(maxCount, count) for (i in dungeons.indices) { if (!visited[i] && currentK >= dungeo.. 2024. 6. 27.