Conversation
|
Note: The student has provided multiple solutions for the same problem. You should evaluate the last solution for the problem "FirstAndLastPosition" (the one that is not commented out). The other files are for different problems and should be ignored for this evaluation. Lets begin.Student's solution for the problem "Find the First and Last Position of an Element in given Sorted Array" is provided in the file Let's look at the first implementation (the one with two binary searches): class Solution {
public int[] searchRange(int[] nums, int target) {
int n = nums.length;
int low =0;
int high = n-1;
int first = firstBinarySearch(nums, target, low, high);
int second = secondBinarySearch(nums, target, low, high);
return new int[]{first,second};
}
private int firstBinarySearch(int[] nums, int target, int low, int high) {
while(low<=high) {
int mid = low + (high-low)/2;
if(nums[mid] == target) {
if(mid==0 || nums[mid-1]<target) {
return mid;
}
}
if(nums[mid] < target) {
low = mid+1;
} else {
high = mid-1;
}
}
return -1;
}
private int secondBinarySearch(int[] nums, int target, int low, int high) {
while(low<=high) {
int mid = low + (high-low)/2;
if(nums[mid] == target) {
if(mid==nums.length-1 || nums[mid+1]>target) {
return mid;
}
}
if(nums[mid] <= target) {
low = mid+1;
} else {
high = mid-1;
}
}
return -1;
}
}Now, let's evaluate this solution.
|
No description provided.