Skip to content

Commit 6468fc0

Browse files
authored
Added tasks 3020-3046
1 parent a6d2b23 commit 6468fc0

File tree

64 files changed

+2344
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2344
-1
lines changed

src/main/kotlin/g2801_2900/s2815_max_pair_sum_in_an_array/Solution.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Solution {
2323
continue
2424
}
2525
val sum = value.poll() + value.poll()
26-
maxSum = max(maxSum.toDouble(), sum.toDouble()).toInt()
26+
maxSum = max(maxSum, sum)
2727
}
2828
return maxSum
2929
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g3001_3100.s3020_find_the_maximum_number_of_elements_in_subset
2+
3+
// #Medium #Array #Hash_Table #Enumeration #2024_03_03_Time_626_ms_(82.22%)_Space_57.8_MB_(80.00%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun maximumLength(nums: IntArray): Int {
9+
return withHashMap(nums)
10+
}
11+
12+
private fun withHashMap(nums: IntArray): Int {
13+
val map: MutableMap<Int, Int> = HashMap()
14+
for (i in nums) {
15+
map[i] = map.getOrDefault(i, 0) + 1
16+
}
17+
var ans = 0
18+
if (map.containsKey(1)) {
19+
ans = if (map[1]!! % 2 == 0) {
20+
map[1]!! - 1
21+
} else {
22+
map[1]!!
23+
}
24+
}
25+
for (key in map.keys) {
26+
if (key == 1) {
27+
continue
28+
}
29+
val len = findSeries(map, key)
30+
ans = max(ans, len)
31+
}
32+
return ans
33+
}
34+
35+
private fun findSeries(map: Map<Int, Int>, key: Int): Int {
36+
val sqr = key * key
37+
return if (map.containsKey(sqr)) {
38+
if (map[key]!! >= 2) {
39+
2 + findSeries(map, sqr)
40+
} else {
41+
1
42+
}
43+
} else {
44+
1
45+
}
46+
}
47+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
3020\. Find the Maximum Number of Elements in Subset
2+
3+
Medium
4+
5+
You are given an array of **positive** integers `nums`.
6+
7+
You need to select a subset of `nums` which satisfies the following condition:
8+
9+
* You can place the selected elements in a **0-indexed** array such that it follows the pattern: <code>[x, x<sup>2</sup>, x<sup>4</sup>, ..., x<sup>k/2</sup>, x<sup>k</sup>, x<sup>k/2</sup>, ..., x<sup>4</sup>, x<sup>2</sup>, x]</code> (**Note** that `k` can be be any **non-negative** power of `2`). For example, `[2, 4, 16, 4, 2]` and `[3, 9, 3]` follow the pattern while `[2, 4, 8, 4, 2]` does not.
10+
11+
Return _the **maximum** number of elements in a subset that satisfies these conditions._
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [5,4,1,2,2]
16+
17+
**Output:** 3
18+
19+
**Explanation:** We can select the subset {4,2,2}, which can be placed in the array as [2,4,2] which follows the pattern and 2<sup>2</sup> == 4. Hence the answer is 3.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [1,3,2,4]
24+
25+
**Output:** 1
26+
27+
**Explanation:** We can select the subset {1}, which can be placed in the array as [1] which follows the pattern. Hence the answer is 1. Note that we could have also selected the subsets {2}, {3}, or {4}, there may be multiple subsets which provide the same answer.
28+
29+
**Constraints:**
30+
31+
* <code>2 <= nums.length <= 10<sup>5</sup></code>
32+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package g3001_3100.s3021_alice_and_bob_playing_flower_game
2+
3+
// #Medium #Math #2024_03_03_Time_141_ms_(43.24%)_Space_33.2_MB_(86.49%)
4+
5+
class Solution {
6+
fun flowerGame(n: Int, m: Int): Long {
7+
val nEven = n.toLong() / 2
8+
val nOdd = n - nEven
9+
val mEven = m.toLong() / 2
10+
val mOdd = m - mEven
11+
return nEven * mOdd + nOdd * mEven
12+
}
13+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
3021\. Alice and Bob Playing Flower Game
2+
3+
Medium
4+
5+
Alice and Bob are playing a turn-based game on a circular field surrounded by flowers. The circle represents the field, and there are `x` flowers in the clockwise direction between Alice and Bob, and `y` flowers in the anti-clockwise direction between them.
6+
7+
The game proceeds as follows:
8+
9+
1. Alice takes the first turn.
10+
2. In each turn, a player must choose either the clockwise or anti-clockwise direction and pick one flower from that side.
11+
3. At the end of the turn, if there are no flowers left at all, the **current** player captures their opponent and wins the game.
12+
13+
Given two integers, `n` and `m`, the task is to compute the number of possible pairs `(x, y)` that satisfy the conditions:
14+
15+
* Alice must win the game according to the described rules.
16+
* The number of flowers `x` in the clockwise direction must be in the range `[1,n]`.
17+
* The number of flowers `y` in the anti-clockwise direction must be in the range `[1,m]`.
18+
19+
Return _the number of possible pairs_ `(x, y)` _that satisfy the conditions mentioned in the statement_.
20+
21+
**Example 1:**
22+
23+
**Input:** n = 3, m = 2
24+
25+
**Output:** 3
26+
27+
**Explanation:** The following pairs satisfy conditions described in the statement: (1,2), (3,2), (2,1).
28+
29+
**Example 2:**
30+
31+
**Input:** n = 1, m = 1
32+
33+
**Output:** 0
34+
35+
**Explanation:** No pairs satisfy the conditions described in the statement.
36+
37+
**Constraints:**
38+
39+
* <code>1 <= n, m <= 10<sup>5</sup></code>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g3001_3100.s3022_minimize_or_of_remaining_elements_using_operations
2+
3+
// #Hard #Array #Greedy #Bit_Manipulation #2024_03_03_Time_516_ms_(77.78%)_Space_62.7_MB_(100.00%)
4+
5+
class Solution {
6+
fun minOrAfterOperations(nums: IntArray, k: Int): Int {
7+
var ans = 0
8+
var mask = 0
9+
for (j in 30 downTo 0) {
10+
mask = mask or (1 shl j)
11+
var consecutiveAnd = mask
12+
var mergeCount = 0
13+
for (i in nums) {
14+
consecutiveAnd = consecutiveAnd and i
15+
if ((consecutiveAnd or ans) != ans) {
16+
mergeCount++
17+
} else {
18+
consecutiveAnd = mask
19+
}
20+
}
21+
if (mergeCount > k) {
22+
ans = ans or (1 shl j)
23+
}
24+
}
25+
return ans
26+
}
27+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
3022\. Minimize OR of Remaining Elements Using Operations
2+
3+
Hard
4+
5+
You are given a **0-indexed** integer array `nums` and an integer `k`.
6+
7+
In one operation, you can pick any index `i` of `nums` such that `0 <= i < nums.length - 1` and replace `nums[i]` and `nums[i + 1]` with a single occurrence of `nums[i] & nums[i + 1]`, where `&` represents the bitwise `AND` operator.
8+
9+
Return _the **minimum** possible value of the bitwise_ `OR` _of the remaining elements of_ `nums` _after applying **at most**_ `k` _operations_.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [3,5,3,2,7], k = 2
14+
15+
**Output:** 3
16+
17+
**Explanation:** Let's do the following operations:
18+
1. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [1,3,2,7].
19+
2. Replace nums[2] and nums[3] with (nums[2] & nums[3]) so that nums becomes equal to [1,3,2].
20+
21+
The bitwise-or of the final array is 3.
22+
23+
It can be shown that 3 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [7,3,15,14,2,8], k = 4
28+
29+
**Output:** 2
30+
31+
**Explanation:** Let's do the following operations:
32+
1. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [3,15,14,2,8].
33+
2. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [3,14,2,8].
34+
3. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [2,2,8].
35+
4. Replace nums[1] and nums[2] with (nums[1] & nums[2]) so that nums becomes equal to [2,0].
36+
37+
The bitwise-or of the final array is 2.
38+
39+
It can be shown that 2 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.
40+
41+
**Example 3:**
42+
43+
**Input:** nums = [10,7,10,3,9,14,9,4], k = 1
44+
45+
**Output:** 15
46+
47+
**Explanation:** Without applying any operations, the bitwise-or of nums is 15. It can be shown that 15 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.
48+
49+
**Constraints:**
50+
51+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
52+
* <code>0 <= nums[i] < 2<sup>30</sup></code>
53+
* `0 <= k < nums.length`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3001_3100.s3024_type_of_triangle
2+
3+
// #Easy #Array #Math #Sorting #2024_03_03_Time_163_ms_(81.03%)_Space_34.7_MB_(93.10%)
4+
5+
class Solution {
6+
fun triangleType(nums: IntArray): String {
7+
if (nums[0] + nums[1] > nums[2] && nums[1] + nums[2] > nums[0] && nums[2] + nums[0] > nums[1]) {
8+
return if (nums[0] == nums[1] && nums[1] == nums[2]) {
9+
"equilateral"
10+
} else if (nums[0] == nums[1] || nums[0] == nums[2] || nums[2] == nums[1]) {
11+
"isosceles"
12+
} else {
13+
"scalene"
14+
}
15+
}
16+
return "none"
17+
}
18+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
3024\. Type of Triangle
2+
3+
Easy
4+
5+
You are given a **0-indexed** integer array `nums` of size `3` which can form the sides of a triangle.
6+
7+
* A triangle is called **equilateral** if it has all sides of equal length.
8+
* A triangle is called **isosceles** if it has exactly two sides of equal length.
9+
* A triangle is called **scalene** if all its sides are of different lengths.
10+
11+
Return _a string representing_ _the type of triangle that can be formed_ _or_ `"none"` _if it **cannot** form a triangle._
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [3,3,3]
16+
17+
**Output:** "equilateral"
18+
19+
**Explanation:** Since all the sides are of equal length, therefore, it will form an equilateral triangle.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [3,4,5]
24+
25+
**Output:** "scalene"
26+
27+
**Explanation:**
28+
29+
nums[0] + nums[1] = 3 + 4 = 7, which is greater than nums[2] = 5.
30+
31+
nums[0] + nums[2] = 3 + 5 = 8, which is greater than nums[1] = 4.
32+
33+
nums[1] + nums[2] = 4 + 5 = 9, which is greater than nums[0] = 3.
34+
35+
Since the sum of the two sides is greater than the third side for all three cases, therefore, it can form a triangle.
36+
37+
As all the sides are of different lengths, it will form a scalene triangle.
38+
39+
**Constraints:**
40+
41+
* `nums.length == 3`
42+
* `1 <= nums[i] <= 100`
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g3001_3100.s3025_find_the_number_of_ways_to_place_people_i
2+
3+
// #Medium #Array #Math #Sorting #Enumeration #Geometry
4+
// #2024_03_03_Time_252_ms_(44.12%)_Space_44.1_MB_(73.53%)
5+
6+
class Solution {
7+
fun numberOfPairs(points: Array<IntArray>): Int {
8+
points.sortWith { a: IntArray, b: IntArray -> if (a[0] == b[0]) b[1] - a[1] else a[0] - b[0] }
9+
var cnt = 0
10+
for (i in points.indices) {
11+
var bot = Int.MIN_VALUE
12+
var top = points[i][1]
13+
for (j in i + 1 until points.size) {
14+
val y1 = points[j][1]
15+
if (y1 <= top && y1 > bot) {
16+
cnt++
17+
bot = y1
18+
if (y1 == top) {
19+
top--
20+
}
21+
}
22+
}
23+
}
24+
return cnt
25+
}
26+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
3025\. Find the Number of Ways to Place People I
2+
3+
Medium
4+
5+
You are given a 2D array `points` of size `n x 2` representing integer coordinates of some points on a 2D-plane, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>.
6+
7+
We define the **right** direction as positive x-axis (**increasing x-coordinate**) and the **left** direction as negative x-axis (**decreasing x-coordinate**). Similarly, we define the **up** direction as positive y-axis (**increasing y-coordinate**) and the **down** direction as negative y-axis (**decreasing y-coordinate**)
8+
9+
You have to place `n` people, including Alice and Bob, at these points such that there is **exactly one** person at every point. Alice wants to be alone with Bob, so Alice will build a rectangular fence with Alice's position as the **upper left corner** and Bob's position as the **lower right corner** of the fence (**Note** that the fence **might not** enclose any area, i.e. it can be a line). If any person other than Alice and Bob is either **inside** the fence or **on** the fence, Alice will be sad.
10+
11+
Return _the number of **pairs of points** where you can place Alice and Bob, such that Alice **does not** become sad on building the fence_.
12+
13+
**Note** that Alice can only build a fence with Alice's position as the upper left corner, and Bob's position as the lower right corner. For example, Alice cannot build either of the fences in the picture below with four corners `(1, 1)`, `(1, 3)`, `(3, 1)`, and `(3, 3)`, because:
14+
15+
* With Alice at `(3, 3)` and Bob at `(1, 1)`, Alice's position is not the upper left corner and Bob's position is not the lower right corner of the fence.
16+
* With Alice at `(1, 3)` and Bob at `(1, 1)`, Bob's position is not the lower right corner of the fence.
17+
18+
![](https://assets.leetcode.com/uploads/2024/01/04/example0alicebob-1.png)
19+
20+
**Example 1:**
21+
22+
![](https://assets.leetcode.com/uploads/2024/01/04/example1alicebob.png)
23+
24+
**Input:** points = [[1,1],[2,2],[3,3]]
25+
26+
**Output:** 0
27+
28+
**Explanation:** There is no way to place Alice and Bob such that Alice can build a fence with Alice's position as the upper left corner and Bob's position as the lower right corner. Hence we return 0.
29+
30+
**Example 2:**
31+
32+
![](https://assets.leetcode.com/uploads/2024/02/04/example2alicebob.png)
33+
34+
**Input:** points = [[6,2],[4,4],[2,6]]
35+
36+
**Output:** 2
37+
38+
**Explanation:** There are two ways to place Alice and Bob such that Alice will not be sad:
39+
40+
- Place Alice at (4, 4) and Bob at (6, 2).
41+
42+
- Place Alice at (2, 6) and Bob at (4, 4).
43+
44+
You cannot place Alice at (2, 6) and Bob at (6, 2) because the person at (4, 4) will be inside the fence.
45+
46+
**Example 3:**
47+
48+
![](https://assets.leetcode.com/uploads/2024/02/04/example4alicebob.png)
49+
50+
**Input:** points = [[3,1],[1,3],[1,1]]
51+
52+
**Output:** 2
53+
54+
**Explanation:** There are two ways to place Alice and Bob such that Alice will not be sad:
55+
56+
- Place Alice at (1, 1) and Bob at (3, 1).
57+
58+
- Place Alice at (1, 3) and Bob at (1, 1).
59+
60+
You cannot place Alice at (1, 3) and Bob at (3, 1) because the person at (1, 1) will be on the fence.
61+
62+
Note that it does not matter if the fence encloses any area, the first and second fences in the image are valid.
63+
64+
**Constraints:**
65+
66+
* `2 <= n <= 50`
67+
* `points[i].length == 2`
68+
* `0 <= points[i][0], points[i][1] <= 50`
69+
* All `points[i]` are distinct.

0 commit comments

Comments
 (0)