-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIPO.java
More file actions
259 lines (178 loc) · 6.9 KB
/
IPO.java
File metadata and controls
259 lines (178 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package Algorithms.HeapAlgos;
import java.util.*;
import java.util.stream.IntStream;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 20 April 2026
* @link 502. IPO <a href="https://leetcode.com/problems/ipo/">LeetCode Link</a>
* @topics Principal, Array, Greedy, Sorting, Heap(PriorityQueue)
* @companies Amazon(3), Uber(3), Bloomberg(2), Microsoft(8), Google(5), Meta(4), Zeta Global(3), PhonePe(2), Innovaccer(2), Stackline(2), Gameskraft(2)
*/
public class IPO {
public static void main(String[] args) {
int[] capital = {1, 2, 3};
int[] profits = {6, 9, 8};
int k = 2, w = 1;
System.out.println("findMaximizedCapital TLE => " + findMaximizedCapitalTLE1(k, w, profits, capital));
System.out.println("findMaximizedCapital => " + findMaximizedCapital1(k, w, profits, capital));
System.out.println("findMaximizedCapital2 => " + findMaximizedCapital2(k, w, profits, capital));
}
/**
* @TimeComplexity O(n^2)
* @SpaceComplexity O(n)
*/
public static int findMaximizedCapitalTLE1(int k, int w, int[] profits, int[] capital) {
int n = profits.length;
k = Math.min(k, n);
Set<Integer> invested = new HashSet<>();
int[][] projects = new int[n][2];
for (int i = 0; i < n; i++) {
projects[i][0] = profits[i];
projects[i][1] = capital[i];
}
// Arrays.sort(projects, (a,b)-> a[0]==b[0]? b[1]-a[1] : b[0]-a[0]);
// Arrays.sort(projects, Comparator.comparingInt(a -> a[0]));
while (invested.size() < k) {
int bestIndex = -1, bestProfit = -1;
for (int i = 0; i < n; i++) {
if (invested.contains(i) || projects[i][1] > w) continue;
if (projects[i][0] > bestProfit) {
bestProfit = projects[i][0];
bestIndex = i;
}
}
if (bestIndex == -1) break;
w += projects[bestIndex][0];
invested.add(bestIndex);
}
return w;
}
/**
* @TimeComplexity O(nlogn + klogn) = O(nlogn)
* @SpaceComplexity O(n)
*/
public static int findMaximizedCapital1(int k, int w, int[] profits, int[] capital) {
int n = profits.length;
int[][] projects = new int[n][2];
for (int i = 0; i < n; i++) {
projects[i][0] = capital[i];
projects[i][1] = profits[i];
}
Arrays.sort(projects, Comparator.comparingInt(a -> a[0])); // sort based on the capital not the profit
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder()); // maintain profit
int i = 0;
while (k-- > 0) {
while (i < n && projects[i][0] <= w) {
maxHeap.offer(projects[i][1]);
i++;
}
if (maxHeap.isEmpty()) break;
w += maxHeap.poll();
}
return w;
}
/**
* @TimeComplexity O(nlogn + klogn) = O(nlogn)
* @SpaceComplexity O(n)
*/
public static int findMaximizedCapital2(int k, int w, int[] profits, int[] capital) {
int n = profits.length;
Project[] projects = new Project[n];
for (int i = 0; i < n; i++) {
projects[i] = new Project(capital[i], profits[i]);
}
Arrays.sort(projects);
PriorityQueue<Integer> q = new PriorityQueue<Integer>(n, Collections.reverseOrder());
int ptr = 0;
for (int i = 0; i < k; i++) {
while (ptr < n && projects[ptr].capital <= w) {
q.add(projects[ptr++].profit);
}
if (q.isEmpty()) {
break;
}
w += q.poll();
}
return w;
}
static class Project implements Comparable<Project> {
int capital, profit;
public Project(int capital, int profit) {
this.capital = capital;
this.profit = profit;
}
public int compareTo(Project project) {
return capital - project.capital;
}
}
/**
sort profits and capital
create int[]{0 to n} ---> sort these i's based on profits
now sort profits[] and capital[] based on these i's
loop capital and check if we can invest in it ---> track Set<Integer> invested - for capitalIs
till invested < k
return invested.stream().map(i -> profits[i]).reduce(0, Integer::sum);
*/
public int findMaximizedCapitalNotWorking1(int k, int w, int[] profits, int[] capital) {
int n = profits.length;
Set<Integer> invested = new HashSet<>();
int[] indices = IntStream.range(0, n).boxed().sorted(Comparator.comparingInt(i -> profits[i])).mapToInt(i->i).toArray();
k = Math.min(k, n);
while(invested.size() < k) {
for (int i : indices) {
if (invested.contains(i) || capital[i] < w || invested.size() == k) continue;
w += profits[i];
invested.add(i);
}
}
return w;
}
/**
We're getting issue if we invested multiple projects in single for loop - "w" calculation
*/
public int findMaximizedCapitalNotWorking2(int k, int w, int[] profits, int[] capital) {
int n = profits.length;
k = Math.min(k, n);
Set<Integer> invested = new HashSet<>();
int[][] projects = new int[n][2];
for (int i = 0; i < n; i++) {
projects[i][0] = profits[i];
projects[i][1] = capital[i];
}
Arrays.sort(projects, (a,b)-> a[0]==b[0]? b[1]-a[1] : b[0]-a[0]);
while(invested.size() < k) {
int prevInvested = invested.size();
for (int i = 0; i<n; i++) {
int[] project = projects[i];
if (invested.contains(i) || project[1] > w || invested.size() == k) continue;
w += project[0];
invested.add(i);
}
if (invested.size() == prevInvested) break;
}
return w;
}
public int findMaximizedCapitalNotWorking3(int k, int w, int[] profits, int[] capital) {
int n = profits.length;
k = Math.min(k, n);
Set<Integer> invested = new HashSet<>();
int[][] projects = new int[n][2];
for (int i = 0; i < n; i++) {
projects[i][0] = profits[i];
projects[i][1] = capital[i];
}
Arrays.sort(projects, (a,b)-> a[0]==b[0]? b[1]-a[1] : b[0]-a[0]);
while(invested.size() < k) {
int currW = w;
for (int i = 0; i<n; i++) {
int[] project = projects[i];
if (invested.contains(i) || project[1] > currW || invested.size() == k) continue;
currW -= project[0];
w += project[0];
invested.add(i);
}
if (currW == w) break;
}
return w;
}
}