본문 바로가기

분류 전체보기148

[Swift] 프로그래머스 - 카카오 길 찾기 게임 문제: https://programmers.co.kr/learn/courses/30/lessons/42892 코딩테스트 연습 - 길 찾기 게임 [[5,3],[11,5],[13,3],[3,5],[6,1],[1,3],[8,6],[7,2],[2,2]] [[7,4,6,9,1,8,5,2,3],[9,6,5,8,1,4,3,2,7]] programmers.co.kr 단계는 총 두 가지입니다. 1. 주어진 배열을 아래 그림과 같이 정렬하기 // [x, y, value] var nodes: [[Int]] = nodeinfo.enumerated().map { length = max(length, $0.element[0]) return [$0.element[0], $0.element[1], $0.offset + 1] }.so.. 2022. 3. 4.
[Swift] 프로그래머스 - 카카오 캐시 문제: https://programmers.co.kr/learn/courses/30/lessons/17680 Int { var result = 0 var queue = [String]() for city in cities { let city = city.lowercased() if queue.contains(city) { // hit result += 1 queue.remove(at: queue.firstIndex(of: city)!) } else { // miss result += 5 if cacheSize == 0 { continue } if queue.count >= cacheSize { queue.removeFirst() } } queue.append(city) } return result } 2022. 2. 28.
[Swift] 프로그래머스 - 카카오 다트 게임 문제: https://programmers.co.kr/learn/courses/30/lessons/17682 코딩테스트 연습 - [1차] 다트 게임 programmers.co.kr 문제에서 요구하는대로 구현했습니다. import Foundation func solution(_ dartResult:String) -> Int { var answer = [Int]() var temp = "" for dart in dartResult { if Int(String(dart)) != nil { temp.append(dart) } else if "SDT".contains(dart) { let num = Int(temp)! temp = "" if dart == "S" { answer.append(num) } else .. 2022. 2. 28.
[Swift] 4가지 방법의 피보나치 함수 1. 재귀 구조 브루트 포스 func fib(_ n: Int) -> Int { if n Int { if n Int { if n Int { var x = 0, y = 1 for _ in 0.. 2022. 2. 28.
[Swift] LeetCode - 134. Gas Station 문제: https://leetcode.com/problems/gas-station/ Gas Station - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 전체 기름의 양 < 전체 비용일 경우 반드시 모든 주유소를 방문할 수 있습니다. O(n)으로 돌며 성립되지 않는 지점이 있다면 출발점을 그 다음 지점으로 갱신합니다. import Foundation class Solution { func canCompleteCircuit(_ gas: [Int], _ cost:.. 2022. 2. 28.
[Swift] LeetCode - 136. Single Number 문제: https://leetcode.com/problems/single-number/ Single Number - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com XOR연산으로 구할 수 있습니다. XOR로 두 번 등장하는 숫자는 0으로 초기화되고 한 번만 등장하는 숫자는 그대로 보존됩니다. import Foundation class Solution { func singleNumber(_ nums: [Int]) -> Int { var result = 0 for n.. 2022. 2. 17.