Skip to content

Commit 9571087

Browse files
committed
Added tasks 34, 35.
1 parent bfdf75f commit 9571087

File tree

6 files changed

+182
-0
lines changed

6 files changed

+182
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g0001_0100.s0034_find_first_and_last_position_of_element_in_sorted_array
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search
4+
// #Algorithm_II_Day_1_Binary_Search #Binary_Search_I_Day_5
5+
// #2022_04_30_Time_228_ms_(83.38%)_Space_39.6_MB_(85.97%)
6+
7+
class Solution constructor() {
8+
fun searchRange(nums: IntArray, target: Int): IntArray {
9+
val ans = IntArray(2)
10+
ans[0] = helper(nums, target, false)
11+
ans[1] = helper(nums, target, true)
12+
return ans
13+
}
14+
15+
private fun helper(nums: IntArray, target: Int, equals: Boolean): Int {
16+
var l = 0
17+
var r: Int = nums.size - 1
18+
var result: Int = -1
19+
while (l <= r) {
20+
val mid: Int = l + (r - l) / 2
21+
if (nums[mid] == target) {
22+
result = mid
23+
}
24+
if (nums[mid] < target || (nums[mid] == target && equals)) {
25+
l = mid + 1
26+
} else {
27+
r = mid - 1
28+
}
29+
}
30+
return result
31+
}
32+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
34\. Find First and Last Position of Element in Sorted Array
2+
3+
Medium
4+
5+
Given an array of integers `nums` sorted in non-decreasing order, find the starting and ending position of a given `target` value.
6+
7+
If `target` is not found in the array, return `[-1, -1]`.
8+
9+
You must write an algorithm with `O(log n)` runtime complexity.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [5,7,7,8,8,10], target = 8
14+
15+
**Output:** [3,4]
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [5,7,7,8,8,10], target = 6
20+
21+
**Output:** [-1,-1]
22+
23+
**Example 3:**
24+
25+
**Input:** nums = [], target = 0
26+
27+
**Output:** [-1,-1]
28+
29+
**Constraints:**
30+
31+
* <code>0 <= nums.length <= 10<sup>5</sup></code>
32+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
33+
* `nums` is a non-decreasing array.
34+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0001_0100.s0035_search_insert_position
2+
3+
// #Easy #Top_100_Liked_Questions #Array #Binary_Search #Algorithm_I_Day_1_Binary_Search
4+
// #Binary_Search_I_Day_2 #2022_04_30_Time_267_ms_(50.32%)_Space_39.5_MB_(33.93%)
5+
6+
class Solution {
7+
fun searchInsert(nums: IntArray, target: Int): Int {
8+
var lo = 0
9+
var hi = nums.size - 1
10+
while (lo <= hi) {
11+
val mid = lo + (hi - lo) / 2
12+
if (target == nums[mid]) {
13+
return mid
14+
} else if (target < nums[mid]) {
15+
hi = mid - 1
16+
} else if (target > nums[mid]) {
17+
lo = mid + 1
18+
}
19+
}
20+
return lo
21+
}
22+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
35\. Search Insert Position
2+
3+
Easy
4+
5+
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
6+
7+
You must write an algorithm with `O(log n)` runtime complexity.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,3,5,6], target = 5
12+
13+
**Output:** 2
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [1,3,5,6], target = 2
18+
19+
**Output:** 1
20+
21+
**Example 3:**
22+
23+
**Input:** nums = [1,3,5,6], target = 7
24+
25+
**Output:** 4
26+
27+
**Example 4:**
28+
29+
**Input:** nums = [1,3,5,6], target = 0
30+
31+
**Output:** 0
32+
33+
**Example 5:**
34+
35+
**Input:** nums = [1], target = 0
36+
37+
**Output:** 0
38+
39+
**Constraints:**
40+
41+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
42+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
43+
* `nums` contains **distinct** values sorted in **ascending** order.
44+
* <code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g0001_0100.s0034_find_first_and_last_position_of_element_in_sorted_array
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun searchRange() {
10+
val expected = intArrayOf(3, 4)
11+
val actual = Solution().searchRange(intArrayOf(5, 7, 7, 8, 8, 10), 8)
12+
assertThat(actual, equalTo(expected))
13+
}
14+
15+
@Test
16+
fun searchRange2() {
17+
val expected = intArrayOf(-1, -1)
18+
val actual = Solution().searchRange(intArrayOf(5, 7, 7, 8, 8, 10), 6)
19+
assertThat(actual, equalTo(expected))
20+
}
21+
22+
@Test
23+
fun searchRange3() {
24+
val expected = intArrayOf(-1, -1)
25+
val actual = Solution().searchRange(intArrayOf(), 0)
26+
assertThat(actual, equalTo(expected))
27+
}
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0001_0100.s0035_search_insert_position
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun searchInsert() {
10+
assertThat(Solution().searchInsert(intArrayOf(1, 3, 5, 6), 5), equalTo(2))
11+
}
12+
13+
@Test
14+
fun searchInsert2() {
15+
assertThat(Solution().searchInsert(intArrayOf(1, 3, 5, 6), 2), equalTo(1))
16+
}
17+
18+
@Test
19+
fun searchInsert3() {
20+
assertThat(Solution().searchInsert(intArrayOf(1, 3, 5, 6), 7), equalTo(4))
21+
}
22+
}

0 commit comments

Comments
 (0)