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 1c5edd5

Browse files
committed
feat: add solutions to lc problems: No.1188,1196
* No.1188.Design Bounded Blocking Queue * No.1196.How Many Apples Can You Put into the Basket
1 parent 3c0591e commit 1c5edd5

File tree

20 files changed

+425
-155
lines changed

20 files changed

+425
-155
lines changed

‎solution/0100-0199/0167.Two Sum II - Input Array Is Sorted/README_EN.md‎

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

55
## Description
66

7-
<p>Given a <strong>1-indexed</strong> array of integers <code>numbers</code> that is already <strong><em>sorted in non-decreasing order</em></strong>, find two numbers such that they add up to a specific <code>target</code> number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index<sub>2</sub>]</code> where <code>1 &lt;= index<sub>1</sub> &lt; index<sub>2</sub> &lt;= numbers.length</code>.</p>
7+
<p>Given a <strong>1-indexed</strong> array of integers <code>numbers</code> that is already <strong><em>sorted in non-decreasing order</em></strong>, find two numbers such that they add up to a specific <code>target</code> number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index<sub>2</sub>]</code> where <code>1 &lt;= index<sub>1</sub> &lt; index<sub>2</sub> &lt;&nbsp;numbers.length</code>.</p>
88

99
<p>Return<em> the indices of the two numbers, </em><code>index<sub>1</sub></code><em> and </em><code>index<sub>2</sub></code><em>, <strong>added by one</strong> as an integer array </em><code>[index<sub>1</sub>, index<sub>2</sub>]</code><em> of length 2.</em></p>
1010

‎solution/0500-0599/0594.Longest Harmonious Subsequence/README_EN.md‎

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,34 @@
1111
<p>A <strong>subsequence</strong> of array is a sequence that can be derived from the array by deleting some or no elements without changing the order of the remaining elements.</p>
1212

1313
<p>&nbsp;</p>
14-
1514
<p><strong class="example">Example 1:</strong></p>
1615

1716
<pre>
18-
1917
<strong>Input:</strong> nums = [1,3,2,2,5,2,3,7]
20-
2118
<strong>Output:</strong> 5
22-
2319
<strong>Explanation:</strong> The longest harmonious subsequence is [3,2,2,2,3].
24-
2520
</pre>
2621

2722
<p><strong class="example">Example 2:</strong></p>
2823

2924
<pre>
30-
3125
<strong>Input:</strong> nums = [1,2,3,4]
32-
3326
<strong>Output:</strong> 2
34-
3527
</pre>
3628

3729
<p><strong class="example">Example 3:</strong></p>
3830

3931
<pre>
40-
4132
<strong>Input:</strong> nums = [1,1,1,1]
42-
4333
<strong>Output:</strong> 0
44-
4534
</pre>
4635

4736
<p>&nbsp;</p>
48-
4937
<p><strong>Constraints:</strong></p>
5038

5139
<ul>
52-
53-
<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
54-
55-
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
56-
40+
<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
41+
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
5742
</ul>
5843

5944
## Solutions

‎solution/0600-0699/0684.Redundant Connection/README.md‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>树可以看成是一个连通且 <strong>无环</strong>的<strong>无向</strong>图。</p>
9+
<p>树可以看成是一个连通且 <strong>无环&nbsp;</strong>的&nbsp;<strong>无向&nbsp;</strong>图。</p>
1010

11-
<p>给定往一棵<code>n</code> 个节点 (节点值<code>1〜n</code>) 的树中添加一条边后的图。添加的边的两个顶点包含在 <code>1</code> 到 <code>n</code>中间,且这条附加的边不属于树中已存在的边。图的信息记录于长度为 <code>n</code> 的二维数组 <code>edges</code>,<code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>表示图中在 <code>ai</code> 和 <code>bi</code> 之间存在一条边。</p>
11+
<p>给定往一棵&nbsp;<code>n</code> 个节点 (节点值&nbsp;<code>1〜n</code>) 的树中添加一条边后的图。添加的边的两个顶点包含在 <code>1</code> 到 <code>n</code>&nbsp;中间,且这条附加的边不属于树中已存在的边。图的信息记录于长度为 <code>n</code> 的二维数组 <code>edges</code>&nbsp;,<code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>&nbsp;表示图中在 <code>ai</code> 和 <code>bi</code> 之间存在一条边。</p>
1212

