본문 바로가기
728x90

알고리즘193

알고리즘(C++) / 프로그래머스 level 3 : 정수 삼각형 level 3 : 정수 삼각형 https://programmers.co.kr/learn/courses/30/lessons/43105 코딩테스트 연습 - 정수 삼각형 [[7], [3, 8], [8, 1, 0], [2, 7, 4, 4], [4, 5, 2, 6, 5]] 30 programmers.co.kr 코드 //프로그래머스 정수 삼각형 #include #include #include #include using namespace std; int solution(vector triangle) { int answer = 0; for (int i = 1; i < triangle.size(); i++) { //두번째 줄부터 for (int j = 0; j < triangle[i].size(); j++) { if (.. 2021. 9. 28.
알고리즘 / DP(Dynamic programming) 동적계획법 DP(Dynamic programming) 동적 계획법 동적 계획법(DP)은 복잡한 문제를 간단한 여러 개의 문제로 나누어 푸는 방법이다. state의 값을 메모리에 저장하여 반복적인 연산을 피해 시간 복잡도를 낮추고 메모리 낭비를 막는다. 코딩 테스트에 dp는 자주 나오는 문제이다. 하지만 나는 dp 푸는 것이 두려워 계속 미뤄뒀지만.. 이제는 진짜 풀어볼 때가 되었다... DP를 푸는 방법 DP를 사용하여 푸는 문제인지 확인 어떻게 상태를 표현할지 정함 상태 관계를 수식화 top-down or bottom-up 방식으로 풀지 정함 1. DP를 사용하는 푸는 문제인지 확인 특정 수량의 최대 최소를 구하는 문제들, 특정 조건을 만족하는 배열 개수를 세는 문제 등 DP로 풀 수 있다. 중복되는 함수들을 생.. 2021. 9. 28.
알고리즘(C++) / 프로그래머스 위클리챌린지 : 최소직사각형 위클리챌린지 : 최소직사각형 https://programmers.co.kr/learn/courses/30/lessons/86491?language=cpp 코딩테스트 연습 - 8주차 [[10, 7], [12, 3], [8, 15], [14, 7], [5, 15]] 120 [[14, 4], [19, 6], [6, 16], [18, 7], [7, 11]] 133 programmers.co.kr 코드 //프로그래머스 최소직사각형 #include #include #include #include using namespace std; int solution(vector sizes) { int answer = 0; int max_col = 0; int max_row = 0; for (int i = 0; i < size.. 2021. 9. 27.
알고리즘(C++) / 프로그래머스 level 3 : 가장 긴 팰린드롬 level 3 : 가장 긴 팰린드롬 https://programmers.co.kr/learn/courses/30/lessons/12904?language=cpp 코딩테스트 연습 - 가장 긴 팰린드롬 앞뒤를 뒤집어도 똑같은 문자열을 팰린드롬(palindrome)이라고 합니다. 문자열 s가 주어질 때, s의 부분문자열(Substring)중 가장 긴 팰린드롬의 길이를 return 하는 solution 함수를 완성해 주세요. 예를들 programmers.co.kr 코드 //프로그래머스 가장 긴 팰린드롬 #include #include #include using namespace std; int palindrome(string& s, int left, int right) { while (left >= 0 && r.. 2021. 9. 24.
알고리즘(C++) / 프로그래머스 level 2 : JadenCase 문자열 만들기 level 2 : JadenCase 문자열 만들기 https://programmers.co.kr/learn/courses/30/lessons/12951?language=cpp 코딩테스트 연습 - JadenCase 문자열 만들기 JadenCase란 모든 단어의 첫 문자가 대문자이고, 그 외의 알파벳은 소문자인 문자열입니다. 문자열 s가 주어졌을 때, s를 JadenCase로 바꾼 문자열을 리턴하는 함수, solution을 완성해주세요. 제한 조건 programmers.co.kr 코드 //프로그래머스 JadenCase 문자열 만들기 #include #include #include using namespace std; string solution(string s) { int check = 1; for (int.. 2021. 9. 21.
알고리즘(C++) / 프로그래머스 level 2 : H-Index level 2 : H-Index https://programmers.co.kr/learn/courses/30/lessons/42747?language=cpp 코딩테스트 연습 - H-Index H-Index는 과학자의 생산성과 영향력을 나타내는 지표입니다. 어느 과학자의 H-Index를 나타내는 값인 h를 구하려고 합니다. 위키백과1에 따르면, H-Index는 다음과 같이 구합니다. 어떤 과학자가 발표 programmers.co.kr 코드 //프로그래머스 H-Index #include #include #include #include using namespace std; int solution(vector citations) { int answer = 0; sort(citations.begin(), cita.. 2021. 9. 17.
알고리즘(C++) / 프로그래머스 level 2 : 소수 찾기 level 2 : 소수 찾기 https://programmers.co.kr/learn/courses/30/lessons/42839?language=cpp 코딩테스트 연습 - 소수 찾기 한자리 숫자가 적힌 종이 조각이 흩어져있습니다. 흩어진 종이 조각을 붙여 소수를 몇 개 만들 수 있는지 알아내려 합니다. 각 종이 조각에 적힌 숫자가 적힌 문자열 numbers가 주어졌을 때, 종이 programmers.co.kr 코드 //프로그래머스 소수찾기 #include #include #include #include #include #include using namespace std; //소수 판별 bool prime(int num) { if (num < 2) return false; for (int i = 2; i 2021. 9. 16.
알고리즘(C++) / 프로그래머스 level 1 : 제일 작은 수 제거하기 level 1 : 제일 작은 수 제거하기 https://programmers.co.kr/learn/courses/30/lessons/12935?language=cpp 코딩테스트 연습 - 제일 작은 수 제거하기 정수를 저장한 배열, arr 에서 가장 작은 수를 제거한 배열을 리턴하는 함수, solution을 완성해주세요. 단, 리턴하려는 배열이 빈 배열인 경우엔 배열에 -1을 채워 리턴하세요. 예를들어 arr이 [4,3,2,1 programmers.co.kr 코드 //프로그래머스 제일 작은 수 제거하기 #include #include #include #include using namespace std; vector solution(vector arr) { vector answer; int min = *mi.. 2021. 9. 15.
알고리즘(C++) / 프로그래머스 위클리 챌린지 7주차 : 입실 퇴실 위클리 챌린지 7주차 : 입실 퇴실 https://programmers.co.kr/learn/courses/30/lessons/86048?language=cpp# 코딩테스트 연습 - 7주차 사회적 거리두기를 위해 회의실에 출입할 때 명부에 이름을 적어야 합니다. 입실과 퇴실이 동시에 이뤄지는 경우는 없으며, 입실 시각과 퇴실 시각은 따로 기록하지 않습니다. 오늘 회의실에는 programmers.co.kr 코드 //프로그래머스 7주차 입실 퇴실 #include #include #include #include using namespace std; vector solution(vector enter, vector leave) { vector answer(enter.size() + 1); vector inout.. 2021. 9. 14.
알고리즘(C++) / 프로그래머스 위클리 챌린지 6주차 : 복서 정렬하기 위클리 챌린지 6주차 : 복서 정렬하기 https://programmers.co.kr/learn/courses/30/lessons/85002?language=cpp 코딩테스트 연습 - 6주차_복서 정렬하기 복서 선수들의 몸무게 weights와, 복서 선수들의 전적을 나타내는 head2head가 매개변수로 주어집니다. 복서 선수들의 번호를 다음과 같은 순서로 정렬한 후 return 하도록 solution 함수를 완성해주세요 programmers.co.kr 코드 //프로그래머스 6주차 복서 정렬하기 #include #include #include #include using namespace std; struct Person { int num = 0; //자신 번호 int weight = 0; //자신 몸무게.. 2021. 9. 14.
알고리즘(C++) / 프로그래머스 level 3 : 불량 사용자 level 3 : 불량 사용자 https://programmers.co.kr/learn/courses/30/lessons/64064?language=cpp 코딩테스트 연습 - 불량 사용자 개발팀 내에서 이벤트 개발을 담당하고 있는 "무지"는 최근 진행된 카카오이모티콘 이벤트에 비정상적인 방법으로 당첨을 시도한 응모자들을 발견하였습니다. 이런 응모자들을 따로 모아 불량 programmers.co.kr 코드 //프로그래머스 불량 사용자 #include #include #include #include using namespace std; bool visited[8]; set s; //중복 포함 x void DFS(vector& user_id, vector& banned_id, int index) { if (i.. 2021. 9. 10.
알고리즘(C++) / 프로그래머스 level 3 : 자물쇠와 열쇠 level 3 : 자물쇠와 열쇠 https://programmers.co.kr/learn/courses/30/lessons/60059?language=cpp 코딩테스트 연습 - 자물쇠와 열쇠 [[0, 0, 0], [1, 0, 0], [0, 1, 1]] [[1, 1, 1], [1, 1, 0], [1, 0, 1]] true programmers.co.kr 코드 //프로그래머스 자물쇠와 열쇠 #include #include #include #include #include #include using namespace std; //시계방향 90도 회전 void rotation(vector& key) { vector key_rotation; for (int i = 0; i < key.size(); i++) { ve.. 2021. 9. 7.