Skip to content

Commit 3d48b3c

Browse files
Merge pull request #119 from Ashwagandha-coder/develop
31.12 pr
2 parents 62b1fd3 + 8c2d7c2 commit 3d48b3c

File tree

6 files changed

+290
-2
lines changed

6 files changed

+290
-2
lines changed

app/src/main/java/com/leetcode_kotlin/AlgorithmLeetcode.kt

+114
Original file line numberDiff line numberDiff line change
@@ -3893,6 +3893,120 @@ class SolutionOth : VersionControl() {
38933893
}
38943894
}
38953895

3896+
/**
3897+
* 275. H-Index II
3898+
*/
3899+
3900+
fun hIndex(citations: IntArray): Int {
3901+
val n = citations.size
3902+
var left = 0
3903+
var right = n - 1
3904+
3905+
while (left <= right) {
3906+
val mid = left + (right - left) / 2
3907+
if (citations[mid] >= n - mid) {
3908+
right = mid - 1
3909+
} else {
3910+
left = mid + 1
3911+
}
3912+
}
3913+
3914+
return n - left
3915+
}
3916+
3917+
/**
3918+
* 378. Kth Smallest Element in a Sorted Matrix
3919+
*/
3920+
3921+
fun kthSmallest(matrix: Array<IntArray>, k: Int): Int {
3922+
val pq = PriorityQueue<Int>()
3923+
val len = matrix.size
3924+
for (i in 0 until len) {
3925+
for (j in 0 until len) {
3926+
pq.offer(matrix[i][j])
3927+
}
3928+
}
3929+
var res = 0
3930+
for (i in 0 until k) {
3931+
res = pq.poll()
3932+
}
3933+
return res
3934+
}
3935+
3936+
/**
3937+
* 74. Search a 2D Matrix
3938+
*/
3939+
3940+
fun searchMatrix2D(matrix: Array<IntArray>, target: Int): Boolean {
3941+
var row = 0
3942+
var col = matrix[0].size - 1
3943+
while (row < matrix.size && col >= 0) {
3944+
if (matrix[row][col] == target) return true
3945+
else if (matrix[row][col] < target) row++
3946+
else col--
3947+
}
3948+
return false
3949+
}
3950+
3951+
/**
3952+
* 33. Search in Rotated Sorted Array
3953+
*/
3954+
3955+
3956+
fun search(nums: IntArray, target: Int): Int {
3957+
var left = 0
3958+
var right = nums.size - 1
3959+
3960+
while (left <= right) {
3961+
val mid = left + (right - left) / 2
3962+
3963+
if (nums[mid] == target) {
3964+
return mid
3965+
}
3966+
3967+
if (nums[left] <= nums[mid]) {
3968+
if (target >= nums[left] && target < nums[mid]) {
3969+
right = mid - 1
3970+
} else {
3971+
left = mid + 1
3972+
}
3973+
} else {
3974+
if (target > nums[mid] && target <= nums[right]) {
3975+
left = mid + 1
3976+
} else {
3977+
right = mid - 1
3978+
}
3979+
}
3980+
}
3981+
3982+
return -1
3983+
}
3984+
3985+
/**
3986+
* 81. Search in Rotated Sorted Array II
3987+
*/
3988+
3989+
3990+
fun searchII(nums: IntArray, target: Int): Boolean {
3991+
var l = 0
3992+
var r = nums.size - 1
3993+
while (l <= r) {
3994+
var m = l + (r - l) / 2
3995+
if (nums[m] == target) return true
3996+
if (nums[l] == nums[m] && nums[m] == nums[r]) {
3997+
l++
3998+
r--
3999+
} else if (nums[l] <= nums[m]) {
4000+
if (nums[l] <= target && nums[m] >= target) r = m - 1
4001+
else l = m + 1
4002+
} else {
4003+
if (target > nums[m] && target <= nums[r]) l = m + 1
4004+
else r = m - 1
4005+
}
4006+
}
4007+
return false
4008+
}
4009+
38964010

38974011

38984012

app/src/main/java/com/leetcode_kotlin/AlgorithmProdVariant.kt

