[알고리즘 + 자료구조]/[프로그래머스]
프로그래머스 카펫
Hevton
2023. 8. 7. 12:16
반응형
전체 타일 갯수를 먼저 구하고.
brwon 타일 갯수는 (row * 2) + ((col - 2) * 2) 일 테므로, 이게 맞아떨어지면 yellow도 맞아떨어진다는 원리를 이용한다.
#include <string>
#include <vector>
using namespace std;
vector<int> solution(int brown, int yellow) {
vector<int> answer;
int tiles = brown + yellow;
for(int i = 1; i * i <= tiles; i++) {
if(tiles % i == 0) {
int row = tiles / i;
int col = i;
if((row * 2) + ((col - 2) * 2) == brown)
return {row, col};
}
}
return answer; // 없는 경우 없으니, 닿지않음
}
괜찮은 풀이일지도!
반응형