Skip to content

Commit 8d4fb98

Browse files
committed
feat: add solutions to lc problem: No.0502
No.0502.IPO
1 parent bf92031 commit 8d4fb98

File tree

9 files changed

+312
-44
lines changed

9 files changed

+312
-44
lines changed

solution/0400-0499/0496.Next Greater Element I/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ for i in range(n):
6767
stk.append(i)
6868
```
6969

70-
对于本题,先对将 $nums2$ 中的每一个元素,求出其下一个更大的元素。随后对于将这些答案放入哈希表 $m$ 中,再遍历数组 $nums1$,并直接找出答案。对于 $nums2$,可以使用单调栈来解决这个问题。
70+
对于本题,先对将 `nums2` 中的每一个元素,求出其下一个更大的元素。随后对于将这些答案放入哈希表 $m$ 中,再遍历数组 `nums1`,并直接找出答案。对于 `nums2`,可以使用单调栈来解决这个问题。
7171

72-
时间复杂度 $O(M+N)$,其中 $M$ 表示 $nums1$ 的长度,$N$ 表示 $nums2$ 的长度。
72+
时间复杂度 $O(M+N)$,其中 $M$ 和 $N$ 分别为数组 `nums1``nums2` 的长度。
7373

7474
<!-- tabs:start -->
7575

solution/0400-0499/0498.Diagonal Traverse/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@
4242

4343
**方法一:定点遍历**
4444

45-
对于每一轮 $k$,我们固定从右上方开始往左下方遍历,得到 $t$。如果 $k$ 为偶数,再将 $t$ 逆序。然后将 $t$ 添加到结果数组 $ans$ 中。
45+
对于每一轮 $k$,我们固定从右上方开始往左下方遍历,得到 $t$。如果 $k$ 为偶数,再将 $t$ 逆序。然后将 $t$ 添加到结果数组 `ans` 中。
4646

4747
问题的关键在于确定轮数以及每一轮的起始坐标点 $(i,j)$。
4848

49-
时间复杂度 $O(m*n)$其中 $m$ 表示 $mat$ 的行数,$n$ 表示 $mat$ 的列数
49+
时间复杂度 $O(m\times n)$其中 $m$ 和 $n$ 分别为矩阵的行数和列数
5050

5151
<!-- tabs:start -->
5252

solution/0400-0499/0499.The Maze III/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070

7171
<!-- 这里可写通用的实现逻辑 -->
7272

73-
BFS
73+
**方法一:BFS**
7474

7575
<!-- tabs:start -->
7676

solution/0500-0599/0502.IPO/README.md

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,132 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
**方法一:贪心 + 优先队列(双堆)**
60+
61+
将每个项目放入优先队列 $q_1$ 中,按照启动资本从小到大排序。如果堆顶元素启动资本不超过当前已有的资金,则循环弹出,放入另一个优先队列 $q_2$ 中,按照纯利润从大到小排序。取出当前利润最大的项目,将其纯利润加入到当前资金中,重复上述操作 $k$ 次。
62+
63+
时间复杂度 $O(n\log n)$,空间复杂度 $O(n)$。其中 $n$ 为项目数。
64+
5965
<!-- tabs:start -->
6066

6167
### **Python3**
6268

6369
<!-- 这里可写当前语言的特殊实现逻辑 -->
6470

6571
```python
66-
72+
class Solution:
73+
def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int:
74+
h1 = [(c, p) for c, p in zip(capital, profits)]
75+
heapify(h1)
76+
h2 = []
77+
while k:
78+
while h1 and h1[0][0] <= w:
79+
heappush(h2, -heappop(h1)[1])
80+
if not h2:
81+
break
82+
w -= heappop(h2)
83+
k -= 1
84+
return w
6785
```
6886

6987
### **Java**
7088

7189
<!-- 这里可写当前语言的特殊实现逻辑 -->
7290

7391
```java
92+
class Solution {
93+
public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
94+
int n = capital.length;
95+
PriorityQueue<int[]> q1 = new PriorityQueue<>((a, b) -> a[0] - b[0]);
96+
for (int i = 0; i < n; ++i) {
97+
q1.offer(new int[] {capital[i], profits[i]});
98+
}
99+
PriorityQueue<Integer> q2 = new PriorityQueue<>((a, b) -> b - a);
100+
while (k-- > 0) {
101+
while (!q1.isEmpty() && q1.peek()[0] <= w) {
102+
q2.offer(q1.poll()[1]);
103+
}
104+
if (q2.isEmpty()) {
105+
break;
106+
}
107+
w += q2.poll();
108+
}
109+
return w;
110+
}
111+
}
112+
```
113+
114+
### **C++**
115+
116+
```cpp
117+
using pii = pair<int, int>;
118+
119+
class Solution {
120+
public:
121+
int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {
122+
priority_queue<pii, vector<pii>, greater<pii>> q1;
123+
int n = profits.size();
124+
for (int i = 0; i < n; ++i) {
125+
q1.push({capital[i], profits[i]});
126+
}
127+
priority_queue<int> q2;
128+
while (k--) {
129+
while (!q1.empty() && q1.top().first <= w) {
130+
q2.push(q1.top().second);
131+
q1.pop();
132+
}
133+
if (q2.empty()) {
134+
break;
135+
}
136+
w += q2.top();
137+
q2.pop();
138+
}
139+
return w;
140+
}
141+
};
142+
```
74143
144+
### **Go**
145+
146+
```go
147+
func findMaximizedCapital(k int, w int, profits []int, capital []int) int {
148+
q1 := hp2{}
149+
for i, c := range capital {
150+
heap.Push(&q1, pair{c, profits[i]})
151+
}
152+
q2 := hp{}
153+
for k > 0 {
154+
for len(q1) > 0 && q1[0].c <= w {
155+
heap.Push(&q2, heap.Pop(&q1).(pair).p)
156+
}
157+
if q2.Len() == 0 {
158+
break
159+
}
160+
w += heap.Pop(&q2).(int)
161+
k--
162+
}
163+
return w
164+
}
165+
166+
type hp struct{ sort.IntSlice }
167+
168+
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
169+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
170+
func (h *hp) Pop() interface{} {
171+
a := h.IntSlice
172+
v := a[len(a)-1]
173+
h.IntSlice = a[:len(a)-1]
174+
return v
175+
}
176+
177+
type pair struct{ c, p int }
178+
type hp2 []pair
179+
180+
func (h hp2) Len() int { return len(h) }
181+
func (h hp2) Less(i, j int) bool { return h[i].c < h[j].c }
182+
func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
183+
func (h *hp2) Push(v interface{}) { *h = append(*h, v.(pair)) }
184+
func (h *hp2) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
75185
```
76186

77187
### **...**

solution/0500-0599/0502.IPO/README_EN.md

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,117 @@ Therefore, output the final maximized capital, which is 0 + 1 + 3 = 4.
5454
### **Python3**
5555

5656
```python
57-
57+
class Solution:
58+
def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int:
59+
h1 = [(c, p) for c, p in zip(capital, profits)]
60+
heapify(h1)
61+
h2 = []
62+
while k:
63+
while h1 and h1[0][0] <= w:
64+
heappush(h2, -heappop(h1)[1])
65+
if not h2:
66+
break
67+
w -= heappop(h2)
68+
k -= 1
69+
return w
5870
```
5971

6072
### **Java**
6173

6274
```java
75+
class Solution {
76+
public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
77+
int n = capital.length;
78+
PriorityQueue<int[]> q1 = new PriorityQueue<>((a, b) -> a[0] - b[0]);
79+
for (int i = 0; i < n; ++i) {
80+
q1.offer(new int[] {capital[i], profits[i]});
81+
}
82+
PriorityQueue<Integer> q2 = new PriorityQueue<>((a, b) -> b - a);
83+
while (k-- > 0) {
84+
while (!q1.isEmpty() && q1.peek()[0] <= w) {
85+
q2.offer(q1.poll()[1]);
86+
}
87+
if (q2.isEmpty()) {
88+
break;
89+
}
90+
w += q2.poll();
91+
}
92+
return w;
93+
}
94+
}
95+
```
96+
97+
### **C++**
98+
99+
```cpp
100+
using pii = pair<int, int>;
101+
102+
class Solution {
103+
public:
104+
int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {
105+
priority_queue<pii, vector<pii>, greater<pii>> q1;
106+
int n = profits.size();
107+
for (int i = 0; i < n; ++i) {
108+
q1.push({capital[i], profits[i]});
109+
}
110+
priority_queue<int> q2;
111+
while (k--) {
112+
while (!q1.empty() && q1.top().first <= w) {
113+
q2.push(q1.top().second);
114+
q1.pop();
115+
}
116+
if (q2.empty()) {
117+
break;
118+
}
119+
w += q2.top();
120+
q2.pop();
121+
}
122+
return w;
123+
}
124+
};
125+
```
63126
127+
### **Go**
128+
129+
```go
130+
func findMaximizedCapital(k int, w int, profits []int, capital []int) int {
131+
q1 := hp2{}
132+
for i, c := range capital {
133+
heap.Push(&q1, pair{c, profits[i]})
134+
}
135+
q2 := hp{}
136+
for k > 0 {
137+
for len(q1) > 0 && q1[0].c <= w {
138+
heap.Push(&q2, heap.Pop(&q1).(pair).p)
139+
}
140+
if q2.Len() == 0 {
141+
break
142+
}
143+
w += heap.Pop(&q2).(int)
144+
k--
145+
}
146+
return w
147+
}
148+
149+
type hp struct{ sort.IntSlice }
150+
151+
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
152+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
153+
func (h *hp) Pop() interface{} {
154+
a := h.IntSlice
155+
v := a[len(a)-1]
156+
h.IntSlice = a[:len(a)-1]
157+
return v
158+
}
159+
160+
type pair struct{ c, p int }
161+
type hp2 []pair
162+
163+
func (h hp2) Len() int { return len(h) }
164+
func (h hp2) Less(i, j int) bool { return h[i].c < h[j].c }
165+
func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
166+
func (h *hp2) Push(v interface{}) { *h = append(*h, v.(pair)) }
167+
func (h *hp2) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
64168
```
65169

66170
### **...**
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using pii = pair<int, int>;
2+
3+
class Solution {
4+
public:
5+
int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {
6+
priority_queue<pii, vector<pii>, greater<pii>> q1;
7+
int n = profits.size();
8+
for (int i = 0; i < n; ++i) {
9+
q1.push({capital[i], profits[i]});
10+
}
11+
priority_queue<int> q2;
12+
while (k--) {
13+
while (!q1.empty() && q1.top().first <= w) {
14+
q2.push(q1.top().second);
15+
q1.pop();
16+
}
17+
if (q2.empty()) {
18+
break;
19+
}
20+
w += q2.top();
21+
q2.pop();
22+
}
23+
return w;
24+
}
25+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
func findMaximizedCapital(k int, w int, profits []int, capital []int) int {
2+
q1 := hp2{}
3+
for i, c := range capital {
4+
heap.Push(&q1, pair{c, profits[i]})
5+
}
6+
q2 := hp{}
7+
for k > 0 {
8+
for len(q1) > 0 && q1[0].c <= w {
9+
heap.Push(&q2, heap.Pop(&q1).(pair).p)
10+
}
11+
if q2.Len() == 0 {
12+
break
13+
}
14+
w += heap.Pop(&q2).(int)
15+
k--
16+
}
17+
return w
18+
}
19+
20+
type hp struct{ sort.IntSlice }
21+
22+
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
23+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
24+
func (h *hp) Pop() interface{} {
25+
a := h.IntSlice
26+
v := a[len(a)-1]
27+
h.IntSlice = a[:len(a)-1]
28+
return v
29+
}
30+
31+
type pair struct{ c, p int }
32+
type hp2 []pair
33+
34+
func (h hp2) Len() int { return len(h) }
35+
func (h hp2) Less(i, j int) bool { return h[i].c < h[j].c }
36+
func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
37+
func (h *hp2) Push(v interface{}) { *h = append(*h, v.(pair)) }
38+
func (h *hp2) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }

0 commit comments

Comments
 (0)