๋ฌธ์
A non-empty array A consisting of N integers is given. Array A represents numbers on a tape.
Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P − 1] and A[P], A[P + 1], ..., A[N − 1].
The difference between the two parts is the value of: |(A[0] + A[1] + ... + A[P − 1]) − (A[P] + A[P + 1] + ... + A[N − 1])|
In other words, it is the absolute difference between the sum of the first part and the sum of the second part.
For example, consider array A such that:
A[0] = 3 A[1] = 1 A[2] = 2 A[3] = 4 A[4] = 3
We can split this tape in four places:
P = 1, difference = |3 − 10| = 7
P = 2, difference = |4 − 9| = 5
P = 3, difference = |6 − 7| = 1
P = 4, difference = |10 − 3| = 7
Write a function:
public func solution(_ A : inout [Int]) -> Int
that, given a non-empty array A of N integers, returns the minimal difference that can be achieved.
For example, given:
A[0] = 3 A[1] = 1 A[2] = 2 A[3] = 4 A[4] = 3
the function should return 1, as explained above.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [2..100,000];
each element of array A is an integer within the range [−1,000..1,000].
๋งํฌ
TapeEquilibrium coding task - Learn to Code - Codility
Minimize the value |(A[0] + ... + A[P-1]) - (A[P] + ... + A[N-1])|.
app.codility.com
๋ฌธ์ ๋ถ์
- N๊ฐ์ ์ ์๋ก ๊ตฌ์ฑ๋ ๋ฐฐ์ด A๊ฐ ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง๋ค.
- ๋ฐฐ์ด A๋ฅผ ์ ์ P๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ ๊ตฌ๊ฐ์ผ๋ก ๋๋๋ค.
- ๊ฐ ๊ตฌ๊ฐ ํฉ์ ์ฐจ์ด๊ฐ ๊ฐ์ฅ ์์ ๋, ๋ ๊ตฌ๊ฐ ํฉ์ ์ฐจ์ด๋ฅผ ๋ฐํํ๋ค.
- |A[0...(P-1) - A[P...(N-1)|
ex) A = [3, 1, 2, 4, 3]
P = 1, difference = |3 − 10| = 7
P = 2, difference = |4 − 9| = 5
P = 3, difference = |6 − 7| = 1
P = 4, difference = |10 − 3| = 7
๋ ๊ตฌ๊ฐ ํฉ์ ์ฐจ์ด๊ฐ ๊ฐ์ฅ ์์ ๋๋ P๊ฐ 3์ผ ๋, ๊ฐ์ 1์ด๋ค.
ํด๊ฒฐ๋ฐฉ์ 1 - ์คํจ
- ๋ฐฐ์ด A์ ์์๊ฐ 2๊ฐ ๋ผ๋ฉด ๋ ๊ฐ์ ์ฐจ์ด๋ฅผ ๋ฐํ
- 0๋ถํฐ P-1๊น์ง์ ๊ตฌ๊ฐํฉ๊ณผ P๋ถํฐ N-1๊น์ง์ ๊ตฌ๊ฐํฉ์ ๊ณ์ฐํด ์ฐจ์ด๋ฅผ ๊ณ์ฐ
- ์ด์ ์ ์ฐจ์ด ๊ฐ๊ณผ ํ์ฌ์ ์ฐจ์ด ๊ฐ ์ค ๋ ์์ ๊ฐ์ ๋ณ์์ ์ ์ฅ
- 2,3๋ฒ ๊ณผ์ ์ P๋ฅผ 1๋ถํฐ (N-1)๊น์ง ์ฆ๊ฐ์ํค๋ฉฐ ๋ฐ๋ณต
Solution 1
public func solution(_ A : inout [Int]) -> Int {
if A.count == 2{
return abs(A[0]-A[1])
}
var re = Int.max
for P in (1..<A.count){
let sum = abs(A[0...P-1].reduce(0, +) - A[P...A.count-1].reduce(0, +))
re = min(sum, re)
}
return re
}
- ๋ฐฐ์ด์ ํฌ๊ธฐ๊ฐ ํฐ ๊ฒฝ์ฐ timeout ์ค๋ฅ ๋ฐ์
- ์๊ฐ ๋ณต์ก๋: O(N * N)
ํด๊ฒฐ๋ฐฉ์ 2 - ์ฑ๊ณต
- ๋ฐฐ์ด A์ ์์๊ฐ 2๊ฐ ๋ผ๋ฉด ๋ ๊ฐ์ ์ฐจ์ด๋ฅผ ๋ฐํ
- front ๋ณ์๋ฅผ ์ ์ธํ์ฌ 0์ผ๋ก ์ด๊ธฐํํ๊ณ , back ๋ณ์๋ฅผ ์ ์ธํ์ฌ ๋ฐฐ์ด A์ ์์๋ค์ ์ดํฉ์ ๊ณ์ฐํ์ฌ ์ ์ฅ
- ๋ฐฐ์ด A์ ์์๋ค์ 0๋ฒ ์์๋ถํฐ N-2๊น์ง ์์๋๋ก ๊ณ์ฐ
- front ๋ณ์์๋ ํ์ฌ index์ ์์ ๊ฐ์ ํ๋ฌ์ค
- back ๋ณ์์์๋ ํ์ฌ index์ ์์ ๊ฐ์ ๋ง์ด๋์ค
- ๋์ ์ฐจ์ด๋ฅผ ๊ตฌํ ํ ์ด์ ์ ๊ณ์ฐ ๊ฐ๊ณผ ํ์ฌ์ ๊ณ์ฐ ๊ฐ ์ค ๋ ์์ ์๋ฅผ ๋ณ์์ ์ ์ฅ
Solution 2
public func solution(_ A : inout [Int]) -> Int {
if A.count == 2{
return abs(A[0] - A[1])
}
var front = 0, back = A.reduce(0, +)
var re = Int.max
for (index, num) in A.enumerated() {
front += num
back -= num
let diff = abs(front - back)
if index == A.count - 1 {
break
} else {
re = min(re, diff)
}
}
return re
}
- ์๊ฐ ๋ณต์ก๋: O(N)
'โจ๏ธ Language > swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] Codility Lesson 4 - PermCheck (0) | 2022.07.23 |
---|---|
[Swift] Codility Lesson 4 - FrogRiverOne (0) | 2022.07.23 |
[Swift] Codility Lesson 3 - PermMissingElem (0) | 2022.07.23 |
[Swift] Codility Lesson 3 - FrogJmp (0) | 2022.07.23 |
[Swift] Codility Lesson 2 - OddOccurrencesInArray (0) | 2022.07.23 |