- vừa được xem lúc

Leetcode 295 - Find the Kth Largest Element in an Array

0 0 5

Người đăng: Văn Lê Đình

Theo Viblo Asia

Problem Statement

Given an integer array nums and an integer k, return the kth largest element in the array. Note that it is the kth largest element in the sorted order, not the kth distinct element.

Constraints

  • 1<=k<=nums.length<=1051 <= k <= nums.length <= 10^5

  • 104<=nums[i]<=10410^4 <= nums[i] <= 10^4

Solutions

1. Brute-Force Method

The most straightforward solution is to sort the array in decreasing order, and then return the element at index k - 1. This solution has a time complexity of O(n log n), where n is the number of elements in the array. This is because the time complexity of sorting an array is O(n log n).

public int FindKthLargest(int[] nums, int k) { Array.Sort(nums); return nums[nums.Length - k];
}

2. QuickSelect Method

The QuickSelect algorithm is an efficient in-place variation of the QuickSort algorithm. It is used to solve the "selection problem", which is to find the kth smallest (or largest) element in an unordered list. The QuickSelect algorithm works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. The time complexity of this algorithm is O(n) in the average case, but can be O(n^2) in the worst case. The space complexity is O(1).

public class Solution { private static readonly Random rand = new(); public int FindKthLargest(int[] nums, int k) { return Select(nums, 0, nums.Length - 1, nums.Length - k); } private int Select(int[] nums, int left, int right, int kSmallest) { if (left == right) { return nums[left]; } int pivotIndex = rand.Next(left, right); pivotIndex = Partition(nums, left, right, pivotIndex); if (kSmallest == pivotIndex) { return nums[kSmallest]; } else if (kSmallest < pivotIndex) { return Select(nums, left, pivotIndex - 1, kSmallest); } else { return Select(nums, pivotIndex + 1, right, kSmallest); } } private int Partition(int[] nums, int left, int right, int pivotIndex) { int pivot = nums[pivotIndex]; Swap(nums, pivotIndex, right); int storeIndex = left; for (int i = left; i <= right; i++) { if (nums[i] < pivot) { Swap(nums, storeIndex, i); storeIndex++; } } Swap(nums, storeIndex, right); return storeIndex; } private void Swap(int[] nums, int a, int b) { int tmp = nums[a]; nums[a] = nums[b]; nums[b] = tmp; }
}

NOTE: Bài viết này có tiếng Việt. Bạn có thể đọc ở đây.

Bình luận

Bài viết tương tự

- vừa được xem lúc

Thuật toán quay lui (Backtracking)

Quay lui là một kĩ thuật thiết kế giải thuật dựa trên đệ quy. Ý tưởng của quay lui là tìm lời giải từng bước, mỗi bước chọn một trong số các lựa chọn khả dĩ và đệ quy.

0 0 38

- vừa được xem lúc

Các thuật toán cơ bản trong AI - Phân biệt Best First Search và Uniform Cost Search (UCS)

Nếu bạn từng đọc các thuật toán trong AI (Artificial Intelligence - Trí tuệ nhân tạo), rất có thể bạn từng nghe qua về các thuật toán tìm kiếm cơ bản: UCS (thuộc chiến lược tìm kiếm mù) và Best First Search (thuộc chiến lược tìm kiếm kinh nghiệm). Khác nhau rõ từ khâu phân loại rồi, thế nhưng hai th

0 0 152

- vừa được xem lúc

Sử dụng vector trong lập trình C++ - giải bài toán lập trình muôn thủa

Chào buổi tối mọi người, hôm nay lang thang trên mạng bắt gặp bài toán quen thuộc một thời của quãng đường sinh viên IT. Đấy chính là câu số 1 trong đề thi dưới đây:.

0 0 36

- vừa được xem lúc

MÔ PHỎNG THUẬT TOÁN VƯƠNG HẠO TRONG PROLOG

. 1. Các luật suy diễn trong thuật toán Vương Hạo. Luật 1: Chuyển vế các giả thuyết và kết luận ở dạng phủ định. Ví dụ: p v q, !(r ^ s), !q, p v r -> s, !p <=> p v q, p v r, p -> s, r ^ s, q.

0 0 77

- vừa được xem lúc

A* Search Algorithm

What is A* Search Algorithm. How it works. . Explanation.

0 0 43

- vừa được xem lúc

Python: Jump Search

Search là một từ khóa khá là quen thuộc đối với chúng ta. Hiểu theo đúng nghĩa đen của nó chính là "Tìm kiếm".

0 0 36