Skip to content

Commit a8040f7

Browse files
authored
Added tasks 1822, 1823, 1824, 1825, 1827.
1 parent 381f938 commit a8040f7

File tree

15 files changed

+559
-0
lines changed

15 files changed

+559
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g1801_1900.s1822_sign_of_the_product_of_an_array;
2+
3+
// #Easy #Array #Math #Programming_Skills_I_Day_4_Loop
4+
// #2022_05_06_Time_1_ms_(58.05%)_Space_44.1_MB_(44.59%)
5+
6+
public class Solution {
7+
public int arraySign(int[] nums) {
8+
int negativeCount = 0;
9+
for (int num : nums) {
10+
if (num == 0) {
11+
return 0;
12+
} else if (num < 0) {
13+
negativeCount++;
14+
}
15+
}
16+
return negativeCount % 2 == 0 ? 1 : -1;
17+
}
18+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
1822\. Sign of the Product of an Array
2+
3+
Easy
4+
5+
There is a function `signFunc(x)` that returns:
6+
7+
* `1` if `x` is positive.
8+
* `-1` if `x` is negative.
9+
* `0` if `x` is equal to `0`.
10+
11+
You are given an integer array `nums`. Let `product` be the product of all values in the array `nums`.
12+
13+
Return `signFunc(product)`.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [-1,-2,-3,-4,3,2,1]
18+
19+
**Output:** 1
20+
21+
**Explanation:** The product of all values in the array is 144, and signFunc(144) = 1
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [1,5,0,2,-3]
26+
27+
**Output:** 0
28+
29+
**Explanation:** The product of all values in the array is 0, and signFunc(0) = 0
30+
31+
**Example 3:**
32+
33+
**Input:** nums = [-1,1,-1,1,-1]
34+
35+
**Output:** -1
36+
37+
**Explanation:** The product of all values in the array is -1, and signFunc(-1) = -1
38+
39+
**Constraints:**
40+
41+
* `1 <= nums.length <= 1000`
42+
* `-100 <= nums[i] <= 100`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g1801_1900.s1823_find_the_winner_of_the_circular_game;
2+
3+
// #Medium #Array #Math #Simulation #Recursion #Queue #Data_Structure_II_Day_14_Stack_Queue
4+
// #2022_05_06_Time_3_ms_(64.85%)_Space_41.9_MB_(31.61%)
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class Solution {
10+
public int findTheWinner(int n, int k) {
11+
List<Integer> list = new ArrayList<>(n);
12+
for (int i = 0; i < n; i++) {
13+
list.add(i + 1);
14+
}
15+
int startIndex = 0;
16+
while (list.size() != 1) {
17+
int removeIndex = (startIndex + k - 1) % list.size();
18+
list.remove(removeIndex);
19+
startIndex = removeIndex;
20+
}
21+
return list.get(0);
22+
}
23+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
1823\. Find the Winner of the Circular Game
2+
3+
Medium
4+
5+
There are `n` friends that are playing a game. The friends are sitting in a circle and are numbered from `1` to `n` in **clockwise order**. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for `1 <= i < n`, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.
6+
7+
The rules of the game are as follows:
8+
9+
1. **Start** at the <code>1<sup>st</sup></code> friend.
10+
2. Count the next `k` friends in the clockwise direction **including** the friend you started at. The counting wraps around the circle and may count some friends more than once.
11+
3. The last friend you counted leaves the circle and loses the game.
12+
4. If there is still more than one friend in the circle, go back to step `2` **starting** from the friend **immediately clockwise** of the friend who just lost and repeat.
13+
5. Else, the last friend in the circle wins the game.
14+
15+
Given the number of friends, `n`, and an integer `k`, return _the winner of the game_.
16+
17+
**Example 1:**
18+
19+
![](https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png)
20+
21+
**Input:** n = 5, k = 2
22+
23+
**Output:** 3
24+
25+
**Explanation:** Here are the steps of the game:
26+
27+
1) Start at friend 1.
28+
29+
2) Count 2 friends clockwise, which are friends 1 and 2.
30+
31+
3) Friend 2 leaves the circle. Next start is friend 3.
32+
33+
4) Count 2 friends clockwise, which are friends 3 and 4.
34+
35+
5) Friend 4 leaves the circle. Next start is friend 5.
36+
37+
6) Count 2 friends clockwise, which are friends 5 and 1.
38+
39+
7) Friend 1 leaves the circle. Next start is friend 3.
40+
41+
8) Count 2 friends clockwise, which are friends 3 and 5.
42+
43+
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.
44+
45+
**Example 2:**
46+
47+
**Input:** n = 6, k = 5
48+
49+
**Output:** 1
50+
51+
**Explanation:** The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
52+
53+
**Constraints:**
54+
55+
* `1 <= k <= n <= 500`
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g1801_1900.s1824_minimum_sideway_jumps;
2+
3+
// #Medium #Array #Dynamic_Programming #Greedy
4+
// #2022_05_06_Time_17_ms_(96.34%)_Space_217.7_MB_(56.10%)
5+
6+
public class Solution {
7+
public int minSideJumps(int[] obstacles) {
8+
int sideJumps = 0;
9+
int currLane = 2;
10+
int i = 0;
11+
while (i < obstacles.length - 1) {
12+
if (obstacles[i + 1] == currLane) {
13+
if (obstacles[i] != 0) {
14+
currLane = getNextLane(obstacles[i], obstacles[i + 1]);
15+
} else {
16+
int j = i + 2;
17+
while (j < obstacles.length
18+
&& (obstacles[j] == 0 || obstacles[j] == obstacles[i + 1])) {
19+
j++;
20+
}
21+
if (j < obstacles.length) {
22+
currLane = getNextLane(obstacles[i + 1], obstacles[j]);
23+
} else {
24+
i = obstacles.length - 1;
25+
}
26+
}
27+
sideJumps++;
28+
}
29+
i++;
30+
}
31+
return sideJumps;
32+
}
33+
34+
private int getNextLane(int nextObstacle, int nextNextObstacle) {
35+
if ((nextObstacle == 2 && nextNextObstacle == 3)
36+
|| (nextObstacle == 3 && nextNextObstacle == 2)) {
37+
return 1;
38+
}
39+
if ((nextObstacle == 1 && nextNextObstacle == 3)
40+
|| (nextObstacle == 3 && nextNextObstacle == 1)) {
41+
return 2;
42+
} else {
43+
return 3;
44+
}
45+
}
46+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
1827\. Minimum Operations to Make the Array Increasing
2+
3+
Easy
4+
5+
You are given an integer array `nums` (**0-indexed**). In one operation, you can choose an element of the array and increment it by `1`.
6+
7+
* For example, if `nums = [1,2,3]`, you can choose to increment `nums[1]` to make `nums = [1,**3**,3]`.
8+
9+
Return _the **minimum** number of operations needed to make_ `nums` _**strictly** **increasing**._
10+
11+
An array `nums` is **strictly increasing** if `nums[i] < nums[i+1]` for all `0 <= i < nums.length - 1`. An array of length `1` is trivially strictly increasing.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,1,1]
16+
17+
**Output:** 3
18+
19+
**Explanation:** You can do the following operations: 1) Increment nums[2], so nums becomes [1,1,**2**]. 2) Increment nums[1], so nums becomes [1,**2**,2]. 3) Increment nums[2], so nums becomes [1,2,**3**].
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [1,5,2,4,1]
24+
25+
**Output:** 14
26+
27+
**Example 3:**
28+
29+
**Input:** nums = [8]
30+
31+
**Output:** 0
32+
33+
**Constraints:**
34+
35+
* `1 <= nums.length <= 5000`
36+
* <code>1 <= nums[i] <= 10<sup>4</sup></code>
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package g1801_1900.s1825_finding_mk_average;
2+
3+
// #Hard #Design #Heap_Priority_Queue #Ordered_Set #Queue
4+
// #2022_05_06_Time_83_ms_(60.59%)_Space_96.3_MB_(77.83%)
5+
6+
import java.util.ArrayDeque;
7+
import java.util.Deque;
8+
import java.util.TreeMap;
9+
10+
public class MKAverage {
11+
private final double m;
12+
private final double k;
13+
private final double c;
14+
private double avg;
15+
private final Bst middle;
16+
private final Bst min;
17+
private final Bst max;
18+
private final Deque<Integer> q;
19+
20+
public MKAverage(int m, int k) {
21+
this.m = m;
22+
this.k = k;
23+
this.c = m - k * 2;
24+
this.avg = 0;
25+
this.middle = new Bst();
26+
this.min = new Bst();
27+
this.max = new Bst();
28+
this.q = new ArrayDeque<>();
29+
}
30+
31+
public void addElement(int num) {
32+
if (min.size < k) {
33+
min.add(num);
34+
q.offer(num);
35+
return;
36+
}
37+
if (max.size < k) {
38+
min.add(num);
39+
max.add(min.removeMax());
40+
q.offer(num);
41+
return;
42+
}
43+
44+
if (num >= min.lastKey() && num <= max.firstKey()) {
45+
middle.add(num);
46+
avg += num / c;
47+
} else if (num < min.lastKey()) {
48+
min.add(num);
49+
int val = min.removeMax();
50+
middle.add(val);
51+
avg += val / c;
52+
} else if (num > max.firstKey()) {
53+
max.add(num);
54+
int val = max.removeMin();
55+
middle.add(val);
56+
avg += val / c;
57+
}
58+
59+
q.offer(num);
60+
61+
if (q.size() > m) {
62+
num = q.poll();
63+
if (middle.containsKey(num)) {
64+
avg -= num / c;
65+
middle.remove(num);
66+
} else if (min.containsKey(num)) {
67+
min.remove(num);
68+
int val = middle.removeMin();
69+
avg -= val / c;
70+
min.add(val);
71+
} else if (max.containsKey(num)) {
72+
max.remove(num);
73+
int val = middle.removeMax();
74+
avg -= val / c;
75+
max.add(val);
76+
}
77+
}
78+
}
79+
80+
public int calculateMKAverage() {
81+
if (q.size() < m) {
82+
return -1;
83+
}
84+
return (int) avg;
85+
}
86+
87+
static class Bst {
88+
TreeMap<Integer, Integer> map;
89+
int size;
90+
91+
public Bst() {
92+
this.map = new TreeMap<>();
93+
this.size = 0;
94+
}
95+
96+
void add(int num) {
97+
int count = map.getOrDefault(num, 0) + 1;
98+
map.put(num, count);
99+
size++;
100+
}
101+
102+
void remove(int num) {
103+
int count = map.getOrDefault(num, 1) - 1;
104+
if (count > 0) {
105+
map.put(num, count);
106+
} else {
107+
map.remove(num);
108+
}
109+
size--;
110+
}
111+
112+
int removeMin() {
113+
int key = map.firstKey();
114+
115+
remove(key);
116+
117+
return key;
118+
}
119+
120+
int removeMax() {
121+
int key = map.lastKey();
122+
123+
remove(key);
124+
125+
return key;
126+
}
127+
128+
boolean containsKey(int key) {
129+
return map.containsKey(key);
130+
}
131+
132+
int firstKey() {
133+
return map.firstKey();
134+
}
135+
136+
int lastKey() {
137+
return map.lastKey();
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)