๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โŒจ๏ธ Language/swift

[Swift] Codility Lesson 3 - TapeEquilibrium

by hyebin (Helia) 2022. 7. 23.
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

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 - ์‹คํŒจ

  1. ๋ฐฐ์—ด A์˜ ์š”์†Œ๊ฐ€ 2๊ฐœ ๋ผ๋ฉด ๋‘ ๊ฐ’์˜ ์ฐจ์ด๋ฅผ ๋ฐ˜ํ™˜
  2. 0๋ถ€ํ„ฐ P-1๊นŒ์ง€์˜ ๊ตฌ๊ฐ„ํ•ฉ๊ณผ P๋ถ€ํ„ฐ N-1๊นŒ์ง€์˜ ๊ตฌ๊ฐ„ํ•ฉ์„ ๊ณ„์‚ฐํ•ด ์ฐจ์ด๋ฅผ ๊ณ„์‚ฐ
  3. ์ด์ „์˜ ์ฐจ์ด ๊ฐ’๊ณผ ํ˜„์žฌ์˜ ์ฐจ์ด ๊ฐ’ ์ค‘ ๋” ์ž‘์€ ๊ฐ’์„ ๋ณ€์ˆ˜์— ์ €์žฅ
  4. 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 - ์„ฑ๊ณต

  1. ๋ฐฐ์—ด A์˜ ์š”์†Œ๊ฐ€ 2๊ฐœ ๋ผ๋ฉด ๋‘ ๊ฐ’์˜ ์ฐจ์ด๋ฅผ ๋ฐ˜ํ™˜
  2. front ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜์—ฌ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ณ , back ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜์—ฌ ๋ฐฐ์—ด A์˜ ์š”์†Œ๋“ค์˜ ์ดํ•ฉ์„ ๊ณ„์‚ฐํ•˜์—ฌ ์ €์žฅ
  3. ๋ฐฐ์—ด A์˜ ์š”์†Œ๋“ค์„ 0๋ฒˆ ์š”์†Œ๋ถ€ํ„ฐ N-2๊นŒ์ง€ ์ˆœ์„œ๋Œ€๋กœ ๊ณ„์‚ฐ
    1. front ๋ณ€์ˆ˜์—๋Š” ํ˜„์žฌ index์˜ ์š”์†Œ ๊ฐ’์„ ํ”Œ๋Ÿฌ์Šค
    2. back ๋ณ€์ˆ˜์—์„œ๋Š” ํ˜„์žฌ index์˜ ์š”์†Œ ๊ฐ’์„ ๋งˆ์ด๋„ˆ์Šค
    3. ๋‘˜์˜ ์ฐจ์ด๋ฅผ ๊ตฌํ•œ ํ›„ ์ด์ „์˜ ๊ณ„์‚ฐ ๊ฐ’๊ณผ ํ˜„์žฌ์˜ ๊ณ„์‚ฐ ๊ฐ’ ์ค‘ ๋” ์ž‘์€ ์ˆ˜๋ฅผ ๋ณ€์ˆ˜์— ์ €์žฅ

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