본문 바로가기

Algorithm

(401)
[프로그래머스] 정렬 - H-Index / greater<>() 문제 풀이 #include #include #include // sort using namespace std; int solution(vector citations) { int Hindex = 0; sort(citations.begin(), citations.end()); for(int i = 0; i
[프로그래머스]스택 큐 - 다리를 지나는 트럭 문제 풀이 #include #include #include using namespace std; int solution(int bridge_length, int weight, vector truck_weights) { int count = 0; // 시간 int noww = 0; // 현재 다리 위에있는 트럭들의 무게 queue q; for(int i=0 ;i weight){ count = q.front().second + bridge_length; noww -= q.front().first; q.pop(); } q.push(pair(truck_weights[i], count)); noww += truck_weights[i]; } count += bridge_length; return count; } p..
[프로그래머스] n^2 배열 자르기 문제 풀이 #include #include using namespace std; vector solution(int n, long long left, long long right) { vector answer; int num = left/(long long)n; for(long long i=left; i
[프로그래머스] 완전탐색 - 피로도 문제 풀이 #include #include using namespace std; int s; int answer = 0; vector visited; void dfs(int len, int count, int HP, vector dungeons){ if(len == s){ answer ans ? s : ans; } } return ans; } auto d2 = vector(d.begin(), it); 코드의 위 부분을 이해를 못하고있다. 인자가 2개인 벡터 ... 알게되면 다시 수정하여 적도록 하겠다! vector.insert vector.insert(const_iterator position, InputIterator first, InputIterator last) position : 원소를 추가할 위..
[프로그래머스] 위장 문제 풀이 #include #include #include using namespace std; // (x+a)(x+b)(x+c) = x3 + (a+b+c)x2 + (ab+bc+ca)x + (abc) int solution(vector clothes) { map map; int answer = 0; for(vector v : clothes){ map[v[1]]++; } int n = 1; for(auto m : map){ n *= (m.second+1); } answer += n -1; return answer; } (x+a)(x+b)(x+c) = x3 + (a+b+c)x2 + (ab+bc+ca)x + (abc) 수학적 해결방법으로 알고리즘을 구현했다. 출처: 프로그래머스 코딩 테스트 연습, https:..
[프로그래머스] 예상대진표 문제 풀이 1 #include using namespace std; int solution(int n, int a, int b) { int answer = 0; vector v; v.assign(n, 0); v[a-1] = a; v[b-1] = b; bool roof = true; while(roof){ answer ++; for(int i=0; i
[프로그래머스] 조이스틱 문제 풀이 #include #include #include using namespace std; int sol1(char c, string al){ int val = 0; int p2 = find(al.begin(), al.end(),c) - al.begin(); if(p2 > al.size()/2){ p2 = al.size() - p2; } return p2; } int solution(string name) { int answer = 0; int idx, move = 0; int l_len = 0, r_len = 0; string al = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 1. 가장 긴 A len 구하기 move = name.size() -1; // 정방향 for(int i=..
[프로그래머스] 게임 맵 최단거리 / BFS, DFS, Queue 문제 풀이 1. (DFS) #include #include // memset using namespace std; bool visited[100][100]; int dx[4]; int dy[4]; int N, M, min_count = 10000; bool enable = false; vector map; /********************************* dfs로 구현 버전 테스트 케이스는 전부 통과하였으나 효율성을 통과하지 못함 *********************************/ void dfs(int y, int x, int count){ count++; if(y == N-1 && x == M-1){ if(count < min_count) min_count = count; en..