Skip to content

Commit fffd59c

Browse files
authored
Added tasks 3070-3108
1 parent 965c4b2 commit fffd59c

File tree

42 files changed

+2751
-29
lines changed
  • src/main/kotlin
    • g1201_1300/s1275_find_winner_on_a_tic_tac_toe_game
    • g1501_1600/s1579_remove_max_number_of_edges_to_keep_graph_fully_traversable
    • g1601_1700
      • s1632_rank_transform_of_a_matrix
      • s1665_minimum_initial_energy_to_finish_tasks
      • s1691_maximum_height_by_stacking_cuboids
      • s1697_checking_existence_of_edge_length_limited_paths
    • g1701_1800
      • s1710_maximum_units_on_a_truck
      • s1751_maximum_number_of_events_that_can_be_attended_ii
    • g1801_1900
    • g3001_3100
      • s3070_count_submatrices_with_top_left_element_and_sum_less_than_k
      • s3071_minimum_operations_to_write_the_letter_y_on_a_grid
      • s3072_distribute_elements_into_two_arrays_ii
      • s3074_apple_redistribution_into_boxes
      • s3075_maximize_happiness_of_selected_children
      • s3076_shortest_uncommon_substring_in_an_array
      • s3077_maximum_strength_of_k_disjoint_subarrays
      • s3079_find_the_sum_of_encrypted_integers
      • s3080_mark_elements_on_array_by_performing_queries
      • s3081_replace_question_marks_in_string_to_minimize_its_value
      • s3082_find_the_sum_of_the_power_of_all_subsequences
      • s3083_existence_of_a_substring_in_a_string_and_its_reverse
      • s3084_count_substrings_starting_and_ending_with_given_character
      • s3085_minimum_deletions_to_make_string_k_special
      • s3086_minimum_moves_to_pick_k_ones
      • s3090_maximum_length_substring_with_two_occurrences
      • s3091_apply_operations_to_make_sum_of_array_greater_than_or_equal_to_k
      • s3092_most_frequent_ids
      • s3093_longest_common_suffix_queries
      • s3095_shortest_subarray_with_or_at_least_k_i
      • s3096_minimum_levels_to_gain_more_points
      • s3097_shortest_subarray_with_or_at_least_k_ii
      • s3098_find_the_sum_of_subsequence_powers
      • s3099_harshad_number
      • s3100_water_bottles_ii
    • g3101_3200
      • s3101_count_alternating_subarrays
      • s3102_minimize_manhattan_distances
      • s3105_longest_strictly_increasing_or_strictly_decreasing_subarray
      • s3106_lexicographically_smallest_string_after_operations_with_constraint
      • s3107_minimum_operations_to_make_median_of_array_equal_to_k
      • s3108_minimum_cost_walk_in_weighted_graph

Some content is hidden

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

42 files changed

+2751
-29
lines changed

README.md

