#include <string> #include <vector> #include <iostream> using namespace std; bool cal(vector<int> stones, int mid, int k) { int cnt = 0; for(int i = 0; i < stones.size(); i++) { if(stones[i] - mid <= 0) { cnt++; } else { cnt = 0; } if(cnt >= k) { return true; } } return false; } int solution(vector<int> stones, int k) { int answer = 0; int left = 1; int right = 200000000; int Max = 0; while(left <= right) { // 친구의 수 int mid = (left + right) / 2; if(cal(stones, mid, k)) { right = mid-1; } else { left = mid+1; // 연속된 (stone - 친구 == 0) 부분이 k개 미만인 최댓값 구하기 if(Max < mid) { Max = mid; } } } // 연속된 (stone - 친구 == 0) 부분이 k개 이상이려면 최댓값+1 return Max+1; } | cs |
'Programmers > Level 3' 카테고리의 다른 글
[프로그래머스 3] 블록 이동하기 (C/C++) (★★) (0) | 2020.03.11 |
---|---|
[프로그래머스 3] GPS (C/C++) (★★★★) (0) | 2020.03.11 |
[프로그래머스 3] 리틀 프렌즈 사천성 (C/C++) (★★) (0) | 2020.03.10 |
[프로그래머스 3] 방문 길이 (C/C++) (0) | 2020.03.09 |
[프로그래머스 3] 숫자 게임 (C/C++) (0) | 2020.03.09 |