Skip to content

Commit 7979b70

Browse files
authored
Added tasks 2546-2598
1 parent a9ca633 commit 7979b70

File tree

58 files changed

+4017
-156
lines changed
  • src/main/kotlin
    • g0001_0100
    • g0101_0200
      • s0105_construct_binary_tree_from_preorder_and_inorder_traversal
      • s0107_binary_tree_level_order_traversal_ii
    • g0701_0800/s0770_basic_calculator_iv
    • g0901_1000/s0938_range_sum_of_bst
    • g2501_2600
      • s2546_apply_bitwise_operations_to_make_strings_equal
      • s2547_minimum_cost_to_split_an_array
      • s2549_count_distinct_numbers_on_board
      • s2550_count_collisions_of_monkeys_on_a_polygon
      • s2551_put_marbles_in_bags
      • s2552_count_increasing_quadruplets
      • s2553_separate_the_digits_in_an_array
      • s2554_maximum_number_of_integers_to_choose_from_a_range_i
      • s2555_maximize_win_from_two_segments
      • s2556_disconnect_path_in_a_binary_matrix_by_at_most_one_flip
      • s2558_take_gifts_from_the_richest_pile
      • s2559_count_vowel_strings_in_ranges
      • s2560_house_robber_iv
      • s2561_rearranging_fruits
      • s2562_find_the_array_concatenation_value
      • s2563_count_the_number_of_fair_pairs
      • s2564_substring_xor_queries
      • s2565_subsequence_with_the_minimum_score
      • s2566_maximum_difference_by_remapping_a_digit
      • s2567_minimum_score_by_changing_two_elements
      • s2568_minimum_impossible_or
      • s2569_handling_sum_queries_after_update
      • s2570_merge_two_2d_arrays_by_summing_values
      • s2571_minimum_operations_to_reduce_an_integer_to_0
      • s2572_count_the_number_of_square_free_subsets
      • s2573_find_the_string_with_lcp
      • s2574_left_and_right_sum_differences
      • s2575_find_the_divisibility_array_of_a_string
      • s2576_find_the_maximum_number_of_marked_indices
      • s2577_minimum_time_to_visit_a_cell_in_a_grid
      • s2578_split_with_minimum_sum
      • s2579_count_total_number_of_colored_cells
      • s2580_count_ways_to_group_overlapping_ranges
      • s2581_count_number_of_possible_root_nodes
      • s2582_pass_the_pillow
      • s2583_kth_largest_sum_in_a_binary_tree
      • s2584_split_the_array_to_make_coprime_products
      • s2585_number_of_ways_to_earn_points
      • s2586_count_the_number_of_vowel_strings_in_range
      • s2587_rearrange_array_to_maximize_prefix_score
      • s2588_count_the_number_of_beautiful_subarrays
      • s2589_minimum_time_to_complete_all_tasks
      • s2591_distribute_money_to_maximum_children
      • s2592_maximize_greatness_of_an_array
      • s2593_find_score_of_an_array_after_marking_all_elements
      • s2594_minimum_time_to_repair_cars
      • s2595_number_of_even_and_odd_bits
      • s2596_check_knight_tour_configuration
      • s2597_the_number_of_beautiful_subsets
      • s2598_smallest_missing_non_negative_integer_after_operations

Some content is hidden

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

58 files changed

+4017
-156
lines changed

README.md

Lines changed: 192 additions & 142 deletions
Large diffs are not rendered by default.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ A **word** is a maximal substring consisting of non-space characters only.
4343

