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 a45de88

Browse files
Added tasks 611, 621, 622.
1 parent eaf7495 commit a45de88

File tree

9 files changed

+302
-0
lines changed

9 files changed

+302
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g0601_0700.s0611_valid_triangle_number;
2+
3+
// #Medium #Array #Sorting #Greedy #Binary_Search #Two_Pointers
4+
5+
public class Solution {
6+
public int triangleNumber(int[] nums) {
7+
int n;
8+
int max = 0;
9+
int[] count = new int[1001];
10+
for (int i : nums) {
11+
count[i]++;
12+
max = Math.max(max, i);
13+
}
14+
count[0] = 0;
15+
int idx = 0;
16+
for (int i = 1; i <= max; ++i) {
17+
for (int j = 0; j < count[i]; ++j, ++idx) {
18+
nums[idx] = i;
19+
}
20+
count[i] += count[i - 1];
21+
}
22+
n = idx;
23+
int r = 0;
24+
for (int i = 0; i < n - 2; ++i) {
25+
for (int j = i + 1; j < n - 1; ++j) {
26+
if (nums[i] + nums[j] > max) {
27+
r += (n - j) * (n - j - 1) / 2;
28+
break;
29+
}
30+
r += count[nums[i] + nums[j] - 1] - j - 1;
31+
}
32+
}
33+
return r;
34+
}
35+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
611\. Valid Triangle Number
2+
3+
Medium
4+
5+
Given an integer array `nums`, return _the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle_.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [2,2,3,4]
10+
11+
**Output:** 3
12+
13+
**Explanation:** Valid combinations are: 2,3,4 (using the first 2) 2,3,4 (using the second 2) 2,2,3
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [4,2,3,4]
18+
19+
**Output:** 4
20+
21+
**Constraints:**
22+
23+
* `1 <= nums.length <= 1000`
24+
* `0 <= nums[i] <= 1000`
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g0601_0700.s0621_task_scheduler;
2+
3+
// #Medium #Array #Hash_Table #Sorting #Greedy #Heap_Priority_Queue #Counting
4+
5+
public class Solution {
6+
public int leastInterval(char[] tasks, int n) {
7+
if (n <= 0) {
8+
return tasks.length;
9+
}
10+
int[] counters = new int[26];
11+
int maxCount = 0;
12+
for (char task : tasks) {
13+
int idx = task - 'A';
14+
counters[idx]++;
15+
maxCount = Math.max(maxCount, counters[idx]);
16+
}
17+
int maxNum = 0;
18+
for (int counter : counters) {
19+
if (counter == maxCount) {
20+
maxNum++;
21+
}
22+
}
23+
return Math.max(tasks.length, (maxCount - 1) * (n + 1) + maxNum);
24+
}
25+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
621\. Task Scheduler
2+
3+
Medium
4+
5+
Given a characters array `tasks`, representing the tasks a CPU needs to do, where each letter represents a different task. Tasks could be done in any order. Each task is done in one unit of time. For each unit of time, the CPU could complete either one task or just be idle.
6+
7+
However, there is a non-negative integer `n` that represents the cooldown period between two **same tasks** (the same letter in the array), that is that there must be at least `n` units of time between any two same tasks.
8+
9+
Return _the least number of units of times that the CPU will take to finish all the given tasks_.
10+
11+
**Example 1:**
12+
13+
**Input:** tasks = ["A","A","A","B","B","B"], n = 2
14+
15+
**Output:** 8
16+
17+
**Explanation:** A -> B -> idle -> A -> B -> idle -> A -> B There is at least 2 units of time between any two same tasks.
18+
19+
**Example 2:**
20+
21+
**Input:** tasks = ["A","A","A","B","B","B"], n = 0
22+
23+
**Output:** 6
24+
25+
**Explanation:** On this case any permutation of size 6 would work since n = 0. ["A","A","A","B","B","B"] ["A","B","A","B","A","B"] ["B","B","B","A","A","A"] ... And so on.
26+
27+
**Example 3:**
28+
29+
**Input:** tasks = ["A","A","A","A","A","A","B","C","D","E","F","G"], n = 2
30+
31+
**Output:** 16
32+
33+
**Explanation:** One possible solution is A -> B -> C -> A -> D -> E -> A -> F -> G -> A -> idle -> idle -> A -> idle -> idle -> A
34+
35+
**Constraints:**
36+
37+
* <code>1 <= task.length <= 10<sup>4</sup></code>
38+
* `tasks[i]` is upper-case English letter.
39+
* The integer `n` is in the range `[0, 100]`.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package g0601_0700.s0622_design_circular_queue;
2+
3+
// #Medium #Array #Design #Linked_List #Queue
4+
5+
public class MyCircularQueue {
6+
private final DoubleLinkedNode dumyHead = new DoubleLinkedNode(0);
7+
private final int maxSize;
8+
private int size = 0;
9+
10+
public MyCircularQueue(int k) {
11+
this.maxSize = k;
12+
dumyHead.left = dumyHead;
13+
dumyHead.right = dumyHead;
14+
}
15+
16+
public boolean enQueue(int value) {
17+
if (size == maxSize) {
18+
return false;
19+
}
20+
DoubleLinkedNode node = new DoubleLinkedNode(value);
21+
DoubleLinkedNode right = dumyHead.right;
22+
dumyHead.right = node;
23+
node.left = dumyHead;
24+
node.right = right;
25+
right.left = node;
26+
size++;
27+
return true;
28+
}
29+
30+
public boolean deQueue() {
31+
if (size == 0) {
32+
return false;
33+
}
34+
DoubleLinkedNode left = dumyHead.left;
35+
dumyHead.left = left.left;
36+
dumyHead.left.right = dumyHead;
37+
size--;
38+
return true;
39+
}
40+
41+
public int rear() {
42+
if (size == 0) {
43+
return -1;
44+
}
45+
return dumyHead.right.val;
46+
}
47+
48+
public int front() {
49+
if (size == 0) {
50+
return -1;
51+
}
52+
return dumyHead.left.val;
53+
}
54+
55+
public boolean isEmpty() {
56+
return size == 0;
57+
}
58+
59+
public boolean isFull() {
60+
return size == maxSize;
61+
}
62+
63+
static class DoubleLinkedNode {
64+
private final int val;
65+
private DoubleLinkedNode left;
66+
private DoubleLinkedNode right;
67+
68+
public DoubleLinkedNode(int val) {
69+
this.val = val;
70+
}
71+
}
72+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
622\. Design Circular Queue
2+
3+
Medium
4+
5+
Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".
6+
7+
One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the space to store new values.
8+
9+
Implementation the `MyCircularQueue` class:
10+
11+
* `MyCircularQueue(k)` Initializes the object with the size of the queue to be `k`.
12+
* `int Front()` Gets the front item from the queue. If the queue is empty, return `-1`.
13+
* `int Rear()` Gets the last item from the queue. If the queue is empty, return `-1`.
14+
* `boolean enQueue(int value)` Inserts an element into the circular queue. Return `true` if the operation is successful.
15+
* `boolean deQueue()` Deletes an element from the circular queue. Return `true` if the operation is successful.
16+
* `boolean isEmpty()` Checks whether the circular queue is empty or not.
17+
* `boolean isFull()` Checks whether the circular queue is full or not.
18+
19+
You must solve the problem without using the built-in queue data structure in your programming language.
20+
21+
**Example 1:**
22+
23+
**Input** ["MyCircularQueue", "enQueue", "enQueue", "enQueue", "enQueue", "Rear", "isFull", "deQueue", "enQueue", "Rear"] [[3], [1], [2], [3], [4], [], [], [], [4], []]
24+
25+
**Output:** [null, true, true, true, false, 3, true, true, true, 4]
26+
27+
**Explanation:** MyCircularQueue myCircularQueue = new MyCircularQueue(3); myCircularQueue.enQueue(1); // return True myCircularQueue.enQueue(2); // return True myCircularQueue.enQueue(3); // return True myCircularQueue.enQueue(4); // return False myCircularQueue.Rear(); // return 3 myCircularQueue.isFull(); // return True myCircularQueue.deQueue(); // return True myCircularQueue.enQueue(4); // return True myCircularQueue.Rear(); // return 4
28+
29+
**Constraints:**
30+
31+
* `1 <= k <= 1000`
32+
* `0 <= value <= 1000`
33+
* At most `3000` calls will be made to `enQueue`, `deQueue`, `Front`, `Rear`, `isEmpty`, and `isFull`.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0601_0700.s0611_valid_triangle_number;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void triangleNumber() {
11+
assertThat(new Solution().triangleNumber(new int[] {2, 2, 3, 4}), equalTo(3));
12+
}
13+
14+
@Test
15+
void triangleNumber2() {
16+
assertThat(new Solution().triangleNumber(new int[] {4, 2, 3, 4}), equalTo(4));
17+
}
18+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g0601_0700.s0621_task_scheduler;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void leastInterval() {
11+
assertThat(
12+
new Solution().leastInterval(new char[] {'A', 'A', 'A', 'B', 'B', 'B'}, 2),
13+
equalTo(8));
14+
}
15+
16+
@Test
17+
void leastInterval2() {
18+
assertThat(
19+
new Solution().leastInterval(new char[] {'A', 'A', 'A', 'B', 'B', 'B'}, 0),
20+
equalTo(6));
21+
}
22+
23+
@Test
24+
void leastInterval3() {
25+
assertThat(
26+
new Solution()
27+
.leastInterval(
28+
new char[] {
29+
'A', 'A', 'A', 'A', 'A', 'A', 'B', 'C', 'D', 'E', 'F', 'G'
30+
},
31+
2),
32+
equalTo(16));
33+
}
34+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0601_0700.s0622_design_circular_queue;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class MyCircularQueueTest {
9+
@Test
10+
void myCircularQueue() {
11+
MyCircularQueue myCircularQueue = new MyCircularQueue(3);
12+
assertThat(myCircularQueue.enQueue(1), equalTo(true));
13+
assertThat(myCircularQueue.enQueue(2), equalTo(true));
14+
assertThat(myCircularQueue.enQueue(3), equalTo(true));
15+
assertThat(myCircularQueue.enQueue(4), equalTo(false));
16+
assertThat(myCircularQueue.rear(), equalTo(3));
17+
assertThat(myCircularQueue.isFull(), equalTo(true));
18+
assertThat(myCircularQueue.deQueue(), equalTo(true));
19+
assertThat(myCircularQueue.enQueue(4), equalTo(true));
20+
assertThat(myCircularQueue.rear(), equalTo(4));
21+
}
22+
}

0 commit comments

Comments
(0)

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