Skip to content

Commit 32d0db1

Browse files
authored
Added tasks 2857-2900
1 parent d66fea3 commit 32d0db1

File tree

64 files changed

+2310
-35
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

+2310
-35
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package g2801_2900.s2829_determine_the_minimum_sum_of_a_k_avoiding_array
22

33
// #Medium #Math #Greedy #2023_12_18_Time_162_ms_(75.00%)_Space_34.7_MB_(75.00%)
44

5+
@Suppress("NAME_SHADOWING")
56
class Solution {
67
fun minimumSum(n: Int, k: Int): Int {
78
var k = k

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package g2801_2900.s2842_count_k_subsequences_of_a_string_with_maximum_beauty
33
// #Hard #String #Hash_Table #Math #Greedy #Combinatorics
44
// #2023_12_18_Time_217_ms_(100.00%)_Space_43.7_MB_(25.00%)
55

6+
@Suppress("NAME_SHADOWING")
67
class Solution {
78
fun countKSubsequencesWithMaxBeauty(s: String, k: Int): Int {
89
var k = k
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g2801_2900.s2857_count_pairs_of_points_with_distance_k
2+
3+
// #Medium #Array #Hash_Table #Bit_Manipulation
4+
// #2023_12_21_Time_1212_ms_(100.00%)_Space_81.3_MB_(100.00%)
5+
6+
class Solution {
7+
fun countPairs(coordinates: List<List<Int>>, k: Int): Int {
8+
var ans = 0
9+
val map: MutableMap<Long, Int> = HashMap()
10+
for (p in coordinates) {
11+
val p0 = p[0]
12+
val p1 = p[1]
13+
for (i in 0..k) {
14+
val x1 = i xor p0
15+
val y1 = (k - i) xor p1
16+
val key2 = hash(x1, y1)
17+
if (map.containsKey(key2)) {
18+
ans += map[key2]!!
19+
}
20+
}
21+
val key = hash(p0, p1)
22+
map[key] = map.getOrDefault(key, 0) + 1
23+
}
24+
return ans
25+
}
26+
27+
private fun hash(x1: Int, y1: Int): Long {
28+
val r = 1e8.toLong()
29+
return x1 * r + y1
30+
}
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2857\. Count Pairs of Points With Distance k
2+
3+
Medium
4+
5+
You are given a **2D** integer array `coordinates` and an integer `k`, where <code>coordinates[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> are the coordinates of the <code>i<sup>th</sup></code> point in a 2D plane.
6+
7+
We define the **distance** between two points <code>(x<sub>1</sub>, y<sub>1</sub>)</code> and <code>(x<sub>2</sub>, y<sub>2</sub>)</code> as `(x1 XOR x2) + (y1 XOR y2)` where `XOR` is the bitwise `XOR` operation.
8+
9+
Return _the number of pairs_ `(i, j)` _such that_ `i < j` _and the distance between points_ `i` _and_ `j` _is equal to_ `k`.
10+
11+
**Example 1:**
12+
13+
**Input:** coordinates = [[1,2],[4,2],[1,3],[5,2]], k = 5
14+
15+
**Output:** 2
16+
17+
**Explanation:** We can choose the following pairs:
18+
- (0,1): Because we have (1 XOR 4) + (2 XOR 2) = 5.
19+
- (2,3): Because we have (1 XOR 5) + (3 XOR 2) = 5.
20+
21+
**Example 2:**
22+
23+
**Input:** coordinates = [[1,3],[1,3],[1,3],[1,3],[1,3]], k = 0
24+
25+
**Output:** 10
26+
27+
**Explanation:** Any two chosen pairs will have a distance of 0. There are 10 ways to choose two pairs.
28+
29+
**Constraints:**
30+
31+
* `2 <= coordinates.length <= 50000`
32+
* <code>0 <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>6</sup></code>
33+
* `0 <= k <= 100`
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package g2801_2900.s2858_minimum_edge_reversals_so_every_node_is_reachable
2+
3+
// #Hard #Dynamic_Programming #Depth_First_Search #Breadth_First_Search #Graph
4+
// #2023_12_21_Time_1161_ms_(100.00%)_Space_139.8_MB_(100.00%)
5+
6+
import java.util.LinkedList
7+
import java.util.Queue
8+
9+
class Solution {
10+
fun minEdgeReversals(n: Int, edges: Array<IntArray>): IntArray {
11+
val nexts: Array<MutableList<IntArray>> = Array(n) { ArrayList() }
12+
for (edge in edges) {
13+
val u = edge[0]
14+
val v = edge[1]
15+
nexts[u].add(intArrayOf(1, v))
16+
nexts[v].add(intArrayOf(-1, u))
17+
}
18+
val res = IntArray(n)
19+
for (i in 0 until n) {
20+
res[i] = -1
21+
}
22+
res[0] = dfs(nexts, 0, -1)
23+
val queue: Queue<Int> = LinkedList()
24+
queue.add(0)
25+
while (queue.isNotEmpty()) {
26+
val index = queue.remove()
27+
val `val` = res[index]
28+
val next: List<IntArray> = nexts[index]
29+
for (node in next) {
30+
if (res[node[1]] == -1) {
31+
if (node[0] == 1) {
32+
res[node[1]] = `val` + 1
33+
} else {
34+
res[node[1]] = `val` - 1
35+
}
36+
queue.add(node[1])
37+
}
38+
}
39+
}
40+
return res
41+
}
42+
43+
private fun dfs(nexts: Array<MutableList<IntArray>>, index: Int, pre: Int): Int {
44+
var res = 0
45+
val next: List<IntArray> = nexts[index]
46+
for (node in next) {
47+
if (node[1] != pre) {
48+
if (node[0] == -1) {
49+
res++
50+
}
51+
res += dfs(nexts, node[1], index)
52+
}
53+
}
54+
return res
55+
}
56+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2858\. Minimum Edge Reversals So Every Node Is Reachable
2+
3+
Hard
4+
5+
There is a **simple directed graph** with `n` nodes labeled from `0` to `n - 1`. The graph would form a **tree** if its edges were bi-directional.
6+
7+
You are given an integer `n` and a **2D** integer array `edges`, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> represents a **directed edge** going from node <code>u<sub>i</sub></code> to node <code>v<sub>i</sub></code>.
8+
9+
An **edge reversal** changes the direction of an edge, i.e., a directed edge going from node <code>u<sub>i</sub></code> to node <code>v<sub>i</sub></code> becomes a directed edge going from node <code>v<sub>i</sub></code> to node <code>u<sub>i</sub></code>.
10+
11+
For every node `i` in the range `[0, n - 1]`, your task is to **independently** calculate the **minimum** number of **edge reversals** required so it is possible to reach any other node starting from node `i` through a **sequence** of **directed edges**.
12+
13+
Return _an integer array_ `answer`_, where_ `answer[i]` _is the_ _**minimum** number of **edge reversals** required so it is possible to reach any other node starting from node_ `i` _through a **sequence** of **directed edges**._
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2023/08/26/image-20230826221104-3.png)
18+
19+
**Input:** n = 4, edges = [[2,0],[2,1],[1,3]]
20+
21+
**Output:** [1,1,0,2]
22+
23+
**Explanation:** The image above shows the graph formed by the edges.
24+
25+
For node 0: after reversing the edge [2,0], it is possible to reach any other node starting from node 0.
26+
27+
So, answer[0] = 1.
28+
29+
For node 1: after reversing the edge [2,1], it is possible to reach any other node starting from node 1.
30+
31+
So, answer[1] = 1.
32+
33+
For node 2: it is already possible to reach any other node starting from node 2.
34+
35+
So, answer[2] = 0.
36+
37+
For node 3: after reversing the edges [1,3] and [2,1], it is possible to reach any other node starting from node 3.
38+
39+
So, answer[3] = 2.
40+
41+
**Example 2:**
42+
43+
![](https://assets.leetcode.com/uploads/2023/08/26/image-20230826225541-2.png)
44+
45+
**Input:** n = 3, edges = [[1,2],[2,0]]
46+
47+
**Output:** [2,0,1]
48+
49+
**Explanation:** The image above shows the graph formed by the edges.
50+
51+
For node 0: after reversing the edges [2,0] and [1,2], it is possible to reach any other node starting from node 0.
52+
53+
So, answer[0] = 2.
54+
55+
For node 1: it is already possible to reach any other node starting from node 1.
56+
57+
So, answer[1] = 0.
58+
59+
For node 2: after reversing the edge [1, 2], it is possible to reach any other node starting from node 2.
60+
61+
So, answer[2] = 1.
62+
63+
**Constraints:**
64+
65+
* <code>2 <= n <= 10<sup>5</sup></code>
66+
* `edges.length == n - 1`
67+
* `edges[i].length == 2`
68+
* <code>0 <= u<sub>i</sub> == edges[i][0] < n</code>
69+
* <code>0 <= v<sub>i</sub> == edges[i][1] < n</code>
70+
* <code>u<sub>i</sub> != v<sub>i</sub></code>
71+
* The input is generated such that if the edges were bi-directional, the graph would be a tree.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g2801_2900.s2859_sum_of_values_at_indices_with_k_set_bits
2+
3+
// #Easy #Array #Bit_Manipulation #2023_12_21_Time_177_ms_(100.00%)_Space_37.9_MB_(62.50%)
4+
5+
@Suppress("NAME_SHADOWING")
6+
class Solution {
7+
fun sumIndicesWithKSetBits(nums: List<Int>, k: Int): Int {
8+
var sum = 0
9+
for (i in nums.indices) {
10+
if (countSetBits(i) == k) {
11+
sum += nums[i]
12+
}
13+
}
14+
return sum
15+
}
16+
17+
companion object {
18+
fun countSetBits(num: Int): Int {
19+
var num = num
20+
var count = 0
21+
while (num > 0) {
22+
num = num and (num - 1)
23+
count++
24+
}
25+
return count
26+
}
27+
}
28+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2859\. Sum of Values at Indices With K Set Bits
2+
3+
Easy
4+
5+
You are given a **0-indexed** integer array `nums` and an integer `k`.
6+
7+
Return _an integer that denotes the **sum** of elements in_ `nums` _whose corresponding **indices** have **exactly**_ `k` _set bits in their binary representation._
8+
9+
The **set bits** in an integer are the `1`'s present when it is written in binary.
10+
11+
* For example, the binary representation of `21` is `10101`, which has `3` set bits.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [5,10,1,5,2], k = 1
16+
17+
**Output:** 13
18+
19+
**Explanation:** The binary representation of the indices are:
20+
21+
0 = 000<sub>2</sub>
22+
23+
1 = 001<sub>2</sub>
24+
25+
2 = 010<sub>2</sub>
26+
27+
3 = 011<sub>2</sub>
28+
29+
4 = 100<sub>2</sub>
30+
31+
Indices 1, 2, and 4 have k = 1 set bits in their binary representation. Hence, the answer is nums[1] + nums[2] + nums[4] = 13.
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [4,3,2,1], k = 2
36+
37+
**Output:** 1
38+
39+
**Explanation:** The binary representation of the indices are:
40+
41+
0 = 00<sub>2</sub>
42+
43+
1 = 01<sub>2</sub>
44+
45+
2 = 10<sub>2</sub>
46+
47+
3 = 11<sub>2</sub>
48+
49+
Only index 3 has k = 2 set bits in its binary representation. Hence, the answer is nums[3] = 1.
50+
51+
**Constraints:**
52+
53+
* `1 <= nums.length <= 1000`
54+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
55+
* `0 <= k <= 10`
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g2801_2900.s2860_happy_students
2+
3+
// #Medium #Array #Sorting #Enumeration #2023_12_21_Time_512_ms_(100.00%)_Space_56.2_MB_(50.00%)
4+
5+
import java.util.Collections
6+
7+
class Solution {
8+
fun countWays(nums: List<Int>): Int {
9+
Collections.sort(nums)
10+
var cnt = 0
11+
val n = nums.size
12+
if (nums[0] != 0) {
13+
cnt++
14+
}
15+
for (i in 0 until n - 1) {
16+
if (nums[i] < (i + 1) && (nums[i + 1] > (i + 1))) {
17+
cnt++
18+
}
19+
}
20+
if (n > nums[n - 1]) {
21+
cnt++
22+
}
23+
return cnt
24+
}
25+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2860\. Happy Students
2+
3+
Medium
4+
5+
You are given a **0-indexed** integer array `nums` of length `n` where `n` is the total number of students in the class. The class teacher tries to select a group of students so that all the students remain happy.
6+
7+
The <code>i<sup>th</sup></code> student will become happy if one of these two conditions is met:
8+
9+
* The student is selected and the total number of selected students is **strictly greater than** `nums[i]`.
10+
* The student is not selected and the total number of selected students is **strictly** **less than** `nums[i]`.
11+
12+
Return _the number of ways to select a group of students so that everyone remains happy._
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,1]
17+
18+
**Output:** 2
19+
20+
**Explanation:**
21+
22+
The two possible ways are:
23+
24+
The class teacher selects no student.
25+
26+
The class teacher selects both students to form the group.
27+
28+
If the class teacher selects just one student to form a group then the both students will not be happy.
29+
30+
Therefore, there are only two possible ways.
31+
32+
**Example 2:**
33+
34+
**Input:** nums = [6,0,3,3,6,7,2,7]
35+
36+
**Output:** 3
37+
38+
**Explanation:**
39+
40+
The three possible ways are:
41+
42+
The class teacher selects the student with index = 1 to form the group.
43+
44+
The class teacher selects the students with index = 1, 2, 3, 6 to form the group.
45+
46+
The class teacher selects all the students to form the group.
47+
48+
**Constraints:**
49+
50+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
51+
* `0 <= nums[i] < nums.length`

0 commit comments

Comments
 (0)