From 72f0e8aff2d6555a6ddc418c1d653ad0de528195 Mon Sep 17 00:00:00 2001 From: InSange Date: Wed, 17 Jul 2024 20:44:36 +0900 Subject: [PATCH 1/5] 20-InSange --- InSange/README.md | 5 +- .../2751_Robot Collisions.cpp" | 70 +++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 "InSange/\354\212\244\355\203\235/2751_Robot Collisions.cpp" diff --git a/InSange/README.md b/InSange/README.md index 80a1712..c6c803d 100644 --- a/InSange/README.md +++ b/InSange/README.md @@ -20,5 +20,8 @@ | 16차시 | 2024.06.28 | 그래프 | [Maximum_Total_Importance_ofRoads](https://leetcode.com/problems/maximum-total-importance-of-roads/) | [#16](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/63)] | 17차시 | 2024.06.28 | 문자열 | [Zigzag Conversion](https://leetcode.com/problems/zigzag-conversion/) | [#17](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/64)] | 18차시 | 2024.07.10 | 문자열 | [Crawler Log Folder](https://leetcode.com/problems/crawler-log-folder/) | [#18](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/64)] -| 19차시 | 2024.07.13 | 스택 | [Crawler Log Folder](https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/) | [#19](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/64)] +| 19차시 | 2024.07.13 | 스택 | [Crawler Log Folder](https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/) | [#19](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/72)] +| 20차시 | 2024.07.17 | 스택 | [Robot Collisions](https://leetcode.com/problems/robot-collisions/) | [#20](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/74)] --- + +https://leetcode.com/problems/robot-collisions/ diff --git "a/InSange/\354\212\244\355\203\235/2751_Robot Collisions.cpp" "b/InSange/\354\212\244\355\203\235/2751_Robot Collisions.cpp" new file mode 100644 index 0000000..5aeb56a --- /dev/null +++ "b/InSange/\354\212\244\355\203\235/2751_Robot Collisions.cpp" @@ -0,0 +1,70 @@ +#include +#include +#include +#include + +using namespace std; + +class Solution { +public: + vector survivedRobotsHealths(vector& positions, vector& healths, string directions) { + vector ans; + + priority_queue, vector>, greater>> IndexArr; + stack> Rst; + + for (int i = 0; i < positions.size(); i++) + { + IndexArr.push({ positions[i], i }); + } + + while (!IndexArr.empty()) + { + int val = IndexArr.top().first; + int index = IndexArr.top().second; + + cout << val << ", " << index << "\n"; + IndexArr.pop(); + + if (directions[index] == 'L') + { + while (!Rst.empty()) + { + int stVal = Rst.top().first; + int stIndex = Rst.top().second; + + if (healths[stIndex] == healths[index]) + { + healths[stIndex] = 0; + healths[index] = 0; + Rst.pop(); + break; + } + else if (healths[stIndex] > healths[index]) + { + healths[index] = 0; + healths[stIndex]--; + break; + } + else + { + healths[stIndex] = 0; + healths[index]--; + Rst.pop(); + } + } + } + else + { + Rst.push({ val, index }); + } + } + + for (int health : healths) + { + if (health) ans.push_back(health); + } + + return ans; + } +}; \ No newline at end of file From 8c54d0442874a7074f196eacf466ce72194f868d Mon Sep 17 00:00:00 2001 From: InSange Date: Sun, 21 Jul 2024 13:07:14 +0900 Subject: [PATCH 2/5] 2024-07-21 Find Valid Matrix Given Row and Column Sums --- InSange/README.md | 1 + ...alid Matrix Given Row and Column Sums.cpp" | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 "InSange/\352\267\270\353\246\254\353\224\224/1605_Find Valid Matrix Given Row and Column Sums.cpp" diff --git a/InSange/README.md b/InSange/README.md index c6c803d..0aa97b7 100644 --- a/InSange/README.md +++ b/InSange/README.md @@ -22,6 +22,7 @@ | 18차시 | 2024.07.10 | 문자열 | [Crawler Log Folder](https://leetcode.com/problems/crawler-log-folder/) | [#18](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/64)] | 19차시 | 2024.07.13 | 스택 | [Crawler Log Folder](https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/) | [#19](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/72)] | 20차시 | 2024.07.17 | 스택 | [Robot Collisions](https://leetcode.com/problems/robot-collisions/) | [#20](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/74)] +| 21차시 | 2024.07.21 | 그리디 | [Find Valid Matrix Given Row and Column Sums](https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/) | [#21](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/75)] --- https://leetcode.com/problems/robot-collisions/ diff --git "a/InSange/\352\267\270\353\246\254\353\224\224/1605_Find Valid Matrix Given Row and Column Sums.cpp" "b/InSange/\352\267\270\353\246\254\353\224\224/1605_Find Valid Matrix Given Row and Column Sums.cpp" new file mode 100644 index 0000000..feb76fc --- /dev/null +++ "b/InSange/\352\267\270\353\246\254\353\224\224/1605_Find Valid Matrix Given Row and Column Sums.cpp" @@ -0,0 +1,49 @@ +#include + +using namespace std; + +class Solution { +public: + vector> restoreMatrix(vector& rowSum, vector& colSum) { + vector> ans; + int rsize = rowSum.size(); + int csize = colSum.size(); + int rindex = 0; + int cindex = 0; + + ans.assign(rsize, vector(csize, 0)); + + while (rindex < rsize || cindex < csize) + { + if (rindex >= rsize) + { + ans[rindex - 1][cindex] = colSum[cindex]; + cindex++; + continue; + } + else if (cindex >= csize) + { + ans[rindex][cindex - 1] = rowSum[rindex]; + rindex++; + continue; + } + + int val = min(colSum[cindex], rowSum[rindex]); + + ans[rindex][cindex] = val; + rowSum[rindex] -= val; + colSum[cindex] -= val; + + if (!rowSum[rindex]) + { + rindex++; + } + if (!colSum[cindex]) + { + cindex++; + } + } + + return ans; + } +}; \ No newline at end of file From e63fc7a193653b2117f58c2635029fef73fe89b1 Mon Sep 17 00:00:00 2001 From: InSange Date: Thu, 1 Aug 2024 02:35:30 +0900 Subject: [PATCH 3/5] 2024-7-31 Filling Bookcase Shelves --- InSange/DP/1105_FillingBookcaseShelves.cpp | 28 ++++++++++++++++++++++ InSange/README.md | 1 + 2 files changed, 29 insertions(+) create mode 100644 InSange/DP/1105_FillingBookcaseShelves.cpp diff --git a/InSange/DP/1105_FillingBookcaseShelves.cpp b/InSange/DP/1105_FillingBookcaseShelves.cpp new file mode 100644 index 0000000..7453ea7 --- /dev/null +++ b/InSange/DP/1105_FillingBookcaseShelves.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +class Solution { +public: + int minHeightShelves(vector>& books, int shelfWidth) { + int arrSize = books.size(); + vector heightArr(arrSize + 1, 0); + + for (int i = 1; i <= arrSize; i++) + { + int width = books[i - 1][0]; + int height = books[i - 1][1]; + + heightArr[i] = heightArr[i - 1] + height; // check Next Floor + for (int j = i - 1; j > 0; j--) + { + if (width + books[j - 1][0] > shelfWidth) break; + width += books[j - 1][0]; // check the same Floor + height = max(height, books[j - 1][1]); // same Floor height compare with before book height + heightArr[i] = min(heightArr[i], heightArr[j - 1] + height); // where make min + } + } + + return heightArr[arrSize]; + } +}; \ No newline at end of file diff --git a/InSange/README.md b/InSange/README.md index 0aa97b7..4d05640 100644 --- a/InSange/README.md +++ b/InSange/README.md @@ -23,6 +23,7 @@ | 19차시 | 2024.07.13 | 스택 | [Crawler Log Folder](https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/) | [#19](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/72)] | 20차시 | 2024.07.17 | 스택 | [Robot Collisions](https://leetcode.com/problems/robot-collisions/) | [#20](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/74)] | 21차시 | 2024.07.21 | 그리디 | [Find Valid Matrix Given Row and Column Sums](https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/) | [#21](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/75)] +| 22차시 | 2024.07.31 | DP | [Filling Bookcase Shelves](https://leetcode.com/problems/filling-bookcase-shelves/) | [#22](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/81)] --- https://leetcode.com/problems/robot-collisions/ From d06f6f06e1f1044e15fe0c8dcec2a19f82fc6df0 Mon Sep 17 00:00:00 2001 From: InSange Date: Sat, 3 Aug 2024 21:24:15 +0900 Subject: [PATCH 4/5] 2024-08-03 Minimum Swaps to Group All 1's Together 2 --- InSange/README.md | 1 + ...mum Swaps to Group All 1's Together 2.cpp" | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 "InSange/\354\212\254\353\235\274\354\235\264\353\224\251 \354\234\210\353\217\204\354\232\260/2134_Minimum Swaps to Group All 1's Together 2.cpp" diff --git a/InSange/README.md b/InSange/README.md index 4d05640..ec243be 100644 --- a/InSange/README.md +++ b/InSange/README.md @@ -24,6 +24,7 @@ | 20차시 | 2024.07.17 | 스택 | [Robot Collisions](https://leetcode.com/problems/robot-collisions/) | [#20](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/74)] | 21차시 | 2024.07.21 | 그리디 | [Find Valid Matrix Given Row and Column Sums](https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/) | [#21](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/75)] | 22차시 | 2024.07.31 | DP | [Filling Bookcase Shelves](https://leetcode.com/problems/filling-bookcase-shelves/) | [#22](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/81)] +| 23차시 | 2024.08.03 | 슬라이딩 윈도우 | [Minimum Swaps to Group All 1's Together 2](https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together-ii/) | [#23](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/82)] --- https://leetcode.com/problems/robot-collisions/ diff --git "a/InSange/\354\212\254\353\235\274\354\235\264\353\224\251 \354\234\210\353\217\204\354\232\260/2134_Minimum Swaps to Group All 1's Together 2.cpp" "b/InSange/\354\212\254\353\235\274\354\235\264\353\224\251 \354\234\210\353\217\204\354\232\260/2134_Minimum Swaps to Group All 1's Together 2.cpp" new file mode 100644 index 0000000..22f541d --- /dev/null +++ "b/InSange/\354\212\254\353\235\274\354\235\264\353\224\251 \354\234\210\353\217\204\354\232\260/2134_Minimum Swaps to Group All 1's Together 2.cpp" @@ -0,0 +1,42 @@ +#include + +using namespace std; + +class Solution { +public: + int minSwaps(vector& nums) { + int zeroCnt = minSwapsHelper(nums, 0); + int oneCnt = minSwapsHelper(nums, 1); + + return min(zeroCnt, oneCnt); + } + + int minSwapsHelper(vector& data, int val) { + int numSize = data.size(); + int totalCnt = 0; + + for (int num : data) + { + if (num == val) totalCnt++; + } + + if (totalCnt == 0 || totalCnt == numSize) return 0; + + int start = 0, end = 0; + int maxValCnt = 0, curValCnt = 0; + + while (end < totalCnt) { + if (data[end++] == val) curValCnt++; + } + maxValCnt = max(maxValCnt, curValCnt); + + while (end < numSize) + { + if (data[start++] == val) curValCnt--; + if (data[end++] == val) curValCnt++; + maxValCnt = max(maxValCnt, curValCnt); + } + + return totalCnt - maxValCnt; + } +}; \ No newline at end of file From 843a8613bba5c62310b0e44c61ea453d34f070df Mon Sep 17 00:00:00 2001 From: InSange Date: Mon, 5 Aug 2024 00:57:09 +0900 Subject: [PATCH 5/5] =?UTF-8?q?2024-08-04=20=ED=8A=B8=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- InSange/BFS/1068.cpp | 106 +++++++++++++++++++++++++++++++++++++++++++ InSange/README.md | 1 + 2 files changed, 107 insertions(+) create mode 100644 InSange/BFS/1068.cpp diff --git a/InSange/BFS/1068.cpp b/InSange/BFS/1068.cpp new file mode 100644 index 0000000..c1cefeb --- /dev/null +++ b/InSange/BFS/1068.cpp @@ -0,0 +1,106 @@ +#include +#include +#include + +using namespace std; + +struct Node +{ + Node() + { + parent = nullptr; + leafNode = true; + } + + vector childs; + Node* parent; + bool leafNode; +}; + +vector nodes; +int N, ans = 0, rIndex; + +void NodeUpdate(Node* curNode) +{ + for (auto child : curNode->childs) + { + if (nodes[child] != nullptr) + { + curNode->leafNode = false; + return; + } + } + curNode->leafNode = true; +} + +void Solve() +{ + cin >> N; + + for (int i = 0; i < N; i++) + { + Node* node = new Node(); + + nodes.push_back(node); + } + int root_index = 0; + + for(int i = 0; i < N; i++) + { + int p; + cin >> p; + + if (p == -1) + { + root_index = i; + continue; + } + nodes[i]->parent = nodes[p]; + nodes[i]->parent->childs.push_back(i); + if (nodes[i]->parent) + { + NodeUpdate(nodes[i]->parent); + } + } + + cin >> rIndex; + if (rIndex == root_index) // Ȱ Ʈ + { + cout << ans << "\n"; + return; + } + Node* parentNode = nodes[rIndex]->parent; + delete nodes[rIndex]; + nodes[rIndex] = nullptr; + NodeUpdate(parentNode); + + queue q; + q.push(nodes[root_index]); + + while (!q.empty()) + { + Node* cur = q.front(); + q.pop(); + + if (cur->leafNode) ans++; + for (auto index : cur->childs) + { + if (nodes[index] != nullptr) + { + q.push(nodes[index]); + } + } + } + + cout << ans; +} + +int main() +{ + cin.tie(nullptr); + ios::sync_with_stdio(false); + + Solve(); + + return 0; +} \ No newline at end of file diff --git a/InSange/README.md b/InSange/README.md index ec243be..537b1a9 100644 --- a/InSange/README.md +++ b/InSange/README.md @@ -25,6 +25,7 @@ | 21차시 | 2024.07.21 | 그리디 | [Find Valid Matrix Given Row and Column Sums](https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/) | [#21](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/75)] | 22차시 | 2024.07.31 | DP | [Filling Bookcase Shelves](https://leetcode.com/problems/filling-bookcase-shelves/) | [#22](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/81)] | 23차시 | 2024.08.03 | 슬라이딩 윈도우 | [Minimum Swaps to Group All 1's Together 2](https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together-ii/) | [#23](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/82)] +| 24차시 | 2024.08.04 | BFS | [트리](https://www.acmicpc.net/problem/1068) | [#24](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/83)] --- https://leetcode.com/problems/robot-collisions/