728x90
반응형

프로그래머스 63

프로그래머스, 타겟 넘버 : C++ [CPP]

이건 그냥 backtracking이라고 생각하면 편하다. 계속 끝까지 돌려보는 것이다. 다만 중간중간 처리해줘야하는 부분이 있는 것이고 https://programmers.co.kr/learn/courses/30/lessons/43165?language=cpp #맞는 풀이 #include #include using namespace std; int answer; //해당 벡터에서 sum을 끝까지해서 target이 되는 경우 void func(vector numbers, int target, int sum, int cnt){ //index다 뒤지면 종료 if(cnt == numbers.size()){ // 끝났을 때 target이 되었다면 if(sum == target){ answer++; } return..

프로그래머스, 카펫 : C++ [CPP]

정말 수학문제다. 한줄로만 어떠한 것을 감싸려면 어떻게 해야하는가? 를 생각하면 바로 풀린다. 난 이런 문제가 좋다. https://programmers.co.kr/learn/courses/30/lessons/42842 #맞는 풀이 #include #include #include using namespace std; vector solution(int brown, int yellow) { vector answer; int temp = (brown-4)/2; // 노랑이 x,y의 길이를 가진다면 x+y 의 값 for(int i = 1; i

프로그래머스, 숫자의 표현 : C++ [CPP]

음.. 그냥 생각나는대로 했지만 n^2의 알고리즘은 너무 혐오스럽길래... 과연 이렇게 짜도 되나생각했다. https://programmers.co.kr/learn/courses/30/lessons/12924 #맞는 풀이 #include #include using namespace std; int solution(int n) { int answer = 0; //1부터 시작O(n) for(int i = 1; i 하지만 j가 n/2에 가까울수록 이 루프가 실행되는 빈도가 줄어듬 while(1){ for(int j = i+1; j= n){ break; } } //종료후 같은지 넘친지 조사 if(res == n) answer++; else break; } } //n은 무조건 n하나로 된다. answer++; r..

프로그래머스, [3차] n진수 게임 : C++ [CPP]

이것도 어렵다기보다.... 귀찮은 방법?? 우선 다른 사람이 더 잘풀었을 것 같다. 나는 생각대로 코딩했다. https://programmers.co.kr/learn/courses/30/lessons/17687 #내 맞는 풀이 #include #include #include using namespace std; //n을 x진법한 문자열 반환 -> 정방향으로 출력 string func(int n, int x){ string s = ""; int div = n; //새로 추가되는 것들이 앞에옴 while(1){ if(div >= x){ int res = div%x; if(res == 10){ s = 'A' + s; }else if(res == 11){ s = 'B' + s; }else if(res == 12..

프로그래머스, 땅따먹기 : C++ [CPP]

DP문제인 것은 알았지만 바로 생각해내기에 어려운 문제.. 우선 나는 그랬다. https://programmers.co.kr/learn/courses/30/lessons/12913 이전 열에서의 최댓값을 채택하는 것이 현재열에서도 최댓값을 보장하는가? 이것에 대해 말할 수 있다면 풀 수 있다. **다만 C++의 max는 2개의 비교만 되어서.. 하나씩 해줬다. max_element는 iterator가 필요해서 그냥 나는 이걸로 했다. # 맞는 풀이 #include #include #include using namespace std; int solution(vector land) { int answer = 0; //x열을 골랐을 때의 최댓값 = 이전 열에서 x를 제외하고 최댓값 + 현재 x열의 값. // ..

프로그래머스, 피보나치 수 : C++ [CPP]

DP의 개념이 있고 Memoization의 개념이 있어야 한다. https://programmers.co.kr/learn/courses/30/lessons/12945?language=cpp #배운 점 Memoization과 하위 문제로 나눌 수 있는지 DP 풀이 계산된 결과를 저장하고 바로 불러올 수 있는가? # 맞는 풀이 #include #include using namespace std; /* //재귀로 구현한 피보나치 (memoization x) int func(int n){ if(n == 0)return 0; if(n == 1) return 1; return (func(n-1) + func(n-2)) % 1234567; } */ //for memoization int arr[100001] = {0..

프로그래머스, N개의 최소공배수 : C++ [CPP]

이것도 그냥 수학이다. 유클리드 호제법을 이용하면 된다. 간략하게 말해서 G(A,B) = G(B, A%B)와 같다. https://programmers.co.kr/learn/courses/30/lessons/12953 #맞은 풀이 #include #include #include using namespace std; //최소 공배수를 구하기 위해 최대공약수를 알아보자 //유클리드 호제법 사용 int max(int a, int b){ //a가 b보다 크다는 가정 if(a n개의 원소면 n번 연산 while(size){ def = min(def,arr[size-1]); //현재까지 계산된 최소 공배수와 다음 원소와의 최소공배수 size--; } // 끝나면 def가 모든 수에 대한 최소공배수임. answer ..

프로그래머스, 예산 : C++ [CPP]

Greed 하게 풀었는데 Greed를 시도해보니 맞았다. 보다는 Greed를 쓴 이유를 알면 좋다. https://programmers.co.kr/learn/courses/30/lessons/12982?language=cpp #맞은 풀이 #include #include #include #include #include using namespace std; int solution(vector d, int budget) { int answer = 0; sort(d.begin(), d.end()); // 오름차순 정렬. //Greed 방법 적용 -> 가장 적은 부서부터 적용 for(int x : d){ if(x

728x90
반응형