Skip to content

Commit e7a9312

Browse files
authored
Added tasks 3232-3245
1 parent e4ec307 commit e7a9312

File tree

13 files changed

+1622
-108
lines changed
  • src/main/java/g3201_3300
    • s3232_find_if_digit_game_can_be_won
    • s3233_find_the_count_of_numbers_which_are_not_special
    • s3234_count_the_number_of_substrings_with_dominant_ones
    • s3235_check_if_the_rectangle_corner_is_reachable
    • s3238_find_the_number_of_winning_players
    • s3239_minimum_number_of_flips_to_make_binary_grid_palindromic_i
    • s3240_minimum_number_of_flips_to_make_binary_grid_palindromic_ii
    • s3241_time_taken_to_mark_all_nodes
    • s3242_design_neighbor_sum_service
    • s3243_shortest_distance_after_road_addition_queries_i
    • s3244_shortest_distance_after_road_addition_queries_ii
    • s3245_alternating_groups_iii

13 files changed

+1622
-108
lines changed

README.md

Lines changed: 120 additions & 108 deletions
Large diffs are not rendered by default.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3232\. Find if Digit Game Can Be Won
5+
6+
Easy
7+
8+
You are given an array of **positive** integers `nums`.
9+
10+
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.
11+
12+
Return `true` if Alice can win this game, otherwise, return `false`.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,2,3,4,10]
17+
18+
**Output:** false
19+
20+
**Explanation:**
21+
22+
Alice cannot win by choosing either single-digit or double-digit numbers.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [1,2,3,4,5,14]
27+
28+
**Output:** true
29+
30+
**Explanation:**
31+
32+
Alice can win by choosing single-digit numbers which have a sum equal to 15.
33+
34+
**Example 3:**
35+
36+
**Input:** nums = [5,5,5,25]
37+
38+
**Output:** true
39+
40+
**Explanation:**
41+
42+
Alice can win by choosing double-digit numbers which have a sum equal to 25.
43+
44+
**Constraints:**
45+
46+
* `1 <= nums.length <= 100`
47+
* `1 <= nums[i] <= 99`
48+
49+
## Solution
50+
51+
```java
52+
public class Solution {
53+
public boolean canAliceWin(int[] nums) {
54+
int sdSum = 0;
55+
int ddSum = 0;
56+
for (int num : nums) {
57+
if (num / 10 == 0) {
58+
sdSum += num;
59+
} else {
60+
ddSum += num;
61+
}
62+
}
63+
return sdSum != ddSum;
64+
}
65+
}
66+
```
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3233\. Find the Count of Numbers Which Are Not Special
5+
6+
Medium
7+
8+
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`.
9+
10+
A number is called **special** if it has exactly 2 **proper divisors**. For example:
11+
12+
* The number 4 is _special_ because it has proper divisors 1 and 2.
13+
* The number 6 is _not special_ because it has proper divisors 1, 2, and 3.
14+
15+
Return the count of numbers in the range `[l, r]` that are **not** **special**.
16+
17+
**Example 1:**
18+
19+
**Input:** l = 5, r = 7
20+
21+
**Output:** 3
22+
23+
**Explanation:**
24+
25+
There are no special numbers in the range `[5, 7]`.
26+
27+
**Example 2:**
28+
29+
**Input:** l = 4, r = 16
30+
31+
**Output:** 11
32+
33+
**Explanation:**
34+
35+
The special numbers in the range `[4, 16]` are 4 and 9.
36+
37+
**Constraints:**
38+
39+
* <code>1 <= l <= r <= 10<sup>9</sup></code>
40+
41+
## Solution
42+
43+
```java
44+
import java.util.ArrayList;
45+
import java.util.List;
46+
47+
public class Solution {
48+
public int nonSpecialCount(int l, int r) {
49+
List<Integer> primes = sieveOfEratosthenes((int) Math.sqrt(r));
50+
int specialCount = 0;
51+
52+
for (Integer prime : primes) {
53+
long primeSquare = (long) prime * prime;
54+
if (primeSquare >= l && primeSquare <= r) {
55+
specialCount++;
56+
}
57+
}
58+
59+
int totalNumbersInRange = r - l + 1;
60+
return totalNumbersInRange - specialCount;
61+
}
62+
63+
private List<Integer> sieveOfEratosthenes(int n) {
64+
boolean[] isPrime = new boolean[n + 1];
65+
for (int i = 2; i <= n; i++) {
66+
isPrime[i] = true;
67+
}
68+
69+
for (int p = 2; p * p <= n; p++) {
70+
if (isPrime[p]) {
71+
for (int i = p * p; i <= n; i += p) {
72+
isPrime[i] = false;
73+
}
74+
}
75+
}
76+
77+
List<Integer> primes = new ArrayList<>();
78+
for (int i = 2; i <= n; i++) {
79+
if (isPrime[i]) {
80+
primes.add(i);
81+
}
82+
}
83+
return primes;
84+
}
85+
}
86+
```
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3234\. Count the Number of Substrings With Dominant Ones
5+
6+
Medium
7+
8+
You are given a binary string `s`.
9+
10+
Return the number of substrings with **dominant** ones.
11+
12+
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.
13+
14+
**Example 1:**
15+
16+
**Input:** s = "00011"
17+
18+
**Output:** 5
19+
20+
**Explanation:**
21+
22+
The substrings with dominant ones are shown in the table below.
23+
24+
| i | j | s[i..j] | Number of Zeros | Number of Ones |
25+
|---|---|---------|-----------------|----------------|
26+
| 3 | 3 | 1 | 0 | 1 |
27+
| 4 | 4 | 1 | 0 | 1 |
28+
| 2 | 3 | 01 | 1 | 1 |
29+
| 3 | 4 | 11 | 0 | 2 |
30+
| 2 | 4 | 011 | 1 | 2 |
31+
32+
**Example 2:**
33+
34+
**Input:** s = "101101"
35+
36+
**Output:** 16
37+
38+
**Explanation:**
39+
40+
The substrings with **non-dominant** ones are shown in the table below.
41+
42+
Since there are 21 substrings total and 5 of them have non-dominant ones, it follows that there are 16 substrings with dominant ones.
43+
44+
| i | j | s[i..j] | Number of Zeros | Number of Ones |
45+
|---|---|---------|-----------------|----------------|
46+
| 1 | 1 | 0 | 1 | 0 |
47+
| 4 | 4 | 0 | 1 | 0 |
48+
| 1 | 4 | 0110 | 2 | 2 |
49+
| 0 | 4 | 10110 | 2 | 3 |
50+
| 1 | 5 | 01101 | 2 | 3 |
51+
52+
**Constraints:**
53+
54+
* <code>1 <= s.length <= 4 * 10<sup>4</sup></code>
55+
* `s` consists only of characters `'0'` and `'1'`.
56+
57+
## Solution
58+
59+
```java
60+
import java.util.ArrayList;
61+
import java.util.List;
62+
63+
public class Solution {
64+
public int numberOfSubstrings(String s) {
65+
List<Integer> zero = new ArrayList<>();
66+
zero.add(-1);
67+
int result = 0;
68+
for (int i = 0; i < s.length(); i++) {
69+
if (s.charAt(i) == '0') {
70+
zero.add(i);
71+
} else {
72+
result += i - zero.get(zero.size() - 1);
73+
}
74+
for (int j = 1; j < zero.size(); j++) {
75+
int len = j * (j + 1);
76+
if (len > i + 1) {
77+
break;
78+
}
79+
int prev = zero.get(zero.size() - j - 1);
80+
int from = Math.min(i - len + 1, zero.get(zero.size() - j));
81+
if (from > prev) {
82+
result += from - prev;
83+
}
84+
}
85+
}
86+
return result;
87+
}
88+
}
89+
```

0 commit comments

Comments
 (0)