Skip to content

Commit 9f1c86b

Browse files
authored
Added tasks 1813-1824
1 parent ae16d67 commit 9f1c86b

File tree

31 files changed

+1056
-0
lines changed

31 files changed

+1056
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.13'
617617
|-|-|-|-|-|-
618618
| 0155 |[Min Stack](src.save/main/kotlin/g0101_0200/s0155_min_stack/MinStack.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Stack, Design | 331 | 84.88
619619
| 1249 |[Minimum Remove to Make Valid Parentheses](src.save/main/kotlin/g1201_1300/s1249_minimum_remove_to_make_valid_parentheses/Solution.kt)| Medium | String, Stack | 218 | 100.00
620+
| 1823 |[Find the Winner of the Circular Game](src/main/kotlin/g1801_1900/s1823_find_the_winner_of_the_circular_game/Solution.kt)| Medium | Array, Math, Simulation, Recursion, Queue | 119 | 87.50
620621

621622
#### Day 15 Tree
622623

@@ -1057,6 +1058,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.13'
10571058
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10581059
|-|-|-|-|-|-
10591060
| 1482 |[Minimum Number of Days to Make m Bouquets](src.save/main/kotlin/g1401_1500/s1482_minimum_number_of_days_to_make_m_bouquets/Solution.kt)| Medium | Array, Binary_Search | 538 | 50.00
1061+
| 1818 |[Minimum Absolute Sum Difference](src/main/kotlin/g1801_1900/s1818_minimum_absolute_sum_difference/Solution.kt)| Medium | Array, Sorting, Binary_Search, Ordered_Set | 447 | 100.00
10601062

10611063
#### Day 8
10621064

@@ -1327,6 +1329,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.13'
13271329

13281330
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
13291331
|-|-|-|-|-|-
1332+
| 1822 |[Sign of the Product of an Array](src/main/kotlin/g1801_1900/s1822_sign_of_the_product_of_an_array/Solution.kt)| Easy | Array, Math | 170 | 92.51
13301333
| 1502 |[Can Make Arithmetic Progression From Sequence](src.save/main/kotlin/g1501_1600/s1502_can_make_arithmetic_progression_from_sequence/Solution.kt)| Easy | Array, Sorting | 156 | 94.82
13311334
| 0202 |[Happy Number](src.save/main/kotlin/g0201_0300/s0202_happy_number/Solution.kt)| Easy | Top_Interview_Questions, Hash_Table, Math, Two_Pointers | 261 | 45.08
13321335
| 1790 |[Check if One String Swap Can Make Strings Equal](src.save/main/kotlin/g1701_1800/s1790_check_if_one_string_swap_can_make_strings_equal/Solution.kt)| Easy | String, Hash_Table, Counting | 138 | 100.00
@@ -1831,6 +1834,16 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.13'
18311834

18321835
| # | Title | Difficulty | Tag | Time, ms | Time, %
18331836
|------|----------------|-------------|-------------|----------|---------
1837+
| 1824 |[Minimum Sideway Jumps](src/main/kotlin/g1801_1900/s1824_minimum_sideway_jumps/Solution.kt)| Medium | Array, Dynamic_Programming, Greedy | 726 | 100.00
1838+
| 1823 |[Find the Winner of the Circular Game](src/main/kotlin/g1801_1900/s1823_find_the_winner_of_the_circular_game/Solution.kt)| Medium | Array, Math, Simulation, Recursion, Queue, Data_Structure_II_Day_14_Stack_Queue | 119 | 87.50
1839+
| 1822 |[Sign of the Product of an Array](src/main/kotlin/g1801_1900/s1822_sign_of_the_product_of_an_array/Solution.kt)| Easy | Array, Math, Programming_Skills_I_Day_4_Loop | 170 | 92.51
1840+
| 1819 |[Number of Different Subsequences GCDs](src/main/kotlin/g1801_1900/s1819_number_of_different_subsequences_gcds/Solution.kt)| Hard | Array, Math, Counting, Number_Theory | 624 | 100.00
1841+
| 1818 |[Minimum Absolute Sum Difference](src/main/kotlin/g1801_1900/s1818_minimum_absolute_sum_difference/Solution.kt)| Medium | Array, Sorting, Binary_Search, Ordered_Set, Binary_Search_II_Day_7 | 447 | 100.00
1842+
| 1817 |[Finding the Users Active Minutes](src/main/kotlin/g1801_1900/s1817_finding_the_users_active_minutes/Solution.kt)| Medium | Array, Hash_Table | 652 | 100.00
1843+
| 1816 |[Truncate Sentence](src/main/kotlin/g1801_1900/s1816_truncate_sentence/Solution.kt)| Easy | Array, String | 147 | 94.74
1844+
| 1815 |[Maximum Number of Groups Getting Fresh Donuts](src/main/kotlin/g1801_1900/s1815_maximum_number_of_groups_getting_fresh_donuts/Solution.kt)| Hard | Array, Dynamic_Programming, Bit_Manipulation, Bitmask, Memoization | 1073 | 100.00
1845+
| 1814 |[Count Nice Pairs in an Array](src/main/kotlin/g1801_1900/s1814_count_nice_pairs_in_an_array/Solution.kt)| Medium | Array, Hash_Table, Math, Counting | 520 | 100.00
1846+
| 1813 |[Sentence Similarity III](src/main/kotlin/g1801_1900/s1813_sentence_similarity_iii/Solution.kt)| Medium | Array, String, Two_Pointers | 162 | 100.00
18341847
| 1812 |[Determine Color of a Chessboard Square](src/main/kotlin/g1801_1900/s1812_determine_color_of_a_chessboard_square/Solution.kt)| Easy | String, Math | 127 | 92.86
18351848
| 1808 |[Maximize Number of Nice Divisors](src/main/kotlin/g1801_1900/s1808_maximize_number_of_nice_divisors/Solution.kt)| Hard | Math, Recursion | 135 | 100.00
18361849
| 1807 |[Evaluate the Bracket Pairs of a String](src/main/kotlin/g1801_1900/s1807_evaluate_the_bracket_pairs_of_a_string/Solution.kt)| Medium | Array, String, Hash_Table | 689 | 100.00
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g1801_1900.s1813_sentence_similarity_iii
2+
3+
// #Medium #Array #String #Two_Pointers #2023_06_20_Time_162_ms_(100.00%)_Space_35.9_MB_(100.00%)
4+
5+
class Solution {
6+
fun areSentencesSimilar(sentence1: String, sentence2: String): Boolean {
7+
val words1 = sentence1.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
8+
val words2 = sentence2.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
9+
var i = 0
10+
val n1 = words1.size
11+
val n2 = words2.size
12+
if (n1 > n2) {
13+
return areSentencesSimilar(sentence2, sentence1)
14+
}
15+
while (i < n1 && words1[i] == words2[i]) {
16+
++i
17+
}
18+
while (i < n1 && words1[i] == words2[n2 - n1 + i]) {
19+
++i
20+
}
21+
return i == n1
22+
}
23+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
1813\. Sentence Similarity III
2+
3+
Medium
4+
5+
A sentence is a list of words that are separated by a single space with no leading or trailing spaces. For example, `"Hello World"`, `"HELLO"`, `"hello world hello world"` are all sentences. Words consist of **only** uppercase and lowercase English letters.
6+
7+
Two sentences `sentence1` and `sentence2` are **similar** if it is possible to insert an arbitrary sentence **(possibly empty)** inside one of these sentences such that the two sentences become equal. For example, `sentence1 = "Hello my name is Jane"` and `sentence2 = "Hello Jane"` can be made equal by inserting `"my name is"` between `"Hello"` and `"Jane"` in `sentence2`.
8+
9+
Given two sentences `sentence1` and `sentence2`, return `true` _if_ `sentence1` _and_ `sentence2` _are similar._ Otherwise, return `false`.
10+
11+
**Example 1:**
12+
13+
**Input:** sentence1 = "My name is Haley", sentence2 = "My Haley"
14+
15+
**Output:** true
16+
17+
**Explanation:** sentence2 can be turned to sentence1 by inserting "name is" between "My" and "Haley".
18+
19+
**Example 2:**
20+
21+
**Input:** sentence1 = "of", sentence2 = "A lot of words"
22+
23+
**Output:** false
24+
25+
**Explanation:** No single sentence can be inserted inside one of the sentences to make it equal to the other.
26+
27+
**Example 3:**
28+
29+
**Input:** sentence1 = "Eating right now", sentence2 = "Eating"
30+
31+
**Output:** true
32+
33+
**Explanation:** sentence2 can be turned to sentence1 by inserting "right now" at the end of the sentence.
34+
35+
**Constraints:**
36+
37+
* `1 <= sentence1.length, sentence2.length <= 100`
38+
* `sentence1` and `sentence2` consist of lowercase and uppercase English letters and spaces.
39+
* The words in `sentence1` and `sentence2` are separated by a single space.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g1801_1900.s1814_count_nice_pairs_in_an_array
2+
3+
// #Medium #Array #Hash_Table #Math #Counting
4+
// #2023_06_20_Time_520_ms_(100.00%)_Space_58.8_MB_(100.00%)
5+
6+
@Suppress("NAME_SHADOWING")
7+
class Solution {
8+
private fun rev(n: Int): Int {
9+
var n = n
10+
var r = 0
11+
while (n > 0) {
12+
r = r * 10 + n % 10
13+
n /= 10
14+
}
15+
return r
16+
}
17+
18+
fun countNicePairs(nums: IntArray): Int {
19+
val revMap = HashMap<Int, Int>()
20+
var cnt = 0
21+
for (num in nums) {
22+
val lhs = num - rev(num)
23+
val prevCnt = revMap.getOrDefault(lhs, 0)
24+
cnt += prevCnt
25+
val mod = 1000000007
26+
cnt %= mod
27+
revMap[lhs] = prevCnt + 1
28+
}
29+
return cnt
30+
}
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
1814\. Count Nice Pairs in an Array
2+
3+
Medium
4+
5+
You are given an array `nums` that consists of non-negative integers. Let us define `rev(x)` as the reverse of the non-negative integer `x`. For example, `rev(123) = 321`, and `rev(120) = 21`. A pair of indices `(i, j)` is **nice** if it satisfies all of the following conditions:
6+
7+
* `0 <= i < j < nums.length`
8+
* `nums[i] + rev(nums[j]) == nums[j] + rev(nums[i])`
9+
10+
Return _the number of nice pairs of indices_. Since that number can be too large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [42,11,1,97]
15+
16+
**Output:** 2
17+
18+
**Explanation:** The two pairs are:
19+
20+
- (0,3) : 42 + rev(97) = 42 + 79 = 121, 97 + rev(42) = 97 + 24 = 121.
21+
22+
- (1,2) : 11 + rev(1) = 11 + 1 = 12, 1 + rev(11) = 1 + 11 = 12.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [13,10,35,24,76]
27+
28+
**Output:** 4
29+
30+
**Constraints:**
31+
32+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
33+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package g1801_1900.s1815_maximum_number_of_groups_getting_fresh_donuts
2+
3+
// #Hard #Array #Dynamic_Programming #Bit_Manipulation #Bitmask #Memoization
4+
// #2023_06_20_Time_1073_ms_(100.00%)_Space_71.3_MB_(100.00%)
5+
6+
import java.util.Objects
7+
8+
class Solution {
9+
inner class Data(var idx: Int, var arrHash: Int) {
10+
override fun equals(other: Any?): Boolean {
11+
if (this === other) {
12+
return true
13+
}
14+
if (other == null || javaClass != other.javaClass) {
15+
return false
16+
}
17+
val data = other as Data
18+
return idx == data.idx && arrHash == data.arrHash
19+
}
20+
21+
override fun hashCode(): Int {
22+
return Objects.hash(idx, arrHash)
23+
}
24+
}
25+
26+
private var dp: HashMap<Data, Int> = HashMap()
27+
fun maxHappyGroups(batchSize: Int, groups: IntArray): Int {
28+
val arr = IntArray(batchSize)
29+
for (group in groups) {
30+
arr[group % batchSize]++
31+
}
32+
return arr[0] + solve(0, arr)
33+
}
34+
35+
private fun solve(num: Int, arr: IntArray): Int {
36+
if (isFull(arr)) {
37+
return 0
38+
}
39+
val key = Data(num, arr.contentHashCode())
40+
if (dp.containsKey(key)) {
41+
return dp[key]!!
42+
}
43+
var best = Int.MIN_VALUE / 2
44+
if (num == 0) {
45+
for (i in 1 until arr.size) {
46+
if (arr[i] <= 0) {
47+
continue
48+
}
49+
arr[i]--
50+
best = Math.max(best, 1 + solve(i, arr))
51+
arr[i]++
52+
}
53+
} else {
54+
for (i in 1 until arr.size) {
55+
if (arr[i] > 0) {
56+
arr[i]--
57+
best = best.coerceAtLeast(solve((num + i) % arr.size, arr))
58+
arr[i]++
59+
}
60+
}
61+
}
62+
dp[key] = best
63+
return best
64+
}
65+
66+
private fun isFull(arr: IntArray): Boolean {
67+
var sum = 0
68+
for (i in 1 until arr.size) {
69+
sum += arr[i]
70+
}
71+
return sum == 0
72+
}
73+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
1815\. Maximum Number of Groups Getting Fresh Donuts
2+
3+
Hard
4+
5+
There is a donuts shop that bakes donuts in batches of `batchSize`. They have a rule where they must serve **all** of the donuts of a batch before serving any donuts of the next batch. You are given an integer `batchSize` and an integer array `groups`, where `groups[i]` denotes that there is a group of `groups[i]` customers that will visit the shop. Each customer will get exactly one donut.
6+
7+
When a group visits the shop, all customers of the group must be served before serving any of the following groups. A group will be happy if they all get fresh donuts. That is, the first customer of the group does not receive a donut that was left over from the previous group.
8+
9+
You can freely rearrange the ordering of the groups. Return _the **maximum** possible number of happy groups after rearranging the groups._
10+
11+
**Example 1:**
12+
13+
**Input:** batchSize = 3, groups = [1,2,3,4,5,6]
14+
15+
**Output:** 4
16+
17+
**Explanation:** You can arrange the groups as [6,2,4,5,1,3]. Then the 1<sup>st</sup>, 2<sup>nd</sup>, 4<sup>th</sup>, and 6<sup>th</sup> groups will be happy.
18+
19+
**Example 2:**
20+
21+
**Input:** batchSize = 4, groups = [1,3,2,5,2,2,1,6]
22+
23+
**Output:** 4
24+
25+
**Constraints:**
26+
27+
* `1 <= batchSize <= 9`
28+
* `1 <= groups.length <= 30`
29+
* <code>1 <= groups[i] <= 10<sup>9</sup></code>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g1801_1900.s1816_truncate_sentence
2+
3+
// #Easy #Array #String #2023_06_20_Time_147_ms_(94.74%)_Space_35.9_MB_(78.95%)
4+
5+
class Solution {
6+
fun truncateSentence(s: String, k: Int): String {
7+
val words = s.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
8+
val sb = StringBuilder()
9+
for (i in 0 until k) {
10+
sb.append(words[i])
11+
sb.append(" ")
12+
}
13+
return sb.substring(0, sb.toString().length - 1)
14+
}
15+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
1816\. Truncate Sentence
2+
3+
Easy
4+
5+
A **sentence** is a list of words that are separated by a single space with no leading or trailing spaces. Each of the words consists of **only** uppercase and lowercase English letters (no punctuation).
6+
7+
* For example, `"Hello World"`, `"HELLO"`, and `"hello world hello world"` are all sentences.
8+
9+
You are given a sentence `s` and an integer `k`. You want to **truncate** `s` such that it contains only the **first** `k` words. Return `s`_ after **truncating** it._
10+
11+
**Example 1:**
12+
13+
**Input:** s = "Hello how are you Contestant", k = 4
14+
15+
**Output:** "Hello how are you"
16+
17+
**Explanation:**
18+
19+
The words in s are ["Hello", "how" "are", "you", "Contestant"].
20+
21+
The first 4 words are ["Hello", "how", "are", "you"].
22+
23+
Hence, you should return "Hello how are you".
24+
25+
**Example 2:**
26+
27+
**Input:** s = "What is the solution to this problem", k = 4
28+
29+
**Output:** "What is the solution"
30+
31+
**Explanation:**
32+
33+
The words in s are ["What", "is" "the", "solution", "to", "this", "problem"].
34+
35+
The first 4 words are ["What", "is", "the", "solution"].
36+
37+
Hence, you should return "What is the solution".
38+
39+
**Example 3:**
40+
41+
**Input:** s = "chopper is not a tanuki", k = 5
42+
43+
**Output:** "chopper is not a tanuki"
44+
45+
**Constraints:**
46+
47+
* `1 <= s.length <= 500`
48+
* `k` is in the range `[1, the number of words in s]`.
49+
* `s` consist of only lowercase and uppercase English letters and spaces.
50+
* The words in `s` are separated by a single space.
51+
* There are no leading or trailing spaces.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g1801_1900.s1817_finding_the_users_active_minutes
2+
3+
// #Medium #Array #Hash_Table #2023_06_20_Time_652_ms_(100.00%)_Space_57.8_MB_(80.00%)
4+
5+
class Solution {
6+
fun findingUsersActiveMinutes(logs: Array<IntArray>, k: Int): IntArray {
7+
if (logs.size == 1) {
8+
val res = IntArray(k)
9+
res[0] = 1
10+
return res
11+
}
12+
logs.sortWith(compareBy { a: IntArray -> a[0] }.thenComparingInt { a: IntArray -> a[1] })
13+
val result = IntArray(k)
14+
var start = 1
15+
var prevUser = logs[0][0]
16+
var prevMin = logs[0][1]
17+
var count = 1
18+
while (true) {
19+
while (start < logs.size && prevUser == logs[start][0]) {
20+
if (prevMin != logs[start][1]) {
21+
count++
22+
}
23+
prevMin = logs[start][1]
24+
start++
25+
}
26+
result[count - 1]++
27+
if (start >= logs.size) {
28+
break
29+
}
30+
count = 1
31+
prevUser = logs[start][0]
32+
prevMin = logs[start][1]
33+
}
34+
return result
35+
}
36+
}

0 commit comments

Comments
 (0)