[Codility] The Matrix
[Codility] The Matrix
https://app.codility.com/programmers/challenges/matrix2021/
The Matrix 2021 challenge
Take part in our The Matrix 2021 challenge.
app.codility.com

해결전략
이분탐색
정답 코드
#include <vector> #include <algorithm> using namespace std; bool Check(vector<int>& A, int k) // Check 함수는 주어진 배열 A에서 k보다 크거나 같은 연속된 숫자의 개수가 k보다 크거나 같은지를 확인하는 함수 { int i = 0; while (i < A.size()) { int j = i; while (j < A.size() && A[j] >= k) { // j는 i부터 시작해서 A[j]가 k보다 크거나 같은 동안 j를 증가 j++; } // 만약 j - i가 k보다 크거나 같다면, k보다 크거나 같은 연속된 숫자의 개수가 k보다 크거나 같다는 것이므로 true를 반환 if (j - i >= k) return true; i = max(j, i + 1); // j가 i + 1보다 크다면 i를 j로 설정하고, 그렇지 않다면 i를 하나 증가 } return false; // 위의 조건을 만족하는 k가 없다면 false를 반환 } int solution(vector<int> &A) // 배열 A에서 가장 큰 k를 찾는 함수 { int lt = 1; int rt = A.size(); while (lt < rt) // 이진 탐색을 사용하여 lt와 rt 사이에서 k를 찾음 { int mid = (lt + rt + 1) / 2; if (Check(A, mid)) // Check(A, mid)가 true라면, mid보다 크거나 같은 k가 있다는 의미이므로 lt를 mid로 설정 lt = mid; else // 그렇지 않다면, mid보다 큰 k가 없다는 의미이므로 rt를 mid - 1로 설정 rt = mid - 1; } return lt; // lt는 조건을 만족하는 가장 큰 k다 }
'⭐ 코딩테스트 > Codility' 카테고리의 다른 글
[Codility] Array Inversion Count (0) | 2024.02.18 |
---|---|
[Codility] Tree Height (0) | 2024.02.18 |
[Codility] Tree Longest Zigzag (0) | 2024.02.18 |
[Codility] FirstUnique (0) | 2024.02.07 |
[Codility] MaxNonoverlappingSegments (1) | 2024.02.06 |
댓글
이 글 공유하기
다른 글
-
[Codility] Tree Height
[Codility] Tree Height
2024.02.18[Codility] Tree Height 해결전략 이진트리 Binary Tree 정답코드 #include int answer; void DFS(tree* node, int currLength) { if(node == nullptr) return; answer = max(answer, currLength); if(node->l != nullptr){ DFS(node->l, currLength + 1); } if(node->r != nullptr){ DFS(node->r, currLength + 1); } } int solution(tree* T) { DFS(T, 0); return answer; } 유사문제 2024.02.18 - [⭐ 코딩테스트/Codility] - [Codility] Tree Longe… -
[Codility] Tree Longest Zigzag
[Codility] Tree Longest Zigzag
2024.02.18[Codility] Tree Longest Zigzag https://app.codility.com/programmers/trainings/7/tree_longest_zig_zag/start/ Codility Your browser is not supported Please, update your browser or switch to a different one. Learn more about what browsers are supported app.codility.com 해결전략 이진트리 Binary Tree 정답코드 #include using namespace std; int answer; // Longest Zigzag Length void DFS(tree* node, int currLength, … -
[Codility] FirstUnique
[Codility] FirstUnique
2024.02.07[Codility] FirstUnique https://app.codility.com/programmers/trainings/4/first_unique/start/ Codility Your browser is not supported Please, update your browser or switch to a different one. Learn more about what browsers are supported app.codility.com 해결전략 정렬 sort 정답 코드 #include #include #include int solution(vector &A) { int n = A.size(); queue Q; map myMap; for(int i = 0; i < n; i++){ Q.push(A[… -
[Codility] MaxNonoverlappingSegments
[Codility] MaxNonoverlappingSegments
2024.02.06[Codility] MaxNonoverlappingSegments https://app.codility.com/programmers/lessons/16-greedy_algorithms/ 16. Greedy algorithms lesson - Learn to Code - Codility Tie adjacent ropes to achieve the maximum number of ropes of length >= K. app.codility.com 해결전략 그리디 Greedy A[i]가 cur보다 클 때만 쌍을 선택하므로, 항상 A 벡터의 값이 증가하는 방향으로 요소를 선택한다. 정답코드 #include int solution(vector &A, vector &B) { int n = A.size(); if(…
댓글을 사용할 수 없습니다.