-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
Labels
Description
๐ฌย ๋ฌธ์
https://app.codility.com/programmers/lessons/5-prefix_sums/passing_cars/
๐ฌย Idea
- P๊ฐ Q๋ณด๋ค ์์ ๋๋ง ์์์์ด ๋ง๋ค์ด์ง๋ค๋ ์๋ฆฌ๋ฅผ ์ด์ฉํ์ฌ ํ์ด์ฃผ์๋ค.
- ๋จผ์ ์์ชฝ์ผ๋ก ํฅํ๋ ์ฐจ์ ๊ฐ์๋ฅผ ๊ตฌํ์ฌ westCarCount์ ์ ์ฅํด๋๋๋ค.
- ์ดํ ๋ฐ๋ณต๋ฌธ์ ๋๋ฉด์
- ์ฐจ๊ฐ ์์ชฝ์ผ๋ก ํฅํ ๋๋ง๋ค westCarCount๋ฅผ -1์ฉ ์ฐจ๊ฐํด์ฃผ๊ณ
- ๋์ชฝ์ผ๋ก ํฅํ๋ ์ฐจ๊ฐ ๋์ค๋ฉด ๋น์์ westCarCount๋ฅผ ans์ ๋ํ์ฌ ์์์์ ๊ฐ์๋ฅผ ๋์ถํด์ฃผ์๋ค.
๐ฌย ํ์ด
public func solution(_ A : inout [Int]) -> Int {
var westCarCount = A.filter({ $0 == 1 }).count
var ans = 0
for a in A {
if a == 0 {
ans += westCarCount
} else {
westCarCount -= 1
}
}
return ans > 1000000000 ? -1 : ans
}
์์์๊ฐ
: 12๋ถ
์๊ฐ ๋ณต์ก๋
: O(N)
ํ๊ฐํ
: https://app.codility.com/demo/results/trainingYDRNDZ-VG7/