diff --git a/solution/0500-0599/0502.IPO/Solution.java b/solution/0500-0599/0502.IPO/Solution.java new file mode 100644 index 0000000000000..da68eded5751f --- /dev/null +++ b/solution/0500-0599/0502.IPO/Solution.java @@ -0,0 +1,38 @@ +class Solution { + /** + * 贪心算法 + */ + public int findMaximizedCapital(int k, int W, int[] Profits, int[] Capital) { + // 首先检查是否存在所有项目都可投资且初始资本 W >= max(Capital) 的情况。如果是,返回利润中前 k 个最大元素的和。 + boolean speedUp = true; + for (int c : Capital) if (W < c) speedUp = false; + if (speedUp) { + PriorityQueue heap = new PriorityQueue<>(); + for (int p : Profits) { + heap.add(p); + if (heap.size() > k) heap.poll(); + } + for (int h : heap) W += h; + return W; + } + + int idx; + int n = Profits.length; + for (int i = 0; i < Math.min(k, n); ++i) { + idx = -1; + // 找到获取利润最多的项目 + for (int j = 0; j < n; ++j) { + if (W >= Capital[j]) { + if (idx == -1) idx = j; + else if (Profits[idx] < Profits[j]) idx = j; + } + } + // 找不到合适的项目 + if (idx == -1) break; + // 累计当前项目的利润到总利润中,并把当前项目标记为"不可用"(设置成最大整数) + W += Profits[idx]; + Capital[idx] = Integer.MAX_VALUE; + } + return W; + } +} diff --git a/solution/1100-1199/1117.Building H2O/Solution.java b/solution/1100-1199/1117.Building H2O/Solution.java new file mode 100644 index 0000000000000..62ce05189de29 --- /dev/null +++ b/solution/1100-1199/1117.Building H2O/Solution.java @@ -0,0 +1,24 @@ +class H2O { + + private Semaphore h = new Semaphore(2); + private Semaphore o = new Semaphore(0); + public H2O() { + + } + + public void hydrogen(Runnable releaseHydrogen) throws InterruptedException { + + // releaseHydrogen.run() outputs "H". Do not change or remove this line. + h.acquire(); + releaseHydrogen.run(); + o.release(); + } + + public void oxygen(Runnable releaseOxygen) throws InterruptedException { + + // releaseOxygen.run() outputs "O". Do not change or remove this line. + o.acquire(2); + releaseOxygen.run(); + h.release(2); + } +} diff --git a/solution/1100-1199/1195.Fizz Buzz Multithreaded/Solution.java b/solution/1100-1199/1195.Fizz Buzz Multithreaded/Solution.java new file mode 100644 index 0000000000000..81d5497d8423a --- /dev/null +++ b/solution/1100-1199/1195.Fizz Buzz Multithreaded/Solution.java @@ -0,0 +1,60 @@ +class FizzBuzz { + private int n; + + public FizzBuzz(int n) { + this.n = n; + } + + private Semaphore fSema = new Semaphore(0); + private Semaphore bSema = new Semaphore(0); + private Semaphore fbSema = new Semaphore(0); + private Semaphore nSema = new Semaphore(1); + + // printFizz.run() outputs "fizz". + public void fizz(Runnable printFizz) throws InterruptedException { + for (int i = 3; i <= n; i = i + 3) { + if (i % 5 != 0) { + fSema.acquire(); + printFizz.run(); + nSema.release(); + } + } + } + + // printBuzz.run() outputs "buzz". + public void buzz(Runnable printBuzz) throws InterruptedException { + for (int i = 5; i <= n; i = i + 5) { + if (i % 3 != 0) { + bSema.acquire(); + printBuzz.run(); + nSema.release(); + } + } + } + + // printFizzBuzz.run() outputs "fizzbuzz". + public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException { + for (int i = 15; i <= n; i = i + 15) { + fbSema.acquire(); + printFizzBuzz.run(); + nSema.release(); + } + } + + // printNumber.accept(x) outputs "x", where x is an integer. + public void number(IntConsumer printNumber) throws InterruptedException { + for (int i = 1; i <= n; i++) { + nSema.acquire(); + if (i % 3 == 0 && i % 5 == 0) { + fbSema.release(); + } else if (i % 3 == 0) { + fSema.release(); + } else if (i % 5 == 0) { + bSema.release(); + } else { + printNumber.accept(i); + nSema.release(); + } + } + } +}