본문 바로가기

전체 글

(152)
Lesson 15. Caterpillar method - CountTriangles 1. 문제An array A consisting of N integers is given. A triplet (P, Q, R) is triangular if it is possible to build a triangle with sides of lengths A[P], A[Q] and A[R]. In other words, triplet (P, Q, R) is triangular if 0 ≤ P   - A[P] + A[Q] > A[R],  - A[Q] + A[R] > A[P],  - A[R] + A[P] > A[Q].For example, consider array A such that:A[0] = 10A[1] = 2A[2] = 5 A[3] = 1 A[4] = 8 A[5] = 12There are fou..
Lesson 15. Caterpillar method - CountDistinctSlices #2 (속도 개선) 1. 속도 개선 아이디어이전 코드에서는 모든걸 하나하나씩 세고 있다.  예제를 다시 보면, A[0]A[1]A[2]A[3]A[4]34552 (0, 0) = 3(0, 1) = 3, 4(0, 2) = 3, 4, 5(1, 1) = 4(1, 2) = 4, 5... 이하 생략 ... 이전 코드에서는 첫번째 반복에서 (3), (3, 4), (3, 4, 5)를 세고,두번째 반복에서 (4), (4, 5)를 다시 세고 있다. 사실 처음에 계산한 slice에서 맨 앞 숫자인 3만을 제외하면, 두번째 반복에서 얻어지는 slice를 구할 수 있다.즉, 두번째 반복에서 처음부터 다시 세는것이 아니라, 그냥 3만을 제외하면, 두번째 반복에서 얻을 수 있는 slice를 모두 구할 수 있다. 2. 문제풀이 전략첫번째 반복문에서 3개를..
Lesson 15. Caterpillar method - CountDistinctSlices #1 1. 문제An integer M and a non-empty array A consisting of N non-negative integers are given. All integers in array A are less than or equal to M.A pair of integers (P, Q), such that 0 ≤ P ≤ Q For example, consider integer M = 6 and array A such that:A[0] = 3 A[1] = 4 A[2] = 5 A[3] = 5 A[4] = 2There are exactly nine distinct slices: (0, 0), (0, 1), (0, 2), (1, 1), (1, 2), (2, 2), (3, 3), (3, 4) and..
Lesson 15. Caterpillar method - AbsDistinct 1. 문제A non-empty array A consisting of N numbers is given. The array is sorted in non-decreasing order. The absolute distinct count of this array is the number of distinct absolute values among the elements of the array.For example, consider array A such that:A[0] = -5A[1] = -3 A[2] = -1 A[3] = 0A[4] = 3A[5] = 6The absolute distinct count of this array is 5, because there are 5 distinct absolute..
Lesson 14. Binary search algorithm - NailingPlanks #4 (속도 개선) 1. 속도 개선 아이디어이진 탐색 대상을 바꿔보자. AS_IS: 어떤 판자에다가 못질을 할까?TO_BE: 몇 번째 못을 사용하면, 모든 판자에 못질이 될까?  그런데 못은 순서대로 사용해야 한다. 따라서 이진 탐색으로 3번째 못을 골랐다면, 1, 2번째 못을 사용해야한다.이러면, 1번째, 2번째, 3번째, 순차적으로 못질하는 것과 같지 않을까?  이를 해결하기 위해선, 이번에도 생각의 전환이 필요하다.못을 들고, 판자에다가 하나하나 박을게 아니라, 현재까지 사용된 못 상태를 놓고, 판자를 못 상태에 대조하는 형태로 가야한다. 예시)C[0]C[1]C[2]C[3]C[4]467102 이를 배열로 표현해보면 다음과 같다.12345678910 못 못 못못  못 C[0] 못을 골랐다고 생각해보자.못의 상태 배열은 ..
Lesson 14. Binary search algorithm - NailingPlanks #3 (속도 개선) 1. 속도 개선 아이디어주제가 이진탐색인데, 아직까지 이진탐색을 시도하지 않았다.map은 find함수를 통해 이진탐색을 제공하지만, 처음에 판자를 정리할 때만 잠깐 사용했고, 이후 못질하는 곳에서는 전혀 사용되지 않았다. 못질하는 곳에 이진탐색을 사용해보자.  2. 코드 (C++)현재 자료형은 map이므로, 항상 순회해야 한다. 중간위치에 바로 접근하기 위해 판자들을 vector에 옮겨 담았다.#include int solution(vector &A, vector &B, vector &C) { map planks = {}; for(size_t i = 0; i second) it->second = B[i]; } } //판자를 vector에 저장한다 vector> bo..
Lesson 14. Binary search algorithm - NailingPlanks #2 (속도 개선) 1. 속도 개선 아이디어시작점이 같은 판자들이 여러개 있을 때, 이들을 모두 기억할 필요가 없다. (판자1) ---------(판자2) -------------- (판자1)에 못이 박힌다면, (판자2)는 자동으로 못이 박힌다. 2. 코드 (C++)multimap을 map으로 수정했다.시작위치가 같은 판자들은 가장 짧은 판자만 기억하면 된다.#include int solution(vector &A, vector &B, vector &C) { map planks = {}; for(size_t i = 0; i second) it->second = B[i]; } } int i = 0; for(i = 0; i first) break; if(it->firs..
Lesson 14. Binary search algorithm - NailingPlanks #1 1. 문제You are given two non-empty arrays A and B consisting of N integers. These arrays represent N planks.More precisely, A[K] is the start and B[K] the end of the K−th plank.Next, you are given a non-empty array C consisting of M integers. This array represents M nails. More precisely, C[I] is the position where you can hammer in the I−th nail.We say that a plank (A[K], B[K]) is nailed if there..