Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit96fe5ee

Browse files
authored
Merge pull request6boris#445 from 0xff-dev/master
Add solution and test-cases for problem 502
2 parents7a0400f +4ed22b6 commit96fe5ee

File tree

3 files changed

+90
-25
lines changed

3 files changed

+90
-25
lines changed

‎leetcode/501-600/0502.IPO/README.md

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
#[502.IPO][title]
22

3-
>[!WARNING|style:flat]
4-
>This question is temporarily unanswered if you have good ideas. Welcome to[Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
##Description
4+
Suppose LeetCode will start its**IPO** soon. In order to sell a good price of its shares to Venture Capital, LeetCode would like to work on some projects to increase its capital before the**IPO**. Since it has limited resources, it can only finish at most`k` distinct projects before the**IPO**. Help LeetCode design the best way to maximize its total capital after finishing at most`k` distinct projects.
5+
6+
You are given`n` projects where the i<sup>th</sup> project has a pure profit`profits[i]` and a minimum capital of`capital[i]` is needed to start it.
7+
8+
Initially, you have`w` capital. When you finish a project, you will obtain its pure profit and the profit will be added to your total capital.
9+
10+
Pick a list of**at most**`k` distinct projects from given projects to**maximize your final capital**, and return the final maximized capital.
11+
12+
The answer is guaranteed to fit in a 32-bit signed integer.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: k = 2, w = 0, profits = [1,2,3], capital = [0,1,1]
18+
Output: 4
19+
Explanation: Since your initial capital is 0, you can only start the project indexed 0.
20+
After finishing it you will obtain profit 1 and your capital becomes 1.
21+
With capital 1, you can either start the project indexed 1 or the project indexed 2.
22+
Since you can choose at most 2 projects, you need to finish the project indexed 2 to get the maximum capital.
23+
Therefore, output the final maximized capital, which is 0 + 1 + 3 = 4.
1324
```
1425

15-
##题意
16-
>...
17-
18-
##题解
26+
**Example 2:**
1927

20-
###思路1
21-
>...
22-
IPO
23-
```go
2428
```
25-
29+
Input: k = 3, w = 0, profits = [1,2,3], capital = [0,1,2]
30+
Output: 6
31+
```
2632

2733
##结语
2834

‎leetcode/501-600/0502.IPO/Solution.go

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,64 @@
11
package Solution
22

3-
funcSolution(xbool)bool {
3+
import (
4+
"container/heap"
5+
"sort"
6+
)
7+
8+
typeheap502Itemstruct {
9+
cost,profitint
10+
}
11+
12+
typeheap502ItemList []heap502Item
13+
14+
func (h*heap502ItemList)Len()int {
15+
returnlen(*h)
16+
}
17+
18+
func (h*heap502ItemList)Less(i,jint)bool {
19+
a,b:= (*h)[i], (*h)[j]
20+
ifa.profit==b.profit {
21+
returna.cost<b.cost
22+
}
23+
returna.profit>b.profit
24+
}
25+
26+
func (h*heap502ItemList)Swap(i,jint) {
27+
(*h)[i], (*h)[j]= (*h)[j], (*h)[i]
28+
}
29+
30+
func (h*heap502ItemList)Push(xinterface{}) {
31+
*h=append(*h,x.(heap502Item))
32+
}
33+
34+
func (h*heap502ItemList)Pop()interface{} {
35+
old:=*h
36+
l:=len(old)
37+
x:=old[l-1]
38+
*h=old[:l-1]
439
returnx
540
}
41+
42+
funcSolution(kint,wint,profits []int,capital []int)int {
43+
x:=make([]heap502Item,len(profits))
44+
fori:=0;i<len(profits);i++ {
45+
x[i]=heap502Item{cost:capital[i],profit:profits[i]}
46+
}
47+
48+
sort.Slice(x,func(i,jint)bool {
49+
returnx[i].cost<x[j].cost
50+
})
51+
h:=heap502ItemList{}
52+
pick:=0
53+
for ;k>0;k-- {
54+
for ;pick<len(x)&&x[pick].cost<=w;pick++ {
55+
heap.Push(&h,x[pick])
56+
}
57+
iflen(h)==0 {
58+
break
59+
}
60+
x:=heap.Pop(&h).(heap502Item)
61+
w+=x.profit
62+
}
63+
returnw
64+
}

‎leetcode/501-600/0502.IPO/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
//测试用例
1111
cases:= []struct {
1212
namestring
13-
inputsbool
14-
expectbool
13+
k,wint
14+
p,c []int
15+
expectint
1516
}{
16-
{"TestCase",true,true},
17-
{"TestCase",true,true},
18-
{"TestCase",false,false},
17+
{"TestCase1",2,0, []int{1,2,3}, []int{0,1,1},4},
18+
{"TestCase2",3,0, []int{1,2,3}, []int{0,1,2},6},
1919
}
2020

2121
//开始测试
2222
fori,c:=rangecases {
2323
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
24-
got:=Solution(c.inputs)
24+
got:=Solution(c.k,c.w,c.p,c.c)
2525
if!reflect.DeepEqual(got,c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect,got,c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v %v",
27+
c.expect,got,c.k,c.w,c.p,c.c)
2828
}
2929
})
3030
}
3131
}
3232

33-
//压力测试
33+
//压力测试
3434
funcBenchmarkSolution(b*testing.B) {
3535
}
3636

37-
//使用案列
37+
//使用案列
3838
funcExampleSolution() {
3939
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp