Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g2201_2300.s2210_count_hills_and_valleys_in_an_array;

// #Easy #Array #2022_06_12_Time_0_ms_(100.00%)_Space_41.8_MB_(60.16%)

public class Solution {
public int countHillValley(int[] nums) {
int left = nums[0];
int count = 0;
for (int i = 1; i < nums.length - 1; i++) {
if ((left > nums[i] && nums[i + 1] > nums[i])
|| (left < nums[i] && nums[i + 1] < nums[i])) {
count++;
left = nums[i];
}
}
return count;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
2210\. Count Hills and Valleys in an Array

Easy

You are given a **0-indexed** integer array `nums`. An index `i` is part of a **hill** in `nums` if the closest non-equal neighbors of `i` are smaller than `nums[i]`. Similarly, an index `i` is part of a **valley** in `nums` if the closest non-equal neighbors of `i` are larger than `nums[i]`. Adjacent indices `i` and `j` are part of the **same** hill or valley if `nums[i] == nums[j]`.

Note that for an index to be part of a hill or valley, it must have a non-equal neighbor on **both** the left and right of the index.

Return _the number of hills and valleys in_ `nums`.

**Example 1:**

**Input:** nums = [2,4,1,1,6,5]

**Output:** 3

**Explanation:**

At index 0: There is no non-equal neighbor of 2 on the left, so index 0 is neither a hill nor a valley.

At index 1: The closest non-equal neighbors of 4 are 2 and 1. Since 4 > 2 and 4 > 1, index 1 is a hill.

At index 2: The closest non-equal neighbors of 1 are 4 and 6. Since 1 < 4 and 1 < 6, index 2 is a valley.

At index 3: The closest non-equal neighbors of 1 are 4 and 6. Since 1 < 4 and 1 < 6, index 3 is a valley, but note that it is part of the same valley as index 2. At index 4: The closest non-equal neighbors of 6 are 1 and 5. Since 6 > 1 and 6 > 5, index 4 is a hill.

At index 5: There is no non-equal neighbor of 5 on the right, so index 5 is neither a hill nor a valley. There are 3 hills and valleys so we return 3.

**Example 2:**

**Input:** nums = [6,6,5,5,4,1]

**Output:** 0

**Explanation:**

At index 0: There is no non-equal neighbor of 6 on the left, so index 0 is neither a hill nor a valley.

At index 1: There is no non-equal neighbor of 6 on the left, so index 1 is neither a hill nor a valley.

At index 2: The closest non-equal neighbors of 5 are 6 and 4. Since 5 < 6 and 5 > 4, index 2 is neither a hill nor a valley.

At index 3: The closest non-equal neighbors of 5 are 6 and 4. Since 5 < 6 and 5 > 4, index 3 is neither a hill nor a valley.

At index 4: The closest non-equal neighbors of 4 are 5 and 1. Since 4 < 5 and 4 > 1, index 4 is neither a hill nor a valley.

At index 5: There is no non-equal neighbor of 1 on the right, so index 5 is neither a hill nor a valley. There are 0 hills and valleys so we return 0.

**Constraints:**

* `3 <= nums.length <= 100`
* `1 <= nums[i] <= 100`
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package g2201_2300.s2211_count_collisions_on_a_road;

// #Medium #String #Stack #2022_06_12_Time_113_ms_(45.96%)_Space_54.3_MB_(57.28%)

import java.util.ArrayDeque;
import java.util.Deque;

public class Solution {
public int countCollisions(String directions) {
if (directions == null || directions.length() == 1) {
return 0;
}
Deque<Character> stack = new ArrayDeque<>();
char[] direction = directions.toCharArray();
char prevc = '0';
int collision = 0;
for (int i = 0; i < direction.length; i++) {
if (direction[i] == 'R') {
stack.push(direction[i]);
} else {
if ((direction[i] == 'S' && prevc == 'R')) {
if (!stack.isEmpty()) {
stack.pop();
}
collision += 1;
direction[i] = 'S';
while (!stack.isEmpty()) {
collision++;
stack.pop();
}
}
if ((direction[i] == 'L' && prevc == 'R')) {
stack.pop();
collision += 2;
direction[i] = 'S';
while (!stack.isEmpty()) {
collision++;
stack.pop();
}
}
if (direction[i] == 'L' && prevc == 'S') {
collision++;
direction[i] = 'S';
}
}
prevc = direction[i];
}
return collision;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
2211\. Count Collisions on a Road

Medium

There are `n` cars on an infinitely long road. The cars are numbered from `0` to `n - 1` from left to right and each car is present at a **unique** point.

You are given a **0-indexed** string `directions` of length `n`. `directions[i]` can be either `'L'`, `'R'`, or `'S'` denoting whether the <code>i<sup>th</sup></code> car is moving towards the **left**, towards the **right**, or **staying** at its current point respectively. Each moving car has the **same speed**.

The number of collisions can be calculated as follows:

* When two cars moving in **opposite** directions collide with each other, the number of collisions increases by `2`.
* When a moving car collides with a stationary car, the number of collisions increases by `1`.

After a collision, the cars involved can no longer move and will stay at the point where they collided. Other than that, cars cannot change their state or direction of motion.

Return _the **total number of collisions** that will happen on the road_.

**Example 1:**

**Input:** directions = "RLRSLL"

**Output:** 5

**Explanation:** The collisions that will happen on the road are:

- Cars 0 and 1 will collide with each other. Since they are moving in opposite directions, the number of collisions becomes 0 + 2 = 2.

- Cars 2 and 3 will collide with each other. Since car 3 is stationary, the number of collisions becomes 2 + 1 = 3.

- Cars 3 and 4 will collide with each other. Since car 3 is stationary, the number of collisions becomes 3 + 1 = 4.

- Cars 4 and 5 will collide with each other. After car 4 collides with car 3, it will stay at the point of collision and get hit by car 5. The number of collisions becomes 4 + 1 = 5.

Thus, the total number of collisions that will happen on the road is 5.

**Example 2:**

**Input:** directions = "LLRR"

**Output:** 0

**Explanation:** No cars will collide with each other. Thus, the total number of collisions that will happen on the road is 0.

**Constraints:**

* <code>1 <= directions.length <= 10<sup>5</sup></code>
* `directions[i]` is either `'L'`, `'R'`, or `'S'`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package g2201_2300.s2212_maximum_points_in_an_archery_competition;

// #Medium #Array #Bit_Manipulation #Recursion #Enumeration
// #2022_06_12_Time_7_ms_(78.16%)_Space_43.3_MB_(36.78%)

public class Solution {
private final int[] ans = new int[12];
private final int[] ans1 = new int[12];
private int max = 0;

public int[] maximumBobPoints(int numArrows, int[] aliceArrows) {
solve(numArrows, aliceArrows, 11, 0);
return ans1;
}

private void solve(int numArrows, int[] aliceArrows, int index, int sum) {
if (numArrows <= 0 || index < 0) {
if (max < sum) {
max = sum;
ans1[0] = Math.max(ans[0], ans[0] + numArrows);
System.arraycopy(ans, 1, ans1, 1, 11);
}
return;
}
if (aliceArrows[index] + 1 <= numArrows) {
ans[index] = aliceArrows[index] + 1;
solve(numArrows - (aliceArrows[index] + 1), aliceArrows, index - 1, sum + index);
ans[index] = 0;
}
solve(numArrows, aliceArrows, index - 1, sum);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
2212\. Maximum Points in an Archery Competition

Medium

Alice and Bob are opponents in an archery competition. The competition has set the following rules:

1. Alice first shoots `numArrows` arrows and then Bob shoots `numArrows` arrows.
2. The points are then calculated as follows:
1. The target has integer scoring sections ranging from `0` to `11` **inclusive**.
2. For **each** section of the target with score `k` (in between `0` to `11`), say Alice and Bob have shot <code>a<sub>k</sub></code> and <code>b<sub>k</sub></code> arrows on that section respectively. If <code>a<sub>k</sub> >= b<sub>k</sub></code>, then Alice takes `k` points. If <code>a<sub>k</sub> < b<sub>k</sub></code>, then Bob takes `k` points.
3. However, if <code>a<sub>k</sub> == b<sub>k</sub> == 0</code>, then **nobody** takes `k` points.

* For example, if Alice and Bob both shot `2` arrows on the section with score `11`, then Alice takes `11` points. On the other hand, if Alice shot `0` arrows on the section with score `11` and Bob shot `2` arrows on that same section, then Bob takes `11` points.


You are given the integer `numArrows` and an integer array `aliceArrows` of size `12`, which represents the number of arrows Alice shot on each scoring section from `0` to `11`. Now, Bob wants to **maximize** the total number of points he can obtain.

Return _the array_ `bobArrows` _which represents the number of arrows Bob shot on **each** scoring section from_ `0` _to_ `11`. The sum of the values in `bobArrows` should equal `numArrows`.

If there are multiple ways for Bob to earn the maximum total points, return **any** one of them.

**Example 1:**

![](https://assets.leetcode.com/uploads/2022/02/24/ex1.jpg)

**Input:** numArrows = 9, aliceArrows = [1,1,0,1,0,0,2,1,0,1,2,0]

**Output:** [0,0,0,0,1,1,0,0,1,2,3,1]

**Explanation:** The table above shows how the competition is scored. Bob earns a total point of 4 + 5 + 8 + 9 + 10 + 11 = 47. It can be shown that Bob cannot obtain a score higher than 47 points.

**Example 2:**

![](https://assets.leetcode.com/uploads/2022/02/24/ex2new.jpg)

**Input:** numArrows = 3, aliceArrows = [0,0,1,0,0,0,0,0,0,0,0,2]

**Output:** [0,0,0,0,0,0,0,0,1,1,1,0]

**Explanation:** The table above shows how the competition is scored. Bob earns a total point of 8 + 9 + 10 = 27. It can be shown that Bob cannot obtain a score higher than 27 points.

**Constraints:**

* <code>1 <= numArrows <= 10<sup>5</sup></code>
* `aliceArrows.length == bobArrows.length == 12`
* `0 <= aliceArrows[i], bobArrows[i] <= numArrows`
* `sum(aliceArrows[i]) == numArrows`
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package g2201_2300.s2213_longest_substring_of_one_repeating_character;

// #Hard #Array #String #Ordered_Set #Segment_Tree
// #2022_06_12_Time_141_ms_(86.81%)_Space_148.3_MB_(47.22%)

public class Solution {
static class TreeNode {
int start;
int end;
char leftChar;
int leftCharLen;
char rightChar;
int rightCharLen;
int max;
TreeNode left;
TreeNode right;

TreeNode(int start, int end) {
this.start = start;
this.end = end;
left = null;
right = null;
}
}

public int[] longestRepeating(String s, String queryCharacters, int[] queryIndices) {
char[] sChar = s.toCharArray();
char[] qChar = queryCharacters.toCharArray();
TreeNode root = buildTree(sChar, 0, sChar.length - 1);
int[] result = new int[qChar.length];
for (int i = 0; i < qChar.length; i++) {
updateTree(root, queryIndices[i], qChar[i]);
if (root != null) {
result[i] = root.max;
}
}
return result;
}

private TreeNode buildTree(char[] s, int from, int to) {
if (from > to) {
return null;
}
TreeNode root = new TreeNode(from, to);
if (from == to) {
root.max = 1;
root.rightChar = root.leftChar = s[from];
root.leftCharLen = root.rightCharLen = 1;
return root;
}
int middle = from + (to - from) / 2;
root.left = buildTree(s, from, middle);
root.right = buildTree(s, middle + 1, to);
updateNode(root);
return root;
}

private void updateTree(TreeNode root, int index, char c) {
if (root == null || root.start > index || root.end < index) {
return;
}
if (root.start == index && root.end == index) {
root.leftChar = root.rightChar = c;
return;
}
updateTree(root.left, index, c);
updateTree(root.right, index, c);
updateNode(root);
}

private void updateNode(TreeNode root) {
if (root == null) {
return;
}
root.leftChar = root.left.leftChar;
root.leftCharLen = root.left.leftCharLen;
root.rightChar = root.right.rightChar;
root.rightCharLen = root.right.rightCharLen;
root.max = Math.max(root.left.max, root.right.max);
if (root.left.rightChar == root.right.leftChar) {
int len = root.left.rightCharLen + root.right.leftCharLen;
if (root.left.leftChar == root.left.rightChar
&& root.left.leftCharLen == root.left.end - root.left.start + 1) {
root.leftCharLen = len;
}
if (root.right.leftChar == root.right.rightChar
&& root.right.leftCharLen == root.right.end - root.right.start + 1) {
root.rightCharLen = len;
}
root.max = Math.max(root.max, len);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
2213\. Longest Substring of One Repeating Character

Hard

You are given a **0-indexed** string `s`. You are also given a **0-indexed** string `queryCharacters` of length `k` and a **0-indexed** array of integer **indices** `queryIndices` of length `k`, both of which are used to describe `k` queries.

The <code>i<sup>th</sup></code> query updates the character in `s` at index `queryIndices[i]` to the character `queryCharacters[i]`.

Return _an array_ `lengths` _of length_ `k` _where_ `lengths[i]` _is the **length** of the **longest substring** of_ `s` _consisting of **only one repeating** character **after** the_ <code>i<sup>th</sup></code> _query_ _is performed._

**Example 1:**

**Input:** s = "babacc", queryCharacters = "bcb", queryIndices = [1,3,3]

**Output:** [3,3,4]

**Explanation:** - 1<sup>st</sup> query updates s = "b**b**bacc". The longest substring consisting of one repeating character is "bbb" with length 3.

- 2<sup>nd</sup> query updates s = "bbb**c**cc". The longest substring consisting of one repeating character can be "bbb" or "ccc" with length 3.

- 3<sup>rd</sup> query updates s = "bbb**b**cc". The longest substring consisting of one repeating character is "bbbb" with length 4.

Thus, we return [3,3,4].

**Example 2:**

**Input:** s = "abyzz", queryCharacters = "aa", queryIndices = [2,1]

**Output:** [2,3]

**Explanation:** - 1<sup>st</sup> query updates s = "ab**a**zz". The longest substring consisting of one repeating character is "zz" with length 2.

- 2<sup>nd</sup> query updates s = "a**a**azz". The longest substring consisting of one repeating character is "aaa" with length 3.

Thus, we return [2,3].

**Constraints:**

* <code>1 <= s.length <= 10<sup>5</sup></code>
* `s` consists of lowercase English letters.
* `k == queryCharacters.length == queryIndices.length`
* <code>1 <= k <= 10<sup>5</sup></code>
* `queryCharacters` consists of lowercase English letters.
* `0 <= queryIndices[i] < s.length`
Loading