+31
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,37 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 3108 |[Minimum Cost Walk in Weighted Graph](src/main/kotlin/g3101_3200/s3108_minimum_cost_walk_in_weighted_graph)| Hard | Array, Bit_Manipulation, Graph, Union_Find | 791 | 100.00
1820+
| 3107 |[Minimum Operations to Make Median of Array Equal to K](src/main/kotlin/g3101_3200/s3107_minimum_operations_to_make_median_of_array_equal_to_k)| Medium | Array, Sorting, Greedy | 554 | 100.00
1821+
| 3106 |[Lexicographically Smallest String After Operations With Constraint](src/main/kotlin/g3101_3200/s3106_lexicographically_smallest_string_after_operations_with_constraint)| Medium | String, Greedy | 162 | 74.19
1822+
| 3105 |[Longest Strictly Increasing or Strictly Decreasing Subarray](src/main/kotlin/g3101_3200/s3105_longest_strictly_increasing_or_strictly_decreasing_subarray)| Easy | Array | 159 | 94.00
1823+
| 3102 |[Minimize Manhattan Distances](src/main/kotlin/g3101_3200/s3102_minimize_manhattan_distances)| Hard | Array, Math | 701 | 95.83
1824+
| 3101 |[Count Alternating Subarrays](src/main/kotlin/g3101_3200/s3101_count_alternating_subarrays)| Medium | Array, Math | 499 | 97.78
1825+
| 3100 |[Water Bottles II](src/main/kotlin/g3001_3100/s3100_water_bottles_ii)| Medium | Math, Simulation | 137 | 70.49
1826+
| 3099 |[Harshad Number](src/main/kotlin/g3001_3100/s3099_harshad_number)| Easy | Math | 119 | 82.67
1827+
| 3098 |[Find the Sum of Subsequence Powers](src/main/kotlin/g3001_3100/s3098_find_the_sum_of_subsequence_powers)| Hard | Array, Dynamic_Programming, Sorting | 294 | 77.78
1828+
| 3097 |[Shortest Subarray With OR at Least K II](src/main/kotlin/g3001_3100/s3097_shortest_subarray_with_or_at_least_k_ii)| Medium | Array, Bit_Manipulation, Sliding_Window | 489 | 93.33
1829+
| 3096 |[Minimum Levels to Gain More Points](src/main/kotlin/g3001_3100/s3096_minimum_levels_to_gain_more_points)| Medium | Array, Prefix_Sum | 850 | 100.00
1830+
| 3095 |[Shortest Subarray With OR at Least K I](src/main/kotlin/g3001_3100/s3095_shortest_subarray_with_or_at_least_k_i)| Easy | Array, Bit_Manipulation, Sliding_Window | 161 | 95.65
1831+
| 3093 |[Longest Common Suffix Queries](src/main/kotlin/g3001_3100/s3093_longest_common_suffix_queries)| Hard | Array, String, Trie | 860 | 89.29
1832+
| 3092 |[Most Frequent IDs](src/main/kotlin/g3001_3100/s3092_most_frequent_ids)| Medium | Array, Hash_Table, Heap_Priority_Queue, Ordered_Set | 758 | 100.00
1833+
| 3091 |[Apply Operations to Make Sum of Array Greater Than or Equal to k](src/main/kotlin/g3001_3100/s3091_apply_operations_to_make_sum_of_array_greater_than_or_equal_to_k)| Medium | Math, Greedy, Enumeration | 134 | 73.21
1834+
| 3090 |[Maximum Length Substring With Two Occurrences](src/main/kotlin/g3001_3100/s3090_maximum_length_substring_with_two_occurrences)| Easy | String, Hash_Table, Sliding_Window | 157 | 90.24
1835+
| 3086 |[Minimum Moves to Pick K Ones](src/main/kotlin/g3001_3100/s3086_minimum_moves_to_pick_k_ones)| Hard | Array, Greedy, Prefix_Sum, Sliding_Window | 368 | 100.00
1836+
| 3085 |[Minimum Deletions to Make String K-Special](src/main/kotlin/g3001_3100/s3085_minimum_deletions_to_make_string_k_special)| Medium | String, Hash_Table, Sorting, Greedy, Counting | 221 | 93.33
1837+
| 3084 |[Count Substrings Starting and Ending with Given Character](src/main/kotlin/g3001_3100/s3084_count_substrings_starting_and_ending_with_given_character)| Medium | String, Math, Counting | 177 | 98.55
1838+
| 3083 |[Existence of a Substring in a String and Its Reverse](src/main/kotlin/g3001_3100/s3083_existence_of_a_substring_in_a_string_and_its_reverse)| Easy | String, Hash_Table | 168 | 79.49
1839+
| 3082 |[Find the Sum of the Power of All Subsequences](src/main/kotlin/g3001_3100/s3082_find_the_sum_of_the_power_of_all_subsequences)| Hard | Array, Dynamic_Programming | 176 | 90.00
1840+
| 3081 |[Replace Question Marks in String to Minimize Its Value](src/main/kotlin/g3001_3100/s3081_replace_question_marks_in_string_to_minimize_its_value)| Medium | String, Hash_Table, Sorting, Greedy, Heap_Priority_Queue, Counting | 249 | 100.00
1841+
| 3080 |[Mark Elements on Array by Performing Queries](src/main/kotlin/g3001_3100/s3080_mark_elements_on_array_by_performing_queries)| Medium | Array, Hash_Table, Sorting, Heap_Priority_Queue, Simulation | 937 | 97.78
1842+
| 3079 |[Find the Sum of Encrypted Integers](src/main/kotlin/g3001_3100/s3079_find_the_sum_of_encrypted_integers)| Easy | Array, Math | 172 | 80.60
1843+
| 3077 |[Maximum Strength of K Disjoint Subarrays](src/main/kotlin/g3001_3100/s3077_maximum_strength_of_k_disjoint_subarrays)| Hard | Array, Dynamic_Programming, Prefix_Sum | 351 | 75.00
1844+
| 3076 |[Shortest Uncommon Substring in an Array](src/main/kotlin/g3001_3100/s3076_shortest_uncommon_substring_in_an_array)| Medium | Array, String, Hash_Table, Trie | 256 | 100.00
1845+
| 3075 |[Maximize Happiness of Selected Children](src/main/kotlin/g3001_3100/s3075_maximize_happiness_of_selected_children)| Medium | Array, Sorting, Greedy | 608 | 93.24
1846+
| 3074 |[Apple Redistribution into Boxes](src/main/kotlin/g3001_3100/s3074_apple_redistribution_into_boxes)| Easy | Array, Sorting, Greedy | 168 | 97.37
1847+
| 3072 |[Distribute Elements Into Two Arrays II](src/main/kotlin/g3001_3100/s3072_distribute_elements_into_two_arrays_ii)| Hard | Array, Simulation, Segment_Tree, Binary_Indexed_Tree | 890 | 100.00
1848+
| 3071 |[Minimum Operations to Write the Letter Y on a Grid](src/main/kotlin/g3001_3100/s3071_minimum_operations_to_write_the_letter_y_on_a_grid)| Medium | Array, Hash_Table, Matrix, Counting | 268 | 91.11
1849+
| 3070 |[Count Submatrices with Top-Left Element and Sum Less Than k](src/main/kotlin/g3001_3100/s3070_count_submatrices_with_top_left_element_and_sum_less_than_k)| Medium | Array, Matrix, Prefix_Sum | 773 | 85.71
18191850
| 3069 |[Distribute Elements Into Two Arrays I](src/main/kotlin/g3001_3100/s3069_distribute_elements_into_two_arrays_i)| Easy | Array, Simulation | 207 | 96.92
18201851
| 3068 |[Find the Maximum Sum of Node Values](src/main/kotlin/g3001_3100/s3068_find_the_maximum_sum_of_node_values)| Hard | Array, Dynamic_Programming, Sorting, Greedy, Tree, Bit_Manipulation | 531 | 66.67
18211852
| 3067 |[Count Pairs of Connectable Servers in a Weighted Tree Network](src/main/kotlin/g3001_3100/s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network)| Medium | Array, Depth_First_Search, Tree | 578 | 83.33

