Skip to content

Commit 12914bc

Browse files
authored
Added tasks 1670, 1671.
1 parent 5bb42b7 commit 12914bc

File tree

6 files changed

+267
-0
lines changed

6 files changed

+267
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package g1601_1700.s1670_design_front_middle_back_queue;
2+
3+
// #Medium #Array #Design #Linked_List #Queue #Data_Stream
4+
// #2022_04_22_Time_5_ms_(100.00%)_Space_42.8_MB_(84.98%)
5+
6+
public class FrontMiddleBackQueue {
7+
private int[] queue = new int[1000];
8+
private int cur = -1;
9+
10+
public FrontMiddleBackQueue() {
11+
// empty constructor
12+
}
13+
14+
public void pushFront(int val) {
15+
cur++;
16+
for (int i = cur; i > 0; i--) {
17+
queue[i] = queue[i - 1];
18+
}
19+
queue[0] = val;
20+
}
21+
22+
public void pushMiddle(int val) {
23+
if (cur < 0) {
24+
pushFront(val);
25+
return;
26+
}
27+
cur++;
28+
int mid = cur / 2;
29+
for (int i = cur; i > mid; i--) {
30+
queue[i] = queue[i - 1];
31+
}
32+
queue[mid] = val;
33+
}
34+
35+
public void pushBack(int val) {
36+
if (cur < 0) {
37+
pushFront(val);
38+
return;
39+
}
40+
cur++;
41+
queue[cur] = val;
42+
}
43+
44+
public int popFront() {
45+
if (cur < 0) {
46+
return -1;
47+
}
48+
int result = queue[0];
49+
for (int i = 0; i < cur; i++) {
50+
queue[i] = queue[i + 1];
51+
}
52+
cur--;
53+
return result;
54+
}
55+
56+
public int popMiddle() {
57+
if (cur < 0) {
58+
return -1;
59+
}
60+
int mid = cur / 2;
61+
int result = queue[mid];
62+
for (int i = mid; i < cur; i++) {
63+
queue[i] = queue[i + 1];
64+
}
65+
cur--;
66+
return result;
67+
}
68+
69+
public int popBack() {
70+
if (cur < 0) {
71+
return -1;
72+
}
73+
int result = queue[cur];
74+
cur--;
75+
return result;
76+
}
77+
}
78+
79+
/*
80+
* Your FrontMiddleBackQueue object will be instantiated and called as such:
81+
* FrontMiddleBackQueue obj = new FrontMiddleBackQueue();
82+
* obj.pushFront(val);
83+
* obj.pushMiddle(val);
84+
* obj.pushBack(val);
85+
* int param_4 = obj.popFront();
86+
* int param_5 = obj.popMiddle();
87+
* int param_6 = obj.popBack();
88+
*/
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
1670\. Design Front Middle Back Queue
2+
3+
Medium
4+
5+
Design a queue that supports `push` and `pop` operations in the front, middle, and back.
6+
7+
Implement the `FrontMiddleBack` class:
8+
9+
* `FrontMiddleBack()` Initializes the queue.
10+
* `void pushFront(int val)` Adds `val` to the **front** of the queue.
11+
* `void pushMiddle(int val)` Adds `val` to the **middle** of the queue.
12+
* `void pushBack(int val)` Adds `val` to the **back** of the queue.
13+
* `int popFront()` Removes the **front** element of the queue and returns it. If the queue is empty, return `-1`.
14+
* `int popMiddle()` Removes the **middle** element of the queue and returns it. If the queue is empty, return `-1`.
15+
* `int popBack()` Removes the **back** element of the queue and returns it. If the queue is empty, return `-1`.
16+
17+
**Notice** that when there are **two** middle position choices, the operation is performed on the **frontmost** middle position choice. For example:
18+
19+
* Pushing `6` into the middle of `[1, 2, 3, 4, 5]` results in `[1, 2, 6, 3, 4, 5]`.
20+
* Popping the middle from `[1, 2, 3, 4, 5, 6]` returns `3` and results in `[1, 2, 4, 5, 6]`.
21+
22+
**Example 1:**
23+
24+
**Input:** ["FrontMiddleBackQueue", "pushFront", "pushBack", "pushMiddle", "pushMiddle", "popFront", "popMiddle", "popMiddle", "popBack", "popFront"]
25+
26+
[[], [1], [2], [3], [4], [], [], [], [], []]
27+
28+
**Output:** [null, null, null, null, null, 1, 3, 4, 2, -1]
29+
30+
**Explanation:**
31+
32+
FrontMiddleBackQueue q = new FrontMiddleBackQueue(); q.pushFront(1); // [1]
33+
q.pushBack(2); // [1, 2]
34+
q.pushMiddle(3); // [1, 3, 2]
35+
q.pushMiddle(4); // [1, 4, 3, 2]
36+
q.popFront(); // return 1 -> [4, 3, 2]
37+
q.popMiddle(); // return 3 -> [4, 2]
38+
q.popMiddle(); // return 4 -> [2]
39+
q.popBack(); // return 2 -> []
40+
q.popFront(); // return -1 -> [] (The queue is empty)
41+
42+
**Constraints:**
43+
44+
* <code>1 <= val <= 10<sup>9</sup></code>
45+
* At most `1000` calls will be made to `pushFront`, `pushMiddle`, `pushBack`, `popFront`, `popMiddle`, and `popBack`.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g1601_1700.s1671_minimum_number_of_removals_to_make_mountain_array;
2+
3+
// #Hard #Array #Dynamic_Programming #Greedy #Binary_Search
4+
// #2022_04_22_Time_19_ms_(81.23%)_Space_48.7_MB_(6.83%)
5+
6+
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.List;
9+
10+
public class Solution {
11+
public int minimumMountainRemovals(int[] nums) {
12+
int n = nums.length;
13+
// lbs -> longest bitomic subsequence
14+
int lbs = 0;
15+
int[] dp = new int[n];
16+
// dp[i] -> lis end at index i, dp2[i] -> lds end at index i
17+
int[] dp2 = new int[n];
18+
List<Integer> lis = new ArrayList<>();
19+
// calculate longest increasing subsequence
20+
for (int i = 0; i < n - 1; i++) {
21+
if (lis.isEmpty() || lis.get(lis.size() - 1) < nums[i]) {
22+
lis.add(nums[i]);
23+
} else {
24+
int idx = Collections.binarySearch(lis, nums[i]);
25+
if (idx < 0) {
26+
lis.set(-idx - 1, nums[i]);
27+
}
28+
}
29+
dp[i] = lis.size();
30+
}
31+
lis = new ArrayList<>();
32+
// calculate longest decreasing subsequence
33+
for (int i = n - 1; i >= 1; i--) {
34+
if (lis.isEmpty() || lis.get(lis.size() - 1) < nums[i]) {
35+
lis.add(nums[i]);
36+
} else {
37+
int idx = Collections.binarySearch(lis, nums[i]);
38+
if (idx < 0) {
39+
lis.set(-idx - 1, nums[i]);
40+
}
41+
}
42+
dp2[i] = lis.size();
43+
if (dp[i] > 1 && dp2[i] > 1) {
44+
lbs = Math.max(lbs, dp[i] + dp2[i] - 1);
45+
}
46+
}
47+
return n - lbs;
48+
}
49+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
1671\. Minimum Number of Removals to Make Mountain Array
2+
3+
Hard
4+
5+
You may recall that an array `arr` is a **mountain array** if and only if:
6+
7+
* `arr.length >= 3`
8+
* There exists some index `i` (**0-indexed**) with `0 < i < arr.length - 1` such that:
9+
* `arr[0] < arr[1] < ... < arr[i - 1] < arr[i]`
10+
* `arr[i] > arr[i + 1] > ... > arr[arr.length - 1]`
11+
12+
Given an integer array `nums`, return _the **minimum** number of elements to remove to make_ `nums` _a **mountain array**._
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,3,1]
17+
18+
**Output:** 0
19+
20+
**Explanation:** The array itself is a mountain array so we do not need to remove any elements.
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [2,1,1,5,6,2,3,1]
25+
26+
**Output:** 3
27+
28+
**Explanation:** One solution is to remove the elements at indices 0, 1, and 5, making the array nums = [1,5,6,3,1].
29+
30+
**Constraints:**
31+
32+
* `3 <= nums.length <= 1000`
33+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
34+
* It is guaranteed that you can make a mountain array out of `nums`.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g1601_1700.s1670_design_front_middle_back_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 FrontMiddleBackQueueTest {
9+
@Test
10+
void frontMiddleBackQueue() {
11+
FrontMiddleBackQueue q = new FrontMiddleBackQueue();
12+
// [1]
13+
q.pushFront(1);
14+
// [1, 2]
15+
q.pushBack(2);
16+
// [1, 3, 2]
17+
q.pushMiddle(3);
18+
// [1, 4, 3, 2]
19+
q.pushMiddle(4);
20+
// return 1 -> [4, 3, 2]
21+
assertThat(q.popFront(), equalTo(1));
22+
// return 3 -> [4, 2]
23+
assertThat(q.popMiddle(), equalTo(3));
24+
// return 4 -> [2]
25+
assertThat(q.popMiddle(), equalTo(4));
26+
// return 2 -> []
27+
assertThat(q.popBack(), equalTo(2));
28+
// return -1 -> [] (The queue is empty)
29+
assertThat(q.popFront(), equalTo(-1));
30+
}
31+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g1601_1700.s1671_minimum_number_of_removals_to_make_mountain_array;
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 minimumMountainRemovals() {
11+
assertThat(new Solution().minimumMountainRemovals(new int[] {1, 3, 1}), equalTo(0));
12+
}
13+
14+
@Test
15+
void minimumMountainRemovals2() {
16+
assertThat(
17+
new Solution().minimumMountainRemovals(new int[] {2, 1, 1, 5, 6, 2, 3, 1}),
18+
equalTo(3));
19+
}
20+
}

0 commit comments

Comments
 (0)