알고리즘

    NHN 2020 pre-test 1차 모의고사 - 행렬의 영역(c++)

    풀이 #include #include #include using namespace std; int BFS(int x, int y, int sizeOfMatrix, int **matrix, bool **visit){ if(!matrix[x][y] || visit[x][y]) return -1; int count = 1; queue q; q.push( make_pair(x, y) ); visit[x][y] = true; int dx[] = {1,-1,0,0}; int dy[] = {0,0,1,-1}; while(!q.empty()){ int sx = q.front().first; int sy = q.front().second; q.pop(); for(int i=0; i=0 && ny>=0 &&..

    프로그래머스 - 삼각 달팽이(c++)

    문제 설명 정수 n이 매개변수로 주어집니다. 다음 그림과 같이 밑변의 길이와 높이가 n인 삼각형에서 맨 위 꼭짓점부터 반시계 방향으로 달팽이 채우기를 진행한 후, 첫 행부터 마지막 행까지 모두 순서대로 합친 새로운 배열을 return 하도록 solution 함수를 완성해주세요. 제한사항 n은 1 이상 1,000 이하입니다. 입출력 예 입출력 예 설명 입출력 예 #1 문제 예시와 같습니다. 입출력 예 #2 문제 예시와 같습니다. 입출력 예 #3 문제 예시와 같습니다. 풀이 이 문제는 규칙을 찾아야 하는데 어떻게 푸냐에 따라서 많은 방법이 있을 것 같다. 반시계 방향 순서대로 왼쪽 -> 아래 -> 오른쪽 순서로 3등분으로 나눠서 풀었다. #include #include #include using namesp..

    코딜리티 - Distinct(c++)

    문제 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] = 2 A[1] = 1 A[2] = 1 A[3] = 2 A[4] = 3 A[5] = 1 the function should return 3, because there are 3 distinct values appearing in array A, namely 1, 2 and 3. Write an efficient algorithm..

    코딜리티 - CountDiv(c++)

    문제 Write a function: int solution(int A, int B, int K); that, given three integers A, B and K, returns 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 a..

    코딜리티 - MaxCounters(c++)

    문제 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 then operation ..

    코딜리티 - OddOccurrencesInArray(c++)

    문제 A non-empty array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired. For example, in array A such that: A[0] = 9 A[1] = 3 A[2] = 9 A[3] = 3 A[4] = 9 A[5] = 7 A[6] = 9 the elements at indexes 0 and 2 have value 9, the elements a..