src/main/kotlin/g1201_1300/s1275_find_winner_on_a_tic_tac_toe_game/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,4 @@ class Solution {
111111
}
112112
}
113113
}
114-
```
114+
```

src/main/kotlin/g1501_1600/s1579_remove_max_number_of_edges_to_keep_graph_fully_traversable/readme.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,9 @@ Return _the maximum number of edges you can remove, or return_ `-1` _if it's imp
5757
## Solution
5858

5959
```kotlin
60-
import java.util.Arrays
61-
6260
class Solution {
6361
fun maxNumEdgesToRemove(n: Int, edges: Array<IntArray>): Int {
64-
Arrays.sort(edges) { a: IntArray, b: IntArray -> b[0] - a[0] }
62+
edges.sortWith { a: IntArray, b: IntArray -> b[0] - a[0] }
6563
val alice = IntArray(n + 1)
6664
val rankAlice = IntArray(n + 1)
6765
val bob = IntArray(n + 1)

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ The rank of matrix[1][1] is 3 because matrix[1][1] > matrix[0][1], matrix[1][1]
6262
## Solution
6363

6464
```kotlin
65-
import java.util.Arrays
66-
6765
class Solution {
6866
fun matrixRankTransform(matrix: Array<IntArray>): Array<IntArray> {
6967
val rowCount = matrix.size
@@ -108,7 +106,7 @@ class Solution {
108106
} else {
109107
val rowCount = matrix.size
110108
val ufind = IntArray(rowCount + matrix[0].size)
111-
Arrays.fill(ufind, -1)
109+
ufind.fill(-1)
112110
for (nIdx in startIdx until endIdx) {
113111
val r = nums[nIdx].toInt() shr 16 and 0xFFFF
114112
val c = nums[nIdx].toInt() and 0xFFFF

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,9 @@ Starting with 27 energy, we finish the tasks in the following order:
8484
## Solution
8585

8686
```kotlin
87-
import java.util.Arrays
88-
8987
class Solution {
9088
fun minimumEffort(tasks: Array<IntArray>): Int {
91-
Arrays.sort(tasks) { a: IntArray, b: IntArray -> a[1] - a[0] - b[1] + b[0] }
89+
tasks.sortWith { a: IntArray, b: IntArray -> a[1] - a[0] - b[1] + b[0] }
9290
var prev = 0
9391
for (item in tasks) {
9492
prev = Math.max(prev + item[0], item[1])

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,13 @@ The maximum height of stacked cuboids is 6 \* 17 = 102.
6464
## Solution
6565

6666
```kotlin
67-
import java.util.Arrays
68-
6967
class Solution {
7068
fun maxHeight(cuboids: Array<IntArray>): Int {
7169
for (a in cuboids) {
7270
a.sort()
7371
}
74-
Arrays.sort(
75-
cuboids
76-
) { a: IntArray, b: IntArray ->
72+
73+
cuboids.sortWith sort@{ a: IntArray, b: IntArray ->
7774
if (a[0] != b[0]) {
7875
return@sort a[0] - b[0]
7976
} else if (a[1] != b[1]) {

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ For the second query, there is a path (0 -> 1 -> 2) of two edges with distances
4848
## Solution
4949

5050
```kotlin
51-
import java.util.Arrays
52-
5351
class Solution {
5452
private class Dsu(n: Int) {
5553
private val parent: IntArray
@@ -77,12 +75,12 @@ class Solution {
7775
}
7876

7977
fun distanceLimitedPathsExist(n: Int, edgeList: Array<IntArray>, queries: Array<IntArray>): BooleanArray {
80-
Arrays.sort(edgeList) { o1: IntArray, o2: IntArray -> Integer.compare(o1[2], o2[2]) }
78+
edgeList.sortWith { o1: IntArray, o2: IntArray -> Integer.compare(o1[2], o2[2]) }
8179
val data = Array(queries.size) { IntArray(4) }
8280
for (i in queries.indices) {
8381
data[i] = intArrayOf(queries[i][0], queries[i][1], queries[i][2], i)
8482
}
85-
Arrays.sort(data) { o1: IntArray, o2: IntArray -> Integer.compare(o1[2], o2[2]) }
83+
data.sortWith { o1: IntArray, o2: IntArray -> Integer.compare(o1[2], o2[2]) }
8684
val d = Dsu(n)
8785
var j = 0
8886
val ans = BooleanArray(queries.size)

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,11 @@ Return _the **maximum** total number of **units** that can be put on the truck._
4343
## Solution
4444

4545
```kotlin
46-
import java.util.Arrays
47-
4846
@Suppress("NAME_SHADOWING")
4947
class Solution {
5048
fun maximumUnits(boxTypes: Array<IntArray>, truckSize: Int): Int {
5149
var truckSize = truckSize
52-
Arrays.sort(boxTypes) { b1: IntArray, b2: IntArray -> Integer.compare(b2[1], b1[1]) }
50+
boxTypes.sortWith { b1: IntArray, b2: IntArray -> Integer.compare(b2[1], b1[1]) }
5351
var maxUnits = 0
5452
var i = 0
5553
while (truckSize > 0 && i < boxTypes.size) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class Solution {
6565
}
6666
}
6767
val n = events.size
68-
Arrays.sort(events, { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) })
68+
events.sortWith { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) }
6969
val memo = Array(n) { IntArray(k + 1) }
7070
return dfs(events, 0, k, memo)
7171
}

src/main/kotlin/g1801_1900/s1847_closest_room/readme.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ Query = [2,5]: Room number 3 is the only room with a size of at least 5. The ans
5656
## Solution
5757

5858
```kotlin
59-
import java.util.Arrays
6059
import java.util.TreeSet
6160

6261
class Solution {
@@ -66,8 +65,8 @@ class Solution {
6665
for (i in 0 until numQuery) {
6766
queries[i] = intArrayOf(queries[i][0], queries[i][1], i)
6867
}
69-
Arrays.sort(rooms) { a: IntArray, b: IntArray -> if (a[1] != b[1]) a[1] - b[1] else a[0] - b[0] }
70-
Arrays.sort(queries) { a: IntArray, b: IntArray -> if (a[1] != b[1]) a[1] - b[1] else a[0] - b[0] }
68+
rooms.sortWith { a: IntArray, b: IntArray -> if (a[1] != b[1]) a[1] - b[1] else a[0] - b[0] }
69+
queries.sortWith { a: IntArray, b: IntArray -> if (a[1] != b[1]) a[1] - b[1] else a[0] - b[0] }
7170
val roomIds = TreeSet<Int>()
7271
val result = IntArray(numQuery)
7372
var j = numRoom - 1

src/main/kotlin/g1801_1900/s1851_minimum_interval_to_include_each_query/readme.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ Return _an array containing the answers to the queries_.
5454
## Solution
5555

5656
```kotlin
57-
import java.util.Arrays
5857
import java.util.PriorityQueue
5958

6059
class Solution {
@@ -64,8 +63,8 @@ class Solution {
6463
for (i in 0 until numQuery) {
6564
queriesWithIndex[i] = intArrayOf(queries[i], i)
6665
}
67-
Arrays.sort(intervals, { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) })
68-
Arrays.sort(queriesWithIndex, { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) })
66+
intervals.sortWith { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) }
67+
queriesWithIndex.sortWith { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) }
6968
val minHeap = PriorityQueue({ a: IntArray, b: IntArray -> (a[1] - a[0]).compareTo(b[1] - b[0]) })
7069
val result = IntArray(numQuery)
7170
var j = 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
## 3070\. Count Submatrices with Top-Left Element and Sum Less Than k
5+
6+
Medium
7+
8+
You are given a **0-indexed** integer matrix `grid` and an integer `k`.
9+
10+
Return _the **number** of submatrices that contain the top-left element of the_ `grid`, _and have a sum less than or equal to_ `k`.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2024/01/01/example1.png)
15+
16+
**Input:** grid = \[\[7,6,3],[6,6,1]], k = 18
17+
18+
**Output:** 4
19+
20+
**Explanation:** There are only 4 submatrices, shown in the image above, that contain the top-left element of grid, and have a sum less than or equal to 18.
21+
22+
**Example 2:**
23+
24+
![](https://assets.leetcode.com/uploads/2024/01/01/example21.png)
25+
26+
**Input:** grid = \[\[7,2,9],[1,5,0],[2,6,6]], k = 20
27+
28+
**Output:** 6
29+
30+
**Explanation:** There are only 6 submatrices, shown in the image above, that contain the top-left element of grid, and have a sum less than or equal to 20.
31+
32+
**Constraints:**
33+
34+
* `m == grid.length`
35+
* `n == grid[i].length`
36+
* `1 <= n, m <= 1000`
37+
* `0 <= grid[i][j] <= 1000`
38+
* <code>1 <= k <= 10<sup>9</sup></code>
39+
40+
## Solution
41+
42+
```kotlin
43+
class Solution {
44+
fun countSubmatrices(grid: Array<IntArray>, k: Int): Int {
45+
val n = grid[0].size
46+
val sums = IntArray(n)
47+
var ans = 0
48+
for (ints in grid) {
49+
var sum = 0
50+
for (col in 0 until n) {
51+
sum += ints[col]
52+
sums[col] += sum
53+
if (sums[col] <= k) {
54+
ans++
55+
} else {
56+
break
57+
}
58+
}
59+
}
60+
return ans
61+
}
62+
}
63+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
## 3071\. Minimum Operations to Write the Letter Y on a Grid
5+
6+
Medium
7+
8+
You are given a **0-indexed** `n x n` grid where `n` is odd, and `grid[r][c]` is `0`, `1`, or `2`.
9+
10+
We say that a cell belongs to the Letter **Y** if it belongs to one of the following:
11+
12+
* The diagonal starting at the top-left cell and ending at the center cell of the grid.
13+
* The diagonal starting at the top-right cell and ending at the center cell of the grid.
14+
* The vertical line starting at the center cell and ending at the bottom border of the grid.
15+
16+
The Letter **Y** is written on the grid if and only if:
17+
18+
* All values at cells belonging to the Y are equal.
19+
* All values at cells not belonging to the Y are equal.
20+
* The values at cells belonging to the Y are different from the values at cells not belonging to the Y.
21+
22+
Return _the **minimum** number of operations needed to write the letter Y on the grid given that in one operation you can change the value at any cell to_ `0`_,_ `1`_,_ _or_ `2`_._
23+
24+
**Example 1:**
25+
26+
![](https://assets.leetcode.com/uploads/2024/01/22/y2.png)
27+
28+
**Input:** grid = \[\[1,2,2],[1,1,0],[0,1,0]]
29+
30+
**Output:** 3
31+
32+
**Explanation:** We can write Y on the grid by applying the changes highlighted in blue in the image above. After the operations, all cells that belong to Y, denoted in bold, have the same value of 1 while those that do not belong to Y are equal to 0. It can be shown that 3 is the minimum number of operations needed to write Y on the grid.
33+
34+
**Example 2:**
35+
36+
![](https://assets.leetcode.com/uploads/2024/01/22/y3.png)
37+
38+
**Input:** grid = \[\[0,1,0,1,0],[2,1,0,1,2],[2,2,2,0,1],[2,2,2,2,2],[2,1,2,2,2]]
39+
40+
**Output:** 12
41+
42+
**Explanation:** We can write Y on the grid by applying the changes highlighted in blue in the image above. After the operations, all cells that belong to Y, denoted in bold, have the same value of 0 while those that do not belong to Y are equal to 2. It can be shown that 12 is the minimum number of operations needed to write Y on the grid.
43+
44+
**Constraints:**
45+
46+
* `3 <= n <= 49`
47+
* `n == grid.length == grid[i].length`
48+
* `0 <= grid[i][j] <= 2`
49+
* `n` is odd.
50+
51+
## Solution
52+
53+
```kotlin
54+
import kotlin.math.min
55+
56+
class Solution {
57+
fun minimumOperationsToWriteY(arr: Array<IntArray>): Int {
58+
val n = arr.size
59+
val cnt1 = IntArray(3)
60+
val cnt2 = IntArray(3)
61+
val x = n / 2
62+
val y = n / 2
63+
for (j in x until n) {
64+
cnt1[arr[j][y]]++
65+
arr[j][y] = 3
66+
}
67+
for (j in x downTo 0) {
68+
if (arr[j][j] != 3) {
69+
cnt1[arr[j][j]]++
70+
}
71+
arr[j][j] = 3
72+
}
73+
for (j in x downTo 0) {
74+
if (arr[j][n - 1 - j] != 3) {
75+
cnt1[arr[j][n - 1 - j]]++
76+
}
77+
arr[j][n - 1 - j] = 3
78+
}
79+
for (ints in arr) {
80+
for (j in 0 until n) {
81+
if (ints[j] != 3) {
82+
cnt2[ints[j]]++
83+
}
84+
}
85+
}
86+
val s1 = cnt1[0] + cnt1[1] + cnt1[2]
87+
val s2 = cnt2[0] + cnt2[1] + cnt2[2]
88+
var min = Int.MAX_VALUE
89+
for (i in 0..2) {
90+
for (j in 0..2) {
91+
if (i != j) {
92+
min = min((s1 - cnt1[i] + s2 - cnt2[j]), min)
93+
}
94+
}
95+
}
96+
return min
97+
}
98+
}
99+
```

0 commit comments

Comments
 (0)