Skip to content

Commit

Permalink
Added tasks 3232-3235
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Aug 3, 2024
1 parent 4da0051 commit eb94f50
Show file tree
Hide file tree
Showing 13 changed files with 705 additions and 194 deletions.
388 changes: 194 additions & 194 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g3201_3300.s3232_find_if_digit_game_can_be_won

// #Easy #Array #Math #2024_08_03_Time_194_ms_(36.00%)_Space_40_MB_(5.33%)

class Solution {
fun canAliceWin(nums: IntArray): Boolean {
var sdSum = 0
var ddSum = 0
for (num in nums) {
if (num / 10 == 0) {
sdSum += num
} else {
ddSum += num
}
}
return sdSum != ddSum
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
3232\. Find if Digit Game Can Be Won

Easy

You are given an array of **positive** integers `nums`.

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.

Return `true` if Alice can win this game, otherwise, return `false`.

**Example 1:**

**Input:** nums = [1,2,3,4,10]

**Output:** false

**Explanation:**

Alice cannot win by choosing either single-digit or double-digit numbers.

**Example 2:**

**Input:** nums = [1,2,3,4,5,14]

**Output:** true

**Explanation:**

Alice can win by choosing single-digit numbers which have a sum equal to 15.

**Example 3:**

**Input:** nums = [5,5,5,25]

**Output:** true

**Explanation:**

Alice can win by choosing double-digit numbers which have a sum equal to 25.

**Constraints:**

* `1 <= nums.length <= 100`
* `1 <= nums[i] <= 99`
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package g3201_3300.s3233_find_the_count_of_numbers_which_are_not_special

// #Medium #Array #Math #Number_Theory #2024_08_03_Time_215_ms_(76.19%)_Space_36.9_MB_(61.90%)

import kotlin.math.sqrt

class Solution {
fun nonSpecialCount(l: Int, r: Int): Int {
val primes = sieveOfEratosthenes(sqrt(r.toDouble()).toInt())
var specialCount = 0

for (prime in primes) {
val primeSquare = prime.toLong() * prime
if (primeSquare in l..r) {
specialCount++
}
}

val totalNumbersInRange = r - l + 1
return totalNumbersInRange - specialCount
}

private fun sieveOfEratosthenes(n: Int): List<Int> {
val isPrime = BooleanArray(n + 1)
for (i in 2..n) {
isPrime[i] = true
}

var p = 2
while (p * p <= n) {
if (isPrime[p]) {
var i = p * p
while (i <= n) {
isPrime[i] = false
i += p
}
}
p++
}

val primes: MutableList<Int> = ArrayList()
for (i in 2..n) {
if (isPrime[i]) {
primes.add(i)
}
}
return primes
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
3233\. Find the Count of Numbers Which Are Not Special

Medium

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`.

A number is called **special** if it has exactly 2 **proper divisors**. For example:

* The number 4 is _special_ because it has proper divisors 1 and 2.
* The number 6 is _not special_ because it has proper divisors 1, 2, and 3.

Return the count of numbers in the range `[l, r]` that are **not** **special**.

**Example 1:**

**Input:** l = 5, r = 7

**Output:** 3

**Explanation:**

There are no special numbers in the range `[5, 7]`.

**Example 2:**

**Input:** l = 4, r = 16

**Output:** 11

**Explanation:**

The special numbers in the range `[4, 16]` are 4 and 9.

**Constraints:**

* <code>1 <= l <= r <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package g3201_3300.s3234_count_the_number_of_substrings_with_dominant_ones

// #Medium #String #Sliding_Window #Enumeration
// #2024_08_03_Time_356_ms_(100.00%)_Space_38_MB_(76.92%)

import kotlin.math.min

class Solution {
fun numberOfSubstrings(s: String): Int {
val zero: MutableList<Int> = ArrayList()
zero.add(-1)
var result = 0
for (i in s.indices) {
if (s[i] == '0') {
zero.add(i)
} else {
result += i - zero[zero.size - 1]
}
for (j in 1 until zero.size) {
val len = j * (j + 1)
if (len > i + 1) {
break
}
val prev = zero[zero.size - j - 1]
val from = min((i - len + 1), zero[zero.size - j])
if (from > prev) {
result += from - prev
}
}
}
return result
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
3234\. Count the Number of Substrings With Dominant Ones

Medium

You are given a binary string `s`.

Return the number of substrings with **dominant** ones.

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.

**Example 1:**

**Input:** s = "00011"

**Output:** 5

**Explanation:**

The substrings with dominant ones are shown in the table below.

| i | j | s[i..j] | Number of Zeros | Number of Ones |
|---|---|---------|-----------------|----------------|
| 3 | 3 | 1 | 0 | 1 |
| 4 | 4 | 1 | 0 | 1 |
| 2 | 3 | 01 | 1 | 1 |
| 3 | 4 | 11 | 0 | 2 |
| 2 | 4 | 011 | 1 | 2 |

**Example 2:**

**Input:** s = "101101"

**Output:** 16

**Explanation:**

The substrings with **non-dominant** ones are shown in the table below.

Since there are 21 substrings total and 5 of them have non-dominant ones, it follows that there are 16 substrings with dominant ones.

| i | j | s[i..j] | Number of Zeros | Number of Ones |
|---|---|---------|-----------------|----------------|
| 1 | 1 | 0 | 1 | 0 |
| 4 | 4 | 0 | 1 | 0 |
| 1 | 4 | 0110 | 2 | 2 |
| 0 | 4 | 10110 | 2 | 3 |
| 1 | 5 | 01101 | 2 | 3 |

**Constraints:**

* <code>1 <= s.length <= 4 * 10<sup>4</sup></code>
* `s` consists only of characters `'0'` and `'1'`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package g3201_3300.s3235_check_if_the_rectangle_corner_is_reachable

// #Hard #Array #Math #Depth_First_Search #Breadth_First_Search #Union_Find #Geometry
// #2024_08_03_Time_612_ms_(66.67%)_Space_50.5_MB_(88.89%)

import kotlin.math.pow
import kotlin.math.sqrt

class Solution {
fun canReachCorner(x: Int, y: Int, circles: Array<IntArray>): Boolean {
val n = circles.size
val ds = DisjointSet(n + 5)

// Special nodes for boundaries
val leftBoundary = n + 3
val topBoundary = n
val rightBoundary = n + 1
val bottomBoundary = n + 2

var i = 0
for (it in circles) {
val xi = it[0]
val yi = it[1]
val ri = it[2]
if (yi - ri >= y || xi - ri >= x) {
continue
}
if (((xi > (x + y) || yi > y) && (xi > x || yi > x + y))) {
continue
}
if (xi <= ri) {
ds.dsu(i, leftBoundary)
}
if (yi <= ri) {
ds.dsu(i, topBoundary)
}
if (x - xi <= ri) {
ds.dsu(i, rightBoundary)
}
if (y - yi <= ri) {
ds.dsu(i, bottomBoundary)
}
i++
}

// Union circles that overlap
i = 0
while (i < n) {
val x1 = circles[i][0]
val y1 = circles[i][1]
val r1 = circles[i][2]
if (y1 - r1 >= y || x1 - r1 >= x) {
i++
continue
}
if (((x1 > (x + y) || y1 > y) && (x1 > x || y1 > x + y))) {
i++
continue
}

for (j in i + 1 until n) {
val x2 = circles[j][0]
val y2 = circles[j][1]
val r2 = circles[j][2]
val dist = sqrt(
(x1 - x2.toDouble()).pow(2.0) + (y1 - y2.toDouble()).pow(2.0)
)
if (dist <= (r1 + r2)) {
ds.dsu(i, j)
}
}
i++
}

// Check if left is connected to right or top is connected to bottom
if (ds.findUpar(leftBoundary) == ds.findUpar(rightBoundary) ||
ds.findUpar(leftBoundary) == ds.findUpar(topBoundary)
) {
return false
}
return (
ds.findUpar(bottomBoundary) != ds.findUpar(rightBoundary) &&
ds.findUpar(bottomBoundary) != ds.findUpar(topBoundary)
)
}

private class DisjointSet(n: Int) {
private val parent: IntArray
private val size = IntArray(n + 1)

init {
size.fill(1)
parent = IntArray(n + 1)
for (i in 0..n) {
parent[i] = i
}
}

fun findUpar(u: Int): Int {
if (u == parent[u]) {
return u
}
parent[u] = findUpar(parent[u])
return parent[u]
}

fun dsu(u: Int, v: Int) {
val ulpu = findUpar(u)
val ulpv = findUpar(v)
if (ulpv == ulpu) {
return
}
if (size[ulpu] < size[ulpv]) {
parent[ulpu] = ulpv
size[ulpv] += size[ulpu]
} else {
parent[ulpv] = ulpu
size[ulpu] += size[ulpv]
}
}
}
}
Loading

0 comments on commit eb94f50

Please sign in to comment.