Skip to content

Commit 0928add

Browse files
committed
add solution for q837 and q2768, remove junit4
1 parent e3d0505 commit 0928add

File tree

10 files changed

+258
-38
lines changed

10 files changed

+258
-38
lines changed

.idea/kotlinc.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

-8
This file was deleted.

leetcode.iml

-18
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,4 @@
1010
</list>
1111
</option>
1212
</component>
13-
<component name="FacetManager">
14-
<facet type="kotlin-language" name="Kotlin">
15-
<configuration version="5" platform="JVM 1.8" allPlatforms="JVM [1.8]" useProjectSettings="false">
16-
<compilerSettings />
17-
<compilerArguments>
18-
<stringArguments>
19-
<stringArg name="jvmTarget" arg="1.8" />
20-
<stringArg name="apiVersion" arg="1.7" />
21-
<stringArg name="languageVersion" arg="1.7" />
22-
</stringArguments>
23-
<arrayArguments>
24-
<arrayArg name="pluginClasspaths" />
25-
<arrayArg name="pluginOptions" />
26-
</arrayArguments>
27-
</compilerArguments>
28-
</configuration>
29-
</facet>
30-
</component>
3113
</module>

pom.xml

+4-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<version>0.0.1-SNAPSHOT</version>
77

88
<properties>
9-
<kotlin.version>1.7.10</kotlin.version>
9+
<kotlin.version>1.9.0</kotlin.version>
1010
</properties>
1111

1212

@@ -22,11 +22,6 @@
2222
<artifactId>junit-jupiter-engine</artifactId>
2323
<scope>test</scope>
2424
</dependency>
25-
<dependency>
26-
<groupId>org.junit.vintage</groupId>
27-
<artifactId>junit-vintage-engine</artifactId>
28-
<scope>test</scope>
29-
</dependency>
3025
<dependency>
3126
<groupId>org.junit.jupiter</groupId>
3227
<artifactId>junit-jupiter-params</artifactId>
@@ -60,11 +55,12 @@
6055
<testSourceDirectory>src/test</testSourceDirectory>
6156
<plugins>
6257
<plugin>
58+
<groupId>org.apache.maven.plugins</groupId>
6359
<artifactId>maven-compiler-plugin</artifactId>
6460
<version>3.8.0</version>
6561
<configuration>
66-
<source>1.8</source>
67-
<target>1.8</target>
62+
<source>16</source>
63+
<target>16</target>
6864
</configuration>
6965
</plugin>
7066
<plugin>

src/main/kotlin/q2768/Solution.kt

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package q2768
2+
3+
class Solution {
4+
fun countBlackBlocks(m: Int, n: Int, coordinates: Array<IntArray>): LongArray {
5+
val positionToBlackPointCountMap = mutableMapOf<Position, Int>()
6+
coordinates.forEach { point ->
7+
val row = point[0]
8+
val col = point[1]
9+
if (row > 0) {
10+
if (col > 0) {
11+
positionToBlackPointCountMap.merge(Position(row - 1, col - 1), 1) { int1, int2 -> int1 + int2 }
12+
}
13+
if (col < n - 1) {
14+
positionToBlackPointCountMap.merge(Position(row - 1, col), 1) { int1, int2 -> int1 + int2 }
15+
}
16+
}
17+
if (row < m - 1) {
18+
if (col > 0) {
19+
positionToBlackPointCountMap.merge(Position(row, col - 1), 1) { int1, int2 -> int1 + int2 }
20+
}
21+
if (col < n - 1) {
22+
positionToBlackPointCountMap.merge(Position(row, col), 1) { int1, int2 -> int1 + int2 }
23+
}
24+
}
25+
}
26+
val totalNumberOfBlock = (m - 1).toLong() * (n - 1).toLong()
27+
val blackBoxCount = LongArray(5) { 0 }
28+
positionToBlackPointCountMap.values.forEach {
29+
blackBoxCount[it]++
30+
}
31+
blackBoxCount[0] =
32+
totalNumberOfBlock - blackBoxCount[1] - blackBoxCount[2] - blackBoxCount[3] - blackBoxCount[4]
33+
return blackBoxCount
34+
}
35+
36+
data class Position(
37+
val row: Int,
38+
val col: Int
39+
)
40+
}

