Skip to content

Commit

Permalink
Merge pull request #81 from AlgoLeadMe/22-InSange
Browse files Browse the repository at this point in the history
22-InSange
  • Loading branch information
InSange authored Aug 9, 2024
2 parents 10b377e + d06f6f0 commit 9541186
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
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];
}
};
2 changes: 2 additions & 0 deletions InSange/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
| 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)]
---

https://leetcode.com/problems/robot-collisions/
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;
}
};

0 comments on commit 9541186

Please sign in to comment.