728x90 Algorithm77 [프로그래머스] 문자열 압축 문제 분석 압축한다.. 딱히 분석할게 없었다. 구현 #include #include #include using namespace std; int solution(string s){ int answer = 0; int findCount = 0; int index = 0; int minCount = 987654321; string findString=""; string compressString = ""; for(int i =1; i 1){ compressString += to_string(matchCount)+findString; } else { compressString += findString; } findString = tmpString; j-=findCount; matchCount = 0; } } .. 2021. 5. 15. [프로그래머스] 올바른 괄호 문제 분석 벡터를 스택처럼 이용하여 푼다. 구현 #include #include #include using namespace std; bool solution(string s) { bool answer = true; // [실행] 버튼을 누르면 출력 값을 볼 수 있습니다. vector check; for(int i = 0; i < s.size(); i ++ ){ if( s.at(i) == '('){ check.push_back(s.at(i)); } else if (s.at(i) == ')') { if(check.empty()){ answer = false; break; } check.pop_back(); } } if( check.size() != 0 ){ answer = false; } return an.. 2021. 5. 15. [프로그래머스] 삼각 달팽이 문제 분석 패턴을 찾기 처음에는 n 만큼 왼쪽 아래 대각선 방향으로 숫자를 채운다. 두번째는 n-1 만큼 아래를 채운다. 세번째는 n-2 만큼 왼쪽 위 대각선 방향으로 채운다. 처음부터 반복한다. 구현 #include #include #include using namespace std; int map[10001][10001]; int dirx[3] = {1, 0, -1}; int diry[3] = {0, 1, -1}; void solve(int n); vector solution(int n) { vector answer; solve(n); for(int i = 0; i < n; i++){ for(int j = 0; j 2021. 5. 15. [Algorithm] Max heapify 구현과 이론 Max heapify 최대힙에 대해서 알아보고 구현까지 해보자 힙이란? 완전이진트리의 한 종류이다. 힙의조건 heap property를 만족해야 된다. Heap property 1.Max heap property 부모는 자식보다 크거나 같다. 2.Min heap property 부모는 자식보다 작거나 같다. 힙의구조 구현방법 1. Tree의 index 표현 root = i; Left child = i * 2 Right child = i * 2 +1 2. Swap 방법 자식노드가 부모노드 보다 크면 자리를 변경한다. 3. 반복하면서 Max heap을 만들어 간다. 4. 실제로 구현할 때는 맨 마지막 노드 즉 index 7번의 부모노드부터 선택하여 반복 정렬해간다. 그래야 순차적으로 root들을 기준으로 m.. 2021. 5. 4. 이전 1 ··· 15 16 17 18 19 20 다음 728x90 반응형