Skip to content

Commit 6c77a74

Browse files
authored
Merge pull request #55 from fartem/661-Image-Smoother
2025-08-19 v. 1.5.0: added "661. Image Smoother"
2 parents 40c7ea7 + e2258fb commit 6c77a74

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
2020
| 575. Distribute Candies | [Link](https://leetcode.com/problems/distribute-candies/) | [Link](./src/main/kotlin/xyz/fartem/leetcodekotlin/easy/DistributeCandies.kt) | [Link](./src/test/kotlin/xyz/fartem/leetcodekotlin/easy/DistributeCandiesTest.kt) |
2121
| 598. Range Addition II | [Link](https://leetcode.com/problems/range-addition-ii/) | [Link](./src/main/kotlin/xyz/fartem/leetcodekotlin/easy/RangeAdditionII.kt) | [Link](./src/test/kotlin/xyz/fartem/leetcodekotlin/easy/RangeAdditionIITest.kt) |
2222
| 605. Can Place Flowers | [Link](https://leetcode.com/problems/can-place-flowers/) | [Link](./src/main/kotlin/xyz/fartem/leetcodekotlin/easy/CanPlaceFlowers.kt) | [Link](./src/test/kotlin/xyz/fartem/leetcodekotlin/easy/CanPlaceFlowersTest.kt) |
23+
| 661. Image Smoother | [Link](https://leetcode.com/problems/image-smoother/) | [Link](./src/main/kotlin/xyz/fartem/leetcodekotlin/easy/ImageSmoother.kt) | [Link](./src/test/kotlin/xyz/fartem/leetcodekotlin/easy/ImageSmootherTest.kt) |
2324
| 696. Count Binary Substrings | [Link](https://leetcode.com/problems/count-binary-substrings/) | [Link](./src/main/kotlin/xyz/fartem/leetcodekotlin/easy/CountBinarySubstrings.kt) | [Link](./src/test/kotlin/xyz/fartem/leetcodekotlin/easy/CountBinarySubstringsTest.kt) |
2425
| 697. Degree of an Array | [Link](https://leetcode.com/problems/degree-of-an-array/) | [Link](./src/main/kotlin/xyz/fartem/leetcodekotlin/easy/DegreeOfAnArray.kt) | [Link](./src/test/kotlin/xyz/fartem/leetcodekotlin/easy/DegreeOfAnArrayTest.kt) |
2526
| 1656. Design an Ordered Stream | [Link](https://leetcode.com/problems/design-an-ordered-stream/) | [Link](./src/main/kotlin/xyz/fartem/leetcodekotlin/easy/DesignAnOrderedStream.kt) | [Link](./src/test/kotlin/xyz/fartem/leetcodekotlin/easy/DesignAnOrderedStreamTest.kt) |

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ plugins {
1111
}
1212

1313
group = "xyz.fartem.leetcodekotlin"
14-
version = "1.4.9"
14+
version = "1.5.0"
1515

1616
repositories {
1717
mavenCentral()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package xyz.fartem.leetcodekotlin.easy
2+
3+
class ImageSmoother {
4+
fun imageSmoother(img: Array<IntArray>): Array<IntArray> {
5+
val m = img.size
6+
val n = img[0].size
7+
val result = Array(m) { IntArray(n) }
8+
9+
for (i in 0 until m) {
10+
for (j in 0 until n) {
11+
var sum = 0
12+
var count = 0
13+
14+
for (x in i - 1..i + 1) {
15+
for (y in j - 1..j + 1) {
16+
if (x in 0 until m && y in 0 until n) {
17+
sum += img[x][y]
18+
count++
19+
}
20+
}
21+
}
22+
23+
result[i][j] = sum / count
24+
}
25+
}
26+
27+
return result
28+
}
29+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package xyz.fartem.leetcodekotlin.easy
2+
3+
import kotlin.test.Test
4+
import kotlin.test.assertContentEquals
5+
6+
class ImageSmootherTest {
7+
private val solution = ImageSmoother()
8+
9+
@Test
10+
fun testDefaultOne() {
11+
val correct = arrayOf(
12+
intArrayOf(0, 0, 0),
13+
intArrayOf(0, 0, 0),
14+
intArrayOf(0, 0, 0)
15+
)
16+
17+
val result = solution.imageSmoother(
18+
arrayOf(
19+
intArrayOf(1, 1, 1),
20+
intArrayOf(1, 0, 1),
21+
intArrayOf(1, 1, 1)
22+
)
23+
)
24+
25+
for (i in 0 until correct.size) {
26+
assertContentEquals(
27+
correct[i],
28+
result[i]
29+
)
30+
}
31+
}
32+
33+
@Test
34+
fun testDefaultTwo() {
35+
val correct = arrayOf(
36+
intArrayOf(137, 141, 137),
37+
intArrayOf(141, 138, 141),
38+
intArrayOf(137, 141, 137)
39+
)
40+
41+
val result = solution.imageSmoother(
42+
arrayOf(
43+
intArrayOf(100, 200, 100),
44+
intArrayOf(200, 50, 200),
45+
intArrayOf(100, 200, 100)
46+
)
47+
)
48+
49+
for (i in 0 until correct.size) {
50+
assertContentEquals(
51+
correct[i],
52+
result[i]
53+
)
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)