Skip to content

Commit f9a7e81

Browse files
committed
Added tasks 3492-3495
1 parent ed00724 commit f9a7e81

File tree

32 files changed

+555
-198
lines changed
  • src/main/kotlin
    • g0001_0100
      • s0034_find_first_and_last_position_of_element_in_sorted_array
      • s0068_text_justification
    • g0201_0300
    • g0301_0400
    • g0401_0500/s0409_longest_palindrome
    • g0701_0800/s0770_basic_calculator_iv
    • g0801_0900/s0811_subdomain_visit_count
    • g0901_1000/s0989_add_to_array_form_of_integer
    • g1001_1100/s1048_longest_string_chain
    • g1601_1700/s1639_number_of_ways_to_form_a_target_string_given_a_dictionary
    • g1701_1800
    • g2001_2100/s2019_the_score_of_students_solving_math_expression
    • g2201_2300
      • s2213_longest_substring_of_one_repeating_character
      • s2281_sum_of_total_strength_of_wizards
    • g2401_2500/s2452_words_within_two_edits_of_dictionary
    • g2501_2600
      • s2553_separate_the_digits_in_an_array
      • s2574_left_and_right_sum_differences
    • g2701_2800/s2729_check_if_the_number_is_fascinating
    • g3201_3300
      • s3245_alternating_groups_iii
      • s3283_maximum_number_of_moves_to_kill_all_pawns
    • g3401_3500
      • s3486_longest_special_path_ii
      • s3490_count_beautiful_numbers
      • s3492_maximum_containers_on_a_ship
      • s3493_properties_graph
      • s3494_find_the_minimum_amount_of_time_to_brew_potions
      • s3495_minimum_operations_to_make_array_elements_zero

32 files changed

+555
-198
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,10 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3495 |[Minimum Operations to Make Array Elements Zero](src/main/kotlin/g3401_3500/s3495_minimum_operations_to_make_array_elements_zero)| Hard | Array, Math, Bit_Manipulation | 12 | 100.00
2092+
| 3494 |[Find the Minimum Amount of Time to Brew Potions](src/main/kotlin/g3401_3500/s3494_find_the_minimum_amount_of_time_to_brew_potions)| Medium | Array, Simulation, Prefix_Sum | 70 | 100.00
2093+
| 3493 |[Properties Graph](src/main/kotlin/g3401_3500/s3493_properties_graph)| Medium | Array, Hash_Table, Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 45 | 100.00
2094+
| 3492 |[Maximum Containers on a Ship](src/main/kotlin/g3401_3500/s3492_maximum_containers_on_a_ship)| Easy | Math | 0 | 100.00
20912095
| 3490 |[Count Beautiful Numbers](src/main/kotlin/g3401_3500/s3490_count_beautiful_numbers)| Hard | Dynamic_Programming | 246 | 100.00
20922096
| 3489 |[Zero Array Transformation IV](src/main/kotlin/g3401_3500/s3489_zero_array_transformation_iv)| Medium | Array, Dynamic_Programming | 104 | 100.00
20932097
| 3488 |[Closest Equal Element Queries](src/main/kotlin/g3401_3500/s3488_closest_equal_element_queries)| Medium | Array, Hash_Table, Binary_Search | 93 | 100.00
@@ -2259,7 +2263,7 @@
22592263
| 3287 |[Find the Maximum Sequence Value of Array](src/main/kotlin/g3201_3300/s3287_find_the_maximum_sequence_value_of_array)| Hard | Array, Dynamic_Programming, Bit_Manipulation | 2893 | 33.33
22602264
| 3286 |[Find a Safe Walk Through a Grid](src/main/kotlin/g3201_3300/s3286_find_a_safe_walk_through_a_grid)| Medium | Array, Breadth_First_Search, Matrix, Heap_Priority_Queue, Graph, Shortest_Path | 357 | 48.28
22612265
| 3285 |[Find Indices of Stable Mountains](src/main/kotlin/g3201_3300/s3285_find_indices_of_stable_mountains)| Easy | Array | 195 | 92.68
2262-
| 3283 |[Maximum Number of Moves to Kill All Pawns](src/main/kotlin/g3201_3300/s3283_maximum_number_of_moves_to_kill_all_pawns)| Hard | Array, Math, Breadth_First_Search, Bit_Manipulation, Bitmask, Game_Theory | 638 | 100.00
2266+
| 3283 |[Maximum Number of Moves to Kill All Pawns](src/main/kotlin/g3201_3300/s3283_maximum_number_of_moves_to_kill_all_pawns)| Hard | Array, Math, Breadth_First_Search, Bit_Manipulation, Bitmask, Game_Theory | 147 | 100.00
22632267
| 3282 |[Reach End of Array With Max Score](src/main/kotlin/g3201_3300/s3282_reach_end_of_array_with_max_score)| Medium | Array, Greedy | 789 | 90.91
22642268
| 3281 |[Maximize Score of Numbers in Ranges](src/main/kotlin/g3201_3300/s3281_maximize_score_of_numbers_in_ranges)| Medium | Array, Sorting, Greedy, Binary_Search | 710 | 88.24
22652269
| 3280 |[Convert Date to Binary](src/main/kotlin/g3201_3300/s3280_convert_date_to_binary)| Easy | String, Math | 174 | 79.31
@@ -3087,7 +3091,7 @@
30873091
| 2217 |[Find Palindrome With Fixed Length](src/main/kotlin/g2201_2300/s2217_find_palindrome_with_fixed_length)| Medium | Array, Math | 541 | 100.00
30883092
| 2216 |[Minimum Deletions to Make Array Beautiful](src/main/kotlin/g2201_2300/s2216_minimum_deletions_to_make_array_beautiful)| Medium | Array, Greedy, Stack | 567 | 66.67
30893093
| 2215 |[Find the Difference of Two Arrays](src/main/kotlin/g2201_2300/s2215_find_the_difference_of_two_arrays)| Easy | Array, Hash_Table | 352 | 98.63
3090-
| 2213 |[Longest Substring of One Repeating Character](src/main/kotlin/g2201_2300/s2213_longest_substring_of_one_repeating_character)| Hard | Array, String, Ordered_Set, Segment_Tree | 879 | 100.00
3094+
| 2213 |[Longest Substring of One Repeating Character](src/main/kotlin/g2201_2300/s2213_longest_substring_of_one_repeating_character)| Hard | Array, String, Ordered_Set, Segment_Tree | 73 | 100.00
30913095
| 2212 |[Maximum Points in an Archery Competition](src/main/kotlin/g2201_2300/s2212_maximum_points_in_an_archery_competition)| Medium | Array, Bit_Manipulation, Recursion, Enumeration | 210 | 100.00
30923096
| 2211 |[Count Collisions on a Road](src/main/kotlin/g2201_2300/s2211_count_collisions_on_a_road)| Medium | String, Stack | 325 | 100.00
30933097
| 2210 |[Count Hills and Valleys in an Array](src/main/kotlin/g2201_2300/s2210_count_hills_and_valleys_in_an_array)| Easy | Array | 153 | 80.00

src/main/kotlin/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ You must write an algorithm with `O(log n)` runtime complexity.
3939
## Solution
4040

