34. Leetcode topic to find the first and last position of the element (moderate) in the sort array

Subject description:

Nums Given an array of integers arranged in ascending order, according to the target and one target value. Identify a given target start and end positions in the array.

Your time complexity of the algorithm must be O (log n) level.

If the target is not present 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]

Topic Analysis:

Method 1: a linear sweep

Check each of the target index, will be able to get the right answer.

First of all, we do a linear traversal of the nums array from left to right, abort when encountering target. If we had not suspended, then the target does not exist, we can return "error code" [-1, -1]. If we found an effective left point coordinates, we can take the second pass linear scan, but this time carried out from right to left. This time, the first target will be met far right (because there is a far left, so there will be a rightmost target). Then we only need to return these two coordinates.

Code:

class Solution {
    public int[] searchRange(int[] nums, int target) {
        int[] targetRange = {-1, -1};

        // find the index of the leftmost appearance of `target`.
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == target) {
                targetRange[0] = i;
                break;
            }
        }

        // if the last loop did not find any index, then there is no valid range
        // and we return [-1, -1].
        if (targetRange[0] == -1) {
            return targetRange;
        }

        // find the index of the rightmost appearance of `target` (by reverse
        // iteration). it is guaranteed to appear.
        for (int j = nums.length-1; j >= 0; j--) {
            if (nums[j] == target) {
                targetRange[1] = j;
                break;
            }
        }

        return targetRange;
    }
}

Time complexity: O (n).

The violent solution examined each element in the array num exactly twice, so the total running time is linear.

Complexity Space: O (1).

The method of using a linear scan array and an integer number of fixed size, so it is a constant level of the space.

Method Two: Two binary search

public int[] searchRange(int[] nums, int target) {
        // 二分查找
        int[] result = new int[2];
        result[0] = searchFirst(nums, target);
        result[1] = searchLast(nums, target);
        
        return result;
    }
    
    private int searchFirst(int[] arr, int target){
        int len = arr.length;
        if(len == 0) return -1;
        
        int left = 0, right = len-1;
        while(left < right){
            int mid = (left + right) >> 1;
            if(arr[mid] < target)  left = mid + 1;
            else right = mid;
        }
        
        return arr[left] == target ? left : -1;
        
    }
    
    private int searchLast(int[] arr, int target){
        int len = arr.length;
        if(len == 0) return -1;
        
        int left = 0, right = len-1;
        while(left < right){
            int mid = (left + right + 1) >> 1;
            if(arr[mid] <= target)  left = mid;
            else right = mid - 1;
        }
        
        return arr[left] == target ? left : -1;
        
    }
}

Time complexity: O (log n)

Space complexity: O (1)

  

 

Guess you like

Origin www.cnblogs.com/ysw-go/p/11773506.html
Recommended