From a2a70cd84f12e0053086d3d5890eadce64708c41 Mon Sep 17 00:00:00 2001 From: HendSame <154903157@qq.com> Date: Sat, 18 Apr 2020 15:36:30 +0800 Subject: [PATCH 1/4] feat:add solution for '0502','1117','1195' --- solution/0500-0599/0502.IPO/Solution.java | 38 ++++++++++++ .../1100-1199/1117.Building H2O/Solution.java | 24 ++++++++ .../Solution.java | 60 +++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 solution/0500-0599/0502.IPO/Solution.java create mode 100644 solution/1100-1199/1117.Building H2O/Solution.java create mode 100644 solution/1100-1199/1195.Fizz Buzz Multithreaded/Solution.java diff --git a/solution/0500-0599/0502.IPO/Solution.java b/solution/0500-0599/0502.IPO/Solution.java new file mode 100644 index 0000000000000..e86902c19fd5d --- /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; + } +} \ No newline at end of file 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..af0ac513344c3 --- /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); + } +} \ No newline at end of file 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..218bccc84f075 --- /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(); + } + } + } +} \ No newline at end of file From 95f4ca903b722dc387b8bf3aad7cfede20206864 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Sat, 18 Apr 2020 16:04:04 +0800 Subject: [PATCH 2/4] Update Solution.java --- .../1100-1199/1195.Fizz Buzz Multithreaded/Solution.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/solution/1100-1199/1195.Fizz Buzz Multithreaded/Solution.java b/solution/1100-1199/1195.Fizz Buzz Multithreaded/Solution.java index 218bccc84f075..81d5497d8423a 100644 --- a/solution/1100-1199/1195.Fizz Buzz Multithreaded/Solution.java +++ b/solution/1100-1199/1195.Fizz Buzz Multithreaded/Solution.java @@ -47,14 +47,14 @@ public void number(IntConsumer printNumber) throws InterruptedException { nSema.acquire(); if (i % 3 == 0 && i % 5 == 0) { fbSema.release(); - }else if (i % 3 == 0) { + } else if (i % 3 == 0) { fSema.release(); - }else if (i % 5 == 0) { + } else if (i % 5 == 0) { bSema.release(); - }else { + } else { printNumber.accept(i); nSema.release(); } } } -} \ No newline at end of file +} From 2beadad9105a5553b85e52d1ee4583ff09b2a787 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Sat, 18 Apr 2020 16:05:19 +0800 Subject: [PATCH 3/4] Update Solution.java --- solution/1100-1199/1117.Building H2O/Solution.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/solution/1100-1199/1117.Building H2O/Solution.java b/solution/1100-1199/1117.Building H2O/Solution.java index af0ac513344c3..62ce05189de29 100644 --- a/solution/1100-1199/1117.Building H2O/Solution.java +++ b/solution/1100-1199/1117.Building H2O/Solution.java @@ -1,7 +1,7 @@ class H2O { - private Semaphore h=new Semaphore(2); - private Semaphore o=new Semaphore(0); + private Semaphore h = new Semaphore(2); + private Semaphore o = new Semaphore(0); public H2O() { } @@ -21,4 +21,4 @@ public void oxygen(Runnable releaseOxygen) throws InterruptedException { releaseOxygen.run(); h.release(2); } -} \ No newline at end of file +} From 184af5a1cf93b8b94db829c86a69b181b33cdb85 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Sat, 18 Apr 2020 16:07:45 +0800 Subject: [PATCH 4/4] Update Solution.java --- solution/0500-0599/0502.IPO/Solution.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/solution/0500-0599/0502.IPO/Solution.java b/solution/0500-0599/0502.IPO/Solution.java index e86902c19fd5d..da68eded5751f 100644 --- a/solution/0500-0599/0502.IPO/Solution.java +++ b/solution/0500-0599/0502.IPO/Solution.java @@ -5,34 +5,34 @@ 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; + for (int c : Capital) if (W < c) speedUp = false; if (speedUp) { PriorityQueue heap = new PriorityQueue<>(); - for (int p: Profits) { + for (int p : Profits) { heap.add(p); if (heap.size() > k) heap.poll(); } - for (int h: heap) W += h; + for (int h : heap) W += h; return W; } int idx; int n = Profits.length; - for(int i = 0; i < Math.min(k, n); ++i) { + for (int i = 0; i < Math.min(k, n); ++i) { idx = -1; // 找到获取利润最多的项目 - for(int j = 0; j < n; ++j) { + for (int j = 0; j < n; ++j) { if (W >= Capital[j]) { - if (idx == -1 ) idx = j; + if (idx == -1) idx = j; else if (Profits[idx] < Profits[j]) idx = j; } } // 找不到合适的项目 - if(idx == -1) break; + if (idx == -1) break; // 累计当前项目的利润到总利润中,并把当前项目标记为"不可用"(设置成最大整数) W += Profits[idx]; Capital[idx] = Integer.MAX_VALUE; } - return W; + return W; } -} \ No newline at end of file +}