Skip to content

Commit

Permalink
Add solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
ashtanko committed Nov 23, 2024
1 parent e1b369e commit c3f14cf
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 10 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@

### Metrics
```text
13688 number of properties
9402 number of functions
8401 number of classes
13696 number of properties
9410 number of functions
8411 number of classes
184 number of packages
3202 number of kt files
3206 number of kt files
```


### Complexity Report
```text
235023 lines of code (loc)
146488 source lines of code (sloc)
107188 logical lines of code (lloc)
63152 comment lines of code (cloc)
23033 cyclomatic complexity (mcc)
19303 cognitive complexity
235265 lines of code (loc)
146626 source lines of code (sloc)
107286 logical lines of code (lloc)
63228 comment lines of code (cloc)
23048 cyclomatic complexity (mcc)
19314 cognitive complexity
0 number of total code smells
43 comment source ratio
214 mcc per 1,000 lloc
Expand Down
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 src/main/kotlin/dev/shtanko/algorithms/leetcode/RotatingTheBox.kt
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
}
}
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())
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())

0 comments on commit c3f14cf

Please sign in to comment.