File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ My solutions to LeetCode problems in Kotlin.
2626
| [1829](https://leetcode.com/problems/maximum-xor-for-each-query/) | [Maximum XOR for Each Query](src/main/kotlin/com/schmoczer/leetcode/_1829/MaximumXorForEachQuery.kt) | Medium |
2727
| [1861](https://leetcode.com/problems/rotating-the-box/) | [Rotating the Box](src/main/kotlin/com/schmoczer/leetcode/_1861/RotatingTheBox.kt) | Medium |
2828
| [1957](https://leetcode.com/problems/delete-characters-to-make-fancy-string/) | [Delete Characters to Make Fancy String](src/main/kotlin/com/schmoczer/leetcode/_1957/DeleteCharactersToMakeFancyString.kt) | Easy |
29+
| [1975](https://leetcode.com/problems/maximum-matrix-sum/) | [Maximum Matrix Sum](src/main/kotlin/com/schmoczer/leetcode/_1975/MaximumMatrixSum.kt) | Medium |
2930
| [2064](https://leetcode.com/problems/minimized-maximum-of-products-distributed-to-any-store/) | [Minimized Maximum of Products Distributed to Any Store](src/main/kotlin/com/schmoczer/leetcode/_2064/MinimizedMaximum.kt) | Medium |
3031
| [2070](https://leetcode.com/problems/most-beautiful-item-for-each-query/) | [Most Beautiful Item for Each Query](src/main/kotlin/com/schmoczer/leetcode/_2070/MostBeautifulItemForEachQuery.kt) | Medium |
3132
| [2275](https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/) | [Largest Combination With Bitwise AND Greater Than Zero](src/main/kotlin/com/schmoczer/leetcode/_2275/LargestCombination.kt) | Medium |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.schmoczer.leetcode._1975
2+
3+
import kotlin.math.abs
4+
5+
class MaximumMatrixSum {
6+
// Attempt 1
7+
fun maxMatrixSum1(matrix: Array<IntArray>): Long {
8+
val countOfMinus = matrix.sumOf { it.count { it < 0 } }
9+
var maxSum: Long = 0
10+
for (row in matrix) {
11+
for (number in row) {
12+
maxSum += abs(number)
13+
}
14+
}
15+
return if (countOfMinus % 2 == 0) {
16+
maxSum
17+
} else {
18+
val minNumber = matrix.minOf { it.minOf { abs(it) } }
19+
maxSum - 2 * minNumber
20+
}
21+
}
22+
23+
// Attempt 2: Runtime 9ms Beats 100.00%
24+
fun maxMatrixSum(matrix: Array<IntArray>): Long {
25+
var countOfMinus = 0
26+
var minNumber = Int.MAX_VALUE
27+
var maxSum: Long = 0
28+
for (row in matrix) {
29+
for (number in row) {
30+
maxSum += abs(number)
31+
if (number < 0) {
32+
countOfMinus++
33+
}
34+
if (abs(number) < minNumber) {
35+
minNumber = abs(number)
36+
}
37+
}
38+
}
39+
return if (countOfMinus % 2 == 0) {
40+
maxSum
41+
} else {
42+
maxSum - 2 * minNumber
43+
}
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Maximum Matrix Sum
2+
3+
You are given an `n x n` integer `matrix`. You can do the following operation any number of times:
4+
5+
- Choose any two adjacent elements of `matrix` and multiply each of them by `-1`.
6+
7+
Two elements are considered adjacent if and only if they share a border.
8+
9+
Your goal is to maximize the summation of the matrix's elements. Return the maximum sum of the matrix's elements using
10+
the operation mentioned above.
11+
12+
Example 1:
13+
14+
> Input: matrix = [[1,-1],[-1,1]]
15+
>
16+
> Output: 4
17+
18+
Example 2:
19+
20+
> Input: matrix = [[1,2,3],[-1,-2,-3],[1,2,3]]
21+
>
22+
> Output: 16
23+
24+
Constraints:
25+
26+
- `n == matrix.length == matrix[i].length`
27+
- `2 <= n <= 250`
28+
- `-10^5 <= matrix[i][j] <= 10^5`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.schmoczer.leetcode._1975
2+
3+
import org.junit.jupiter.api.BeforeEach
4+
import kotlin.test.Test
5+
import kotlin.test.assertEquals
6+
7+
class MaximumMatrixSumTest {
8+
private lateinit var sut: MaximumMatrixSum
9+
10+
@BeforeEach
11+
fun setUp() {
12+
sut = MaximumMatrixSum()
13+
}
14+
15+
@Test
16+
fun `a 2x2 matrix with max sum of 4`() {
17+
val input = arrayOf(intArrayOf(1, -1), intArrayOf(-1, 1))
18+
val expected: Long = 4
19+
20+
val result = sut.maxMatrixSum(input)
21+
22+
assertEquals(expected, result)
23+
}
24+
25+
@Test
26+
fun `a 3x3 matrix with max sum of 16`() {
27+
val input = arrayOf(intArrayOf(1, 2, 3), intArrayOf(-1, -2, -3), intArrayOf(1, 2, 3))
28+
val expected: Long = 16
29+
30+
val result = sut.maxMatrixSum(input)
31+
32+
assertEquals(expected, result)
33+
}
34+
35+
@Test
36+
fun `a 3x3 matrix with all numbers being -10000 has max sum of 70000`() {
37+
val input = arrayOf(intArrayOf(-10000, -10000, -10000), intArrayOf(-10000, -10000, -10000), intArrayOf(-10000, -10000, -10000))
38+
val expected: Long = 70000
39+
40+
val result = sut.maxMatrixSum(input)
41+
42+
assertEquals(expected, result)
43+
}
44+
}

0 commit comments

Comments
 (0)