Algorithm 87

1780번 종이의 개수

출처: www.acmicpc.net/problem/1780 1780번: 종이의 개수 N×N크기의 행렬로 표현되는 종이가 있다. 종이의 각 칸에는 -1, 0, 1의 세 값 중 하나가 저장되어 있다. 우리는 이 행렬을 적절한 크기로 자르려고 하는데, 이때 다음의 규칙에 따라 자르려고 한다. www.acmicpc.net 위 문제는 분할 정복 문제이다. 분할 정복 알고리즘 문제는 처음 풀어보긴 해서, 어떻게 접근할지 몰라 구글링의 힘을 빌렸다. 2번에서 같은 크기의 9개의 종이로 자르고 --> 이 부분을 통해 분할 정복임을 알 수 있다. 풀이의 경우에는 이중 for문과 배열 시작/종료 지점에 대해서만 잘 생각한다면 금방 풀 수 있는 문제이다. 전체 소스: github.com/jmmm25/Algorithm/blo..

Algorithm/BOJ 2021.02.12

Medium 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers

문제 출처: leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/ Partitioning Into Minimum Number Of Deci-Binary Numbers - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com deci-binary란 0으로 시작하지 않는 0과 1로 이루어진 값이다. 32는 10 + 11 + 11 의 구성으로 이루어진 deci-binary로 표현을 할 수..

Algorithm/LeetCode 2021.02.11

11279번 최대 힙

문제 출처: www.acmicpc.net/problem/11279 11279번: 최대 힙 첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 www.acmicpc.net C++ STL의 최대힙 priority_queue를 통해 쉽게 해결할 수 있는 문제이다. Max Heap의 개념 - 최대 트리(Max Tree)는 각 노드의 키(Key)값이 (자식 노드가 있다면) 그 자식의 키(Key)값보다 작지 않은(=크거나 같은) 트리이다. - 최대 힙(Max Heap)은 최대 트리(Max Tree)이면서 완전 이진 트리(Complete Binary Tree..

Algorithm/BOJ 2021.02.11

10819번 차이를 최대로

출처: www.acmicpc.net/problem/10819 10819번: 차이를 최대로 첫째 줄에 N (3 ≤ N ≤ 8)이 주어진다. 둘째 줄에는 배열 A에 들어있는 정수가 주어진다. 배열에 들어있는 정수는 -100보다 크거나 같고, 100보다 작거나 같다. www.acmicpc.net 주어진 수를 적절하게 순서를 변경하여 최대 or 최소 값을 구하는 문제는 순열을 통해 문제 해결을 하면 된다. C++ STL의 next_permutation 함수를 통해 쉽게 문제를 해결할 수 있다. 전체 소스 보기 : github.com/jmmm25/Algorithm/blob/master/BOJ/10819.%20%EC%B0%A8%EC%9D%B4%EB%A5%BC%20%EC%B5%9C%EB%8C%80%EB%A1%9C/m..

Algorithm/BOJ 2021.02.10

1541번 잃어버린 괄호

문제출처: www.acmicpc.net/problem/1541 위 문제는 주어진 식에 괄호를 적절히 쳐서 결과 값을 최소로 만드는 문제이다. 해당 예제 55 - 50 + 40는 55 - (50 + 40)으로 괄호를 적용할 경우 최소가 된다. for문을 통해 문자인 숫자를 붙여주고, 처음에 나온 연산자가 + 일 경우에는 더해주고, 그 외에는 -를 해주는 것이 최소값이 된다. #include using namespace std; string str; int sol(string str) { int ret = 0; string s = ""; bool isMinus = false; for (int i = 0; i > str; cout

Algorithm/BOJ 2020.12.14

Level 2 - 큰 수 만들기

출처: https://programmers.co.kr/learn/courses/30/lessons/42883 코딩테스트 연습 - 큰 수 만들기 programmers.co.kr Greedy 알고리즘으로 각 스텝마다 가장 최적의 해를 찾으면 된다. 1924 일경우에 k=2이므로 2자리 수 중 가장 큰 값을 찾으면 된다. 십의 자리 값은 1, 9, 2 중 가장 큰 수를 찾고, 일의 자리는 그대로 4를 두면 된다. #include #include #include using namespace std; string solution(string number, int k) { string answer = ""; int num_sz = number.length() - k; int idx = 0; for (int i = ..

Level 2 - 조이스틱

출처: https://programmers.co.kr/learn/courses/30/lessons/42860 코딩테스트 연습 - 조이스틱 조이스틱으로 알파벳 이름을 완성하세요. 맨 처음엔 A로만 이루어져 있습니다. ex) 완성해야 하는 이름이 세 글자면 AAA, 네 글자면 AAAA 조이스틱을 각 방향으로 움직이면 아래와 같습니다. ▲ - 다 programmers.co.kr 처음 문제 풀이 접근 방법은 알파벳의 up/down의 최소값을 구해놓고, 왼쪽, 오른쪽으로 움직이는 값을 더해서 문제를 해결하려고 했는데, C++로 index를 왼쪽으로(-) 처리하는 것이 쉽지 않아서 해결 못했던 문제. 다른 방식으로 문제를 해결하였음. 현재 cur_idx에서 가장 가까운 next_idx를 찾아서 answer에 그 이..

Medium 1237. Find Positive Integer Solution for a Given Equation

출처: https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation/ Find Positive Integer Solution for a Given Equation - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 주어진 function_id에 대해서만 잘 처리하면 되는 문제이다. 쉬운 문제 #include #include using namespace std; // This is..

Algorithm/LeetCode 2020.07.19

Medium 1111. Maximum Nesting Depth of Two Valid Parentheses Strings

출처: https://leetcode.com/problems/maximum-nesting-depth-of-two-valid-parentheses-strings/ Maximum Nesting Depth of Two Valid Parentheses Strings - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 처음에 문제가 이해가 잘 안되어서 힘들었던 문제이다. 문제에서 아래 나와있는 depth 개념이 중요하다. depth를 최소화 할 수 있는 그룹핑을 만들면 된..

Algorithm/LeetCode 2020.07.19

Medium 1476. Subrectangle Queries

문제 출처 : https://leetcode.com/problems/subrectangle-queries/ Subrectangle Queries - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 기본문제이며, 소스 코드를 보면 바로 이해할 수 있는 문제다. /** * Your SubrectangleQueries object will be instantiated and called as such: * SubrectangleQueries* obj = new Subr..

Algorithm/LeetCode 2020.06.21