Skip to content

Commit 2ee302c

Browse files
authored
Added task 2293.
1 parent 85f5022 commit 2ee302c

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.10'
13651365
| # | Title | Difficulty | Tag | Time, ms | Time, %
13661366
|------|----------------|-------------|-------------|----------|---------
13671367
| 2300 |[Successful Pairs of Spells and Potions](src/main/java/g2201_2300/s2300_successful_pairs_of_spells_and_potions/Solution.java)| Medium | Array, Two_Pointers, Binary_Search, Sorting | 85 | 71.70
1368+
| 2293 |[Min Max Game](src/main/java/g2201_2300/s2293_min_max_game/Solution.java)| Easy | Array, Simulation | 1 | 90.39
13681369
| 2260 |[Minimum Consecutive Cards to Pick Up](src/main/java/g2201_2300/s2260_minimum_consecutive_cards_to_pick_up/Solution.java)| Medium | Array, Hash_Table, Sliding_Window | 50 | 97.04
13691370
| 2259 |[Remove Digit From Number to Maximize Result](src/main/java/g2201_2300/s2259_remove_digit_from_number_to_maximize_result/Solution.java)| Easy | String, Greedy, Enumeration | 1 | 97.73
13701371
| 2257 |[Count Unguarded Cells in the Grid](src/main/java/g2201_2300/s2257_count_unguarded_cells_in_the_grid/Solution.java)| Medium | Array, Matrix, Simulation | 32 | 70.28
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g2201_2300.s2293_min_max_game;
2+
3+
// #Easy #Array #Simulation #2022_06_14_Time_1_ms_(90.39%)_Space_44_MB_(50.37%)
4+
5+
public class Solution {
6+
public int minMaxGame(int[] nums) {
7+
int n = nums.length;
8+
if (n == 1) {
9+
return nums[0];
10+
}
11+
int[] newNums = new int[n / 2];
12+
for (int i = 0; i < n / 2; i++) {
13+
if (i % 2 == 0) {
14+
newNums[i] = Math.min(nums[2 * i], nums[2 * i + 1]);
15+
} else {
16+
newNums[i] = Math.max(nums[2 * i], nums[2 * i + 1]);
17+
}
18+
}
19+
return minMaxGame(newNums);
20+
}
21+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2293\. Min Max Game
2+
3+
Easy
4+
5+
You are given a **0-indexed** integer array `nums` whose length is a power of `2`.
6+
7+
Apply the following algorithm on `nums`:
8+
9+
1. Let `n` be the length of `nums`. If `n == 1`, **end** the process. Otherwise, **create** a new **0-indexed** integer array `newNums` of length `n / 2`.
10+
2. For every **even** index `i` where `0 <= i < n / 2`, **assign** the value of `newNums[i]` as `min(nums[2 * i], nums[2 * i + 1])`.
11+
3. For every **odd** index `i` where `0 <= i < n / 2`, **assign** the value of `newNums[i]` as `max(nums[2 * i], nums[2 * i + 1])`.
12+
4. **Replace** the array `nums` with `newNums`.
13+
5. **Repeat** the entire process starting from step 1.
14+
15+
Return _the last number that remains in_ `nums` _after applying the algorithm._
16+
17+
**Example 1:**
18+
19+
![](https://assets.leetcode.com/uploads/2022/04/13/example1drawio-1.png)
20+
21+
**Input:** nums = [1,3,5,2,4,8,2,2]
22+
23+
**Output:** 1
24+
25+
**Explanation:** The following arrays are the results of applying the algorithm repeatedly.
26+
27+
First: nums = [1,5,4,2]
28+
29+
Second: nums = [1,4]
30+
31+
Third: nums = [1]
32+
33+
1 is the last remaining number, so we return 1.
34+
35+
**Example 2:**
36+
37+
**Input:** nums = [3]
38+
39+
**Output:** 3
40+
41+
**Explanation:** 3 is already the last remaining number, so we return 3.
42+
43+
**Constraints:**
44+
45+
* `1 <= nums.length <= 1024`
46+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
47+
* `nums.length` is a power of `2`.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2201_2300.s2293_min_max_game;
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 minMaxGame() {
11+
assertThat(new Solution().minMaxGame(new int[] {1, 3, 5, 2, 4, 8, 2, 2}), equalTo(1));
12+
}
13+
14+
@Test
15+
void minMaxGame2() {
16+
assertThat(new Solution().minMaxGame(new int[] {3}), equalTo(3));
17+
}
18+
}

0 commit comments

Comments
 (0)