Algorithm (401) 썸네일형 리스트형 [프로그래머스] 다음 큰 숫자 - bitset 문제 풀이 #include #include using namespace std; int getOneCount(int num){ int num_count = 0; while(num > 0){ if(num % 2 == 1) num_count ++; num /= 2; } return num_count; } int solution(int n) { int count = 0; int answer = 0; count = getOneCount(n); for(int i = n+1 ; ;i++){ int i_count = getOneCount(i); if(i_count == count ) { answer = i; break; } } return answer; } 다른 사람 풀이 #include using namespace.. [프로그래머스] 3 x n 타일링 - MOD 모듈러 연산 문제 풀이 #include #include #include using namespace std; // mod 알고리즘 int solution(int n) { long long mod = 1000000007; vector v = {0,3,11}; if( n%2 != 0) return 0; for(int i=3; i0; j--){ sum += (v[j] * 2) % mod; } v.push_back(sum%mod); } return v[n/2]% mod; } n이 홀수일 경우는 타일을 놓을 수 있는 방법이 없다. 0을 리턴한다. f(2)의 타일을 놓을 수 있는 방법은 3가지이다. f(4)는 이전의 모습에서 타일이 2개 추가되었으므로 f(이전 단계) * f(2)인데, f(4) 이상일 때부터는 특수한 타일 배치.. [프로그래머스] 2 x n 타일링 - 피보나치수열 문제 풀이 #include #include using namespace std; // 피보나치수열 // 1 > 2 > 3 > 5 > 8 > ... int num = 1000000007; int solution(int n) { int bf = 1; int af = 1; int temp; for(int i=1; i [프로그래머스] 가장 큰 정사각형 찾기 - DP 문제 풀이 #include #include #include using namespace std; // 0과 1로 이루어짐. //[[0,0,0,0]] : 0 , [[0,0,1,0]] : 1 // DP 다이나믹 프로그래밍(또는 동적 계획법) int solution(vector board) { int max = 0; int space[1001][1001] = {0}; for(int i=0; i [프로그래머스] 캐시 문제 풀이 #include #include #include using namespace std; // 대소문자 구분을 하지 않는다 // transform(str.begin(), str.end(), str.begin(), ::tolower); // 캐시 교체 알고리즘은 LRU(Least Recently Used) int solution(int cacheSize, vector cities) { int answer = 0; vector cache; if(cacheSize == 0) return cities.size()*5; for(int i=0; ibegin(), it->end(), it->begin(), ::tolower); bool flag = false; for(vector::iterator itt = .. [프로그래머스] 완전탐색 - 전력망을 둘로 나누기 문제 풀이 #include #include #include using namespace std; /************************ n : 송전탑의 개수 wires : 전선 정보 *************************/ int g[101][101] = {0}; vector visited; int dfs(int index, int n){ if(visited[index]) return 0; visited[index] = true; int count = 1; for(int i=1; i [프로그래머스] 완전탐색 - 모음사전 / 중복순열 문제 풀이 #include #include #include using namespace std; vector al = {"A", "E", "I", "O", "U"}; int count = 0; int answer; void dfs(string s, string word){ if(s == word){ answer = count; return; } if(s.size() == 5){ return; } for(int i=0; i [프로그래머스] 완전탐색 - 카펫 문제 풀이 #include #include #include using namespace std; vector solution(int brown, int yellow) { vector answer; vector b; for(int i=1; i w = brown/2 + 2 - h -> w + h = brown/2 + 2 h를 3으로 세팅한 이유는 h가 3 이상일 때 노란 면적이 나오기 때문이다. 이전 1 ··· 38 39 40 41 42 43 44 ··· 51 다음