목록Study/Algorithm (400)
hwooo
https://leetcode.com/problems/count-square-submatrices-with-all-ones/description/풀이주어진 배열을 DP 배열로 사용, 해당 위치를 오른쪽 아래로 하는 사각형의 개수를 크기와 상관 없이 구한다.현재 위치가 사각형이 될 수 있을 때, 왼쪽 대각선/왼쪽/위를 확인 후 가장 작은 값을 구한 후 + 1주어진 배열[i][j] = 현재 위치가 포함된 크기 1 이상의 사각형의 개수 + 크기가 1이고 현재 위치인 사각형의 개수이므로위의 값을 계속 더해 만들 수 있는 총 사각형의 개수를 구한다코드class Solution {public: int countSquares(vector>& matrix) { int row = matrix.size(..
https://school.programmers.co.kr/learn/courses/30/lessons/258712 프로그래머스코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.programmers.co.kr 풀이선물을 주고 받은 기록을 2차원 배열으로 만들어 두고, 선물 지수도 같이 계산했다.서로의 기록과 선물 지수를 비교하며 가장 많이 받은 사람의 수를 계산했다.코드#include #include #include using namespace std;int solution(vector friends, vector gifts) { int maxPresentCnt = 0; int log[50]..
https://school.programmers.co.kr/learn/courses/30/lessons/17677 프로그래머스코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.programmers.co.kr 풀이26 * 26 크기의 2차원 배열을 선언 후 2자리 부분문자열의 개수를 센다.str1, str2 의 부분문자열의 개수가 저장된 2차원 배열을 순회하며 교집합과 합집합의 개수를 구한다.코드#include #include #include using namespace std;int solution(string str1, string str2) { double unionCnt = 0, inter =..
https://leetcode.com/problems/matrix-block-sum/풀이문제의 요구사항을 그대로 구현했더니 시간이 오래 걸려서 누적합 방식으로 다시 풀었다.코드 1 - 요구사항 그대로class Solution {public: vector> matrixBlockSum(vector>& mat, int k) { int m = mat.size(), n = mat[0].size(); vector> ans; for (int i = 0; i col; for (int j = 0; j >& mat) { int sum = 0; for (int r = r1; r 코드 2 - 누적합class Solution {publi..
https://leetcode.com/problems/perfect-squares/description/풀이dp를 이용, 제곱수일 때의 개수를 1로 초기화한다.현재 숫자에 도달하기 위해서는 이전의 제곱수 값들을 더해야 하므로, 현재 숫자보다 작은 제곱수에 저장된 값들로 최소 수의 합을 구한다코드class Solution {public: int numSquares(int n) { int dp[10001]; fill(dp, dp + 10001, 10001); // 제곱수는 1로 초기화 int num = 1; while (num * num
https://leetcode.com/problems/partition-list/description/풀이x를 기준으로 작은 값을 저장할 리스트(small)와 큰 값을 저장할 리스트(big)을 생성주어진 리스트의 노드를 하나씩 탐색하며 x보다 작은 값이라면 small에, 큰 값이라면 big에 넣어 따로 저장 후 이를 합쳐 반환코드class Solution {public: ListNode* partition(ListNode* head, int x) { ListNode* small = new ListNode(); ListNode* big = new ListNode(); ListNode* result = small; ListNode* bigResult =..
https://leetcode.com/problems/maximum-units-on-a-truck/description/풀이numberOfUnitsPerBox 기준 내림차순 정렬 후 numberOfUnitsPerBox가 큰 것부터 차례로 넣어 maximumUnits 도출코드class Solution {public: int maximumUnits(vector>& boxTypes, int truckSize) { // numberOfUnitsPerBox 기준 내림차순 정렬 sort(boxTypes.begin(), boxTypes.end(), [](vector &a, vector &b){ return a[1] > b[1]; }); // ..
https://leetcode.com/problems/reorder-data-in-log-files/description/ 풀이주어진 문자열을 공백 기준으로 잘라서 identifier와 주어진 문자열을 분리하여 저장한 후, Letter-logs만 주어진 기준으로 정렬했다. (문자열 기준 사전 순 정렬 후 동일하다면 식별자 기준으로 사전 순 정렬)정렬된 순서로 Letter-logs 뒤에 Digit-logs를 붙여 반환하였다.코드class Solution {typedef pair ps;public: vector reorderLogFiles(vector& logs) { vector> let, dig; for (int i = 0; i &a, pair &b) { ..