Skip to content

Commit eb94f50

Browse files
authored
Added tasks 3232-3235
1 parent 4da0051 commit eb94f50

File tree

13 files changed

+705
-194
lines changed

13 files changed

+705
-194
lines changed

README.md

Lines changed: 194 additions & 194 deletions
Large diffs are not rendered by default.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3201_3300.s3232_find_if_digit_game_can_be_won
2+
3+
// #Easy #Array #Math #2024_08_03_Time_194_ms_(36.00%)_Space_40_MB_(5.33%)
4+
5+
class Solution {
6+
fun canAliceWin(nums: IntArray): Boolean {
7+
var sdSum = 0
8+
var ddSum = 0
9+
for (num in nums) {
10+
if (num / 10 == 0) {
11+
sdSum += num
12+
} else {
13+
ddSum += num
14+
}
15+
}
16+
return sdSum != ddSum
17+
}
18+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
3232\. Find if Digit Game Can Be Won
2+
3+
Easy
4+
5+
You are given an array of **positive** integers `nums`.
6+
7+
Alice and Bob are playing a game. In the game, Alice can choose **either** all single-digit numbers or all double-digit numbers from `nums`, and the rest of the numbers are given to Bob. Alice wins if the sum of her numbers is **strictly greater** than the sum of Bob's numbers.
8+
9+
Return `true` if Alice can win this game, otherwise, return `false`.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2,3,4,10]
14+
15+
**Output:** false
16+
17+
**Explanation:**
18+
19+
Alice cannot win by choosing either single-digit or double-digit numbers.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [1,2,3,4,5,14]
24+
25+
**Output:** true
26+
27+
**Explanation:**
28+
29+
Alice can win by choosing single-digit numbers which have a sum equal to 15.
30+
31+
**Example 3:**
32+
33+
**Input:** nums = [5,5,5,25]
34+
35+
**Output:** true
36+
37+
**Explanation:**
38+
39+
Alice can win by choosing double-digit numbers which have a sum equal to 25.
40+
41+
**Constraints:**
42+
43+
* `1 <= nums.length <= 100`
44+
* `1 <= nums[i] <= 99`
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g3201_3300.s3233_find_the_count_of_numbers_which_are_not_special
2+
3+
// #Medium #Array #Math #Number_Theory #2024_08_03_Time_215_ms_(76.19%)_Space_36.9_MB_(61.90%)
4+
5+
import kotlin.math.sqrt
6+
7+
class Solution {
8+
fun nonSpecialCount(l: Int, r: Int): Int {
9+
val primes = sieveOfEratosthenes(sqrt(r.toDouble()).toInt())
10+
var specialCount = 0
11+
12+
for (prime in primes) {
13+
val primeSquare = prime.toLong() * prime
14+
if (primeSquare in l..r) {
15+
specialCount++
16+
}
17+
}
18+
19+
val totalNumbersInRange = r - l + 1
20+
return totalNumbersInRange - specialCount
21+
}
22+
23+
private fun sieveOfEratosthenes(n: Int): List<Int> {
24+
val isPrime = BooleanArray(n + 1)
25+
for (i in 2..n) {
26+
isPrime[i] = true
27+
}
28+
29+
var p = 2
30+
while (p * p <= n) {
31+
if (isPrime[p]) {
32+
var i = p * p
33+
while (i <= n) {
34+
isPrime[i] = false
35+
i += p
36+
}
37+
}
38+
p++
39+
}
40+
41+
val primes: MutableList<Int> = ArrayList()
42+
for (i in 2..n) {
43+
if (isPrime[i]) {
44+
primes.add(i)
45+
}
46+
}
47+
return primes
48+
}
49+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
3233\. Find the Count of Numbers Which Are Not Special
2+
3+
Medium
4+
5+
You are given 2 **positive** integers `l` and `r`. For any number `x`, all positive divisors of `x` _except_ `x` are called the **proper divisors** of `x`.
6+
7+
A number is called **special** if it has exactly 2 **proper divisors**. For example:
8+
9+
* The number 4 is _special_ because it has proper divisors 1 and 2.
10+
* The number 6 is _not special_ because it has proper divisors 1, 2, and 3.
11+
12+
Return the count of numbers in the range `[l, r]` that are **not** **special**.
13+
14+
**Example 1:**
15+
16+
**Input:** l = 5, r = 7
17+
18+
**Output:** 3
19+
20+
**Explanation:**
21+
22+
There are no special numbers in the range `[5, 7]`.
23+
24+
**Example 2:**
25+
26+
**Input:** l = 4, r = 16
27+
28+
**Output:** 11
29+
30+
**Explanation:**
31+
32+
The special numbers in the range `[4, 16]` are 4 and 9.
33+
34+
**Constraints:**
35+
36+
* <code>1 <= l <= r <= 10<sup>9</sup></code>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g3201_3300.s3234_count_the_number_of_substrings_with_dominant_ones
2+
3+
// #Medium #String #Sliding_Window #Enumeration
4+
// #2024_08_03_Time_356_ms_(100.00%)_Space_38_MB_(76.92%)
5+
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun numberOfSubstrings(s: String): Int {
10+
val zero: MutableList<Int> = ArrayList()
11+
zero.add(-1)
12+
var result = 0
13+
for (i in s.indices) {
14+
if (s[i] == '0') {
15+
zero.add(i)
16+
} else {
17+
result += i - zero[zero.size - 1]
18+
}
19+
for (j in 1 until zero.size) {
20+
val len = j * (j + 1)
21+
if (len > i + 1) {
22+
break
23+
}
24+
val prev = zero[zero.size - j - 1]
25+
val from = min((i - len + 1), zero[zero.size - j])
26+
if (from > prev) {
27+
result += from - prev
28+
}
29+
}
30+
}
31+
return result
32+
}
33+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
3234\. Count the Number of Substrings With Dominant Ones
2+
3+
Medium
4+
5+
You are given a binary string `s`.
6+
7+
Return the number of substrings with **dominant** ones.
8+
9+
A string has **dominant** ones if the number of ones in the string is **greater than or equal to** the **square** of the number of zeros in the string.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "00011"
14+
15+
**Output:** 5
16+
17+
**Explanation:**
18+
19+
The substrings with dominant ones are shown in the table below.
20+
21+
| i | j | s[i..j] | Number of Zeros | Number of Ones |
22+
|---|---|---------|-----------------|----------------|
23+
| 3 | 3 | 1 | 0 | 1 |
24+
| 4 | 4 | 1 | 0 | 1 |
25+
| 2 | 3 | 01 | 1 | 1 |
26+
| 3 | 4 | 11 | 0 | 2 |
27+
| 2 | 4 | 011 | 1 | 2 |
28+
29+
**Example 2:**
30+
31+
**Input:** s = "101101"
32+
33+
**Output:** 16
34+
35+
**Explanation:**
36+
37+
The substrings with **non-dominant** ones are shown in the table below.
38+
39+
Since there are 21 substrings total and 5 of them have non-dominant ones, it follows that there are 16 substrings with dominant ones.
40+
41+
| i | j | s[i..j] | Number of Zeros | Number of Ones |
42+
|---|---|---------|-----------------|----------------|
43+
| 1 | 1 | 0 | 1 | 0 |
44+
| 4 | 4 | 0 | 1 | 0 |
45+
| 1 | 4 | 0110 | 2 | 2 |
46+
| 0 | 4 | 10110 | 2 | 3 |
47+
| 1 | 5 | 01101 | 2 | 3 |
48+
49+
**Constraints:**
50+
51+
* <code>1 <= s.length <= 4 * 10<sup>4</sup></code>
52+
* `s` consists only of characters `'0'` and `'1'`.
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package g3201_3300.s3235_check_if_the_rectangle_corner_is_reachable
2+
3+
// #Hard #Array #Math #Depth_First_Search #Breadth_First_Search #Union_Find #Geometry
4+
// #2024_08_03_Time_612_ms_(66.67%)_Space_50.5_MB_(88.89%)
5+
6+
import kotlin.math.pow
7+
import kotlin.math.sqrt
8+
9+
class Solution {
10+
fun canReachCorner(x: Int, y: Int, circles: Array<IntArray>): Boolean {
11+
val n = circles.size
12+
val ds = DisjointSet(n + 5)
13+
14+
// Special nodes for boundaries
15+
val leftBoundary = n + 3
16+
val topBoundary = n
17+
val rightBoundary = n + 1
18+
val bottomBoundary = n + 2
19+
20+
var i = 0
21+
for (it in circles) {
22+
val xi = it[0]
23+
val yi = it[1]
24+
val ri = it[2]
25+
if (yi - ri >= y || xi - ri >= x) {
26+
continue
27+
}
28+
if (((xi > (x + y) || yi > y) && (xi > x || yi > x + y))) {
29+
continue
30+
}
31+
if (xi <= ri) {
32+
ds.dsu(i, leftBoundary)
33+
}
34+
if (yi <= ri) {
35+
ds.dsu(i, topBoundary)
36+
}
37+
if (x - xi <= ri) {
38+
ds.dsu(i, rightBoundary)
39+
}
40+
if (y - yi <= ri) {
41+
ds.dsu(i, bottomBoundary)
42+
}
43+
i++
44+
}
45+
46+
// Union circles that overlap
47+
i = 0
48+
while (i < n) {
49+
val x1 = circles[i][0]
50+
val y1 = circles[i][1]
51+
val r1 = circles[i][2]
52+
if (y1 - r1 >= y || x1 - r1 >= x) {
53+
i++
54+
continue
55+
}
56+
if (((x1 > (x + y) || y1 > y) && (x1 > x || y1 > x + y))) {
57+
i++
58+
continue
59+
}
60+
61+
for (j in i + 1 until n) {
62+
val x2 = circles[j][0]
63+
val y2 = circles[j][1]
64+
val r2 = circles[j][2]
65+
val dist = sqrt(
66+
(x1 - x2.toDouble()).pow(2.0) + (y1 - y2.toDouble()).pow(2.0)
67+
)
68+
if (dist <= (r1 + r2)) {
69+
ds.dsu(i, j)
70+
}
71+
}
72+
i++
73+
}
74+
75+
// Check if left is connected to right or top is connected to bottom
76+
if (ds.findUpar(leftBoundary) == ds.findUpar(rightBoundary) ||
77+
ds.findUpar(leftBoundary) == ds.findUpar(topBoundary)
78+
) {
79+
return false
80+
}
81+
return (
82+
ds.findUpar(bottomBoundary) != ds.findUpar(rightBoundary) &&
83+
ds.findUpar(bottomBoundary) != ds.findUpar(topBoundary)
84+
)
85+
}
86+
87+
private class DisjointSet(n: Int) {
88+
private val parent: IntArray
89+
private val size = IntArray(n + 1)
90+
91+
init {
92+
size.fill(1)
93+
parent = IntArray(n + 1)
94+
for (i in 0..n) {
95+
parent[i] = i
96+
}
97+
}
98+
99+
fun findUpar(u: Int): Int {
100+
if (u == parent[u]) {
101+
return u
102+
}
103+
parent[u] = findUpar(parent[u])
104+
return parent[u]
105+
}
106+
107+
fun dsu(u: Int, v: Int) {
108+
val ulpu = findUpar(u)
109+
val ulpv = findUpar(v)
110+
if (ulpv == ulpu) {
111+
return
112+
}
113+
if (size[ulpu] < size[ulpv]) {
114+
parent[ulpu] = ulpv
115+
size[ulpv] += size[ulpu]
116+
} else {
117+
parent[ulpv] = ulpu
118+
size[ulpu] += size[ulpv]
119+
}
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)