-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from AlgoLeadMe/22-InSange
22-InSange
- Loading branch information
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
InSange/μ¬λΌμ΄λ© μλμ°/2134_Minimum Swaps to Group All 1's Together 2.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |