-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirstLastPosition.java
81 lines (57 loc) · 1.52 KB
/
FirstLastPosition.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
package com.vinay.practice.lc;
// https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/description/
public class FirstLastPosition {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = {5,7,7,7,7,7,7,7,8,8,10};
int target = 7;
int position[] = searchRange(arr, target);
System.out.println("The first and last positions are: " + position[0] + ", " + position[1]);
}
public static int[] searchRange(int[] nums, int target) {
/*
// BruteForce
int position[] = {-1, -1};
for (int i=0; i<nums.length; i++) {
if(nums[i] == target) {
position[0] = i;
break;
}
}
for (int i=nums.length-1; i>=0; i--) {
if(nums[i] == target) {
position[1] = i;
break;
}
}
return position;
*
*/
int ansPosition[] = {-1, -1};
ansPosition[0] = getFirstLastPos(nums, target, true);
ansPosition[1] = getFirstLastPos(nums, target, false);
return ansPosition;
}
static int getFirstLastPos(int nums[], int target, boolean isFirstLoc) {
//BinarySearch
int ansPos = -1;
int start = 0;
int end = nums.length-1;
while(start <= end) {
System.out.println("s");
int mid = start + (end - start)/2;
if(target < nums[mid])
end = mid - 1;
else if(target > nums[mid])
start = mid + 1;
else {
ansPos = mid;
if(isFirstLoc)
end = mid - 1;
else
start = mid + 1;
}
}
return ansPos;
}
}