Algorithm-find the elements in the array

1. Find the elements in the array that link up and down

Source of the topic: https://www.nowcoder.com/discuss/478118 (Shanghai Douyin Client Development Experience)

As we all know, Bytedance pays great attention to algorithmic questions, and the algorithm often becomes a veto vote. In the summer internship, I just stumbled on the last algorithmic question on the three sides, which resulted in not getting the internship offer at the beginning. . . .

First look at the title description:

寻找k:在无序数组中,k的所有左值比它更小,所有右值比他更大。限定o(n)。

This question is actually relatively simple. There is an O(N^2) algorithm (traversing each element one by one to see if the left and right elements meet the requirements). However, if the array is slightly longer, for example, to the length of 10w elements, obviously, At this time, the efficiency of the above algorithm is too low. So we need O (N algorithm)

In the O(N^2) algorithm, the traversal after the selected element is equivalent to finding the maximum value in front of the element and the minimum value behind the element. If the element is greater than the previous maximum value and less than the minimum value in the back, then This is the value we need. In fact, we don't need to repeat the calculation of the maximum and minimum values ​​before and after each time, we just save it directly.

But how should it be preserved? Since there may be more than one element in the array that satisfies the conditions of the title, we can’t meet the requirements by using only two values. Therefore, we declare two arrays to store the maximum value before each element of the array and each element. After the minimum value, finally, traverse the two arrays and compare the size of the corresponding position element

/**
 * 寻找k:在无序数组中,k的所有左值比它更小,所有右值比他更大。限定o(n)。
 */
public class FindBridgeNumber {
    
    
    @Test
    public void test(){
    
    
        int[] nums={
    
    2,1,2,3,4,4};
        List<Integer> result=findBridgeNumber(nums);
        System.out.println(result);
    }

    public List<Integer> findBridgeNumber(int[] nums){
    
    
        List<Integer> result=new ArrayList<>();
        if(nums.length<=2){
    
    
            return result;
        }
        int[] dpLeft=new int[nums.length];
        dpLeft[0]=Integer.MIN_VALUE;
        dpLeft[1]=nums[0];
        for (int i=2;i<dpLeft.length;i++){
    
    
            dpLeft[i]=Math.max(nums[i-1],dpLeft[i-1]);
        }

        int[] dpRight=new int[nums.length];
        dpRight[dpRight.length-1]=Integer.MAX_VALUE;
        dpRight[dpRight.length-2]=nums[nums.length-1];

        for (int i=dpRight.length-3;i>=0;i--){
    
    
            dpRight[i]=Math.min(nums[i+1],dpRight[i+1]);
        }
        for (int i=0;i<nums.length;i++){
    
    
            if(dpLeft[i]<nums[i]&&dpRight[i]>nums[i]){
    
    
                result.add(nums[i]);
            }
        }
        return result;
    }
}

Guess you like

Origin blog.csdn.net/qq_23594799/article/details/108038546