Skip to content

Commit 5584291

Browse files
authored
Added tasks 3280-3283
1 parent 205b41b commit 5584291

File tree

12 files changed

+444
-0
lines changed

12 files changed

+444
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3201_3300.s3280_convert_date_to_binary
2+
3+
// #Easy #String #Math #2024_09_11_Time_174_ms_(79.31%)_Space_36.2_MB_(82.76%)
4+
5+
class Solution {
6+
fun convertDateToBinary(dat: String): String {
7+
val str = StringBuilder()
8+
val res = StringBuilder()
9+
for (c in dat.toCharArray()) {
10+
if (c.isDigit()) {
11+
str.append(c)
12+
} else if (c == '-') {
13+
res.append(str.toString().toInt().toString(2))
14+
res.append('-')
15+
str.setLength(0)
16+
}
17+
}
18+
if (str.isNotEmpty()) {
19+
res.append(str.toString().toInt().toString(2))
20+
}
21+
return res.toString()
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
3280\. Convert Date to Binary
2+
3+
Easy
4+
5+
You are given a string `date` representing a Gregorian calendar date in the `yyyy-mm-dd` format.
6+
7+
`date` can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in `year-month-day` format.
8+
9+
Return the **binary** representation of `date`.
10+
11+
**Example 1:**
12+
13+
**Input:** date = "2080-02-29"
14+
15+
**Output:** "100000100000-10-11101"
16+
17+
**Explanation:**
18+
19+
100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.
20+
21+
**Example 2:**
22+
23+
**Input:** date = "1900-01-01"
24+
25+
**Output:** "11101101100-1-1"
26+
27+
**Explanation:**
28+
29+
11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.
30+
31+
**Constraints:**
32+
33+
* `date.length == 10`
34+
* `date[4] == date[7] == '-'`, and all other `date[i]`'s are digits.
35+
* The input is generated such that `date` represents a valid Gregorian calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2100 (both inclusive).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g3201_3300.s3281_maximize_score_of_numbers_in_ranges
2+
3+
// #Medium #Array #Sorting #Greedy #Binary_Search
4+
// #2024_09_11_Time_710_ms_(88.24%)_Space_80.7_MB_(5.88%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun maxPossibleScore(start: IntArray, d: Int): Int {
10+
start.sort()
11+
val n = start.size
12+
var l = 0
13+
var r = start[n - 1] - start[0] + d + 1
14+
while (l < r) {
15+
val m = l + (r - l) / 2
16+
if (isPossible(start, d, m)) {
17+
l = m + 1
18+
} else {
19+
r = m
20+
}
21+
}
22+
return l - 1
23+
}
24+
25+
private fun isPossible(start: IntArray, d: Int, score: Int): Boolean {
26+
var pre = start[0]
27+
for (i in 1 until start.size) {
28+
if (start[i] + d - pre < score) {
29+
return false
30+
}
31+
pre = max(start[i], (pre + score))
32+
}
33+
return true
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
3281\. Maximize Score of Numbers in Ranges
2+
3+
Medium
4+
5+
You are given an array of integers `start` and an integer `d`, representing `n` intervals `[start[i], start[i] + d]`.
6+
7+
You are asked to choose `n` integers where the <code>i<sup>th</sup></code> integer must belong to the <code>i<sup>th</sup></code> interval. The **score** of the chosen integers is defined as the **minimum** absolute difference between any two integers that have been chosen.
8+
9+
Return the **maximum** _possible score_ of the chosen integers.
10+
11+
**Example 1:**
12+
13+
**Input:** start = [6,0,3], d = 2
14+
15+
**Output:** 4
16+
17+
**Explanation:**
18+
19+
The maximum possible score can be obtained by choosing integers: 8, 0, and 4. The score of these chosen integers is `min(|8 - 0|, |8 - 4|, |0 - 4|)` which equals 4.
20+
21+
**Example 2:**
22+
23+
**Input:** start = [2,6,13,13], d = 5
24+
25+
**Output:** 5
26+
27+
**Explanation:**
28+
29+
The maximum possible score can be obtained by choosing integers: 2, 7, 13, and 18. The score of these chosen integers is `min(|2 - 7|, |2 - 13|, |2 - 18|, |7 - 13|, |7 - 18|, |13 - 18|)` which equals 5.
30+
31+
**Constraints:**
32+
33+
* <code>2 <= start.length <= 10<sup>5</sup></code>
34+
* <code>0 <= start[i] <= 10<sup>9</sup></code>
35+
* <code>0 <= d <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g3201_3300.s3282_reach_end_of_array_with_max_score
2+
3+
// #Medium #Array #Greedy #2024_09_11_Time_789_ms_(90.91%)_Space_77.1_MB_(36.36%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun findMaximumScore(nums: List<Int>): Long {
9+
var res: Long = 0
10+
var ma: Long = 0
11+
for (num in nums) {
12+
res += ma
13+
ma = max(ma, num.toLong())
14+
}
15+
return res
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
3282\. Reach End of Array With Max Score
2+
3+
Medium
4+
5+
You are given an integer array `nums` of length `n`.
6+
7+
Your goal is to start at index `0` and reach index `n - 1`. You can only jump to indices **greater** than your current index.
8+
9+
The score for a jump from index `i` to index `j` is calculated as `(j - i) * nums[i]`.
10+
11+
Return the **maximum** possible **total score** by the time you reach the last index.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,3,1,5]
16+
17+
**Output:** 7
18+
19+
**Explanation:**
20+
21+
First, jump to index 1 and then jump to the last index. The final score is `1 * 1 + 2 * 3 = 7`.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [4,3,1,3,2]
26+
27+
**Output:** 16
28+
29+
**Explanation:**
30+
31+
Jump directly to the last index. The final score is `4 * 4 = 16`.
32+
33+
**Constraints:**
34+
35+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
36+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package g3201_3300.s3283_maximum_number_of_moves_to_kill_all_pawns
2+
3+
// #Hard #Array #Math #Breadth_First_Search #Bit_Manipulation #Bitmask #Game_Theory
4+
// #2024_09_11_Time_638_ms_(100.00%)_Space_62.2_MB_(87.50%)
5+
6+
import java.util.LinkedList
7+
import java.util.Queue
8+
import kotlin.math.max
9+
import kotlin.math.min
10+
11+
class Solution {
12+
private lateinit var distances: Array<IntArray>
13+
private lateinit var memo: Array<Array<Int?>?>
14+
15+
fun maxMoves(kx: Int, ky: Int, positions: Array<IntArray>): Int {
16+
val n = positions.size
17+
distances = Array<IntArray>(n + 1) { IntArray(n + 1) { 0 } }
18+
memo = Array<Array<Int?>?>(n + 1) { arrayOfNulls<Int>(1 shl n) }
19+
// Calculate distances between all pairs of positions (including knight's initial position)
20+
for (i in 0 until n) {
21+
distances[n][i] = calculateMoves(kx, ky, positions[i][0], positions[i][1])
22+
for (j in i + 1 until n) {
23+
val dist =
24+
calculateMoves(
25+
positions[i][0], positions[i][1], positions[j][0], positions[j][1]
26+
)
27+
distances[j][i] = dist
28+
distances[i][j] = distances[j][i]
29+
}
30+
}
31+
return minimax(n, (1 shl n) - 1, true)
32+
}
33+
34+
private fun minimax(lastPos: Int, remainingPawns: Int, isAlice: Boolean): Int {
35+
if (remainingPawns == 0) {
36+
return 0
37+
}
38+
if (memo[lastPos]!![remainingPawns] != null) {
39+
return memo[lastPos]!![remainingPawns]!!
40+
}
41+
var result = if (isAlice) 0 else Int.Companion.MAX_VALUE
42+
for (i in 0 until distances.size - 1) {
43+
if ((remainingPawns and (1 shl i)) != 0) {
44+
val newRemainingPawns = remainingPawns and (1 shl i).inv()
45+
val moveValue = distances[lastPos][i] + minimax(i, newRemainingPawns, !isAlice)
46+
result = if (isAlice) {
47+
max(result, moveValue)
48+
} else {
49+
min(result, moveValue)
50+
}
51+
}
52+
}
53+
memo[lastPos]!![remainingPawns] = result
54+
return result
55+
}
56+
57+
private fun calculateMoves(x1: Int, y1: Int, x2: Int, y2: Int): Int {
58+
if (x1 == x2 && y1 == y2) {
59+
return 0
60+
}
61+
val visited = Array<BooleanArray?>(50) { BooleanArray(50) }
62+
val queue: Queue<IntArray> = LinkedList<IntArray>()
63+
queue.offer(intArrayOf(x1, y1, 0))
64+
visited[x1]!![y1] = true
65+
while (queue.isNotEmpty()) {
66+
val current = queue.poll()
67+
val x = current[0]
68+
val y = current[1]
69+
val moves = current[2]
70+
for (move in KNIGHT_MOVES) {
71+
val nx = x + move[0]
72+
val ny = y + move[1]
73+
if (nx == x2 && ny == y2) {
74+
return moves + 1
75+
}
76+
if (nx >= 0 && nx < 50 && ny >= 0 && ny < 50 && !visited[nx]!![ny]) {
77+
queue.offer(intArrayOf(nx, ny, moves + 1))
78+
visited[nx]!![ny] = true
79+
}
80+
}
81+
}
82+
// Should never reach here if input is valid
83+
return -1
84+
}
85+
86+
companion object {
87+
private val KNIGHT_MOVES = arrayOf<IntArray>(
88+
intArrayOf(-2, -1), intArrayOf(-2, 1), intArrayOf(-1, -2), intArrayOf(-1, 2),
89+
intArrayOf(1, -2), intArrayOf(1, 2), intArrayOf(2, -1), intArrayOf(2, 1)
90+
)
91+
}
92+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
3283\. Maximum Number of Moves to Kill All Pawns
2+
3+
Hard
4+
5+
There is a `50 x 50` chessboard with **one** knight and some pawns on it. You are given two integers `kx` and `ky` where `(kx, ky)` denotes the position of the knight, and a 2D array `positions` where <code>positions[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> denotes the position of the pawns on the chessboard.
6+
7+
Alice and Bob play a _turn-based_ game, where Alice goes first. In each player's turn:
8+
9+
* The player _selects_ a pawn that still exists on the board and captures it with the knight in the **fewest** possible **moves**. **Note** that the player can select **any** pawn, it **might not** be one that can be captured in the **least** number of moves.
10+
* In the process of capturing the _selected_ pawn, the knight **may** pass other pawns **without** capturing them. **Only** the _selected_ pawn can be captured in _this_ turn.
11+
12+
Alice is trying to **maximize** the **sum** of the number of moves made by _both_ players until there are no more pawns on the board, whereas Bob tries to **minimize** them.
13+
14+
Return the **maximum** _total_ number of moves made during the game that Alice can achieve, assuming both players play **optimally**.
15+
16+
Note that in one **move,** a chess knight has eight possible positions it can move to, as illustrated below. Each move is two cells in a cardinal direction, then one cell in an orthogonal direction.
17+
18+
![](https://assets.leetcode.com/uploads/2024/08/01/chess_knight.jpg)
19+
20+
**Example 1:**
21+
22+
**Input:** kx = 1, ky = 1, positions = [[0,0]]
23+
24+
**Output:** 4
25+
26+
**Explanation:**
27+
28+
![](https://assets.leetcode.com/uploads/2024/08/16/gif3.gif)
29+
30+
The knight takes 4 moves to reach the pawn at `(0, 0)`.
31+
32+
**Example 2:**
33+
34+
**Input:** kx = 0, ky = 2, positions = [[1,1],[2,2],[3,3]]
35+
36+
**Output:** 8
37+
38+
**Explanation:**
39+
40+
**![](https://assets.leetcode.com/uploads/2024/08/16/gif4.gif)**
41+
42+
* Alice picks the pawn at `(2, 2)` and captures it in two moves: `(0, 2) -> (1, 4) -> (2, 2)`.
43+
* Bob picks the pawn at `(3, 3)` and captures it in two moves: `(2, 2) -> (4, 1) -> (3, 3)`.
44+
* Alice picks the pawn at `(1, 1)` and captures it in four moves: `(3, 3) -> (4, 1) -> (2, 2) -> (0, 3) -> (1, 1)`.
45+
46+
**Example 3:**
47+
48+
**Input:** kx = 0, ky = 0, positions = [[1,2],[2,4]]
49+
50+
**Output:** 3
51+
52+
**Explanation:**
53+
54+
* Alice picks the pawn at `(2, 4)` and captures it in two moves: `(0, 0) -> (1, 2) -> (2, 4)`. Note that the pawn at `(1, 2)` is not captured.
55+
* Bob picks the pawn at `(1, 2)` and captures it in one move: `(2, 4) -> (1, 2)`.
56+
57+
**Constraints:**
58+
59+
* `0 <= kx, ky <= 49`
60+
* `1 <= positions.length <= 15`
61+
* `positions[i].length == 2`
62+
* `0 <= positions[i][0], positions[i][1] <= 49`
63+
* All `positions[i]` are unique.
64+
* The input is generated such that `positions[i] != [kx, ky]` for all `0 <= i < positions.length`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g3201_3300.s3280_convert_date_to_binary
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun convertDateToBinary() {
10+
assertThat<String?>(
11+
Solution().convertDateToBinary("2080-02-29"), equalTo<String?>("100000100000-10-11101")
12+
)
13+
}
14+
15+
@Test
16+
fun convertDateToBinary2() {
17+
assertThat<String?>(
18+
Solution().convertDateToBinary("1900-01-01"),
19+
equalTo<String?>("11101101100-1-1")
20+
)
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3201_3300.s3281_maximize_score_of_numbers_in_ranges
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun maxPossibleScore() {
10+
assertThat<Int?>(
11+
Solution().maxPossibleScore(intArrayOf(6, 0, 3), 2),
12+
equalTo<Int?>(4)
13+
)
14+
}
15+
16+
@Test
17+
fun maxPossibleScore2() {
18+
assertThat<Int?>(
19+
Solution().maxPossibleScore(intArrayOf(2, 6, 13, 13), 5),
20+
equalTo<Int?>(5)
21+
)
22+
}
23+
}

0 commit comments

Comments
 (0)