알고리즘문풀 with SWIFT 108

swift ) Leetcode - 746. Min Cost Climbing Stairs

면접에서 마주한 문제 ....ㅠㅠ 로직 생각하고 구현을 어떻게 시작할지 한참 고민 끝에.. 무지성으로 만들다가 생각나버린.. 아.. 이거 dp구나 싶은 순간 응 코테 종료할게요 알고리즘 열심히 하겠습니다... 눈만 마주쳐도 아.. 이거 dp구나 할 수 있도록.. - 처음 생각한 접근방식 인덱스 0 / 1에서 시작하는 경우를 각각 고려해서 바로 다음 칸 / 두 번째 뒤의 칸의 cost와 각각 비교하여 최소값이 나오는 방향으로 나아감 -> 여기서 발상이 조금 더 나갔어야 했는데.. 아쉬웠다. - DP 활용(Bottom Up DP) class Solution { func minCostClimbingStairs(_ cost: [Int]) -> Int { var dp = cost let n = dp.count f..

swift ) 백준 2164 - 카드2

더블스택 큐 구현하여 풀이.. struct QueueStack { var leftStack: [Int] var rightStack: [Int] = [] init(_ leftStack: [Int]) { self.leftStack = leftStack } var count: Int { return leftStack.count + rightStack.count } mutating func enqueue(_ element: Int) -> Bool { leftStack.append(element) return true } mutating func dequeue() -> Int { if rightStack.isEmpty { rightStack = leftStack.reversed() leftStack.removeA..

swift ) Leetcode - 1880. Check if Word Equals Summation of Two Words.

1880. Check if Word Equals Summation of Two Words. 0ms 처음 보는 듯 // 1880. Check if Word Equals Summation of Two Words. let numDict = ["a":0, "b":1, "c":2, "d":3, "e":4, "f":5, "g":6, "h":7, "i":8, "j":9] func isSumEqual(_ firstWord: String, _ secondWord: String, _ targetWord: String) -> Bool { let n1 = getNum(word: firstWord) let n2 = getNum(word: secondWord) let n3 = getNum(word: targetWord) retu..

swift ) 프로그래머스 - N개의 최소공배수

두 가지 코드 1. 막코딩.. func solution(_ arr:[Int]) -> Int { var sorted = arr.sorted() var count = 1 while true { var result = sorted.last! * count var check = true for i in 0.. Int { return arr.reduce(1) { lcm($0, $1) } } func gcd(_ n1:Int, _ n2:Int) -> Int { let r = n1 % n2 if r != 0 { return gcd(n2, r) } else { return n2 } } func lcm(_ n1:Int, _ n2:Int) -> Int { return (n1 * n2) / gcd(n1,n2) } gcd, l..