13-
<p>请找出一条可以删去的边,删除后可使得剩余部分是一个有着 <code>n</code> 个节点的树。如果有多个答案,则返回数组<code>edges</code> 中最后出现的边。</p>
13+
<p>请找出一条可以删去的边,删除后可使得剩余部分是一个有着 <code>n</code> 个节点的树。如果有多个答案,则返回数组&nbsp;<code>edges</code>&nbsp;中最后出现的那个。</p>
1414

15-
<p></p>
15+
<p>&nbsp;</p>
1616

1717
<p><strong>示例 1:</strong></p>
1818

@@ -32,18 +32,18 @@
3232
<strong>输出:</strong> [1,4]
3333
</pre>
3434

35-
<p></p>
35+
<p>&nbsp;</p>
3636

3737
<p><strong>提示:</strong></p>
3838

3939
<ul>
4040
<li><code>n == edges.length</code></li>
41-
<li><code>3 <= n <= 1000</code></li>
41+
<li><code>3 &lt;= n &lt;= 1000</code></li>
4242
<li><code>edges[i].length == 2</code></li>
43-
<li><code>1 <= ai < bi <= edges.length</code></li>
43+
<li><code>1 &lt;= ai&nbsp;&lt; bi&nbsp;&lt;= edges.length</code></li>
4444
<li><code>ai != bi</code></li>
4545
<li><code>edges</code> 中无重复元素</li>
46-
<li>给定的图是连通的</li>
46+
<li>给定的图是连通的&nbsp;</li>
4747
</ul>
4848

4949
## 解法

‎solution/0800-0899/0827.Making A Large Island/README_EN.md‎

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,55 +11,37 @@
1111
<p>An <strong>island</strong> is a 4-directionally connected group of <code>1</code>s.</p>
1212

1313
<p>&nbsp;</p>
14-
1514
<p><strong class="example">Example 1:</strong></p>
1615

1716
<pre>
18-
1917
<strong>Input:</strong> grid = [[1,0],[0,1]]
20-
2118
<strong>Output:</strong> 3
22-
2319
<strong>Explanation:</strong> Change one 0 to 1 and connect two 1s, then we get an island with area = 3.
24-
2520
</pre>
2621

2722
<p><strong class="example">Example 2:</strong></p>
2823

2924
<pre>
30-
3125
<strong>Input:</strong> grid = [[1,1],[1,0]]
32-
3326
<strong>Output:</strong> 4
34-
3527
<strong>Explanation: </strong>Change the 0 to 1 and make the island bigger, only one island with area = 4.</pre>
3628

3729
<p><strong class="example">Example 3:</strong></p>
3830

3931
<pre>
40-
4132
<strong>Input:</strong> grid = [[1,1],[1,1]]
42-
4333
<strong>Output:</strong> 4
44-
4534
<strong>Explanation:</strong> Can&#39;t change any 0 to 1, only one island with area = 4.
46-
4735
</pre>
4836

4937
<p>&nbsp;</p>
50-
5138
<p><strong>Constraints:</strong></p>
5239

5340
<ul>
54-
55-
<li><code>n == grid.length</code></li>
56-
57-
<li><code>n == grid[i].length</code></li>
58-
59-
<li><code>1 &lt;= n &lt;= 500</code></li>
60-
61-
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
62-
41+
<li><code>n == grid.length</code></li>
42+
<li><code>n == grid[i].length</code></li>
43+
<li><code>1 &lt;= n &lt;= 500</code></li>
44+
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
6345
</ul>
6446

6547
## Solutions

‎solution/1100-1199/1188.Design Bounded Blocking Queue/README.md‎

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,102 @@ queue.size(); // 队列中还有 1 个元素。
100100

101101
<!-- tabs:start -->
102102

103-
### **SQL**
103+
### **Python3**
104104

105-
```sql
105+
```python
106+
from threading import Semaphore
106107

