Skip to content

Commit 2e18ca4

Browse files
authored
Added tasks 2682-2693
1 parent a54ae64 commit 2e18ca4

File tree

14 files changed

+481
-0
lines changed

14 files changed

+481
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g2601_2700.s2682_find_the_losers_of_the_circular_game
2+
3+
// #Easy #Array #Hash_Table #Simulation #2023_07_28_Time_186_ms_(100.00%)_Space_37.4_MB_(81.82%)
4+
5+
class Solution {
6+
fun circularGameLosers(n: Int, k: Int): IntArray {
7+
val pointsMap = IntArray(n)
8+
var friend = 0
9+
var turn = 1
10+
while (true) {
11+
pointsMap[friend] = pointsMap[friend] + 1
12+
if (pointsMap[friend] == 2) break
13+
friend = (friend + turn * k) % n
14+
turn++
15+
}
16+
val result = IntArray(n - (turn - 1))
17+
var i = 0
18+
pointsMap.forEachIndexed { index, points ->
19+
if (points == 0) {
20+
result[i] = index + 1
21+
i++
22+
}
23+
}
24+
return result
25+
}
26+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2682\. Find the Losers of the Circular Game
2+
3+
Easy
4+
5+
There are `n` friends that are playing a game. The friends are sitting in a circle and are numbered from `1` to `n` in **clockwise order**. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for `1 <= i < n`, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.
6+
7+
The rules of the game are as follows:
8+
9+
<code>1<sup>st</sup></code> friend receives the ball.
10+
11+
* After that, <code>1<sup>st</sup></code> friend passes it to the friend who is `k` steps away from them in the **clockwise** direction.
12+
* After that, the friend who receives the ball should pass it to the friend who is `2 * k` steps away from them in the **clockwise** direction.
13+
* After that, the friend who receives the ball should pass it to the friend who is `3 * k` steps away from them in the **clockwise** direction, and so on and so forth.
14+
15+
In other words, on the <code>i<sup>th</sup></code> turn, the friend holding the ball should pass it to the friend who is `i * k` steps away from them in the **clockwise** direction.
16+
17+
The game is finished when some friend receives the ball for the second time.
18+
19+
The **losers** of the game are friends who did not receive the ball in the entire game.
20+
21+
Given the number of friends, `n`, and an integer `k`, return _the array answer, which contains the losers of the game in the **ascending** order_.
22+
23+
**Example 1:**
24+
25+
**Input:** n = 5, k = 2
26+
27+
**Output:** [4,5]
28+
29+
**Explanation:** The game goes as follows:
30+
1) Start at 1<sup>st</sup> friend and pass the ball to the friend who is 2 steps away from them - 3<sup>rd</sup> friend.
31+
2) 3<sup>rd</sup> friend passes the ball to the friend who is 4 steps away from them - 2<sup>nd</sup> friend.
32+
3) 2<sup>nd</sup> friend passes the ball to the friend who is 6 steps away from them - 3<sup>rd</sup> friend.
33+
4) The game ends as 3<sup>rd</sup> friend receives the ball for the second time.
34+
35+
**Example 2:**
36+
37+
**Input:** n = 4, k = 4
38+
39+
**Output:** [2,3,4]
40+
41+
**Explanation:** The game goes as follows:
42+
1) Start at the 1<sup>st</sup> friend and pass the ball to the friend who is 4 steps away from them - 1<sup>st</sup> friend.
43+
2) The game ends as 1<sup>st</sup> friend receives the ball for the second time.
44+
45+
**Constraints:**
46+
47+
* `1 <= k <= n <= 50`
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package g2601_2700.s2683_neighboring_bitwise_xor
2+
3+
// #Medium #Array #Bit_Manipulation #2023_07_28_Time_988_ms_(100.00%)_Space_76.1_MB_(100.00%)
4+
5+
class Solution {
6+
fun doesValidArrayExist(derived: IntArray): Boolean {
7+
var xor = 0
8+
for (j in derived) {
9+
xor = xor xor j
10+
}
11+
return xor == 0
12+
}
13+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2683\. Neighboring Bitwise XOR
2+
3+
Medium
4+
5+
A **0-indexed** array `derived` with length `n` is derived by computing the **bitwise XOR** (⊕) of adjacent values in a **binary array** `original` of length `n`.
6+
7+
Specifically, for each index `i` in the range `[0, n - 1]`:
8+
9+
* If `i = n - 1`, then `derived[i] = original[i] ⊕ original[0]`.
10+
* Otherwise, `derived[i] = original[i] ⊕ original[i + 1]`.
11+
12+
Given an array `derived`, your task is to determine whether there exists a **valid binary array** `original` that could have formed `derived`.
13+
14+
Return _**true** if such an array exists or **false** otherwise._
15+
16+
* A binary array is an array containing only **0's** and **1's**
17+
18+
**Example 1:**
19+
20+
**Input:** derived = [1,1,0]
21+
22+
**Output:** true
23+
24+
**Explanation:** A valid original array that gives derived is [0,1,0].
25+
26+
derived[0] = original[0] ⊕ original[1] = 0 ⊕ 1 = 1
27+
28+
derived[1] = original[1] ⊕ original[2] = 1 ⊕ 0 = 1
29+
30+
derived[2] = original[2] ⊕ original[0] = 0 ⊕ 0 = 0
31+
32+
**Example 2:**
33+
34+
**Input:** derived = [1,1]
35+
36+
**Output:** true
37+
38+
**Explanation:** A valid original array that gives derived is [0,1].
39+
40+
derived[0] = original[0] ⊕ original[1] = 1
41+
42+
derived[1] = original[1] ⊕ original[0] = 1
43+
44+
**Example 3:**
45+
46+
**Input:** derived = [1,0]
47+
48+
**Output:** false
49+
50+
**Explanation:** There is no valid original array that gives derived.
51+
52+
**Constraints:**
53+
54+
* `n == derived.length`
55+
* <code>1 <= n <= 10<sup>5</sup></code>
56+
* The values in `derived` are either **0's** or **1's**
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g2601_2700.s2684_maximum_number_of_moves_in_a_grid
2+
3+
// #Medium #Array #Dynamic_Programming #Matrix
4+
// #2023_07_28_Time_542_ms_(100.00%)_Space_57.4_MB_(57.14%)
5+
6+
class Solution {
7+
fun maxMoves(grid: Array<IntArray>): Int {
8+
val height = grid.size
9+
val width = grid[0].size
10+
val dp = Array(height) { IntArray(width) { Int.MIN_VALUE } }
11+
var result = 0
12+
for (i in 0 until height) {
13+
dp[i][0] = 0
14+
}
15+
for (c in 1 until width) {
16+
for (r in 0 until height) {
17+
if (r > 0 && grid[r - 1][c - 1] < grid[r][c]) {
18+
dp[r][c] = dp[r][c].coerceAtLeast(dp[r - 1][c - 1] + 1)
19+
}
20+
if (grid[r][c - 1] < grid[r][c]) dp[r][c] = dp[r][c].coerceAtLeast(dp[r][c - 1] + 1)
21+
if (r < height - 1 && grid[r + 1][c - 1] < grid[r][c]) {
22+
dp[r][c] = dp[r][c].coerceAtLeast(dp[r + 1][c - 1] + 1)
23+
}
24+
result = result.coerceAtLeast(dp[r][c])
25+
}
26+
}
27+
return result
28+
}
29+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2684\. Maximum Number of Moves in a Grid
2+
3+
Medium
4+
5+
You are given a **0-indexed** `m x n` matrix `grid` consisting of **positive** integers.
6+
7+
You can start at **any** cell in the first column of the matrix, and traverse the grid in the following way:
8+
9+
* From a cell `(row, col)`, you can move to any of the cells: `(row - 1, col + 1)`, `(row, col + 1)` and `(row + 1, col + 1)` such that the value of the cell you move to, should be **strictly** bigger than the value of the current cell.
10+
11+
Return _the **maximum** number of **moves** that you can perform._
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2023/04/11/yetgriddrawio-10.png)
16+
17+
**Input:** grid = [[2,4,3,5],[5,4,9,3],[3,4,2,11],[10,9,13,15]]
18+
19+
**Output:** 3
20+
21+
**Explanation:** We can start at the cell (0, 0) and make the following moves:
22+
- (0, 0) -> (0, 1).
23+
- (0, 1) -> (1, 2).
24+
- (1, 2) -> (2, 3).
25+
26+
It can be shown that it is the maximum number of moves that can be made.
27+
28+
**Example 2:**
29+
30+
![](https://assets.leetcode.com/uploads/2023/04/12/yetgrid4drawio.png) **Input:** grid = [[3,2,4],[2,1,9],[1,1,7]]
31+
32+
**Output:** 0
33+
34+
**Explanation:** Starting from any cell in the first column we cannot perform any moves.
35+
36+
**Constraints:**
37+
38+
* `m == grid.length`
39+
* `n == grid[i].length`
40+
* `2 <= m, n <= 1000`
41+
* <code>4 <= m * n <= 10<sup>5</sup></code>
42+
* <code>1 <= grid[i][j] <= 10<sup>6</sup></code>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g2601_2700.s2685_count_the_number_of_complete_components
2+
3+
// #Medium #Array #Dynamic_Programming #Matrix #Graph #Depth_First_Search #Breadth_First_Search
4+
// #2023_07_28_Time_436_ms_(100.00%)_Space_58.6_MB_(66.67%)
5+
6+
@Suppress("NAME_SHADOWING")
7+
class Solution {
8+
fun countCompleteComponents(n: Int, edges: Array<IntArray>): Int {
9+
val adj = HashMap<Int, ArrayList<Int>>().apply {
10+
for ((u, v) in edges) {
11+
this[u] = getOrDefault(u, arrayListOf()).apply { add(v) }
12+
this[v] = getOrDefault(v, arrayListOf()).apply { add(u) }
13+
}
14+
}
15+
val visited = BooleanArray(n)
16+
fun bfs(i: Int): Pair<Int, Int> {
17+
if (visited[i]) return 0 to 0
18+
visited[i] = true
19+
var nodes = 1
20+
var edges = (adj[i]?.size ?: 0)
21+
adj[i]?.forEach {
22+
val (nodes2, edges2) = bfs(it)
23+
nodes += nodes2
24+
edges += edges2
25+
}
26+
return nodes to edges
27+
}
28+
var res = 0
29+
for (i in 0 until n) {
30+
if (!visited[i]) {
31+
val (nodes, edges) = bfs(i)
32+
if ((nodes * (nodes - 1)) == edges)
33+
res++
34+
}
35+
}
36+
return res
37+
}
38+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2685\. Count the Number of Complete Components
2+
3+
Medium
4+
5+
You are given an integer `n`. There is an **undirected** graph with `n` vertices, numbered from `0` to `n - 1`. You are given a 2D integer array `edges` where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an **undirected** edge connecting vertices <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.
6+
7+
Return _the number of **complete connected components** of the graph_.
8+
9+
A **connected component** is a subgraph of a graph in which there exists a path between any two vertices, and no vertex of the subgraph shares an edge with a vertex outside of the subgraph.
10+
11+
A connected component is said to be **complete** if there exists an edge between every pair of its vertices.
12+
13+
**Example 1:**
14+
15+
**![](https://assets.leetcode.com/uploads/2023/04/11/screenshot-from-2023-04-11-23-31-23.png)**
16+
17+
**Input:** n = 6, edges = [[0,1],[0,2],[1,2],[3,4]]
18+
19+
**Output:** 3
20+
21+
**Explanation:** From the picture above, one can see that all of the components of this graph are complete.
22+
23+
**Example 2:**
24+
25+
**![](https://assets.leetcode.com/uploads/2023/04/11/screenshot-from-2023-04-11-23-32-00.png)**
26+
27+
**Input:** n = 6, edges = [[0,1],[0,2],[1,2],[3,4],[3,5]]
28+
29+
**Output:** 1
30+
31+
**Explanation:** The component containing vertices 0, 1, and 2 is complete since there is an edge between every pair of two vertices. On the other hand, the component containing vertices 3, 4, and 5 is not complete since there is no edge between vertices 4 and 5. Thus, the number of complete components in this graph is 1.
32+
33+
**Constraints:**
34+
35+
* `1 <= n <= 50`
36+
* `0 <= edges.length <= n * (n - 1) / 2`
37+
* `edges[i].length == 2`
38+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code>
39+
* <code>a<sub>i</sub> != b<sub>i</sub></code>
40+
* There are no repeated edges.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2693\. Call Function with Custom Context
2+
3+
Medium
4+
5+
Enhance all functions to have the `callPolyfill` method. The method accepts an object `obj` as it's first parameter and any number of additional arguments. The `obj` becomes the `this` context for the function. The additional arguments are passed to the function (that the `callPolyfill` method belongs on).
6+
7+
For example if you had the function:
8+
9+
function tax(price, taxRate) { const totalCost = price \* (1 + taxRate); console.log(\`The cost of ${this.item} is ${totalCost}\`); }
10+
11+
Calling this function like `tax(10, 0.1)` will log `"The cost of undefined is 11"`. This is because the `this` context was not defined.
12+
13+
However, calling the function like `tax.callPolyfill({item: "salad"}, 10, 0.1)` will log `"The cost of salad is 11"`. The `this` context was appropriately set, and the function logged an appropriate output.
14+
15+
Please solve this without using the built-in `Function.call` method.
16+
17+
**Example 1:**
18+
19+
**Input:**
20+
21+
fn = function add(b) {
22+
return this.a + b;
23+
}
24+
25+
args = [{"a": 5}, 7]
26+
27+
**Output:** 12
28+
29+
**Explanation:**
30+
31+
fn.callPolyfill({"a": 5}, 7); // 12
32+
33+
callPolyfill sets the "this" context to {"a": 5}. 7 is passed as an argument.
34+
35+
**Example 2:**
36+
37+
**Input:**
38+
39+
fn = function tax(price, taxRate) {
40+
return \`The cost of the ${this.item} is ${price \* taxRate}\`;
41+
}
42+
43+
args = [{"item": "burger"}, 10, 1.1]
44+
45+
**Output:** "The cost of the burger is 11"
46+
47+
**Explanation:** callPolyfill sets the "this" context to {"item": "burger"}. 10 and 1.1 are passed as additional arguments.
48+
49+
**Constraints:**
50+
51+
* `typeof args[0] == 'object' and args[0] != null`
52+
* `1 <= args.length <= 100`
53+
* <code>2 <= JSON.stringify(args[0]).length <= 10<sup>5</sup></code>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// #Medium #Array #Dynamic_Programming #Matrix #2023_07_28_Time_51_ms_(97.92%)_Space_43_MB_(91.84%)
2+
3+
declare global {
4+
interface Function {
5+
callPolyfill(context: Record<any, any>, ...args: any[]): any
6+
}
7+
}
8+
9+
Function.prototype.callPolyfill = function (context, ...args): any {
10+
let fn = this.bind(context)
11+
return fn(...args)
12+
}
13+
14+
/*
15+
* function increment() { this.count++; return this.count; }
16+
* increment.callPolyfill({count: 1}); // 2
17+
*/
18+
19+
export { Function.prototype.callPolyfill }

0 commit comments

Comments
 (0)