4444
```kotlin
4545
class Solution {
46-
fun lengthOfLastWord(str: String): Int {
46+
fun lengthOfLastWord(s: String): Int {
4747
var len = 0
48-
for (i in str.length - 1 downTo 0) {
49-
val ch = str[i]
48+
for (i in s.length - 1 downTo 0) {
49+
val ch = s[i]
5050
if (ch == ' ' && len > 0) {
5151
break
5252
} else if (ch != ' ') {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ exection -> execution (insert 'u')
5454

5555
```kotlin
5656
class Solution {
57-
fun minDistance(w1: String, w2: String): Int {
58-
val n1 = w1.length
59-
val n2 = w2.length
57+
fun minDistance(word1: String, word2: String): Int {
58+
val n1 = word1.length
59+
val n2 = word2.length
6060
if (n2 > n1) {
61-
return minDistance(w2, w1)
61+
return minDistance(word2, word1)
6262
}
6363
val dp = IntArray(n2 + 1)
6464
for (j in 0..n2) {
@@ -69,7 +69,7 @@ class Solution {
6969
dp[0] = i
7070
for (j in 1..n2) {
7171
val tmp = dp[j]
72-
dp[j] = if (w1[i - 1] != w2[j - 1]) 1 + Math.min(pre, Math.min(dp[j], dp[j - 1])) else pre
72+
dp[j] = if (word1[i - 1] != word2[j - 1]) 1 + Math.min(pre, Math.min(dp[j], dp[j - 1])) else pre
7373
pre = tmp
7474
}
7575
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ The solution set **must not** contain duplicate subsets. Return the solution in
3030

3131
```kotlin
3232
class Solution {
33-
var allComb: MutableList<List<Int>> = ArrayList()
34-
var comb: MutableList<Int> = ArrayList()
35-
lateinit var nums: IntArray
33+
private var allComb: MutableList<List<Int>> = ArrayList()
34+
private var comb: MutableList<Int> = ArrayList()
35+
private lateinit var nums: IntArray
36+
3637
fun subsetsWithDup(nums: IntArray): List<List<Int>> {
3738
nums.sort()
3839
this.nums = nums

src/main/kotlin/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ Given two integer arrays `preorder` and `inorder` where `preorder` is the preord
3535

3636
```kotlin
3737
import com_github_leetcode.TreeNode
38-
import java.util.HashMap
3938

4039
/*
4140
* Example:

src/main/kotlin/g0101_0200/s0107_binary_tree_level_order_traversal_ii/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import kotlin.collections.ArrayList
5151
*/
5252
class Solution {
5353
private val order: MutableList<MutableList<Int>> = ArrayList()
54+
5455
fun levelOrderBottom(root: TreeNode?): List<MutableList<Int>> {
5556
getOrder(root, 0)
5657
Collections.reverse(order)

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
@@ -64,7 +64,7 @@ The format of the output is as follows:
6464
import java.util.Collections
6565
import java.util.Stack
6666

67-
internal class Solution {
67+
class Solution {
6868
internal inner class Node {
6969
var mem: MutableMap<List<String>, Int> = HashMap()
7070
fun update(cur: List<String>, cnt: Int) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import com_github_leetcode.TreeNode
4949
* var right: TreeNode? = null
5050
* }
5151
*/
52-
internal class Solution {
52+
class Solution {
5353
fun rangeSumBST(root: TreeNode?, low: Int, high: Int): Int {
5454
var ans = 0
5555
if (root == null) return 0
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 2546\. Apply Bitwise Operations to Make Strings Equal
5+
6+
Medium
7+
8+
You are given two **0-indexed binary** strings `s` and `target` of the same length `n`. You can do the following operation on `s` **any** number of times:
9+
10+
* Choose two **different** indices `i` and `j` where `0 <= i, j < n`.
11+
* Simultaneously, replace `s[i]` with (`s[i]` **OR** `s[j]`) and `s[j]` with (`s[i]` **XOR** `s[j]`).
12+
13+
For example, if `s = "0110"`, you can choose `i = 0` and `j = 2`, then simultaneously replace `s[0]` with (`s[0]` **OR** `s[2]` = `0` **OR** `1` = `1`), and `s[2]` with (`s[0]` **XOR** `s[2]` = `0` **XOR** `1` = `1`), so we will have `s = "1110"`.
14+
15+
Return `true` _if you can make the string_ `s` _equal to_ `target`_, or_ `false` _otherwise_.
16+
17+
**Example 1:**
18+
19+
**Input:** s = "1010", target = "0110"
20+
21+
**Output:** true
22+
23+
**Explanation:** We can do the following operations:
24+
25+
- Choose i = 2 and j = 0. We have now s = "**<ins>0</ins>**0**<ins>1</ins>**0".
26+
27+
- Choose i = 2 and j = 1. We have now s = "0**<ins>11</ins>**0". Since we can make s equal to target, we return true.
28+
29+
**Example 2:**
30+
31+
**Input:** s = "11", target = "00"
32+
33+
**Output:** false
34+
35+
**Explanation:** It is not possible to make s equal to target with any number of operations.
36+
37+
**Constraints:**
38+
39+
* `n == s.length == target.length`
40+
* <code>2 <= n <= 10<sup>5</sup></code>
41+
* `s` and `target` consist of only the digits `0` and `1`.
42+
43+
## Solution
44+
45+
```kotlin
46+
class Solution {
47+
fun makeStringsEqual(s: String, target: String): Boolean {
48+
val strLen = s.length
49+
var ans1 = false
50+
var ans2 = false
51+
for (i in 0 until strLen) {
52+
if (s[i] == '1') {
53+
ans1 = true
54+
}
55+
if (target[i] == '1') {
56+
ans2 = true
57+
}
58+
}
59+
return ans1 == ans2
60+
}
61+
}
62+
```
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 2547\. Minimum Cost to Split an Array
5+
6+
Hard
7+
8+
You are given an integer array `nums` and an integer `k`.
9+
10+
Split the array into some number of non-empty subarrays. The **cost** of a split is the sum of the **importance value** of each subarray in the split.
11+
12+
Let `trimmed(subarray)` be the version of the subarray where all numbers which appear only once are removed.
13+
14+
* For example, `trimmed([3,1,2,4,3,4]) = [3,4,3,4].`
15+
16+
The **importance value** of a subarray is `k + trimmed(subarray).length`.
17+
18+
* For example, if a subarray is `[1,2,3,3,3,4,4]`, then trimmed(`[1,2,3,3,3,4,4]) = [3,3,3,4,4].`The importance value of this subarray will be `k + 5`.
19+
20+
Return _the minimum possible cost of a split of_ `nums`.
21+
22+
A **subarray** is a contiguous **non-empty** sequence of elements within an array.
23+
24+
**Example 1:**
25+
26+
**Input:** nums = [1,2,1,2,1,3,3], k = 2
27+
28+
**Output:** 8
29+
30+
**Explanation:** We split nums to have two subarrays: [1,2], [1,2,1,3,3]. '
31+
32+
The importance value of [1,2] is 2 + (0) = 2.
33+
34+
The importance value of [1,2,1,3,3] is 2 + (2 + 2) = 6.
35+
36+
The cost of the split is 2 + 6 = 8. It can be shown that this is the minimum possible cost among all the possible splits.
37+
38+
**Example 2:**
39+
40+
**Input:** nums = [1,2,1,2,1], k = 2
41+
42+
**Output:** 6
43+
44+
**Explanation:** We split nums to have two subarrays: [1,2], [1,2,1].
45+
46+
The importance value of [1,2] is 2 + (0) = 2.
47+
48+
The importance value of [1,2,1] is 2 + (2) = 4.
49+
50+
The cost of the split is 2 + 4 = 6. It can be shown that this is the minimum possible cost among all the possible splits.
51+
52+
**Example 3:**
53+
54+
**Input:** nums = [1,2,1,2,1], k = 5
55+
56+
**Output:** 10
57+
58+
**Explanation:** We split nums to have one subarray: [1,2,1,2,1].
59+
60+
The importance value of [1,2,1,2,1] is 5 + (3 + 2) = 10.
61+
62+
The cost of the split is 10. It can be shown that this is the minimum possible cost among all the possible splits.
63+
64+
**Constraints:**
65+
66+
* `1 <= nums.length <= 1000`
67+
* `0 <= nums[i] < nums.length`
68+
* <code>1 <= k <= 10<sup>9</sup></code>
69+
70+
## Solution
71+
72+
```kotlin
73+
class Solution {
74+
fun minCost(nums: IntArray, k: Int): Int {
75+
val n = nums.size
76+
val dp = IntArray(n)
77+
dp.fill(-1)
78+
val len = Array(n) { IntArray(n) }
79+
for (r in len) r.fill(0)
80+
for (i in 0 until n) {
81+
val count = IntArray(n)
82+
count.fill(0)
83+
var c = 0
84+
for (j in i until n) {
85+
count[nums[j]] += 1
86+
if (count[nums[j]] == 2) c += 2 else if (count[nums[j]] > 2) c += 1
87+
len[i][j] = c
88+
}
89+
}
90+
return f(0, nums, k, len, dp)
91+
}
92+
93+
private fun f(ind: Int, nums: IntArray, k: Int, len: Array<IntArray>, dp: IntArray): Int {
94+
if (ind >= nums.size) return 0
95+
if (dp[ind] != -1) return dp[ind]
96+
dp[ind] = Int.MAX_VALUE
97+
for (i in ind until nums.size) {
98+
val current = len[ind][i] + k
99+
val next = f(i + 1, nums, k, len, dp)
100+
dp[ind] = dp[ind].coerceAtMost(current + next)
101+
}
102+
return dp[ind]
103+
}
104+
}
105+
```

0 commit comments

Comments
 (0)