-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
Description
๐ฌย ๋ฌธ์
https://app.codility.com/programmers/lessons/3-time_complexity/tape_equilibrium/
๐ฌย Idea
- ์๊ฐ๋ณต์ก๋๋ฅผ ์ ๊ฒฝ์ฐ๊ธฐ ์ํด ์ต๋ O(N)์ ์๊ฐ๋ณต์ก๋๋ฅผ ์ฌ์ฉํ๋ ค๊ณ ํ๋ค.
- ๋ฐ๋ผ์ ํฉ์ ๊ตฌํ๋ reduce๋ฅผ ๋ฐ๋ณต๋ฌธ ๋ด์ ์์น์ํค์ง ์๊ณ ์ด 1ํ ์ฌ์ฉํ์ฌ ๋ณ์์ ์ดํฉ์ ์ ์ฅํด๋ ํ ๋ฐ๋ณต๋ฌธ์ ๋๋ฉฐ ํด๋น ๊ฐ์์ ์์์ ๊ฐ์ ๋นผ์ฃผ๋ ๋ฐฉ์์ ์ฌ์ฉํ๋ค.
๐ฌย ํ์ด
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ A : inout [Int]) -> Int {
var asum = 0
var bsum = A.reduce(0, +)
var minInterval = 1000000000000000000
for i in 0..<A.count - 1 {
asum += A[i]
bsum -= A[i]
minInterval = min(abs(asum - bsum), minInterval)
}
return minInterval
}
์์์๊ฐ
: 30๋ถ
์๊ฐ ๋ณต์ก๋
: O(N)
ํ๊ฐํ
: https://app.codility.com/demo/results/trainingAJFBW3-6KN/
- ๋ฌธ์ ์์ ๋น์ด์์ง ์์ ๋ฐฐ์ด์ผ ๊ฒฝ์ฐ์๋ง ์ฐจ๋ฅผ ๊ตฌํด์ค๋ค๋ ์กฐ๊ฑด์ด ์์๋๋ฐ ํด๋น ์กฐ๊ฑด์ ๋ฌด์ํ๊ณ ๋ง์ง๋ง ์์๊น์ง ์ดํฉ์์ ๋นผ์ ์ฐจ๋ฅผ ๊ตฌํ๋ ์ค์๋ฅผ ๋ฒํ๋ค.. ์์ผ๋ก ๋ฌธ์ ๋ ๊ผผ๊ผผํ ์ฝ๊ธฐ..!!!!