-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy path34. Find First and Last Position of Element in Sorted Array.java
executable file
·86 lines (75 loc) · 2.86 KB
/
34. Find First and Last Position of Element in Sorted Array.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
M
tags: Array, Binary Search
time: O(logn)
space: O(1)
#### Binary Search
- need search left bound & right bound.
- use input parameter `direction` to binary search function
- Option0: simplification, inspired by `278. First Bad Version - Method1: Check is-NOT-BadVersion`
- 1) if found match, but NOT sure it is desired boundary, just leave it and keep going
- 2) check the final results after `binary search while loop` completes
- WHY? code is easier to read in this way.
```
/*
Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].
Example 1:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Example 2:
Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]
*/
// Option0: Simplification
class Solution {
public int[] searchRange(int[] nums, int target) {
if (nums == null || nums.length == 0) return new int[]{-1, -1};
return new int[]{
binarySearch(nums, target, true),
binarySearch(nums, target, false)
};
}
private int binarySearch(int[] nums, int target, boolean isLeft) {
int n = nums.length, start = 0, end = n - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2, val = nums[mid];
if (val == target) {
if (isLeft) end = mid;
else start = mid;
} else if (val < target) start = mid;
else end = mid;
}
if (nums[start] == target && nums[end] == target) return isLeft ? start : end;
if (nums[start] == target) return start;
if (nums[end] == target) return end;
return -1;
}
}
// Option1: original, check end state in middle
class Solution {
public int[] searchRange(int[] nums, int target) {
if (nums == null || nums.length == 0) return new int[]{-1, -1};
return new int[]{
binarySearch(nums, target, true),
binarySearch(nums, target, false)
};
}
private int binarySearch(int[] nums, int target, boolean isLeft) {
int n = nums.length, start = 0, end = n - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2, val = nums[mid];
if (val == target) {
if (isLeft && mid - 1 >= 0 && nums[mid - 1] == target) end = mid;
else if (!isLeft && mid + 1 <= n - 1 && nums[mid + 1] == target) start = mid;
else return mid;
} else if (val < target) start = mid;
else end = mid;
}
if (nums[start] == target && nums[end] == target) return isLeft ? start : end;
if (nums[start] == target) return start;
if (nums[end] == target) return end;
return -1;
}
}
```