Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package g3901_4000.s3993_maximum_value_of_an_alternating_sequence;

// #Medium #Math #Greedy #Senior #Biweekly_Contest_187
// #2026_11_17_Time_1_ms_(96.70%)_Space_42.16_MB_(87.91%)

public class Solution {
public long maximumValue(int n, int s, int m) {
if (n == 1) {
return s;
}
return s + (long) (n / 2) * (m - 1) + 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
3993\. Maximum Value of an Alternating Sequence

Medium

You are given three integers `n`, `s`, and `m`.

A sequence `seq` of integers of length `n` is considered **valid** if:

* `seq[0] = s`.
* The sequence is **alternating**, meaning that either:
* `seq[0] > seq[1] < seq[2] > ...`, or
* `seq[0] < seq[1] > seq[2] < ...`.
* For every adjacent pair, `|seq[i] - seq[i - 1]| <= m`.

A sequence of length 1 is considered alternating.

Return the **maximum** possible element that can appear in any valid sequence.

OriginalJava**Example 1:**

**Input:** n = 4, s = 3, m = 5

**Output:** 12

**Explanation:**

* One valid sequence is `[3, 8, 7, 12]`.
* The maximum element in the sequence is 12.

**Example 2:**

**Input:** n = 2, s = 4, m = 3

**Output:** 7

**Explanation:**

* One valid sequence is `[4, 7]`.
* The maximum element in the sequence is 7.

**Constraints:**

* <code>1 <= n, s <= 10<sup>9</sup></code>
* <code>1 <= m <= 10<sup>5</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g3901_4000.s3994_minimum_adjacent_swaps_to_partition_array;

// #Medium #Array #Greedy #Staff #Biweekly_Contest_187
// #2026_11_17_Time_6_ms_(80.28%)_Space_101.87_MB_(61.97%)

public class Solution {
public int minAdjacentSwaps(int[] nums, int a, int b) {
long ans = 0;
long c1 = 0;
long c2 = 0;
for (int v : nums) {
if (v < a) {
ans += c1 + c2;
} else if (v <= b) {
c1++;
ans += c2;
} else {
c2++;
}
}
return (int) (ans % 1000000007);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
3994\. Minimum Adjacent Swaps to Partition Array

Medium

You are given an integer array `nums` and two integers `a` and `b` such that `a < b`.

An array is called **good** if it can be split into three **contiguous** parts, in this order, such that:

* Every element in the first part is **less than** `a`.
* Every element in the second part is **in** the range `[a, b]` inclusive.
* Every element in the third part is **greater than** `b`.

Any of the three parts **may be** empty.

In one **adjacent swap**, you may swap two **neighboring** elements of `nums`.

Return the **minimum number** of adjacent swaps required to make `nums` good. Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.

OriginalJava**Example 1:**

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

**Output:** 1

**Explanation:**

* Swap `nums[1]` and `nums[2]`. The array becomes `[1, 2, 3, 4, 5, 6]`.
* This array is good because it can be split into `[1, 2]`, `[3, 4]`, and `[5, 6]`.

**Example 2:**

**Input:** nums = [9,7,5,3], a = 4, b = 8

**Output:** 5

**Explanation:**

One sequence of optimal swaps is as follows:

* Swap `nums[2]` and `nums[3]`. The array becomes `[9, 7, 3, 5]`.
* Swap `nums[1]` and `nums[2]`. The array becomes `[9, 3, 7, 5]`.
* Swap `nums[0]` and `nums[1]`. The array becomes `[3, 9, 7, 5]`.
* Swap `nums[1]` and `nums[2]`. The array becomes `[3, 7, 9, 5]`.
* Swap `nums[2]` and `nums[3]`. The array becomes `[3, 7, 5, 9]`.
* This array is good because it can be split into `[3]`, `[7, 5]`, and `[9]`.

**Example 3:**

**Input:** nums = [3,7,5,9], a = 4, b = 8

**Output:** 0

**Explanation:**

The array is already good. No swaps are needed.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
* <code>1 <= a < b <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package g3901_4000.s3995_minimum_cost_to_convert_string_iii;

// #Hard #Array #String #Dynamic_Programming #Senior_Staff #Biweekly_Contest_187
// #2026_11_17_Time_10_ms_(100.00%)_Space_47.23_MB_(68.97%)

import java.util.Arrays;
import java.util.List;

@SuppressWarnings("java:S135")
public class Solution {
public int minCost(String source, String target, List<List<String>> rules, int[] costs) {
int n = source.length();
if (n != target.length()) {
return -1;
}
if (source.equals(target)) {
return 0;
}
int m = rules.size();
int[] len = new int[m];
String[] pat = new String[m];
String[] rep = new String[m];
int[] rulesCost = new int[m];
for (int i = 0; i < m; i++) {
String p = rules.get(i).get(0);
String r = rules.get(i).get(1);
pat[i] = p;
rep[i] = r;
len[i] = p.length();
int wild = 0;
for (char c : p.toCharArray()) {
if (c == '*') {
wild++;
}
}
rulesCost[i] = costs[i] + wild;
}
int in = Integer.MAX_VALUE / 2;
int[] dp = new int[n + 1];
Arrays.fill(dp, in);
dp[0] = 0;
for (int i = 0; i < n; i++) {
if (dp[i] == in) {
continue;
}
if (source.charAt(i) == target.charAt(i) && dp[i] < dp[i + 1]) {
dp[i + 1] = dp[i];
}
for (int k = 0; k < m; k++) {
int l = len[k];
if (i + l > n) {
continue;
}
boolean match = true;
for (int j = 0; j < l; j++) {
char pc = pat[k].charAt(j);
char sc = source.charAt(i + j);
if (pc != '*' && pc != sc) {
match = false;
break;
}
}
if (!match) {
continue;
}
if (!target.substring(i, i + l).equals(rep[k])) {
continue;
}
int nd = dp[i] + rulesCost[k];
if (nd < dp[i + l]) {
dp[i + l] = nd;
}
}
}
return dp[n] >= in ? -1 : dp[n];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
3995\. Minimum Cost to Convert String III

Hard

You are given two strings, `source` and `target`.

You are also given a 2D string array `rules`, where <code>rules[i] = [pattern<sub>i</sub>, replacement<sub>i</sub>]</code>, and an integer array `costs`, where `costs[i]` is the base cost of applying `rules[i]`. Both arrays have the same length. Additionally, <code>pattern<sub>i</sub></code> and <code>replacement<sub>i</sub></code> have the same length.

You may apply **any** rule **any** number of times. Each rule application works as follows:

* Choose an index `l` such that the range of positions from `l` to <code>l + pattern<sub>i</sub>.length - 1</code> exists in the current string and **none** of these positions has been used in a previous rule application.
* For each index `j`, the character <code>pattern<sub>i</sub>[j]</code> must either be **equal** to the current character at position `l + j`, or be `'*'`.
* Replace the characters in this range with <code>replacement<sub>i</sub></code>. The replacement is used **exactly** as given and does not contain wildcards.
* The cost of this rule application is `costs[i]` **plus** the number of `'*'` characters in <code>pattern<sub>i</sub></code>.
* Once a character position has been used in a rule application, it **cannot** be used in any **later** rule application.

Since every <code>pattern<sub>i</sub></code> and <code>replacement<sub>i</sub></code> have the same length, character positions are preserved after every rule application.

Return the **minimum** total cost required to transform `source` into `target`. If it is impossible, return -1.

OriginalJava**Example 1:**

**Input:** source = "hello", target = "world", rules = [["he","wo"],["llo","rld"]], costs = [3,4]

**Output:** 7

**Explanation:**

* Apply `rules[0]` to replace `"he"` with `"wo"` at cost 3, so the string becomes `"wollo"`.
* Apply `rules[1]` to replace `"llo"` with `"rld"` at cost 4, so the string becomes `"world"`.
* The total cost is `3 + 4 = 7`.

**Example 2:**

**Input:** source = "cat", target = "dog", rules = [["c\*t","dog"]], costs = [2]

**Output:** 3

**Explanation:**

* Apply `rules[0]` to replace `"cat"` with `"dog"`. The wildcard `'*'` matches `'a'`, adding 1 to the base cost 2.
* The total cost is `2 + 1 = 3`.

**Example 3:**

**Input:** source = "test", target = "next", rules = [["\*e\*t","next"]], costs = [4]

**Output:** 6

**Explanation:**

* Apply `rules[0]` to replace `"test"` with `"next"`. The first wildcard matches `'t'` and the second wildcard matches `'s'`, adding 2 to the base cost 4.
* The total cost is `4 + 2 = 6`.

**Example 4:**

**Input:** source = "ab", target = "bc", rules = [["a\*","bd"]], costs = [9]

**Output:** \-1

**Explanation:**

No sequence of rule applications can transform `source` into `target`, so the answer is -1.

**Constraints:**

* `1 <= source.length == target.length <= 5000`
* `source` and `target` consist of lowercase English letters.
* `1 <= rules.length == costs.length <= 200`
* <code>rules[i] = [pattern<sub>i</sub>, replacement<sub>i</sub>]</code>
* <code>1 <= pattern<sub>i</sub>.length == replacement<sub>i</sub>.length <= 20</code>
* <code>pattern<sub>i</sub></code> contains at least one lowercase English letter and at most 5 `'*'` characters.
* <code>replacement<sub>i</sub></code> contains only lowercase English letters.
* `1 <= costs[i] <= 1000`
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package g3901_4000.s3996_even_number_of_knight_moves;

// #Easy #Array #Math #Mid_Level #Weekly_Contest_511
// #2026_11_17_Time_1_ms_(88.64%)_Space_43.26_MB_(96.70%)

public class Solution {
public boolean canReach(int[] start, int[] target) {
return (start[0] + start[1]) % 2 == (target[0] + target[1]) % 2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
3996\. Even Number of Knight Moves

Easy

You are given two integer arrays `start` and `target`, where each array is of the form `[x, y]` representing a cell on a standard 8 x 8 chessboard.

Return `true` if a knight can move from `start` to `target` in an **even** number of moves. Otherwise, return `false`.

**Note:** A valid knight move consists of moving two squares in one direction and one square perpendicular to it. The figure below illustrates all eight possible moves from a cell.

![](https://assets.leetcode.com/uploads/2018/10/12/knight.png)

OriginalJava**Example 1:**

**Input:** start = [1,1], target = [2,2]

**Output:** true

**Explanation:**

One possible sequence of moves is `(1, 1) -> (3, 2) -> (2, 4) -> (4, 3) -> (2, 2)`.

The knight reaches the target in 4 moves, which is even. Thus, the answer is `true`.

**Example 2:**

**Input:** start = [4,5], target = [6,6]

**Output:** false

**Explanation:**

It is impossible to reach `target = [6, 6]` from `start = [4, 5]` in an even number of moves. Thus, the answer is `false`.

**Constraints:**

* `start.length == target.length == 2`
* `0 <= start[i], target[i] <= 7`
Loading
Loading