ArrayInversionCount

Codility 2014. 10. 24. 14:20
Compute number of inversion in an array.

Task description

A zero-indexed array A consisting of N integers is given. An inversionis a pair of indexes (P, Q) such that P < Q and A[Q] < A[P].

Write a function:

int solution(vector<int> &A);

that computes the number of inversions in A, or returns −1 if it exceeds 1,000,000,000.

Assume that:

  • N is an integer within the range [0..100,000];
  • each element of array A is an integer within the range [−2,147,483,648..2,147,483,647].

For example, in the following array:

A[0] = -1 A[1] = 6 A[2] = 3
A[3] =  4 A[4] = 7 A[5] = 4

there are four inversions:

  (1,2)  (1,3)  (1,5)  (4,5)

so the function should return 4.

Complexity:

  • expected worst-case time complexity is O(N*log(N));
  • expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).

Elements of input arrays can be modified.

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

접근방법 두가지
1. 1) A를 Merge_Sort한 B를 생성
    2) A의 앞부터 하나씩 꺼내면서 B에서 위치를 찾음. 찾은 위치 Index값이 Inversion 개수.
        A와 B에서 방금 원소를 삭제함
    3) A가 비어버릴때까지 2)를 반복하며 Inversion개수를 더한 결과가 정답

2. 1) A를 Merge_Sort하는 각 Step에서 Merge시 Right원소가 Left원소보다 작은게 나올때마다 Inversion개수 증가
    2) Merge_Sort가 종료되는 시점에서 1)단계의 각 Inversion개수의 합이 정답.

* 퀵소트는 최악의 경우 O(n^2)의 수행 시간이 소모 되므로 머지 소트를 사용함.




#include <string.h> typedef std::vector<int>::iterator vec_it; int merge(vector<int> left_vec, vector<int> right_vec, vec_it numbers) { vec_it left = left_vec.begin(); vec_it left_end = left_vec.end(); vec_it right = right_vec.begin(); vec_it right_end = right_vec.end(); int count = 0; int left_index = 0; int left_size = left_vec.size(); while ( left < left_end && right < right_end ) { if ( *left <= *right ) { *numbers = *left; ++left; ++left_index; } else { *numbers = *right; ++right; count += left_size - left_index; } ++numbers; } while (left < left_end) { *numbers = *left; ++numbers; ++left; } while (right < right_end) { *numbers = *right; ++numbers; ++right; } return count; } int MergeSort ( vector<int> &B ) { if (B.size() <= 1) { return 0; } std::vector<int>::size_type middle = (B.size()) / 2; std::vector<int> left(B.begin(), B.begin() + middle); std::vector<int> right(B.begin() + middle, B.end()); return MergeSort(left) + MergeSort(right) + merge(left, right, B.begin()); } int solution(vector<int> &A) { // write your code in C++11 return MergeSort ( A ); }

'Codility' 카테고리의 다른 글

MissingInteger  (0) 2014.10.23
Posted by Yann'
,

MissingInteger

Codility 2014. 10. 23. 19:12

Find the minimal positive integer not occurring in a given sequence.


Write a function:

int solution(vector<int> &A);


that, given a non-empty zero-indexed array A of N integers, returns the minimal positive integer that does not occur in A.

For example, given:

  A[0] = 1    
  A[1] = 3    
  A[2] = 6
  A[3] = 4    
  A[4] = 1    
  A[5] = 2

the function should return 5.

Assume that:

  • N is an integer within the range [1..100,000];
  • each element of array A is an integer within the range [−2,147,483,648..2,147,483,647].

Complexity:

  • expected worst-case time complexity is O(N);
  • expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).

Elements of input arrays can be modified.

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



// you can write to stdout for debugging purposes, e.g. // printf("this is a debug message\n"); #include <algorithm> int solution(vector<int> &A) { // write your code in C++11 sort ( A.begin(), A.end() ); int result = 0; if ( *(A.begin()) > 1 ) result = 1; else if ( *(A.end()-1) <= 0 ) result = 1; else { int beforeValue = 0; for ( vector<int>::iterator it = A.begin(); it < A.end(); it++ ) { if ( *it <= 0 ) continue; else if ( *it > beforeValue+1 ) { result = beforeValue+1; break; } beforeValue = *it; } if ( beforeValue == 0 ) result = 1; else if ( beforeValue == *(A.end()-1) ) result = *(A.end()-1) + 1; } return result; }

'Codility' 카테고리의 다른 글

ArrayInversionCount  (0) 2014.10.24
Posted by Yann'
,

임백준 저자(개발자)의 컴퓨터 관련 서적으로 전공서적과 에세이 사이를 오고 가고 있는 책이다.

임백준 저자의 여느 책과 마찬가지로 지루하고 고리타분한 전공 이야기만 늘어 놓지 않고 중간 중간

이야기하는 개인적인 경험이나 조언?등을 통해 독자들이 살짝 단추를 푸르고 긴장을 늦출 수 있도록

배려하고 있다.


임백준 저자의 책이 마음에 드는 이유는, 그의 느긋하고 반듯한 성격이 잘 들어나는 문체라고 생각한다.

천천히 귀가 펄럭일만한 흥미로운 소재로 이야기를 던져놓고 조금씩 빠른 리듬을 통해 결론을 이끌어내며

재미있는 게임과 같은 질문등도 던지곤 한다.. 저자에 대한 소개는 이만 줄이며.


이 책을 읽기전 보았던 '행복한 프로그래밍'과 같은 느낌의 책이다. 재미 있고 신기한 알고리즘, 문제 부터

개발자라면 한번쯤 접하고 절차를 밟아간 기본적인 알고리즘의 소개까지 제법 넓고 깊은 이야기를 다루고 있다.


(이어서)

Posted by Yann'
,