108+
109+
class BoundedBlockingQueue(object):
110+
111+
def __init__(self, capacity: int):
112+
self.s1 = Semaphore(capacity)
113+
self.s2 = Semaphore(0)
114+
self.q = deque()
115+
116+
def enqueue(self, element: int) -> None:
117+
self.s1.acquire()
118+
self.q.append(element)
119+
self.s2.release()
120+
121+
def dequeue(self) -> int:
122+
self.s2.acquire()
123+
ans = self.q.popleft()
124+
self.s1.release()
125+
return ans
126+
127+
def size(self) -> int:
128+
return len(self.q)
107129
```
108130

131+
### **Java**
132+
133+
```java
134+
class BoundedBlockingQueue {
135+
private Semaphore s1;
136+
private Semaphore s2;
137+
private Deque<Integer> q = new ArrayDeque<>();
138+
139+
public BoundedBlockingQueue(int capacity) {
140+
s1 = new Semaphore(capacity);
141+
s2 = new Semaphore(0);
142+
}
143+
144+
public void enqueue(int element) throws InterruptedException {
145+
s1.acquire();
146+
q.offer(element);
147+
s2.release();
148+
}
149+
150+
public int dequeue() throws InterruptedException {
151+
s2.acquire();;
152+
int ans = q.poll();
153+
s1.release();
154+
return ans;
155+
}
156+
157+
public int size() {
158+
return q.size();
159+
}
160+
}
161+
```
162+
163+
### **C++**
164+
165+
```cpp
166+
#include <semaphore.h>
167+
168+
class BoundedBlockingQueue {
169+
public:
170+
BoundedBlockingQueue(int capacity) {
171+
sem_init(&s1, 0, capacity);
172+
sem_init(&s2, 0, 0);
173+
}
174+
175+
void enqueue(int element) {
176+
sem_wait(&s1);
177+
q.push(element);
178+
sem_post(&s2);
179+
}
180+
181+
int dequeue() {
182+
sem_wait(&s2);
183+
int ans = q.front();
184+
q.pop();
185+
sem_post(&s1);
186+
return ans;
187+
}
188+
189+
int size() {
190+
return q.size();
191+
}
192+
193+
private:
194+
queue<int> q;
195+
sem_t s1, s2;
196+
};
197+
```
198+
199+
### \*\*
200+
109201
<!-- tabs:end -->

‎solution/1100-1199/1188.Design Bounded Blocking Queue/README_EN.md‎

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,100 @@ Since the number of threads for producer/consumer is greater than 1, we do not k
9292

9393
<!-- tabs:start -->
9494

95-
### **SQL**
95+
### **Python3**
9696

97-
```sql
97+
```python
98+
from threading import Semaphore
9899

100+
101+
class BoundedBlockingQueue(object):
102+
103+
def __init__(self, capacity: int):
104+
self.s1 = Semaphore(capacity)
105+
self.s2 = Semaphore(0)
106+
self.q = deque()
107+
108+
def enqueue(self, element: int) -> None:
109+
self.s1.acquire()
110+
self.q.append(element)
111+
self.s2.release()
112+
113+
def dequeue(self) -> int:
114+
self.s2.acquire()
115+
ans = self.q.popleft()
116+
self.s1.release()
117+
return ans
118+
119+
def size(self) -> int:
120+
return len(self.q)
121+
```
122+
123+
### **Java**
124+
125+
```java
126+
class BoundedBlockingQueue {
127+
private Semaphore s1;
128+
private Semaphore s2;
129+
private Deque<Integer> q = new ArrayDeque<>();
130+
131+
public BoundedBlockingQueue(int capacity) {
132+
s1 = new Semaphore(capacity);
133+
s2 = new Semaphore(0);
134+
}
135+
136+
public void enqueue(int element) throws InterruptedException {
137+
s1.acquire();
138+
q.offer(element);
139+
s2.release();
140+
}
141+
142+
public int dequeue() throws InterruptedException {
143+
s2.acquire();;
144+
int ans = q.poll();
145+
s1.release();
146+
return ans;
147+
}
148+
149+
public int size() {
150+
return q.size();
151+
}
152+
}
153+
```
154+
155+
### **C++**
156+
157+
```cpp
158+
#include <semaphore.h>
159+
160+
class BoundedBlockingQueue {
161+
public:
162+
BoundedBlockingQueue(int capacity) {
163+
sem_init(&s1, 0, capacity);
164+
sem_init(&s2, 0, 0);
165+
}
166+
167+
void enqueue(int element) {
168+
sem_wait(&s1);
169+
q.push(element);
170+
sem_post(&s2);
171+
}
172+
173+
int dequeue() {
174+
sem_wait(&s2);
175+
int ans = q.front();
176+
q.pop();
177+
sem_post(&s1);
178+
return ans;
179+
}
180+
181+
int size() {
182+
return q.size();
183+
}
184+
185+
private:
186+
queue<int> q;
187+
sem_t s1, s2;
188+
};
99189
```
100190

101191
<!-- tabs:end -->

0 commit comments

Comments
(0)

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