728x90
반응형

문제풀이(Problem Solving) 326

프로그래머스, 더 맵게 : C++ [CPP]

이것은.. 가장 작은 것에 대해서만 조사한다는 힌트가 주어졌다는 것을 알아채고 Heap을 구성한다면 풀 수 있는 문제다. https://programmers.co.kr/learn/courses/30/lessons/42626?language=cpp #맞는 풀이 #include #include #include #include // 비교함수 greater나 less가 들어있음 //Min Heap을 구성하여 가장 맵지 않은 음식이 맨 위로 오게하자. using namespace std; int solution(vector scoville, int K) { int answer = 0; priority_queue pq; // 크면 밑으로가는 Min Heap을 말함 less가 comparator가 되면 Max Hea..

프로그래머스, 점프와 순간 이동 : C++ [CPP]

어렵지 않다. 다만 문제 그대로 이해해서 쓰면 된다. 이해력의 문제다. https://programmers.co.kr/learn/courses/30/lessons/42747?language=cpp #맞는 풀이 문제를 잘 이해하고 쓸 수 있으면 된다. #include #include using namespace std; int solution(vector citations) { int answer = 0; int n = citations.size(); int h = 0; //h의 최댓값을 찾을 때까지 while(1){ int cnt = 0; //논문을 순회하면서 h값 비교 for(int x:citations){ if(x >= h){ cnt++; } } if(cnt >= h){ //h번 이상 인용된 논문이 ..

프로그래머스, 타겟 넘버 : 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..

728x90
반응형