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

Commit dfc2247

Browse files
committed
Merge branch 'master' of github.com:doocs/leetcode
2 parents 90c2646 + b854efc commit dfc2247

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
/**
3+
* 贪心算法
4+
*/
5+
public int findMaximizedCapital(int k, int W, int[] Profits, int[] Capital) {
6+
// 首先检查是否存在所有项目都可投资且初始资本 W >= max(Capital) 的情况。如果是,返回利润中前 k 个最大元素的和。
7+
boolean speedUp = true;
8+
for (int c : Capital) if (W < c) speedUp = false;
9+
if (speedUp) {
10+
PriorityQueue<Integer> heap = new PriorityQueue<>();
11+
for (int p : Profits) {
12+
heap.add(p);
13+
if (heap.size() > k) heap.poll();
14+
}
15+
for (int h : heap) W += h;
16+
return W;
17+
}
18+
19+
int idx;
20+
int n = Profits.length;
21+
for (int i = 0; i < Math.min(k, n); ++i) {
22+
idx = -1;
23+
// 找到获取利润最多的项目
24+
for (int j = 0; j < n; ++j) {
25+
if (W >= Capital[j]) {
26+
if (idx == -1) idx = j;
27+
else if (Profits[idx] < Profits[j]) idx = j;
28+
}
29+
}
30+
// 找不到合适的项目
31+
if (idx == -1) break;
32+
// 累计当前项目的利润到总利润中,并把当前项目标记为"不可用"(设置成最大整数)
33+
W += Profits[idx];
34+
Capital[idx] = Integer.MAX_VALUE;
35+
}
36+
return W;
37+
}
38+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class H2O {
2+
3+
private Semaphore h = new Semaphore(2);
4+
private Semaphore o = new Semaphore(0);
5+
public H2O() {
6+
7+
}
8+
9+
public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {
10+
11+
// releaseHydrogen.run() outputs "H". Do not change or remove this line.
12+
h.acquire();
13+
releaseHydrogen.run();
14+
o.release();
15+
}
16+
17+
public void oxygen(Runnable releaseOxygen) throws InterruptedException {
18+
19+
// releaseOxygen.run() outputs "O". Do not change or remove this line.
20+
o.acquire(2);
21+
releaseOxygen.run();
22+
h.release(2);
23+
}
24+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class FizzBuzz {
2+
private int n;
3+
4+
public FizzBuzz(int n) {
5+
this.n = n;
6+
}
7+
8+
private Semaphore fSema = new Semaphore(0);
9+
private Semaphore bSema = new Semaphore(0);
10+
private Semaphore fbSema = new Semaphore(0);
11+
private Semaphore nSema = new Semaphore(1);
12+
13+
// printFizz.run() outputs "fizz".
14+
public void fizz(Runnable printFizz) throws InterruptedException {
15+
for (int i = 3; i <= n; i = i + 3) {
16+
if (i % 5 != 0) {
17+
fSema.acquire();
18+
printFizz.run();
19+
nSema.release();
20+
}
21+
}
22+
}
23+
24+
// printBuzz.run() outputs "buzz".
25+
public void buzz(Runnable printBuzz) throws InterruptedException {
26+
for (int i = 5; i <= n; i = i + 5) {
27+
if (i % 3 != 0) {
28+
bSema.acquire();
29+
printBuzz.run();
30+
nSema.release();
31+
}
32+
}
33+
}
34+
35+
// printFizzBuzz.run() outputs "fizzbuzz".
36+
public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
37+
for (int i = 15; i <= n; i = i + 15) {
38+
fbSema.acquire();
39+
printFizzBuzz.run();
40+
nSema.release();
41+
}
42+
}
43+
44+
// printNumber.accept(x) outputs "x", where x is an integer.
45+
public void number(IntConsumer printNumber) throws InterruptedException {
46+
for (int i = 1; i <= n; i++) {
47+
nSema.acquire();
48+
if (i % 3 == 0 && i % 5 == 0) {
49+
fbSema.release();
50+
} else if (i % 3 == 0) {
51+
fSema.release();
52+
} else if (i % 5 == 0) {
53+
bSema.release();
54+
} else {
55+
printNumber.accept(i);
56+
nSema.release();
57+
}
58+
}
59+
}
60+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /