[LeetCode] March 20 punch card -Day5

The minimum number k of title 1

description

Input integer array arr, find the smallest number k. For example, digital input 4,5,1,6,2,7,3,8 eight, the minimum number is four 1,2,3,4.
Example 1:
Input: arr = [3,2,1], k = 2
Output: [1,2] or [2,1]
Example 2:
Input: arr = [0,1,2,1], k = 1
output: [0]
limits:
0 <= K <= arr.length <= 10000
0 <= ARR [I] <= 10000

answer

Thinking: fast first row, and then take the first k elements.
Quick Exhaust thought: recursion, recursion that i <j. And benchmarking size, small in reference to the right, left at large benchmark. Finally, the benchmark ij put in place to meet. First compare the left and then the right side of the comparison.

class Solution {
    public static void quickSort(int[] arr,int low,int high){
        int i,j,temp,t;
        if(low>high){
            return;
        }
        i=low;
        j=high;
        //temp就是基准位
        temp = arr[low];

        while (i<j) {
            //先看右边,依次往左递减
            while (temp<=arr[j]&&i<j) {
                j--;
            }
            //再看左边,依次往右递增
            while (temp>=arr[i]&&i<j) {
                i++;
            }
            //如果满足条件则交换
            if (i<j) {
                t = arr[j];
                arr[j] = arr[i];
                arr[i] = t;
            }

        }
        //最后将基准为与i和j相等位置的数字交换
        arr[low] = arr[i];
        arr[i] = temp;
        //递归调用左半数组
        quickSort(arr, low, j-1);
        //递归调用右半数组
        quickSort(arr, j+1, high);
    }

    public int[] getLeastNumbers(int[] arr, int k) {
        quickSort(arr,0,arr.length-1);
        int[] temp = new int[k]; 
        for (int i = 0; i < k; i++){
            temp[i] = arr[i];
        }
        return temp;
    }
}

Question 2

description

Find the k-th largest element in an unsorted array. Please note that you need to find is the first k largest elements in the array is sorted, rather than the k distinct elements.
Example 1:
Input: [3,2,1,5,6,4] and k = 2
Output: 5
Example 2:
Input: [3,2,3,1,2,4,5,5,6] and k = 4
output: 4
Note:
you can assume k always valid, and 1 ≤ k ≤ length of the array.

Problem solution 1

The idea is similar to questions 1

class Solution {
    
    public void quickSort(int[] nums, int left, int right){
        if(left>right){
            return;
        }
        int i = left;
        int j = right;
        int pivot = nums[left];
        while(i<j){
            while(i<j&&nums[j]<=pivot){
                j--;
            }
            while(i<j&&nums[i]>=pivot){
                i++;
            }
            if(i<j){
                int tmp = nums[i];
                nums[i] = nums[j];
                nums[j] = tmp;
            }
        }
        nums[left] = nums[i];
        nums[i] = pivot;
        quickSort(nums,left,j-1);
        quickSort(nums,j+1,right);
    }
    public int findKthLargest(int[] nums, int k) {
        quickSort(nums, 0, nums.length-1);
        for(int i: nums){
            System.out.println(i);
        }
        return nums[k-1];
    }
}

2 solution to a problem

Idea: Create a heap big top, all the elements into the heap, maintaining the heap size is less than equal to k, to retain elements of the heap is the first k largest elements, top of the heap for the correct answer.

class Solution {
    public int findKthLargest(int[] nums, int k) {
        // init heap 'the smallest element first'
        PriorityQueue<Integer> heap =
            new PriorityQueue<Integer>((n1, n2) -> n1 - n2);

        // keep k largest elements in the heap
        for (int n: nums) {
          heap.add(n);
          if (heap.size() > k)
            heap.poll();
        }

        // output
        return heap.poll();        
  }
}
class Solution:
    def findKthLargest(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        return heapq.nlargest(k, nums)[-1]
Published 29 original articles · won praise 65 · views 5035

Guess you like

Origin blog.csdn.net/xd963625627/article/details/104982810