Skip to content

Commit 73eda43

Browse files
authored
Added tasks 2151-2200
1 parent 511450c commit 73eda43

File tree

114 files changed

+4015
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+4015
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g2101_2200.s2151_maximum_good_people_based_on_statements
2+
3+
// #Hard #Array #Bit_Manipulation #Backtracking #Enumeration
4+
// #2023_06_26_Time_308_ms_(100.00%)_Space_46.3_MB_(100.00%)
5+
6+
class Solution {
7+
fun maximumGood(statements: Array<IntArray>): Int {
8+
val known = IntArray(statements.size)
9+
known.fill(2)
10+
return max(statements, known, 0)
11+
}
12+
13+
private fun max(statements: Array<IntArray>, known: IntArray, position: Int): Int {
14+
return if (position == statements.size) {
15+
known.asSequence().filter { a: Int -> a == 1 }.count()
16+
} else when (known[position]) {
17+
0 -> assumeBad(statements, known, position)
18+
1 -> assumeGood(statements, known, position)
19+
else -> Math.max(
20+
assumeBad(statements, known, position),
21+
assumeGood(statements, known, position)
22+
)
23+
}
24+
}
25+
26+
private fun assumeBad(statements: Array<IntArray>, known: IntArray, position: Int): Int {
27+
val updatedKnown = known.clone()
28+
updatedKnown[position] = 0
29+
return max(statements, updatedKnown, position + 1)
30+
}
31+
32+
private fun assumeGood(statements: Array<IntArray>, known: IntArray, position: Int): Int {
33+
val updatedKnown = known.clone()
34+
var conflictDetected = false
35+
updatedKnown[position] = 1
36+
for (i in statements[position].indices) {
37+
val answer = statements[position][i]
38+
if (answer != 2) {
39+
if (known[i] != 2 && answer != known[i]) {
40+
conflictDetected = true
41+
break
42+
}
43+
updatedKnown[i] = answer
44+
}
45+
}
46+
return if (conflictDetected) 0 else max(statements, updatedKnown, position + 1)
47+
}
48+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
2151\. Maximum Good People Based on Statements
2+
3+
Hard
4+
5+
There are two types of persons:
6+
7+
* The **good person**: The person who always tells the truth.
8+
* The **bad person**: The person who might tell the truth and might lie.
9+
10+
You are given a **0-indexed** 2D integer array `statements` of size `n x n` that represents the statements made by `n` people about each other. More specifically, `statements[i][j]` could be one of the following:
11+
12+
* `0` which represents a statement made by person `i` that person `j` is a **bad** person.
13+
* `1` which represents a statement made by person `i` that person `j` is a **good** person.
14+
* `2` represents that **no statement** is made by person `i` about person `j`.
15+
16+
Additionally, no person ever makes a statement about themselves. Formally, we have that `statements[i][i] = 2` for all `0 <= i < n`.
17+
18+
Return _the **maximum** number of people who can be **good** based on the statements made by the_ `n` _people_.
19+
20+
**Example 1:**
21+
22+
![](https://assets.leetcode.com/uploads/2022/01/15/logic1.jpg)
23+
24+
**Input:** statements = [[2,1,2],[1,2,2],[2,0,2]]
25+
26+
**Output:** 2
27+
28+
**Explanation:**
29+
30+
Each person makes a single statement.
31+
- Person 0 states that person 1 is good.
32+
- Person 1 states that person 0 is good.
33+
- Person 2 states that person 1 is bad.
34+
Let's take person 2 as the key.
35+
- Assuming that person 2 is a good person:
36+
- Based on the statement made by person 2, person 1 is a bad person.
37+
- Now we know for sure that person 1 is bad and person 2 is good.
38+
- Based on the statement made by person 1, and since person 1 is bad, they could be:
39+
- telling the truth. There will be a contradiction in this case and this assumption is invalid.
40+
- lying. In this case, person 0 is also a bad person and lied in their statement.
41+
- Following that person 2 is a good person, there will be only one good person in the group.
42+
- Assuming that person 2 is a bad person:
43+
- Based on the statement made by person 2, and since person 2 is bad, they could be:
44+
- telling the truth. Following this scenario, person 0 and 1 are both bad as explained before.
45+
- Following that person 2 is bad but told the truth, there will be no good persons in the group.
46+
- lying. In this case person 1 is a good person.
47+
- Since person 1 is a good person, person 0 is also a good person.
48+
- Following that person 2 is bad and lied, there will be two good persons in the group.
49+
50+
We can see that at most 2 persons are good in the best case, so we return 2.
51+
Note that there is more than one way to arrive at this conclusion.
52+
53+
**Example 2:**
54+
55+
![](https://assets.leetcode.com/uploads/2022/01/15/logic2.jpg)
56+
57+
**Input:** statements = [[2,0],[0,2]]
58+
59+
**Output:** 1
60+
61+
**Explanation:**
62+
63+
Each person makes a single statement.
64+
- Person 0 states that person 1 is bad.
65+
- Person 1 states that person 0 is bad.
66+
Let's take person 0 as the key.
67+
- Assuming that person 0 is a good person:
68+
- Based on the statement made by person 0, person 1 is a bad person and was lying.
69+
- Following that person 0 is a good person, there will be only one good person in the group.
70+
- Assuming that person 0 is a bad person:
71+
- Based on the statement made by person 0, and since person 0 is bad, they could be:
72+
- telling the truth. Following this scenario, person 0 and 1 are both bad.
73+
- Following that person 0 is bad but told the truth, there will be no good persons in the group.
74+
- lying. In this case person 1 is a good person.
75+
- Following that person 0 is bad and lied, there will be only one good person in the group.
76+
We can see that at most, one person is good in the best case, so we return 1.
77+
Note that there is more than one way to arrive at this conclusion.
78+
79+
**Constraints:**
80+
81+
* `n == statements.length == statements[i].length`
82+
* `2 <= n <= 15`
83+
* `statements[i][j]` is either `0`, `1`, or `2`.
84+
* `statements[i][i] == 2`
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g2101_2200.s2154_keep_multiplying_found_values_by_two
2+
3+
// #Easy #Array #Hash_Table #Sorting #Simulation
4+
// #2023_06_26_Time_183_ms_(85.71%)_Space_36.9_MB_(100.00%)
5+
6+
@Suppress("NAME_SHADOWING")
7+
class Solution {
8+
fun findFinalValue(nums: IntArray, original: Int): Int {
9+
var original = original
10+
var i = 0
11+
while (i < nums.size) {
12+
if (nums[i] == original) {
13+
original = original * 2
14+
i = -1
15+
}
16+
i++
17+
}
18+
return original
19+
}
20+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2154\. Keep Multiplying Found Values by Two
2+
3+
Easy
4+
5+
You are given an array of integers `nums`. You are also given an integer `original` which is the first number that needs to be searched for in `nums`.
6+
7+
You then do the following steps:
8+
9+
1. If `original` is found in `nums`, **multiply** it by two (i.e., set `original = 2 * original`).
10+
2. Otherwise, **stop** the process.
11+
3. **Repeat** this process with the new number as long as you keep finding the number.
12+
13+
Return _the **final** value of_ `original`.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [5,3,6,1,12], original = 3
18+
19+
**Output:** 24
20+
21+
**Explanation:**
22+
- 3 is found in nums. 3 is multiplied by 2 to obtain 6.
23+
24+
- 6 is found in nums. 6 is multiplied by 2 to obtain 12.
25+
26+
- 12 is found in nums. 12 is multiplied by 2 to obtain 24.
27+
28+
- 24 is not found in nums.
29+
Thus, 24 is returned.
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [2,7,9], original = 4
34+
35+
**Output:** 4
36+
37+
**Explanation:**
38+
- 4 is not found in nums.
39+
40+
Thus, 4 is returned.
41+
42+
**Constraints:**
43+
44+
* `1 <= nums.length <= 1000`
45+
* `1 <= nums[i], original <= 1000`
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g2101_2200.s2155_all_divisions_with_the_highest_score_of_a_binary_array
2+
3+
// #Medium #Array #2023_06_26_Time_1171_ms_(100.00%)_Space_56.5_MB_(100.00%)
4+
5+
class Solution {
6+
fun maxScoreIndices(nums: IntArray): List<Int> {
7+
var curone = 0
8+
var curzero = 0
9+
var max = 0
10+
for (i in nums) {
11+
curone += i
12+
}
13+
val list: MutableList<Int> = ArrayList()
14+
for (i in nums.indices) {
15+
if (curzero + curone > max) {
16+
list.clear()
17+
list.add(i)
18+
max = curzero + curone
19+
} else if (curzero + curone == max) {
20+
list.add(i)
21+
}
22+
if (nums[i] == 1) {
23+
curone--
24+
} else {
25+
curzero++
26+
}
27+
}
28+
if (curzero > max) {
29+
list.clear()
30+
list.add(nums.size)
31+
} else if (curzero == max) {
32+
list.add(nums.size)
33+
}
34+
return list
35+
}
36+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2155\. All Divisions With the Highest Score of a Binary Array
2+
3+
Medium
4+
5+
You are given a **0-indexed** binary array `nums` of length `n`. `nums` can be divided at index `i` (where `0 <= i <= n)` into two arrays (possibly empty) <code>nums<sub>left</sub></code> and <code>nums<sub>right</sub></code>:
6+
7+
* <code>nums<sub>left</sub></code> has all the elements of `nums` between index `0` and `i - 1` **(inclusive)**, while <code>nums<sub>right</sub></code> has all the elements of nums between index `i` and `n - 1` **(inclusive)**.
8+
* If `i == 0`, <code>nums<sub>left</sub></code> is **empty**, while <code>nums<sub>right</sub></code> has all the elements of `nums`.
9+
* If `i == n`, <code>nums<sub>left</sub></code> has all the elements of nums, while <code>nums<sub>right</sub></code> is **empty**.
10+
11+
The **division score** of an index `i` is the **sum** of the number of `0`'s in <code>nums<sub>left</sub></code> and the number of `1`'s in <code>nums<sub>right</sub></code>.
12+
13+
Return _**all distinct indices** that have the **highest** possible **division score**_. You may return the answer in **any order**.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [0,0,1,0]
18+
19+
**Output:** [2,4]
20+
21+
**Explanation:** Division at index
22+
- 0: nums<sub>left</sub> is []. nums<sub>right</sub> is [0,0,**1**,0]. The score is 0 + 1 = 1.
23+
24+
- 1: nums<sub>left</sub> is [**0**]. nums<sub>right</sub> is [0,**1**,0]. The score is 1 + 1 = 2.
25+
26+
- 2: nums<sub>left</sub> is [**0**,**0**]. nums<sub>right</sub> is [**1**,0]. The score is 2 + 1 = 3.
27+
28+
- 3: nums<sub>left</sub> is [**0**,**0**,1]. nums<sub>right</sub> is [0]. The score is 2 + 0 = 2.
29+
30+
- 4: nums<sub>left</sub> is [**0**,**0**,1,**0**]. nums<sub>right</sub> is []. The score is 3 + 0 = 3.
31+
32+
Indices 2 and 4 both have the highest possible division score 3.
33+
34+
Note the answer [4,2] would also be accepted.
35+
36+
**Example 2:**
37+
38+
**Input:** nums = [0,0,0]
39+
40+
**Output:** [3]
41+
42+
**Explanation:** Division at index
43+
- 0: nums<sub>left</sub> is []. nums<sub>right</sub> is [0,0,0]. The score is 0 + 0 = 0.
44+
45+
- 1: nums<sub>left</sub> is [**0**]. nums<sub>right</sub> is [0,0]. The score is 1 + 0 = 1.
46+
47+
- 2: nums<sub>left</sub> is [**0**,**0**]. nums<sub>right</sub> is [0]. The score is 2 + 0 = 2.
48+
49+
- 3: nums<sub>left</sub> is [**0**,**0**,**0**]. nums<sub>right</sub> is []. The score is 3 + 0 = 3.
50+
51+
Only index 3 has the highest possible division score 3.
52+
53+
**Example 3:**
54+
55+
**Input:** nums = [1,1]
56+
57+
**Output:** [0]
58+
59+
**Explanation:** Division at index
60+
- 0: nums<sub>left</sub> is []. nums<sub>right</sub> is [**1**,**1**]. The score is 0 + 2 = 2.
61+
62+
- 1: nums<sub>left</sub> is [1]. nums<sub>right</sub> is [**1**]. The score is 0 + 1 = 1.
63+
64+
- 2: nums<sub>left</sub> is [1,1]. nums<sub>right</sub> is []. The score is 0 + 0 = 0.
65+
66+
Only index 0 has the highest possible division score 2.
67+
68+
**Constraints:**
69+
70+
* `n == nums.length`
71+
* <code>1 <= n <= 10<sup>5</sup></code>
72+
* `nums[i]` is either `0` or `1`.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g2101_2200.s2156_find_substring_with_given_hash_value
2+
3+
// #Hard #String #Sliding_Window #Hash_Function #Rolling_Hash
4+
// #2023_06_26_Time_248_ms_(100.00%)_Space_37.5_MB_(100.00%)
5+
6+
class Solution {
7+
fun subStrHash(s: String, power: Int, modulo: Int, k: Int, hashValue: Int): String {
8+
var mul1: Long = 1
9+
var times = k - 1
10+
while (times-- > 0) {
11+
mul1 = mul1 * power % modulo
12+
}
13+
var index = -1
14+
var hash: Long = 0
15+
var end = s.length - 1
16+
for (i in s.length - 1 downTo 0) {
17+
val `val` = s[i].code - 96
18+
hash = (hash * power % modulo + `val`) % modulo
19+
if (end - i + 1 == k) {
20+
if (hash == hashValue.toLong()) {
21+
index = i
22+
}
23+
hash = (hash - (s[end].code - 96) * mul1 % modulo + modulo) % modulo
24+
end--
25+
}
26+
}
27+
return s.substring(index, index + k)
28+
}
29+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2156\. Find Substring With Given Hash Value
2+
3+
Hard
4+
5+
The hash of a **0-indexed** string `s` of length `k`, given integers `p` and `m`, is computed using the following function:
6+
7+
* <code>hash(s, p, m) = (val(s[0]) * p<sup>0</sup> + val(s[1]) * p<sup>1</sup> + ... + val(s[k-1]) * p<sup>k-1</sup>) mod m</code>.
8+
9+
Where `val(s[i])` represents the index of `s[i]` in the alphabet from `val('a') = 1` to `val('z') = 26`.
10+
11+
You are given a string `s` and the integers `power`, `modulo`, `k`, and `hashValue.` Return `sub`, _the **first** **substring** of_ `s` _of length_ `k` _such that_ `hash(sub, power, modulo) == hashValue`.
12+
13+
The test cases will be generated such that an answer always **exists**.
14+
15+
A **substring** is a contiguous non-empty sequence of characters within a string.
16+
17+
**Example 1:**
18+
19+
**Input:** s = "leetcode", power = 7, modulo = 20, k = 2, hashValue = 0
20+
21+
**Output:** "ee"
22+
23+
**Explanation:** The hash of "ee" can be computed to be hash("ee", 7, 20) = (5 \* 1 + 5 \* 7) mod 20 = 40 mod 20 = 0. "ee" is the first substring of length 2 with hashValue 0. Hence, we return "ee".
24+
25+
**Example 2:**
26+
27+
**Input:** s = "fbxzaad", power = 31, modulo = 100, k = 3, hashValue = 32
28+
29+
**Output:** "fbx"
30+
31+
**Explanation:** The hash of "fbx" can be computed to be hash("fbx", 31, 100) = (6 \* 1 + 2 \* 31 + 24 \* 31<sup>2</sup>) mod 100 = 23132 mod 100 = 32.
32+
33+
The hash of "bxz" can be computed to be hash("bxz", 31, 100) = (2 \* 1 + 24 \* 31 + 26 \* 31<sup>2</sup>) mod 100 = 25732 mod 100 = 32. "fbx" is the first substring of length 3 with hashValue 32. Hence, we return "fbx".
34+
35+
Note that "bxz" also has a hash of 32 but it appears later than "fbx".
36+
37+
**Constraints:**
38+
39+
* <code>1 <= k <= s.length <= 2 * 10<sup>4</sup></code>
40+
* <code>1 <= power, modulo <= 10<sup>9</sup></code>
41+
* `0 <= hashValue < modulo`
42+
* `s` consists of lowercase English letters only.
43+
* The test cases are generated such that an answer always **exists**.

0 commit comments

Comments
 (0)