본문 바로가기

반응형

Algorithm Problem

(93)
Lesson 6. Sorting - Triangle #2 (속도개선) 1. 속도개선 아이디어문제에서 삼각형을 만드는 조건을 다음과 같이 제시하고 있다.0 ≤ P A[P] + A[Q] > A[R]A[Q] + A[R] > A[P]A[R] + A[P] > A[Q]여기서 조건1은 삼각형을 만드는데 쓰이는 값(인덱스)이 중복 사용할 수 없다는 뜻이지,Vector A의 값 순서가 지켜져야 한다는 뜻은 아니다. 따라서 Vector A를 오름차순으로 정렬하면, 확인해야 하는 경우의 수가 줄어든다.왜냐하면 오름차순으로 정렬된 상태에서는 조건3과 조건4는 당연하기 때문이다.  (A[R] > A[P], A[Q]) 즉, 우리가 확인해야 하는 경우는 오직 조건2 뿐이다.조건2는 나란히 붙어있는 3개의 값을 확인하면 된다. 2. 코드 (C++)#include #include int solution..
Lesson 6. Sorting - Triangle #1 1. 문제An array A consisting of N integers is given. A 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] = 5A[3] = 1A[4] = 8A[5] = 20Triplet (0, 2, 4) is triangular.Write a function:  int solution(vector &A);that, given an array A consisting of N integers, returns 1 if there exists a trian..
Lesson 6. Sorting - MaxProductOfThree 1. 문제A non-empty array A consisting of N integers is given. The product of triplet (P, Q, R) equates to A[P] * A[Q] * A[R] (0 ≤ P For example, array A such that:A[0] = -3A[1] = 1A[2] = 2A[3] = -2A[4] = 5A[5] = 6contains the following example triplets:(0, 1, 2), product is −3 * 1 * 2 = −6(1, 2, 4), product is 1 * 2 * 5 = 10(2, 4, 5), product is 2 * 5 * 6 = 60Your goal is to find the maximal produ..
Lesson 6. Sorting - Distinct 1. 문제Write a function  int solution(vector &A);that, given an array A consisting of N integers, returns the number of distinct values in array A.For example, given array A consisting of six elements such that:A[0] = 2A[1] = 1A[2] = 1 A[3] = 2 A[4] = 3 A[5] = 1the function should return 3, because there are 3 distinct values appearing in array A, namely 1, 2 and 3.Write an efficient algorithm for..
Lesson 5. Perfix Sums - MinAvgTwoSlice #2 (속도 개선) 1. 속도 개선 아이디어평균에 대한 고찰이 필요하다. A \( \leq \) B 일 때,(A, B)의 평균은 A 보다 크거나 같다. 예)A(2) + B(2) = 4, 평균 2A(2) + B(4) = 6, 평균 3 \( A \leq B \leq C \leq D \) 일 때,(A, B, C, D)의 평균은 (A + B + C + D) / 4 = { (A, B)평균 + (C,D)평균 } / 2(A, B, C, D)의 평균은 (A, B)평균 보다 크거나 같다. 즉, 구하고자 하는 평균은, 평균을 구하기 위해 더해지는 요소의 값들 중, 최저값보다 크거나 같다. 우리는 가장 작은 그룹의, 가장 작은 평균 값을 찾아야 한다.왜냐하면, (A, B, C, D) 평균 값은, (A, B) 두 개의 평균 값보다 작아질 수 없..
Lesson 5. Perfix Sums - MinAvgTwoSlice #1 1. 문제A non-empty array A consisting of N integers is given. A pair of integers (P, Q), such that 0 ≤ P For example, array A such that:A[0] = 4A[1] = 2A[2] = 2 A[3] = 5 A[4] = 1 A[5] = 5 A[6] = 8contains the following example slices:slice (1, 2), whose average is (2 + 2) / 2 = 2;slice (3, 4), whose average is (5 + 1) / 2 = 3;slice (1, 4), whose average is (2 + 2 + 5 + 1) / 4 = 2.5.The goal is to ..
Lesson 5. Perfix Sums - GenomicRangeQuery #2 (속도 개선) 1. 속도 개선 아이디어각 문자가 나타난 횟수를 미리 저장해둔다. 예를들어 S = CAGCCTA 일 때, 다음과 같이 부분 합(Prefix Sums)을 생각해 볼 수 있다.index문자열SACGT0초기 상태00001C01002CA11003CAG11104CAGC12105CAGCC13106CAGCCT13118CAGCCTA2311 P = 2, Q = 4 를 생각해보자.S[2]까지의 부분합은 [1, 1, 0, 0] 이다.S[4]까지의 부분합은 [1, 3, 1, 0] 이다. (초기 상태가 있기 때문에, index 5를 봐야한다.)이제 S[4] - S[2]를 해보면, [0, 2, 1, 0] 임을 알 수 있다. 즉, S[2] ~ S[4] 구간에서 C와 G가 나타났음을 알 수 있다.  2. C++ (코드)vector ..
Lesson 5. Perfix Sums - GenomicRangeQuery #1 1. 문제A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which correspond to the types of successive nucleotides in the sequence. Each nucleotide has an impact factor, which is an integer. Nucleotides of types A, C, G and T have impact factors of 1, 2, 3 and 4, respectively. You are going to answer several queries of the form: What is the minimal impact factor ..

반응형