본문 바로가기

Algorithm Problem/Codility

(91)
Lesson 5. Perfix Sums - CountDiv #1 1. 문제Write a function:  int solution(int A, int B, int K);that, given three integers A, B and KReturns the number of integers within the range [A..B] that are divisible by K, i.e.: { i : A ≤ i ≤ B, i mod K = 0 }For example,for A = 6, B = 11 and K = 2, your function should return 3.Because there are three numbers divisible by 2 within the range [6..11], namely 6, 8 and 10.Write an efficient algor..
Lesson 5. Perfix Sums - PassingCars #4 (번외-prefix sums) 이 문제는 (PassingCars #3)가 가장 좋은 코드다.여기서는 번외로, 부분합(prefix sums) 해법을 제시한다. 1. 코드 (C++)int solution(vector &A) { vector P = {}; //0, 동쪽으로 이동하는 차량 int q_cnt = 0; //서쪽으로 이동하는 차량 수 //동쪽으로 이동하는 차량을 vector P에 저장하고, //서쪽으로 이동하는 차량의 수를 구한다. for(size_t i = 0; i PREFIX_SUMS_Q(A.size(), 0); PREFIX_SUMS_Q[0] = q_cnt; if(A[0] == 1) q_cnt--; //prefix sums 배열에 값을 채운다. 값은 서쪽으로 이동하는 ..
Lesson 5. Perfix Sums - PassingCars #3 (속도 개선-2) 1. 속도 개선 아이디어예제를 다시 분석해보자. vector A마주치는 차량A[0] = 0A[1], A[3], A[4]A[1] = 1 A[2] = 0A[3], A[4]A[3] = 1 A[4] = 1  A[0]이 마주치는 차량과  A[2]가 마주치는 차량이 일부 겹친다.이유는 A[2]가 마주치는 차량은, A[0]도 마주치기 때문이다.즉, 차량을 각각 짝지을 필요 없이, 1을 만날 때마다, 앞에있는 0의 갯수만큼 더해주면 된다. A[0]         ↔  A[1]이 마주치는 차량 1대A[0]/A[2]  ↔  A[3]이 마주치는 차량 2대A[0]/A[2]  ↔  A[4]이 마주치는 차량 2대  2. 코드 (C++)int solution(vector &A) { int cnt = 0; int east..
Lesson 5. Perfix Sums - PassingCars #2 (속도 개선-1) 1. 이전 코드 분석마주치는 차량을 셀 때, 하나하나 직접 세고 있다.  2. 속도 개선 아이디어하나씩 직접 세지 말고, 산술 연산을 하면 좀 더 빠르게 처리할 수 있다.  3. 코드 (C++)//#include int solution(vector &A) { // 0 과 1 분리 vector east = {}; //0 vector west = {}; //1 for(size_t i = 0; i ()); //마주치는 차량 세기, east[i] 1000000000) return -1; else break; } } } return cnt;}  4. 결과
Lesson 5. Perfix Sums - PassingCars #1 1. 문제A non-empty array A consisting of N integers is given. The consecutive elements of array A represent consecutive cars on a road.Array A contains only 0s and/or 1s:  - 0 represents a car traveling east,  - 1 represents a car traveling west.The goal is to count passing cars. We say that a pair of cars (P, Q), where 0 ≤ P For example, consider array A such that:A[0] = 0A[1] = 1 A[2] = 0 A[3] =..
Lesson 4. Counting Elements - MissingInteger 1. 문제Write a function:  int solution(vector &A);that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5.Given A = [1, 2, 3], the function should return 4.Given A = [−1, −3], the function should return 1.Write an efficient algorithm for the following assumptions:  ..
Lesson 4. Counting Elements - MaxCounters #2 (속도 개선) 1. 이전 코드 분석 (MaxCounters #1)MaxCounters #1에서는,(N + 1)을 만났을 때, 그 즉시 모든 요소를 업데이트 했다.이럴경우, 이중 반복문을 사용하는 것과 같다.  2. 속도 개선 아이디어(N + 1)을 만났을 때, 수행해야할 업데이트 작업을 별도로 분리한다. (나중에 수행한다) 예) N = 5vector ARESULT비고A[0] = 3(0, 0, 1, 0, 0) A[1] = 4(0, 0, 1, 1, 0) A[2] = 4(0, 0, 1, 2, 0) A[3] = 6(0, 0, 1, 2, 0)max_value = 2A[4] = 1(3, 0, 1, 2, 0)max_value + 1 A[5] = 4(3, 0, 1, 3, 0)RESULT[4] + 1A[6] = 4(3, 0, 1, 4..
Lesson 4. Counting Elements - MaxCounters #1 1. 문제You are given N counters, initially set to 0, and you have two possible operations on them:  - increase(X) − counter X is increased by 1,  - max counter − all counters are set to the maximum value of any counter.A non-empty array A of M integers is given. This array represents consecutive operations:  - if A[K] = X, such that 1 ≤ X ≤ N, then operation K is increase(X),  - if A[K] = N + 1 th..

반응형