Skip to content

Commit 4e7d35c

Browse files
just4oncegitbook-bot
authored andcommitted
GitBook: [master] 4 pages modified
1 parent 094e609 commit 4e7d35c

File tree

4 files changed

+159
-5
lines changed

4 files changed

+159
-5
lines changed

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ To clone the repo
88
git clone https://github.com/just4once/leetcode.git
99
```
1010

11-
{% hint style="success" %}
12-
Success.
13-
{% endhint %}
14-
1511
To clone the repo with username
1612

1713
```

leetcode/divide-and-conquer/169-majority-element.md

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,84 @@ Output: 2
2424

2525
## Thought Process {#thought-process}
2626

27-
1. asd
27+
1. Sorting
28+
1. Time complexity O\(nlogn\)
29+
2. Space complexity O\(1\)
30+
2. HashMap
31+
1. Time complexity O\(n\)
32+
2. Space complexity O\(n\)
33+
3. Divide and Conquer
34+
1.
35+
4. Boyer-Moore Voting Algorithm
36+
1. O\(n\)
2837

2938
## Solution
3039

3140
```java
41+
class Solution {
42+
public int majorityElement(int[] nums) {
43+
Arrays.sort(nums);
44+
return nums[nums.length/2];
45+
}
46+
}
47+
```
48+
49+
```java
50+
class Solution {
51+
public int majorityElement(int[] nums) {
52+
int major = 0, count = 0;
53+
Map<Integer, Integer> map = new HashMap<>();
54+
for (int num : nums) map.put(num, map.getOrDefault(num, 0) + 1);
55+
for (int key : map.keySet()) {
56+
if (map.get(key) > count) {
57+
count = map.get(key);
58+
major = key;
59+
}
60+
}
61+
return major;
62+
}
63+
}
64+
```
3265

66+
```java
67+
class Solution {
68+
public int majorityElement(int[] nums) {
69+
return getMajor(nums, 0, nums.length - 1);
70+
}
71+
72+
private int getMajor(int[] nums, int lo, int hi) {
73+
if (lo == hi) return nums[lo];
74+
int mi = lo + (hi - lo) / 2;
75+
int left = getMajor(nums, lo, mi);
76+
int right = getMajor(nums, mi + 1, hi);
77+
if (left == right) return left;
78+
int leftCount = countInRange(nums, left, lo, mi);
79+
int rightCount = countInRange(nums, right, mi + 1, hi);
80+
return leftCount > rightCount ? left : right;
81+
}
82+
83+
private int countInRange(int[] nums, int target, int lo, int hi) {
84+
int count = 0;
85+
for (int i = lo; i <= hi; i++) {
86+
if (nums[i] == target) count++;
87+
}
88+
return count;
89+
}
90+
}
91+
```
92+
93+
```java
94+
class Solution {
95+
public int majorityElement(int[] nums) {
96+
int major = 0, count = 0;
97+
for (int i = 0; i < nums.length; i++) {
98+
if (count == 0) major = nums[i];
99+
if (major == nums[i]) count++;
100+
else count--;
101+
}
102+
return major;
103+
}
104+
}
33105
```
34106

35107
## Additional {#additional}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,88 @@
11
# 215-kth-largest-element-in-an-array
22

3+
## Question {#question}
34

5+
[https://leetcode.com/problems/kth-largest-element-in-an-array/description/](https://leetcode.com/problems/kth-largest-element-in-an-array/description/)
6+
7+
Find the **k** th largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
8+
9+
**Example 1:**
10+
11+
```text
12+
Input: [3,2,1,5,6,4] and k = 2
13+
Output: 5
14+
```
15+
16+
**Example 2:**
17+
18+
```text
19+
Input: [3,2,3,1,2,4,5,5,6] and k = 4
20+
Output: 4
21+
```
22+
23+
**Note:**
24+
You may assume k is always valid, 1 ≤ k ≤ array's length.
25+
26+
## Thought Process {#thought-process}
27+
28+
1. MinHeap
29+
1. Use min heap to store up to K elements
30+
2. Everytime, when the heap is full, we poll one out
31+
3. At the end the process the Kth largest element in at the top
32+
4. Time complexity O\(nlogk\)
33+
5. Space complexity O\(K\)
34+
2. QuickSelect
35+
1. Using quick select we can optimize the algorithm's complexity to be O\(n\)
36+
2. Space complexity O\(1\)
37+
38+
## Solution
39+
40+
```java
41+
class Solution {
42+
public int findKthLargest(int[] nums, int k) {
43+
PriorityQueue<Integer> pq = new PriorityQueue<>();
44+
for (int num : nums) {
45+
pq.offer(num);
46+
if (pq.size() > k) pq.poll();
47+
}
48+
return pq.peek();
49+
}
50+
}
51+
```
52+
53+
```java
54+
class Solution {
55+
public int findKthLargest(int[] nums, int k) {
56+
int lo = 0, hi = nums.length - 1;
57+
k = nums.length - k;
58+
while (lo < hi) {
59+
int pivotId = quickSelect(nums, lo, hi);
60+
if (pivotId == k) return nums[k];
61+
else if (pivotId < k) lo = pivotId + 1;
62+
else hi = pivotId - 1;
63+
}
64+
return nums[lo];
65+
}
66+
67+
private int quickSelect(int[] nums, int lo, int hi) {
68+
int pivot = lo;
69+
while (lo <= hi) {
70+
while (lo <= hi && nums[lo] <= nums[pivot]) lo++;
71+
while (lo <= hi && nums[hi] > nums[pivot]) hi--;
72+
if (lo >= hi) break;
73+
swap(nums, lo, hi);
74+
}
75+
swap(nums, pivot, hi);
76+
return hi;
77+
}
78+
79+
private void swap(int[] nums, int i, int j) {
80+
int tmp = nums[i];
81+
nums[i] = nums[j];
82+
nums[j] = tmp;
83+
}
84+
}
85+
```
86+
87+
## Additional {#additional}
488

template/leetcode.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55

66

7+
8+
79
## Thought Process {#thought-process}
810

911
1. asd

0 commit comments

Comments
 (0)