4141
```kotlin
42-
class Solution constructor() {
42+
class Solution {
4343
fun searchRange(nums: IntArray, target: Int): IntArray {
4444
val ans = IntArray(2)
4545
ans[0] = helper(nums, target, false)

src/main/kotlin/g0001_0100/s0068_text_justification/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class Solution {
102102
startWord = i + 1
103103
// resetting these to 0 for processing next line
104104
lineTotal = 0
105-
numWordsOnLine = lineTotal
105+
numWordsOnLine = 0
106106
// need a new StringBuilder for the next line
107107
sb = StringBuilder(maxWidth)
108108
}

src/main/kotlin/g0201_0300/s0289_game_of_life/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class Solution {
9191
}
9292

9393
private fun compute(board: Array<IntArray>, r: Int, c: Int): Int {
94-
var ret: Int = 0
94+
var ret = 0
9595
for (arr in dim) {
9696
val row = arr[0] + r
9797
val col = arr[1] + c

src/main/kotlin/g0201_0300/s0292_nim_game/readme.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ Given `n`, the number of stones in the heap, return `true` _if you can win the g
4343
```kotlin
4444
class Solution {
4545
fun canWinNim(n: Int): Boolean {
46-
if (n % 4 == 0) {
47-
return false
48-
}
49-
return true
46+
return n % 4 != 0
5047
}
5148
}
5249
```

src/main/kotlin/g0301_0400/s0304_range_sum_query_2d_immutable/readme.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ Implement the NumMatrix class:
4646

4747
```kotlin
4848
class NumMatrix(matrix: Array<IntArray>) {
49-
private val M = matrix.size
50-
private val N = if (M > 0) matrix[0].size else 0
49+
private val m = matrix.size
50+
private val n = if (m > 0) matrix[0].size else 0
5151

52-
var array = Array<IntArray> (M + 1) { IntArray(N + 1) }
52+
var array = Array<IntArray> (m + 1) { IntArray(n + 1) }
5353

5454
init {
55-
for (i in 1..M) {
56-
for (j in 1..N) {
55+
for (i in 1..m) {
56+
for (j in 1..n) {
5757
array[i][j] = matrix[i - 1][j - 1] + array[i][j - 1] + array[i - 1][j] - array[i - 1][j - 1]
5858
}
5959
}

src/main/kotlin/g0301_0400/s0310_minimum_height_trees/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Solution {
5858
while (queue.isNotEmpty()) {
5959
val size = queue.size
6060
val newLeaves = mutableListOf<Int>()
61-
for (_sz in 0 until size) {
61+
for (sz in 0 until size) {
6262
val cur = queue.removeFirst()
6363
newLeaves.add(cur)
6464
for (next in graph[cur]) {

src/main/kotlin/g0301_0400/s0312_burst_balloons/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Solution {
5050
while (ei < nums.size) {
5151
val l = if (si - 1 == -1) 1 else nums[si - 1]
5252
val r = if (ei + 1 == nums.size) 1 else nums[ei + 1]
53-
var maxAns = -1e7.toInt()
53+
var maxAns = (-1e7).toInt()
5454
for (cut in si..ei) {
5555
val leftAns = if (si == cut) 0 else dp[si][cut - 1]
5656
val rightAns = if (ei == cut) 0 else dp[cut + 1][ei]

src/main/kotlin/g0401_0500/s0409_longest_palindrome/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Solution {
4141
for (c in s.toCharArray()) {
4242
set.flip(c.code - 'A'.code)
4343
}
44-
return if (set.isEmpty()) {
44+
return if (set.isEmpty) {
4545
s.length
4646
} else {
4747
s.length - set.cardinality() + 1

src/main/kotlin/g0701_0800/s0770_basic_calculator_iv/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class Solution {
155155
}
156156

157157
private fun make(cur: String): Node {
158-
val ans: Node = Node()
158+
val ans = Node()
159159
val tmp: MutableList<String> = ArrayList()
160160
if (Character.isDigit(cur[0])) {
161161
ans.update(tmp, Integer.valueOf(cur))

src/main/kotlin/g0801_0900/s0811_subdomain_visit_count/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Solution {
4545
val fmap: MutableMap<String, Int> = HashMap()
4646
for (s in d) {
4747
var rep = 0
48-
var i: Int = 0
48+
var i = 0
4949
while (i < s.length) {
5050
val c = s[i]
5151
rep = if (c in '0'..'9') {

src/main/kotlin/g0901_1000/s0989_add_to_array_form_of_integer/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ Given `num`, the **array-form** of an integer, and an integer `k`, return _the *
4747
```kotlin
4848
@Suppress("NAME_SHADOWING")
4949
class Solution {
50-
fun addToArrayForm(num: IntArray, k: Int): List<Int?> {
50+
fun addToArrayForm(num: IntArray, k: Int): List<Int> {
5151
var k = k
52-
val result = ArrayList<Int?>()
52+
val result = ArrayList<Int>()
5353
var carry = 0
5454
for (i in num.indices.reversed()) {
5555
val temp = num[i] + k % 10 + carry

src/main/kotlin/g1001_1100/s1048_longest_string_chain/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Return _the **length** of the **longest possible word chain** with words chosen
5050
```kotlin
5151
class Solution {
5252
fun longestStrChain(words: Array<String>): Int {
53-
val lenStr = arrayOfNulls<MutableList<String>?>(20)
53+
val lenStr = arrayOfNulls<MutableList<String>>(20)
5454
for (word in words) {
5555
val len = word.length
5656
if (lenStr[len] == null) {

src/main/kotlin/g1601_1700/s1639_number_of_ways_to_form_a_target_string_given_a_dictionary/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Return _the number of ways to form `target` from `words`_. Since the answer may
6868
class Solution {
6969
fun numWays(words: Array<String>, target: String): Int {
7070
val counts = precompute(words)
71-
val memo = Array(target.length) { arrayOfNulls<Int?>(words[0].length) }
71+
val memo = Array(target.length) { arrayOfNulls<Int>(words[0].length) }
7272
return solve(memo, counts, words, target, 0, 0)
7373
}
7474

src/main/kotlin/g1701_1800/s1739_building_boxes/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class Solution {
8787
}
8888

8989
private fun product(vararg vals: Long): Long {
90-
var product: Long = 1L
90+
var product = 1L
9191
for (`val`: Long in vals) {
9292
product *= `val`
9393
}

src/main/kotlin/g1701_1800/s1748_sum_of_unique_elements/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Return _the **sum** of all the unique elements of_ `nums`.
4444
class Solution {
4545
fun sumOfUnique(nums: IntArray): Int {
4646
val map: MutableMap<Int, Int> = HashMap()
47-
var sum: Int = 0
47+
var sum = 0
4848
for (num: Int in nums) {
4949
map.put(num, map.getOrDefault(num, 0) + 1)
5050
}

src/main/kotlin/g1701_1800/s1749_maximum_absolute_sum_of_any_subarray/readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ Note that `abs(x)` is defined as follows:
4040
```kotlin
4141
class Solution {
4242
fun maxAbsoluteSum(nums: IntArray): Int {
43-
var min: Int = 0
44-
var max: Int = 0
45-
var s: Int = 0
43+
var min = 0
44+
var max = 0
45+
var s = 0
4646
for (num: Int in nums) {
4747
s += num
4848
min = Math.min(min, s)

src/main/kotlin/g2001_2100/s2019_the_score_of_students_solving_math_expression/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class Solution {
8181
val st = ArrayDeque<Int>()
8282
val n = s.length
8383
var i = 0
84-
dp = Array(n) { arrayOfNulls<HashSet<Int>?>(n) }
84+
dp = Array(n) { arrayOfNulls<HashSet<Int>>(n) }
8585
while (i < n) {
8686
if (s[i].code - '0'.code >= 0 && s[i].code - '9'.code <= 0) {
8787
st.push(s[i].code - '0'.code)

src/main/kotlin/g2201_2300/s2213_longest_substring_of_one_repeating_character/readme.md

Lines changed: 51 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -50,86 +50,68 @@ Thus, we return [2,3].
5050

5151
```kotlin
5252
class Solution {
53-
internal class TreeNode(var start: Int, var end: Int) {
54-
var leftChar = 0.toChar()
55-
var leftCharLen = 0
56-
var rightChar = 0.toChar()
57-
var rightCharLen = 0
58-
var max = 0
59-
var left: TreeNode? = null
60-
var right: TreeNode? = null
61-
}
53+
private lateinit var ca: CharArray
6254

6355
fun longestRepeating(s: String, queryCharacters: String, queryIndices: IntArray): IntArray {
64-
val sChar = s.toCharArray()
65-
val qChar = queryCharacters.toCharArray()
66-
val root = buildTree(sChar, 0, sChar.size - 1)
67-
val result = IntArray(qChar.size)
68-
for (i in qChar.indices) {
69-
updateTree(root, queryIndices[i], qChar[i])
70-
if (root != null) {
71-
result[i] = root.max
72-
}
56+
ca = s.toCharArray()
57+
val result = IntArray(queryIndices.size)
58+
val root = SegmentTree(0, ca.size)
59+
for (i in queryIndices.indices) {
60+
ca[queryIndices[i]] = queryCharacters[i]
61+
root.update(queryIndices[i])
62+
result[i] = root.longest
7363
}
7464
return result
7565
}
7666

77-
private fun buildTree(s: CharArray, from: Int, to: Int): TreeNode? {
78-
if (from > to) {
79-
return null
80-
}
81-
val root = TreeNode(from, to)
82-
if (from == to) {
83-
root.max = 1
84-
root.leftChar = s[from]
85-
root.rightChar = root.leftChar
86-
root.rightCharLen = 1
87-
root.leftCharLen = root.rightCharLen
88-
return root
67+
private inner class SegmentTree(val start: Int, val end: Int) {
68+
var longest: Int = 0
69+
var leftLength: Int = 0
70+
var rightLength: Int = 0
71+
private lateinit var left: SegmentTree
72+
private lateinit var right: SegmentTree
73+
74+
init {
75+
if (end - start > 1) {
76+
val mid = (start + end) / 2
77+
left = SegmentTree(start, mid)
78+
right = SegmentTree(mid, end)
79+
merge()
80+
} else {
81+
longest = 1
82+
leftLength = 1
83+
rightLength = 1
84+
}
8985
}
90-
val middle = from + (to - from) / 2
91-
root.left = buildTree(s, from, middle)
92-
root.right = buildTree(s, middle + 1, to)
93-
updateNode(root)
94-
return root
95-
}
9686

97-
private fun updateTree(root: TreeNode?, index: Int, c: Char) {
98-
if (root == null || root.start > index || root.end < index) {
99-
return
100-
}
101-
if (root.start == index && root.end == index) {
102-
root.rightChar = c
103-
root.leftChar = root.rightChar
104-
return
87+
fun update(index: Int) {
88+
if (end - start == 1) return
89+
if (index < (left.end)) {
90+
left.update(index)
91+
} else {
92+
right.update(index)
93+
}
94+
merge()
10595
}
106-
updateTree(root.left, index, c)
107-
updateTree(root.right, index, c)
108-
updateNode(root)
109-
}
11096

111-
private fun updateNode(root: TreeNode?) {
112-
if (root == null) {
113-
return
114-
}
115-
root.leftChar = root.left!!.leftChar
116-
root.leftCharLen = root.left!!.leftCharLen
117-
root.rightChar = root.right!!.rightChar
118-
root.rightCharLen = root.right!!.rightCharLen
119-
root.max = Math.max(root.left!!.max, root.right!!.max)
120-
if (root.left!!.rightChar == root.right!!.leftChar) {
121-
val len = root.left!!.rightCharLen + root.right!!.leftCharLen
122-
if (root.left!!.leftChar == root.left!!.rightChar &&
123-
root.left!!.leftCharLen == root.left!!.end - root.left!!.start + 1
124-
) {
125-
root.leftCharLen = len
126-
}
127-
if (root.right!!.leftChar == root.right!!.rightChar &&
128-
root.right!!.leftCharLen == root.right!!.end - root.right!!.start + 1
129-
) {
130-
root.rightCharLen = len
97+
private fun merge() {
98+
longest = maxOf(left.longest, right.longest)
99+
if (ca[left.end - 1] == ca[right.start]) {
100+
longest = maxOf(longest, left.rightLength + right.leftLength)
101+
leftLength = if (left.leftLength == left.end - left.start) {
102+
left.leftLength + right.leftLength
103+
} else {
104+
left.leftLength
105+
}
106+
rightLength = if (right.rightLength == (right.end - right.start)) {
107+
right.rightLength + left.rightLength
108+
} else {
109+
right.rightLength
110+
}
111+
} else {
112+
leftLength = left.leftLength
113+
rightLength = right.rightLength
131114
}
132-
root.max = Math.max(root.max, len)
133115
}
134116
}
135117
}

0 commit comments

Comments
 (0)