https://school.programmers.co.kr/learn/courses/30/lessons/12979#
1. Swift 풀이
import Foundation
func solution(_ n:Int, _ stations:[Int], _ w:Int) -> Int{
var range = [(Int, Int)]()
for i in 0..<stations.count {
var start = 1
if i > 0 {
start = stations[i-1] + w + 1
}
let end = stations[i] - w - 1
range.append((start, end))
}
if stations.last! != n {
range.append((stations.last!+w+1,n))
}
var answer = 0
for (s, e) in range {
answer += (e-s+1) / (2*w+1)
if (e-s+1) % (2*w+1) > 0 {
answer += 1
}
}
return answer
}
2. Python 풀이
def solution(n, stations, w):
answer = 0
start = 1
for s in stations:
end = s-w-1
answer += (end-start+1) // (2*w+1)
if (end-start+1) % (2*w+1) != 0:
answer += 1
start = s+w+1
if start <= n:
answer += (n-start+1) // (2*w+1)
if (n-start+1) % (2*w+1) != 0:
answer += 1
return answer
[프로그래머스] 다리를 지나는 트럭 (1) | 2024.01.23 |
---|---|
[프로그래머스] 쿼드압축 후 개수 세기 (0) | 2024.01.19 |
[프로그래머스] 숫자 게임 (0) | 2024.01.17 |
[프로그래머스] 택배상자 (1) | 2024.01.16 |
[프로그래머스] 오픈채팅방 (0) | 2024.01.10 |