-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
248 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/kotlin/dev/shtanko/algorithms/leetcode/MaxEqualRowsAfterFlips.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2024 Oleksii Shtanko | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.shtanko.algorithms.leetcode | ||
|
||
import dev.shtanko.algorithms.annotations.level.Medium | ||
|
||
/** | ||
* 1072. Flip Columns For Maximum Number of Equal Rows | ||
* @see <a href="https://leetcode.com/problems/flip-columns-for-maximum-number-of-equal-rows/">Source</a> | ||
*/ | ||
@Medium("https://leetcode.com/problems/flip-columns-for-maximum-number-of-equal-rows/") | ||
fun interface MaxEqualRowsAfterFlips { | ||
operator fun invoke(matrix: Array<IntArray>): Int | ||
} | ||
|
||
class MaxEqualRowsAfterFlipsHashMap : MaxEqualRowsAfterFlips { | ||
override fun invoke(matrix: Array<IntArray>): Int { | ||
val patternFrequency = mutableMapOf<String, Int>() | ||
|
||
for (currentRow in matrix) { | ||
// Convert row to pattern using map and joinToString | ||
// 'T' if element matches first element, 'F' otherwise | ||
val rowPattern = currentRow.joinToString("") { | ||
if (it == currentRow[0]) "T" else "F" | ||
} | ||
|
||
// Update pattern frequency using getOrDefault and put | ||
patternFrequency[rowPattern] = patternFrequency.getOrDefault(rowPattern, 0) + 1 | ||
} | ||
|
||
// Return maximum frequency using values.maxOrNull() with fallback to 0 | ||
return patternFrequency.values.maxOrNull() ?: 0 | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/kotlin/dev/shtanko/algorithms/leetcode/RotatingTheBox.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2024 Oleksii Shtanko | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.shtanko.algorithms.leetcode | ||
|
||
import dev.shtanko.algorithms.annotations.level.Medium | ||
|
||
/** | ||
* 1861. Rotating the Box | ||
* @see <a href="https://leetcode.com/problems/rotating-the-box/">Rotating the Box</a> | ||
*/ | ||
@Medium("https://leetcode.com/problems/rotating-the-box/") | ||
fun interface RotatingTheBox { | ||
operator fun invoke(box: Array<CharArray>): Array<CharArray> | ||
} | ||
|
||
class RotatingTheBoxCombineRotation : RotatingTheBox { | ||
override fun invoke(box: Array<CharArray>): Array<CharArray> { | ||
val m = box.size | ||
val n = box[0].size | ||
val result = Array(n) { CharArray(m) { '.' } } | ||
|
||
// Apply gravity to let stones fall to the lowest possible empty cell in each column | ||
for (i in 0 until m) { | ||
var lowestRowWithEmptyCell = n - 1 | ||
// Process each cell in row `i` in reversed order | ||
for (j in n - 1 downTo 0) { | ||
when (box[i][j]) { | ||
'#' -> { | ||
// Place the stone in the correct position in the rotated grid | ||
result[lowestRowWithEmptyCell][m - i - 1] = '#' | ||
lowestRowWithEmptyCell-- | ||
} | ||
|
||
'*' -> { | ||
// Place the obstacle in the correct position in the rotated grid | ||
result[j][m - i - 1] = '*' | ||
lowestRowWithEmptyCell = j - 1 | ||
} | ||
} | ||
} | ||
} | ||
|
||
return result | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/test/kotlin/dev/shtanko/algorithms/leetcode/MaxEqualRowsAfterFlipsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2024 Oleksii Shtanko | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.shtanko.algorithms.leetcode | ||
|
||
import java.util.stream.Stream | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.extension.ExtensionContext | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.Arguments | ||
import org.junit.jupiter.params.provider.ArgumentsProvider | ||
import org.junit.jupiter.params.provider.ArgumentsSource | ||
|
||
abstract class MaxEqualRowsAfterFlipsTest<T : MaxEqualRowsAfterFlips>(private val strategy: T) { | ||
private class InputArgumentsProvider : ArgumentsProvider { | ||
override fun provideArguments(context: ExtensionContext?): Stream<out Arguments> = Stream.of( | ||
Arguments.of( | ||
arrayOf(intArrayOf(0, 1), intArrayOf(1, 1)), | ||
1, | ||
), | ||
Arguments.of( | ||
arrayOf(intArrayOf(0, 1), intArrayOf(1, 0)), | ||
2, | ||
), | ||
Arguments.of( | ||
arrayOf(intArrayOf(0, 0, 0), intArrayOf(0, 0, 1), intArrayOf(1, 1, 0)), | ||
2, | ||
), | ||
) | ||
} | ||
|
||
@ParameterizedTest | ||
@ArgumentsSource(InputArgumentsProvider::class) | ||
fun maximumEqualRowsAfterFlipsTest(matrix: Array<IntArray>, expected: Int) { | ||
val actual = strategy(matrix) | ||
assertThat(actual).isEqualTo(expected) | ||
} | ||
} | ||
|
||
class MaxEqualRowsAfterFlipsHashMapTest : | ||
MaxEqualRowsAfterFlipsTest<MaxEqualRowsAfterFlips>(MaxEqualRowsAfterFlipsHashMap()) |
77 changes: 77 additions & 0 deletions
77
src/test/kotlin/dev/shtanko/algorithms/leetcode/RotatingTheBoxTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2024 Oleksii Shtanko | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.shtanko.algorithms.leetcode | ||
|
||
import java.util.stream.Stream | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.extension.ExtensionContext | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.Arguments | ||
import org.junit.jupiter.params.provider.ArgumentsProvider | ||
import org.junit.jupiter.params.provider.ArgumentsSource | ||
|
||
abstract class RotatingTheBoxTest<out T : RotatingTheBox>(private val strategy: T) { | ||
private class InputArgumentsProvider : ArgumentsProvider { | ||
override fun provideArguments(context: ExtensionContext?): Stream<out Arguments> = Stream.of( | ||
Arguments.of( | ||
arrayOf(charArrayOf('#', '.', '#')), | ||
arrayOf( | ||
charArrayOf('.'), | ||
charArrayOf('#'), | ||
charArrayOf('#'), | ||
), | ||
), | ||
Arguments.of( | ||
arrayOf( | ||
charArrayOf('#', '.', '*', '.'), | ||
charArrayOf('#', '#', '*', '.'), | ||
), | ||
arrayOf( | ||
charArrayOf('#', '.'), | ||
charArrayOf('#', '#'), | ||
charArrayOf('*', '*'), | ||
charArrayOf('.', '.'), | ||
), | ||
), | ||
Arguments.of( | ||
arrayOf( | ||
charArrayOf('#', '#', '*', '.', '*', '.'), | ||
charArrayOf('#', '#', '#', '*', '.', '.'), | ||
charArrayOf('#', '#', '#', '.', '#', '.'), | ||
), | ||
arrayOf( | ||
charArrayOf('.', '#', '#'), | ||
charArrayOf('.', '#', '#'), | ||
charArrayOf('#', '#', '*'), | ||
charArrayOf('#', '*', '.'), | ||
charArrayOf('#', '.', '*'), | ||
charArrayOf('#', '.', '.'), | ||
), | ||
), | ||
) | ||
} | ||
|
||
@ParameterizedTest | ||
@ArgumentsSource(InputArgumentsProvider::class) | ||
fun `rotate the box test`(box: Array<CharArray>, expected: Array<CharArray>) { | ||
val actual = strategy(box) | ||
assertThat(actual).isEqualTo(expected) | ||
} | ||
} | ||
|
||
class RotatingTheBoxCombineRotationAndGravityTest : | ||
RotatingTheBoxTest<RotatingTheBox>(RotatingTheBoxCombineRotation()) |