프로그래머스 - 예상 대진표
·
Algorithm/programmers
import Foundationfunc solution(_ n:Int, _ a:Int, _ b:Int) -> Int { var count = 0 var A = a var B = b while A != B { A = (A + 1) / 2 B = (B + 1) / 2 count += 1 } return count}
프로그래머스 - 짝지어 제거하기 feat. 스택
·
Algorithm/programmers
시간초과 문제스택이 아닌 String 으로 문제를 접근하였을 때 시간 초과가 발생하였습니다.while 문 내부에 for 문으로 데이터를 계속 수정하는 방식은 입력값이 클 경우에는 매우 효율적이지 못했습니다.import Foundationfunc solution(_ s:String) -> Int{ var answer = s var sets = Set(answer.map { Character(String($0)) }) while true { let previous = answer for set in sets { answer = answer.replacingOccurrences(of: "\(set)\(set)", with: "") } ..
프로그래머스 - 카펫 feat. 완전탐색
·
Algorithm/programmers
import Foundationfunc solution(_ brown:Int, _ yellow:Int) -> [Int] { let sum = brown + yellow let b = brown / 2 var answer: [Int] = [] for i in (3...b) { if sum % i == 0 { for j in (3...b) { if i*j == sum && i >= j && (i-2)*(j-2)==yellow { answer = [i, j] } } } } return answer}
프로그래머스 - 피보나치 수 feat. dp (다이나믹 프로그래밍)
·
Algorithm/programmers
최적화 이전func solution(_ n:Int) -> Int { var arr = [0,1,1,2] if n 최적화 이후func solution(_ n:Int) -> Int { var arr = [0,1,1,2] while arr.count
프로그래머스 - 다음 큰 숫자
·
Algorithm/programmers
진수변환을 하기 위해서는 String 의 radix 파라미터를 사용할 수 있겠습니다 import Foundationfunc solution(_ n:Int) -> Int{ var answer:Int = n+1 let nOne = String(n, radix: 2).filter { $0 == "1" }.count while true { let tmpOne = String(answer, radix: 2).filter { $0 == "1" }.count if nOne == tmpOne { break } answer += 1 } return answer}
프로그래머스 - 이진 변환 반복하기, feat. 이진 변환, 진수 변환
·
Algorithm/programmers
진수 변환을 하는 String 으로는 String(value: string, radix: Int) 를 사용하면 되겠습니다 radix 변환을 사용하지 않고 직접 구현한 코드import Foundationfunc solution(_ s:String) -> [Int] { var tmp = s var totalCount = 0 var zeroCount = 0 while tmp.count > 1 { var one = tmp.filter { $0 == "1" }.count zeroCount += tmp.count - one var stack: [Int] = [] while one > 0 { stack.append(one % ..
ytw_developer
'Algorithm/programmers' 카테고리의 글 목록 (2 Page)