+14
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@ class NumArrayProdVariant(nums: IntArray) {
710710

711711
/**
712712
* 852. Peak Index in a Mountain Array
713+
* Prod Variant
713714
*/
714715

715716
fun peakIndexInMountainArrayProdVariant(arr: IntArray): Int {
@@ -723,3 +724,16 @@ fun peakIndexInMountainArrayProdVariant(arr: IntArray): Int {
723724
return low
724725
}
725726

727+
/**
728+
* 278. First Bad Version
729+
* Prod Variant
730+
*/
731+
732+
733+
fun firstBadVersionProdVariant(n: Int): Int {
734+
val result = (1..n).toList().binarySearch {
735+
if (isBadVersion(it)) -1 else 1
736+
}
737+
return if (result < 0) -result - 1 else result
738+
}
739+

app/src/main/java/com/leetcode_kotlin/AlternativeAlgorithmSolution.kt

+71
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,77 @@ fun findBottomLeftValueAltSolution(root: TreeNode?): Int {
12111211
return value
12121212
}
12131213

1214+
/**
1215+
* 378. Kth Smallest Element in a Sorted Matrix
1216+
* Alternative Solution
1217+
* Time - O(n * log n)
1218+
* Space - O(1)
1219+
*/
1220+
1221+
1222+
fun kthSmallestAltSolution(matrix: Array<IntArray>, k: Int): Int {
1223+
val n = matrix.size
1224+
var low = matrix[0][0]
1225+
var high = matrix[n - 1][n - 1]
1226+
1227+
while (low < high) {
1228+
val mid = low + (high - low) / 2
1229+
val count = countLessOrEqual(matrix, mid)
1230+
1231+
if (count < k) {
1232+
low = mid + 1
1233+
} else {
1234+
high = mid
1235+
}
1236+
}
1237+
1238+
return low
1239+
}
1240+
1241+
private fun countLessOrEqual(matrix: Array<IntArray>, target: Int): Int {
1242+
val n = matrix.size
1243+
var count = 0
1244+
var row = n - 1
1245+
var col = 0
1246+
1247+
while (row >= 0 && col < n) {
1248+
if (matrix[row][col] <= target) {
1249+
count += row + 1
1250+
col++
1251+
} else {
1252+
row--
1253+
}
1254+
}
1255+
1256+
return count
1257+
}
1258+
1259+
/**
1260+
* 240. Search a 2D Matrix II
1261+
* Alternative Solution
1262+
* Time - O(m + n)
1263+
* Space - O(1)
1264+
*/
1265+
1266+
fun searchMatrix(matrix: Array<IntArray>?, target: Int): Boolean {
1267+
if (matrix.isNullOrEmpty() || matrix[0].isEmpty()) {
1268+
return false
1269+
}
1270+
var col = matrix[0].size - 1
1271+
var row = 0
1272+
while (col >= 0 && row <= matrix.size - 1) {
1273+
if (target == matrix[row][col]) {
1274+
return true
1275+
} else if (target < matrix[row][col]) {
1276+
col--
1277+
} else if (target > matrix[row][col]) {
1278+
row++
1279+
}
1280+
}
1281+
return false
1282+
}
1283+
1284+
12141285

12151286

12161287

app/src/main/java/com/leetcode_kotlin/Executing.kt

+14-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,22 @@ package com.leetcode_kotlin
88

99
fun main() {
1010

11-
val arr = intArrayOf(2, 2)
1211

13-
searchRange(arr, 2).also { it.forEach { elem -> println(elem) } }
12+
val matrix = arrayOf(
13+
intArrayOf(1, 5, 9),
14+
intArrayOf(10, 11, 13),
15+
intArrayOf(12, 13, 15)
16+
)
17+
val k = 8
1418

19+
20+
val arr = intArrayOf(1, 0, 1, 1, 1)
21+
22+
val target = 0
23+
24+
searchII(arr, target).also { println(it) }
25+
26+
//kthSmallestAltSolution(matrix, k).also { println(it) }
1527
}
1628

1729

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.leetcode_kotlin
2+
3+
import org.junit.Test
4+
import org.junit.jupiter.api.Assertions.assertEquals
5+
6+
class FirstBadVersionTest {
7+
8+
9+
/**
10+
* isBadVersion - is fake api in Leetcode
11+
*/
12+
13+
@Test
14+
fun `range-case`() {
15+
val expected = 1
16+
val actual = firstBadVersionProdVariant(1)
17+
assertEquals(expected, actual)
18+
}
19+
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.leetcode_kotlin
2+
3+
import org.junit.Assert.assertEquals
4+
import org.junit.Test
5+
6+
class PeakElementFinderTest {
7+
8+
9+
@Test
10+
fun testFindPeakElement_basicCase() {
11+
val nums = intArrayOf(1, 2, 3, 1)
12+
val expected = 2
13+
val actual = findPeakElement(nums)
14+
assertEquals(expected, actual)
15+
}
16+
17+
@Test
18+
fun testFindPeakElement_multiplePeaks() {
19+
val nums = intArrayOf(1, 2, 1, 3, 5, 6, 4)
20+
val expected = 5
21+
val actual = findPeakElement(nums)
22+
assertEquals(expected, actual)
23+
}
24+
25+
@Test
26+
fun testFindPeakElement_singleElement() {
27+
val nums = intArrayOf(1)
28+
val expected = 0
29+
val actual = findPeakElement(nums)
30+
assertEquals(expected, actual)
31+
}
32+
33+
@Test
34+
fun testFindPeakElement_increasingSequence() {
35+
val nums = intArrayOf(1, 2)
36+
val expected = 1
37+
val actual = findPeakElement(nums)
38+
assertEquals(expected, actual)
39+
}
40+
41+
@Test
42+
fun testFindPeakElement_decreasingSequence() {
43+
val nums = intArrayOf(2, 1)
44+
val expected = 0
45+
val actual = findPeakElement(nums)
46+
assertEquals(expected, actual)
47+
}
48+
49+
@Test
50+
fun testFindPeakElement_increasingSequencePeakAtEnd() {
51+
val nums = intArrayOf(1, 2, 3)
52+
val expected = 2
53+
val actual = findPeakElement(nums)
54+
assertEquals(expected, actual)
55+
}
56+
}

0 commit comments

Comments
 (0)