그래오늘은이거야

Codility 06. TapeEquilibrium (Swift) 코딩테스트(알고리즘)CodeTest 본문

세상 개발/IOS(Swift)

Codility 06. TapeEquilibrium (Swift) 코딩테스트(알고리즘)CodeTest

jinhongstar 2019. 7. 2. 15:11
728x90
반응형

요즘 온라인으로 코딩 테스트 하는 곳이 많습니다.

 

해외에 유명한 기업및 국내 잘나가는 IT기업들은 Codility 를 본다.

 

IBM, FIFAOnline, 11번가, 우아한형제들, 티몬, 쿠팡, Grab 등등..

 

저도 우아한형제들 코딩 테스트를 통하여 아래 사이트를 알게 되었습니다.

 

목적은 100% 

 

온라인 코딩 테스트 

 

제가 할 수 있는 언어는 :  Java(상), Object-c(상), Swift(중상), Android(상), C#(하), C,C++(하), SQL(하)

각각의 언어로 프로젝트 한번 이상 해봤습니다.

 

주종목 코드는 Object-c  인데 공부할겸 Swift로 테스트코딩

이 글이 누군가에겐 "약"이 되고 누군가에겐 "독"이 될 것이다.

개발자라면 적어도 기본 알고리즘 정도는 스스로 학습하여 풀어봐야 한다.참고로 코딩테스트 문제를 제출 한 심사위원 분도제 블로그에서 답을 확인 한다는 것을 역으로 알아두고 보세요!! ㅎㅎ

 

요즘 운동에만 신경쓰고 있어서 알고리즘을 다시 풀어 봤을때 너무 놀랐다...

 

나는 코딩을 잘 하고 생각하고 있었는데... 내가 고작 이정도 였을까...

 

총 20개 정도로 알고있다. 이걸 다 작성하고 100점을 목표로 작성해보겠습니다.

 

Lesson 1

Iterations

Lesson 2

Arrays

Lesson 3

Time Complexity

Lesson 4

Counting Elements

Lesson 5

Prefix Sums

Lesson 6

Sorting

Lesson 7

Stacks and Queues

Lesson 8

Leader

Lesson 9

Maximum slice problem

Lesson 10

Prime and composite numbers

Lesson 11

Sieve of Eratosthenes

Lesson 12

Euclidean algorithm

Lesson 13

Fibonacci numbers

Lesson 14

Binary search algorithm

Lesson 15

Caterpillar method

Lesson 16

Greedy algorithms

Lesson 17

Dynamic programming

Lesson 90

Tasks from Indeed Prime 2015 challenge

Lesson 91

Tasks from Indeed Prime 2016 challenge

Lesson 92

Tasks from Indeed Prime 2016 College Coders challenge

Lesson 99

Future training

 

테스트 할 수 있는 언어는 : 

C, C++, C#, Go, Java, Javascript, Kotlin, Lua, Object-c, Pascal, Perl, PHP, Python, Ruby, Scala, Swift4, VisualBasic

 

https://app.codility.com/

 

6.  TapeEquilibrium

 

문제는 아래와 같습니다.

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].

    Copyright 2009–2019 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

 

문제의 내용은 :

 

문제 이해하는데 참 어려웠다....

 

A의 배열의 값을 내려주면 예 를들어

A :  [3, 1, 2, 4, 3]

 

배열 2개로 나눠서 

첫번째 배열은 맨앞배열부터 하나씩 add (insert) 후 전부 합친(더한)값

두번째 배열은 전체 배열에서 맨 앞부터 하나씩 remove(delete) 후 전부 합친(더한) 값

 

첫번째 배열과 두번째 배열을 빼면 값이 나오는데 -값 무시하고 최고 낮은 값을 찾는 문제이다.

 

(3)-(1+2+4+3) = 7

(3+1)-(2+4+3) = 5

 

점수를 높이려면

for문을 한번만써야 한다. 

lib 함수 사용 줄여야 한다.

 

점점 swift를 공부하면서 알고리즘을 만들면서 더 좋은 소스 효율성이 더 좋은 알고리즘에 대하여 다시 찾아보고 공부하게 된다!!

 

import UIKit


var questionVal : [Int]
questionVal = [3, 1, 2, 4, 3]

var totalVal : [Int]
totalVal = [Int]()


var firstVal = 0
var secVal = questionVal.reduce(0, +)

for (_,item) in questionVal.enumerated(){

    firstVal += item
    secVal -= item
    
    totalVal.insert(abs(firstVal - secVal), at: 0)
    
}
  totalVal.sort()
  print(totalVal.first!)

 

 

Buy Me A Coffee

 

100% 모든 문제를 백점! 도전!! ㅜㅜ

 

 

53% 아... 이게 playground  에서 코딩 하고 시험 문제에 복사 붙여 넣기 해서 점수가 낮은건가? 

소스 효율성이 이렇게 떨어지는건가.... ㅜㅜㅜ

반응형
Comments