src/main/kotlin/q837/Solution.kt

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package q837
2+
3+
class Solution {
4+
private lateinit var dp: Array<Double>
5+
6+
/**
7+
* return p = P(totalPts <= n)
8+
* stop when >= k
9+
* 0 <= k <= n <= 10^4
10+
* 1 <= maxPts <= 10^4
11+
* dp memorize
12+
*/
13+
fun new21Game(n: Int, k: Int, maxPts: Int): Double {
14+
dp = Array(k + maxPts + 1) { i ->
15+
when (i) {
16+
in (k..n) -> 1.0
17+
in ((n + 1) until (k + maxPts + 1)) -> 0.0
18+
else -> -1.0
19+
}
20+
}
21+
if (k > 0) {
22+
var pk_1 = 0.0
23+
for (i in (k until maxPts + k)) {
24+
pk_1 += dp[i]
25+
}
26+
pk_1 = pk_1.div(maxPts)
27+
dp[k - 1] = pk_1
28+
}
29+
for (i in (0 until k - 1).reversed()) {
30+
computeProb(i, maxPts)
31+
}
32+
return dp[0]
33+
}
34+
35+
36+
// 22 20 10
37+
// 1 2 3 3 4 5 6 7 8 9 10 11 12
38+
// 13 14 15 16 17 18 19
39+
// 20 21 22 -> 1
40+
// 23 24 25 26 27 28 29 -> 0
41+
// P(19) = Sum(P(20-29)) / 10
42+
// P(18) = Sum(P(19-28)) / 10
43+
// P(17) = Sum(P(18-27)) / 10
44+
// ...
45+
// P(0) = Sum(P(1-10)) / 10
46+
private fun computeProb(currentPts: Int, maxPts: Int) {
47+
val p_mc = dp[maxPts + currentPts + 1]
48+
val p_next = dp[currentPts + 1]
49+
val p_c = (p_next.times(maxPts + 1) - p_mc).div(maxPts)
50+
dp[currentPts] = p_c
51+
}
52+
}

src/test/kotlin/q2768/SolutionTest.kt

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package q2768
2+
3+
import org.junit.jupiter.api.Assertions.assertArrayEquals
4+
import org.junit.jupiter.params.ParameterizedTest
5+
import org.junit.jupiter.params.provider.MethodSource
6+
import java.util.stream.Stream
7+
8+
class SolutionTest {
9+
val solution = Solution()
10+
11+
@ParameterizedTest
12+
@MethodSource("testCases")
13+
fun countBlackBlocksCorrect(testCase: TestCase) {
14+
val actual = solution.countBlackBlocks(testCase.width, testCase.height, testCase.blackBoxes)
15+
assertArrayEquals(testCase.expected, actual)
16+
}
17+
18+
data class TestCase(
19+
val width: Int,
20+
val height: Int,
21+
val blackBoxes: Array<IntArray>,
22+
val expected: LongArray
23+
)
24+
25+
companion object {
26+
@JvmStatic
27+
fun testCases() = Stream.of(
28+
TestCase(
29+
2, 2,
30+
arrayOf(),
31+
longArrayOf(1L, 0L, 0L, 0L, 0L)
32+
),
33+
TestCase(
34+
2, 2,
35+
arrayOf(intArrayOf(0, 0)),
36+
longArrayOf(0L, 1L, 0L, 0L, 0L)
37+
),
38+
TestCase(
39+
2, 2,
40+
arrayOf(intArrayOf(0, 0), intArrayOf(0, 1)),
41+
longArrayOf(0L, 0L, 1L, 0L, 0L)
42+
),
43+
TestCase(
44+
2, 2,
45+
arrayOf(intArrayOf(0, 0), intArrayOf(0, 1), intArrayOf(1, 0)),
46+
longArrayOf(0L, 0L, 0L, 1L, 0L)
47+
),
48+
TestCase(
49+
2, 2,
50+
arrayOf(intArrayOf(0, 0), intArrayOf(0, 1), intArrayOf(1, 0), intArrayOf(1, 1)),
51+
longArrayOf(0L, 0L, 0L, 0L, 1L)
52+
),
53+
TestCase(
54+
3, 3,
55+
arrayOf(intArrayOf(1, 1)),
56+
longArrayOf(0L, 4L, 0L, 0L, 0L)
57+
),
58+
TestCase(
59+
10, 10,
60+
arrayOf(
61+
intArrayOf(0, 0),
62+
intArrayOf(1, 1),
63+
intArrayOf(2, 2),
64+
intArrayOf(3, 3),
65+
intArrayOf(4, 4),
66+
intArrayOf(5, 5),
67+
intArrayOf(6, 6),
68+
intArrayOf(7, 7),
69+
intArrayOf(8, 8),
70+
intArrayOf(9, 9),
71+
),
72+
longArrayOf(56L, 16L, 9L, 0L, 0L)
73+
),
74+
TestCase(
75+
10000, 10000,
76+
Array(1000) { it ->
77+
intArrayOf(it + 1, it + 1)
78+
},
79+
longArrayOf(99977000L, 2002L, 999L, 0L, 0L)
80+
),
81+
TestCase(
82+
100000, 100000,
83+
Array(10000) { it ->
84+
intArrayOf(it + 1, it + 1)
85+
},
86+
longArrayOf(9999770000L, 20002L, 9999L, 0L, 0L)
87+
)
88+
)
89+
}
90+
}

