-
[C++] ํ๋ก๊ทธ๋๋จธ์ค 2019 ์นด์นด์ค ๊ฐ๋ฐ์ ๊ฒจ์ธ ์ธํด์ญ - ์ง๊ฒ๋ค๋ฆฌ ๊ฑด๋๊ธฐ์ฝ๋ฉํ ์คํธ/์ฝ๋ฉํ ์คํธ ์ฐ์ต 2020. 10. 28. 09:24728x90
(2์๊ฐ) ์ ํ์ฑ์ ์ฌ์ด๋ฐ ๋์ ํ ํจ์จ์ฑ ์ฌ๋ฆด ์์ด๋์ด๊ฐ ์๋ ์ฌ๋ผ์ ๋ค๋ฅธ ์ฌ๋ ํ์ด๋ฅผ ์ฐธ๊ณ ํ๋ค. ใ ใ ์ด๋ถํ์ ์ธ ์๊ฐ์กฐ์ฐจ ๋ชปํ๋ค...
์ ํ์ฑ๋ง ๋ง์ ๋ฐฉ๋ฒ(1๋ถํฐ ์์๋๋ก ์นด์ดํ )
#include <string> #include <vector> using namespace std; int solution(vector<int> stones, int k) { int answer = 0; bool check = true; while(check){ int step = 0; for(int i=0; i<stones.size(); i++){ if(stones[i] != 0){ stones[i]--; step = 0; }else{ step++; if(step == k){ check = false; break; } } } if(check){ answer++; } } return answer; }
์ ํ์ฑ ํจ์จ์ฑ(1๋ถํฐ 200000000๊น์ง ๋ฒ์์์ ์ด๋ถํ์)
#include <string> #include <vector> #include <algorithm> using namespace std; bool crossStones(int num, int k, vector<int> stones){ int step = 0; for(int i=0; i<stones.size(); i++){ if(stones[i] - num > 0){ step = 0; }else{ step++; if(step == k) return false; } } return true; } int solution(vector<int> stones, int k) { int left = 1; int right = 200000000; while(left <= right){ int mid = (left + right) / 2; if(crossStones(mid, k, stones)){ left = mid + 1; }else{ right = mid - 1; } } return left; }
728x90'์ฝ๋ฉํ ์คํธ > ์ฝ๋ฉํ ์คํธ ์ฐ์ต' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