Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

24-InSange #83

Merged
merged 5 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions InSange/BFS/1068.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

struct Node
{
Node()
{
parent = nullptr;
leafNode = true;
}

vector<int> childs;
Node* parent;
bool leafNode;
};

vector<Node*> 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<Node*> 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;
}
28 changes: 28 additions & 0 deletions InSange/DP/1105_FillingBookcaseShelves.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <vector>

using namespace std;

class Solution {
public:
int minHeightShelves(vector<vector<int>>& books, int shelfWidth) {
int arrSize = books.size();
vector<int> 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];
}
};
9 changes: 8 additions & 1 deletion InSange/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,12 @@
| 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)]
| 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/
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <vector>

using namespace std;

class Solution {
public:
vector<vector<int>> restoreMatrix(vector<int>& rowSum, vector<int>& colSum) {
vector<vector<int>> ans;
int rsize = rowSum.size();
int csize = colSum.size();
int rindex = 0;
int cindex = 0;

ans.assign(rsize, vector<int>(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;
}
};
70 changes: 70 additions & 0 deletions InSange/์Šคํƒ/2751_Robot Collisions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <iostream>
#include <vector>
#include <queue>
#include <stack>

using namespace std;

class Solution {
public:
vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {
vector<int> ans;

priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> IndexArr;
stack<pair<int, int>> 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;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <vector>

using namespace std;

class Solution {
public:
int minSwaps(vector<int>& nums) {
int zeroCnt = minSwapsHelper(nums, 0);
int oneCnt = minSwapsHelper(nums, 1);

return min(zeroCnt, oneCnt);
}

int minSwapsHelper(vector<int>& 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;
}
};