src/test/kotlin/q36/SolutionTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package q36
22

3-
import org.junit.Test
43
import org.junit.jupiter.api.Assertions.*
4+
import org.junit.jupiter.api.Test
55

66
internal class SolutionTest {
77
val solution = Solution()

src/test/kotlin/q41/SolutionTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package q41
22

3-
import org.junit.Test
4-
import org.junit.jupiter.api.Assertions.*
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
55

66
internal class SolutionTest {
77
var solution = Solution()

src/test/kotlin/q837/SolutionTest.kt

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package q837
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
5+
import org.junit.jupiter.params.ParameterizedTest
6+
import org.junit.jupiter.params.provider.CsvSource
7+
8+
class SolutionTest {
9+
private val solution = Solution()
10+
11+
@Test
12+
fun `when n is 0 then probability is 1`() {
13+
val actual = solution.new21Game(0, 0, 1)
14+
assertEquals(1.0, actual)
15+
}
16+
17+
@ParameterizedTest
18+
@CsvSource(
19+
textBlock = """
20+
n, k, maxPts
21+
2, 1, 1
22+
4, 2, 2
23+
6, 3, 3
24+
10, 5, 5""", useHeadersInDisplayName = true
25+
)
26+
fun `when 2k-1 lt n and maxPts le k then probability is 1`(n: Int, k: Int, maxPts: Int) {
27+
val actual = solution.new21Game(n, k, maxPts)
28+
assertEquals(1.0, actual, 0.00001)
29+
}
30+
31+
@ParameterizedTest
32+
@CsvSource(
33+
textBlock = """
34+
n, k, maxPts, expected
35+
1, 0, 1, 1.0
36+
2, 2, 2, 0.75
37+
2, 2, 3, 0.44444
38+
1, 1, 10, 0.1
39+
2, 1, 10, 0.2
40+
3, 1, 10, 0.3
41+
4, 1, 10, 0.4
42+
5, 1, 10, 0.5
43+
6, 1, 10, 0.6
44+
7, 1, 10, 0.7
45+
8, 1, 10, 0.8
46+
9, 1, 10, 0.9
47+
10, 1, 10, 1.0
48+
1000, 1, 1000, 1.0
49+
5000, 4800, 3000, 0.13231
50+
9301, 9224, 7771, 0.01846
51+
19301,19224, 17771, 0.00778
52+
19302,19224, 17771, 0.00788
53+
19303,19224, 17771, 0.00798
54+
19304,19224, 17771, 0.00808
55+
19305,19224, 17771, 0.00818
56+
19306,19224, 17771, 0.00828
57+
19307,19224, 17771, 0.00838
58+
19308,19224, 17771, 0.00848
59+
19309,19224, 17771, 0.00858
60+
19310,19224, 17771, 0.00868
61+
21, 17, 10, 0.73278""", useHeadersInDisplayName = true
62+
)
63+
fun `when 2k-1 ge n`(n: Int, k: Int, maxPts: Int, expected: Double) {
64+
val actual = solution.new21Game(n, k, maxPts)
65+
assertEquals(expected, actual, 0.00001)
66+
67+
}
68+
}

0 commit comments

Comments
 (0)