From 320870e2f3f921c77d4f6be9e45f117c30e7a840 Mon Sep 17 00:00:00 2001 From: freakin23 Date: Wed, 22 Jan 2025 13:26:36 +0530 Subject: [PATCH 01/12] update problems.json --- content/3_Silver/Greedy_Sorting.problems.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/3_Silver/Greedy_Sorting.problems.json b/content/3_Silver/Greedy_Sorting.problems.json index d9cf8358ca..119abaec5d 100644 --- a/content/3_Silver/Greedy_Sorting.problems.json +++ b/content/3_Silver/Greedy_Sorting.problems.json @@ -200,7 +200,7 @@ } }, { - "uniqueId": "lc-IPO", + "uniqueId": "leetcode-502", "name": "IPO", "url": "https://leetcode.com/problems/ipo", "source": "LC", @@ -208,7 +208,7 @@ "isStarred": false, "tags": ["Greedy", "Sorting", "Priority Queue"], "solutionMetadata": { - "kind": "none" + "kind": "internal" } }, { From c66b89bc4a25094769ef99eb9a1c3d79e2df90c6 Mon Sep 17 00:00:00 2001 From: freakin23 Date: Wed, 22 Jan 2025 13:28:20 +0530 Subject: [PATCH 02/12] add layout --- solutions/silver/leetcode-502.mdx | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 solutions/silver/leetcode-502.mdx diff --git a/solutions/silver/leetcode-502.mdx b/solutions/silver/leetcode-502.mdx new file mode 100644 index 0000000000..296a0b44dd --- /dev/null +++ b/solutions/silver/leetcode-502.mdx @@ -0,0 +1,22 @@ +--- +id: leetcode-502 +source: LC +title: IPO +author: Rameez Parwez +--- + +## Explanation + +## Implementation + +**Time Complexity:** $$ + + + + +```cpp + +``` + + + \ No newline at end of file From 133e4b3484b85f9534c32509d04bc26023676426 Mon Sep 17 00:00:00 2001 From: freakin23 Date: Wed, 22 Jan 2025 13:47:09 +0530 Subject: [PATCH 03/12] add explanation and tc --- solutions/silver/leetcode-502.mdx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/solutions/silver/leetcode-502.mdx b/solutions/silver/leetcode-502.mdx index 296a0b44dd..f2be01b1db 100644 --- a/solutions/silver/leetcode-502.mdx +++ b/solutions/silver/leetcode-502.mdx @@ -7,9 +7,11 @@ author: Rameez Parwez ## Explanation +We can solve this problem using a greedy approach by prioritizing projects that offer the highest profit while ensuring they can be started with the capital we currently have. + ## Implementation -**Time Complexity:** $$ +**Time Complexity:** $ \mathcal{O} (N \logN)$ From 1927a6151393118a6ed7257b1aa49c5b803e2c8d Mon Sep 17 00:00:00 2001 From: freakin23 Date: Wed, 22 Jan 2025 14:01:36 +0530 Subject: [PATCH 04/12] add c++/py implementation --- solutions/silver/leetcode-502.mdx | 67 +++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/solutions/silver/leetcode-502.mdx b/solutions/silver/leetcode-502.mdx index f2be01b1db..12a446236c 100644 --- a/solutions/silver/leetcode-502.mdx +++ b/solutions/silver/leetcode-502.mdx @@ -17,8 +17,75 @@ We can solve this problem using a greedy approach by prioritizing projects that ```cpp +class Solution { +public: + int findMaximizedCapital(int k, int w, vector& profits, vector& capital) { + int n = (int)profits.size(); + vector> projects(n); + for (int i = 0; i < n; i++) { + projects[i] = {capital[i], profits[i]}; + } + + sort(begin(projects), end(projects)); + priority_queue 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; + } else { + w += pq.top(); + pq.pop(); + k -= 1; + } + } + } + + while (k-- && !pq.empty()) { + w += pq.top(); + pq.pop(); + } + + return w; + } +}; ``` + + +```py +import heapq +class Solution: + def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int: + n = len(profits) + projects = [(capital[i], profits[i]) for i in range(n)] + projects.sort() + + 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 +``` + + \ No newline at end of file From e2a5e36bdd52453ecac1ae732258437641e1c269 Mon Sep 17 00:00:00 2001 From: freakin23 Date: Wed, 22 Jan 2025 14:02:02 +0530 Subject: [PATCH 05/12] formatting --- solutions/silver/leetcode-502.mdx | 1 + 1 file changed, 1 insertion(+) diff --git a/solutions/silver/leetcode-502.mdx b/solutions/silver/leetcode-502.mdx index 12a446236c..dfd4a0890b 100644 --- a/solutions/silver/leetcode-502.mdx +++ b/solutions/silver/leetcode-502.mdx @@ -60,6 +60,7 @@ public: ```py import heapq + class Solution: def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int: n = len(profits) From 73d8972fddd50040326b5ad2a43298da851e60ac Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 22 Jan 2025 08:34:09 +0000 Subject: [PATCH 06/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/silver/leetcode-502.mdx | 119 +++++++++++++++--------------- 1 file changed, 60 insertions(+), 59 deletions(-) diff --git a/solutions/silver/leetcode-502.mdx b/solutions/silver/leetcode-502.mdx index dfd4a0890b..a5e5ba1b54 100644 --- a/solutions/silver/leetcode-502.mdx +++ b/solutions/silver/leetcode-502.mdx @@ -18,40 +18,38 @@ We can solve this problem using a greedy approach by prioritizing projects that ```cpp class Solution { -public: - int findMaximizedCapital(int k, int w, vector& profits, vector& capital) { - int n = (int)profits.size(); - vector> projects(n); - for (int i = 0; i < n; i++) { - projects[i] = {capital[i], profits[i]}; - } - - sort(begin(projects), end(projects)); - priority_queue 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; - } else { - w += pq.top(); - pq.pop(); - k -= 1; - } - } - } - - while (k-- && !pq.empty()) { - w += pq.top(); - pq.pop(); - } - - return w; - } + public: + int findMaximizedCapital(int k, int w, vector &profits, vector &capital) { + int n = (int)profits.size(); + vector> projects(n); + for (int i = 0; i < n; i++) { projects[i] = {capital[i], profits[i]}; } + + sort(begin(projects), end(projects)); + priority_queue 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; + } else { + w += pq.top(); + pq.pop(); + k -= 1; + } + } + } + + while (k-- && !pq.empty()) { + w += pq.top(); + pq.pop(); + } + + return w; + } }; ``` @@ -61,32 +59,35 @@ public: ```py import heapq + class Solution: - def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int: - n = len(profits) - projects = [(capital[i], profits[i]) for i in range(n)] - projects.sort() - - 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 + def findMaximizedCapital( + self, k: int, w: int, profits: List[int], capital: List[int] + ) -> int: + n = len(profits) + projects = [(capital[i], profits[i]) for i in range(n)] + projects.sort() + + 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 ``` - \ No newline at end of file + From ec6dce26dda321e7040c1237302ef802337e9136 Mon Sep 17 00:00:00 2001 From: Rameez Parwez <79394137+Sosuke23@users.noreply.github.com> Date: Wed, 22 Jan 2025 14:04:53 +0530 Subject: [PATCH 07/12] Update leetcode-502.mdx --- solutions/silver/leetcode-502.mdx | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/solutions/silver/leetcode-502.mdx b/solutions/silver/leetcode-502.mdx index a5e5ba1b54..aeffa97ce9 100644 --- a/solutions/silver/leetcode-502.mdx +++ b/solutions/silver/leetcode-502.mdx @@ -35,11 +35,10 @@ class Solution { } else { if (pq.empty()) { return w; - } else { - w += pq.top(); - pq.pop(); - k -= 1; } + w += pq.top(); + pq.pop(); + k -= 1; } } From e3665c324e131c32bc23834e9b88c51dc5973d39 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 22 Jan 2025 08:36:45 +0000 Subject: [PATCH 08/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/silver/leetcode-502.mdx | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/solutions/silver/leetcode-502.mdx b/solutions/silver/leetcode-502.mdx index aeffa97ce9..e2d861a4bf 100644 --- a/solutions/silver/leetcode-502.mdx +++ b/solutions/silver/leetcode-502.mdx @@ -33,12 +33,10 @@ class Solution { pq.push(projects[j].second); j++; } else { - if (pq.empty()) { - return w; - } + if (pq.empty()) { return w; } w += pq.top(); pq.pop(); - k -= 1; + k -= 1; } } From 0327e7357bb6b11d72dcf6bb5c49ce3a59a0bf66 Mon Sep 17 00:00:00 2001 From: Rameez Parwez <79394137+Sosuke23@users.noreply.github.com> Date: Wed, 22 Jan 2025 23:14:13 +0530 Subject: [PATCH 09/12] Update leetcode-502.mdx --- solutions/silver/leetcode-502.mdx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/solutions/silver/leetcode-502.mdx b/solutions/silver/leetcode-502.mdx index e2d861a4bf..354ec6b6a8 100644 --- a/solutions/silver/leetcode-502.mdx +++ b/solutions/silver/leetcode-502.mdx @@ -40,9 +40,10 @@ class Solution { } } - while (k-- && !pq.empty()) { + while (k > 0 && !pq.empty()) { w += pq.top(); pq.pop(); + k -= 1; } return w; From e48baa1f31857a1dca2e607145e2b5676bafe4d8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 22 Jan 2025 17:46:52 +0000 Subject: [PATCH 10/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/silver/leetcode-502.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/silver/leetcode-502.mdx b/solutions/silver/leetcode-502.mdx index 354ec6b6a8..0a70c0257f 100644 --- a/solutions/silver/leetcode-502.mdx +++ b/solutions/silver/leetcode-502.mdx @@ -43,7 +43,7 @@ class Solution { while (k > 0 && !pq.empty()) { w += pq.top(); pq.pop(); - k -= 1; + k -= 1; } return w; From b949d52e458c985e8571328e357ee63ddd6a6239 Mon Sep 17 00:00:00 2001 From: Rameez Parwez <79394137+Sosuke23@users.noreply.github.com> Date: Thu, 23 Jan 2025 10:19:44 +0530 Subject: [PATCH 11/12] Update leetcode-502.mdx --- solutions/silver/leetcode-502.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/solutions/silver/leetcode-502.mdx b/solutions/silver/leetcode-502.mdx index 0a70c0257f..d3a95ae6ce 100644 --- a/solutions/silver/leetcode-502.mdx +++ b/solutions/silver/leetcode-502.mdx @@ -7,7 +7,7 @@ author: Rameez Parwez ## Explanation -We can solve this problem using a greedy approach by prioritizing projects that offer the highest profit while ensuring they can be started with the capital we currently have. +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 @@ -36,14 +36,14 @@ class Solution { if (pq.empty()) { return w; } w += pq.top(); pq.pop(); - k -= 1; + k--; } } while (k > 0 && !pq.empty()) { w += pq.top(); pq.pop(); - k -= 1; + k--; } return w; From c0a995e472d2b51aa24d66c75fae86653909b406 Mon Sep 17 00:00:00 2001 From: Kevin Sheng <55369003+SansPapyrus683@users.noreply.github.com> Date: Wed, 22 Jan 2025 21:44:38 -0800 Subject: [PATCH 12/12] Update leetcode-502.mdx --- solutions/silver/leetcode-502.mdx | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/solutions/silver/leetcode-502.mdx b/solutions/silver/leetcode-502.mdx index d3a95ae6ce..1f04cbfd0b 100644 --- a/solutions/silver/leetcode-502.mdx +++ b/solutions/silver/leetcode-502.mdx @@ -7,11 +7,14 @@ 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. +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 \logN)$ +**Time Complexity:** $ \mathcal{O} (N \log N)$ @@ -20,13 +23,13 @@ We can solve this problem using a greedy approach. The idea is to prioritize pro class Solution { public: int findMaximizedCapital(int k, int w, vector &profits, vector &capital) { - int n = (int)profits.size(); + const int n = (int)profits.size(); + vector> projects(n); for (int i = 0; i < n; i++) { projects[i] = {capital[i], profits[i]}; } - sort(begin(projects), end(projects)); - priority_queue pq; + priority_queue pq; int j = 0; while (j < n && k > 0) { if (projects[j].first <= w) { @@ -39,7 +42,6 @@ class Solution { k--; } } - while (k > 0 && !pq.empty()) { w += pq.top(); pq.pop(); @@ -63,12 +65,10 @@ class Solution: self, k: int, w: int, profits: List[int], capital: List[int] ) -> int: n = len(profits) - projects = [(capital[i], profits[i]) for i in range(n)] - projects.sort() + 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])