본문 바로가기

개발/Coding Test74

[프로그래머스 Lv3 C++] 멀리 뛰기 안녕하십니까. Eun🦆입니다. 풀이법 입니다. 코딩테스트 연습 - 멀리 뛰기 효진이는 멀리 뛰기를 연습하고 있습니다. 효진이는 한번에 1칸, 또는 2칸을 뛸 수 있습니다. 칸이 총 4개 있을 때, 효진이는 (1칸, 1칸, 1칸, 1칸) (1칸, 2칸, 1칸) (1칸, 1칸, 2칸) (2칸, 1칸, 1칸) (2칸, 2 programmers.co.kr #include #include #include long long solution(int n) { long long answer = 0; int a = 1; int b = 1; if(n==1) return 1; for(int idx=2;idx 2022. 6. 7.
[프로그래머스 Lv3 C++] 가장 긴 팰린드롬 안녕하십니까. Eun🦆입니다. 풀이법 입니다. 코딩테스트 연습 - 가장 긴 팰린드롬 앞뒤를 뒤집어도 똑같은 문자열을 팰린드롬(palindrome)이라고 합니다. 문자열 s가 주어질 때, s의 부분문자열(Substring)중 가장 긴 팰린드롬의 길이를 return 하는 solution 함수를 완성해 주세요. 예를들 programmers.co.kr #include #include #include ​ int solution(const char* s) { int answer_odd = 0; int answer_even = 0; //printf("len=%d\n",strlen(s)); // odd for(int idx=1;idx 2022. 6. 7.
[프로그래머스 Lv3 C++] 이중우선큐 안녕하십니까. Eun🦆입니다. 풀이법 입니다. 코딩테스트 연습 - 이중우선순위큐 programmers.co.kr #include #include #include using namespace std; vector solution(vector operations) { vector answer; vector queue; for(int idx=0;idx1) sort(queue.begin(), queue.end(), greater()); //printf("D 1:%d\n",queue[0]); if(queue.empty()==0) queue.erase(queue.begin()); } else if(operations[idx]=="D -1") { if(queue.size()>1) sort(queue.begin(), .. 2022. 6. 7.
[프로그래머스 Lv3 C++] 단어 변환 안녕하십니까. Eun🦆입니다. 풀이법 입니다. 코딩테스트 연습 - 단어 변환 두 개의 단어 begin, target과 단어의 집합 words가 있습니다. 아래와 같은 규칙을 이용하여 begin에서 target으로 변환하는 가장 짧은 변환 과정을 찾으려고 합니다. 1. 한 번에 한 개의 알파벳만 바꿀 수 programmers.co.kr #include #include using namespace std; int answer = 99; void dfs(string begin, string target, vector words, vector use, int cnt) { // 매번 들어올 때마다 글자가 한 개 다른 문자를 찾는다. for(int i=0;i 2022. 6. 7.
[프로그래머스 Lv3 C++] 정수 삼각형 안녕하십니까. Eun🦆입니다. 풀이법 입니다. 코딩테스트 연습 - 정수 삼각형 [[7], [3, 8], [8, 1, 0], [2, 7, 4, 4], [4, 5, 2, 6, 5]] 30 programmers.co.kr [시간 초과, DFS로 푼 코드] // 처음 DFS로 완벽하게 풀었다고 좋아했는데... 아쉬운 결과 #include #include using namespace std; int answer = 0; void DFS(vector triangle, int sum, int y, int x) { if(y>=triangle.size()-1) { if(sum > answer) answer = sum; return; } DFS(triangle, sum+triangle[y+1][x], y+1, x); DF.. 2022. 6. 7.
[프로그래머스 Lv3 C++] 단속카메라 안녕하십니까. Eun🦆입니다. 풀이법 입니다. 코딩테스트 연습 - 단속카메라 [[-20,-15], [-14,-5], [-18,-13], [-5,-3]] 2 programmers.co.kr #include #include #include using namespace std; int solution(vector routes) { int answer = 0; // 1. 차량이 서로 만나는 경우 겹치는 위치의 가장 뒤에 카메라를 둔다 // 2. 차량이 만나지 않는 경우엔 카메라 개수를 증가시키고 뒤 차량의 가장 뒤에 카메라를 둔다 sort(routes.begin(), routes.end()); int camera = routes[0][1]; answer++; for(int i=1;i routes[i][1]) c.. 2022. 6. 7.