728x90
반응형

문제풀이 30

백준, BOJ, 9465번, 스티커: C++ [CPP]

어렵다기 보다.. 엄두를 못냈다. 사실 DP란 것을 알았으면.. N번째부터 생각해봤을텐데.. 그게 안된다. https://www.acmicpc.net/problem/9465 #맞은 풀이 #include #include #include using namespace std; int sticker[2][100002]; int dp[2][100002]; // DP int tc; int main(){ ios::sync_with_stdio(0); cin.tie(0); cin >> tc; //테스트케이스 여러개 while(tc--){ //해당 테스트케이스에 대한 입력 int length; cin >> length; //DP 초기화 for(int i = 0; i

백준, BOJ, 1753번, 최단경로 : C++ [CPP] ★★★★★

어렵다. 다익스트라를 배웠지만 코딩테스트에서 다시 쓰니까 어렵다 https://www.acmicpc.net/problem/1753 #맞은 풀이 #include #include #include #include #include #define INF INT_MAX using namespace std; int V,E,start; //벡터를 원소로 가지는 배열 -> vector adj[20001]; // 인접행렬 from,(weight,to) //-> adj[1] = {3,5} 의 의미 1번 정점에서 5번으로 가는 가중치 3 //이런 그래프에서 최단거리 => 다익스트라 테이블 int table[20001]; //최소 간선을 위한 minHeap {거리, 정점 번호} -> 일반적으로 first를 기준으로 오름차순으로..

프로그래머스, 네트워크 : 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]

이것은.. 가장 작은 것에 대해서만 조사한다는 힌트가 주어졌다는 것을 알아채고 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]

정말 수학문제다. 한줄로만 어떠한 것을 감싸려면 어떻게 해야하는가? 를 생각하면 바로 풀린다. 난 이런 문제가 좋다. 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

728x90
반응형