Skip to content

Commit

Permalink
Merge branch 'cpinitiative:master' into fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Sosuke23 authored Jan 24, 2025
2 parents 70a1f6c + 82a2b59 commit c2de1ba
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 6 deletions.
4 changes: 2 additions & 2 deletions content/3_Silver/Greedy_Sorting.problems.json
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,15 @@
}
},
{
"uniqueId": "lc-IPO",
"uniqueId": "leetcode-502",
"name": "IPO",
"url": "https://leetcode.com/problems/ipo",
"source": "LC",
"difficulty": "Normal",
"isStarred": false,
"tags": ["Greedy", "Sorting", "Priority Queue"],
"solutionMetadata": {
"kind": "none"
"kind": "internal"
}
},
{
Expand Down
9 changes: 5 additions & 4 deletions solutions/gold/cf-1006F.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ int main() {
}

// loop over all combinations of moves from (0, 0)

/*
* left[i][j][k] = number of ways we can have
* an xorsum of k ending at (row, col)
Expand All @@ -49,7 +48,8 @@ int main() {

int leftmoves = moves / 2;
for (int i = 0; i < (1 << leftmoves); i++) {
ll r = 0 ll c = 0;
int r = 0;
int c = 0;
ll xorsum = grid[0][0];
bool in_grid = true;
for (int j = 0; j < leftmoves; j++) {
Expand Down Expand Up @@ -78,7 +78,8 @@ int main() {
// here we do the same thing, but we are going up from (n - 1, m - 1)
int rightmoves = moves - leftmoves - 1; // note the minus 1
for (int i = 0; i < (1 << rightmoves); i++) {
ll r = 0 ll c = 0;
int r = n - 1;
int c = m - 1;
ll xorsum = grid[n - 1][m - 1];
bool in_grid = true;
for (int j = 0; j < rightmoves; j++) {
Expand All @@ -89,7 +90,7 @@ int main() {
if (i & (1 << j)) {
r--;
} else {
c++;
c--;
}

if (r < 0 || c < 0) {
Expand Down
91 changes: 91 additions & 0 deletions solutions/silver/leetcode-502.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
---
id: leetcode-502
source: LC
title: IPO
author: Rameez Parwez
---

## Explanation

We can solve this problem using a greedy approach.

The idea is to prioritize projects that offer the highest profit.
At the same time, we ensure that the projects we choose can be started with the capital we currently have.

## Implementation

**Time Complexity:** $ \mathcal{O} (N \log N)$

<LanguageSection>
<CPPSection>

```cpp
class Solution {
public:
int findMaximizedCapital(int k, int w, vector<int> &profits, vector<int> &capital) {
const int n = (int)profits.size();

vector<pair<int, int>> projects(n);
for (int i = 0; i < n; i++) { projects[i] = {capital[i], profits[i]}; }
sort(begin(projects), end(projects));

priority_queue<int> pq;
int j = 0;
while (j < n && k > 0) {
if (projects[j].first <= w) {
pq.push(projects[j].second);
j++;
} else {
if (pq.empty()) { return w; }
w += pq.top();
pq.pop();
k--;
}
}
while (k > 0 && !pq.empty()) {
w += pq.top();
pq.pop();
k--;
}

return w;
}
};
```

</CPPSection>
<PySection>

```py
import heapq


class Solution:
def findMaximizedCapital(
self, k: int, w: int, profits: List[int], capital: List[int]
) -> int:
n = len(profits)
projects = sorted((capital[i], profits[i]) for i in range(n))

j = 0
pq = []
while j < n and k > 0:
if projects[j][0] <= w:
heapq.heappush(pq, -projects[j][1])
j += 1
else:
if not pq:
return w

w += -heapq.heappop(pq)
k -= 1

while k > 0 and pq:
w += -heapq.heappop(pq)
k -= 1

return w
```
</PySection>
</LanguageSection>

0 comments on commit c2de1ba

Please sign in to comment.