-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFirstAndLast.java
More file actions
88 lines (69 loc) · 2.59 KB
/
FirstAndLast.java
File metadata and controls
88 lines (69 loc) · 2.59 KB
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
87
88
//Given an array of integers nums sorted in non-decreasing order,
// find the starting and ending position of a given target value.
//
// If target is not found in the array, return [-1, -1].
//
// You must write an algorithm with O(log n) runtime complexity.
//
import java.util.ArrayList;
public class FirstAndLast {
public static void main(String[] args) {
int[] nums = {1,2,3,4,5,6,6,7,9,9,10};
int[] ans = searchRange(nums,9);
for(int i = 0; i < ans.length; i++) {
System.out.println(ans[i]);
}
}
public static int[] searchRange(int[] nums, int target) {
if (nums.length == 0) return new int[]{-1, -1};
int[] ans = {-1,-1};
ans[0] = findFirstStartPos(nums,target);
ans[1] = findFirstEndPos(nums, target);
return ans;
}
public static int findFirstStartPos(int[] num, int target) {
int left = 0;
int right = num.length - 1;
int start = -1;
while(left <= right) {
int mid = left+(right-left)/2;
if(num[mid] == target) {
start = mid;
right = mid - 1; //See if the are any more possibilities on the left hand side of arr.
}
else if (num[mid] > target) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return start;
}
public static int findFirstEndPos(int[] num, int target) {
int left = 0;
int right = num.length - 1;
int end = -1;
while(left <= right) {
int mid = left+(right-left)/2;
if(num[mid] == target) {
end = mid;
left = mid + 1; //See if there are any more possibilities on the right hand side of arr.
}
else if (num[mid] > target) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return end;
}
}
//Solution:
//Based off of the question....It seems like there are only ever two, one, or zero indexs with the target value
//So we can either return two indexs of the target value in the array, one index and -1, or both -1 for indexs.
//First we find the start position of the answer by doing mid - 1
//Second we find the end position of the answer by doing mid + 1
//The reason we for the first we do mid -1 is because when we find the middle of the array it is left + (right - left)
//
//TIME COMPLEXITY: O(logN) because there is a Binary search that is being implemented in order to find the starting and ending position of
//a target value.