728x90
반응형

프로그래머스 63

백준, BOJ, 2573번, 빙산 : C++ [CPP]

어려웠다. 조건이 많았기 때문이다. https://www.acmicpc.net/problem/2573 #맞는 풀이 #include #include #define X first #define Y second using namespace std; int dx[4] = { -1,1,0,0 }; int dy[4] = { 0,0,-1,1 }; int R, C; int curMap[300][300]; int nextMap[300][300]; int year = 0; int visited[300][300]; //dfs를 위함 void melting(int x, int y) { int cnt = 0; //주변 바다의 칸 for (int dir = 0; dir < 4; dir++) { int nx = x + dx[dir..

프로그래머스, 보석쇼핑 : C++ [CPP]

어렵다. 처음에 생각하기는 어렵지 않은데 효율성 문제가 어렵다. https://programmers.co.kr/learn/courses/30/lessons/67258 효율성이 어렵다 생각나는대로 for문으로 한 번 풀어봤다. #include #include #include #include using namespace std; vector solution(vector gems){ vector answer; vector rank; // 길이 , 시작, 도착 set s(gems.begin(), gems.end()); // gems의 종류를 알기 위함. int size_gem = gems.size(); // 총 탐색해야하는 보석 수 //시작지점 for(int i = 0; i 중복되어도 오류가 생기진 않고 원소는..

프로그래머스, 네트워크 : C++ [CPP]

이것도 생각한지 10분만에 내 머릿속에 있는 생각이 맞았다는 것이 너무 기뻐서 팔짝 뜀 내 생각이 점차 논리적으로 맞아가고 있네 https://programmers.co.kr/learn/courses/30/lessons/43162 #맞는 풀이 #include #include #include using namespace std; int visited[201][201]; // 네트워크를 연결상태 [from][to] 구분하기 위함 bool node[201]; //[노드 방문했는지] int solution(int n, vector computers) { int answer = 0; //모든 노드를 조사해야함 for(int i = 0; i 조사 안했다면 새로운 네트워크임 answer++; // 네트워크 번호를 붙..

프로그래머스, 정수 삼각형 : C++ [CPP]

사실 1로만 테두리가 이루어져있으면 파스칼 삼각형이었던가 이항정리할 때 배운듯한 느낌이 있다 https://programmers.co.kr/learn/courses/30/lessons/43105 #맞은 풀이 #include #include #include using namespace std; int solution(vector triangle) { int answer = 0; int row = triangle.size(); // 삼각형 높이 = 행 숫자; //삼각형 높이가 1이면 작동안함(2번째행부터 항상시작) for(int i = 1; i 해당 행의 원소가 3개 이상일 때만 작동 for(int j=1; j

프로그래머스, 가장 큰 정사각형 찾기 : C++ [CPP]

솔직히 나는 해법을 찾지 못해서 다른 사람 풀이를 찾아왔다. Brute Force에서 케이스를 나누어가며 최대한 줄여봤지만 효율성에서 박살났다. https://programmers.co.kr/learn/courses/30/lessons/12905 #맞는 풀이 #include #include #include using namespace std; //처음부터 생각하진 못함. //효율성 실패때문에 찾아봄. int solution(vector board) { int answer = board[0][0]; // 시작 int r = board.size(); //행 int c = board[0].size();//열 //왼쪽, 왼쪽 위, 위쪽이 존재해야 하기 때문에 //(1,1) 부터(r-1,c-1)까지만 조사하면 된..

프로그래머스, 방문 길이 : C++ [CPP]

재밌어보이길래 한 번 해봤다. 재미있긴 했다. 열거체를 배운김에 써먹었다 https://programmers.co.kr/learn/courses/30/lessons/49994 #맞는 풀이 #include #include using namespace std; enum direction {U,D,L,R}; //0,1,2,3 bool visited[12][12][12][12]; // -5 ~ 5 11개 숫자. 하지만 왼쪽 위가 (0,0)임. //[시작x][시작y][도착x][도착y] int offset = 5; // 맵크기에 비례 int solution(string dirs) { int answer = 0; int arr[2] = {0,0}; // (x,y) arr[0] += offset;//x arr[1] +..

프로그래머스, 스킬트리 : C++ [CPP]

쉽진 않지만 그냥 생각하기 까다롭다는 것? 구현하기는 어렵지 않다는 것 https://programmers.co.kr/learn/courses/30/lessons/49993?language=cpp #맞는 풀이 #include #include using namespace std; int solution(string skill, vector skill_trees) { int answer = 0; //Skil_Tree가 잘 되었는지 하나씩 조사 for(auto x : skill_trees){ string s = ""; //해당 스킬트리에서 순서를 지켜야하는 스킬이 있는지 조사 for(auto y : x){ for(auto a : skill){ if(y == a){ s += a; break; } } } //해당..

프로그래머스, 더 맵게 : 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번 이상 인용된 논문이 ..

728x90
반응형