Skip to content

Commit 3bc25f6

Browse files
committed
feat: add solutions to lcci problem: No.17.14
No.17.14.Smallest K
1 parent 13a3e2a commit 3bc25f6

File tree

6 files changed

+287
-0
lines changed

6 files changed

+287
-0
lines changed

lcci/17.14.Smallest K/README.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,157 @@
1919
## 解法
2020

2121
<!-- 这里可写通用的实现逻辑 -->
22+
23+
**方法一:排序**
24+
25+
直接排序,取前 k 个数即可。
26+
27+
时间复杂度 $O(n\log n)$。其中 $n$ 为数组长度。
28+
29+
**方法二:优先队列(大根堆)**
30+
31+
维护一个大小为 $k$ 的大根堆,遍历数组,将当前元素入堆,如果堆的大小超过 $k$,弹出堆顶元素。
32+
33+
遍历结束,将堆中元素的 $k$ 个元素取出即可。
34+
35+
时间复杂度 $O(n\log k)$。其中 $n$ 为数组长度。
36+
2237
<!-- tabs:start -->
2338

2439
### **Python3**
2540

2641
<!-- 这里可写当前语言的特殊实现逻辑 -->
2742

2843
```python
44+
class Solution:
45+
def smallestK(self, arr: List[int], k: int) -> List[int]:
46+
return sorted(arr)[:k]
47+
```
2948

49+
```python
50+
class Solution:
51+
def smallestK(self, arr: List[int], k: int) -> List[int]:
52+
h = []
53+
for v in arr:
54+
heappush(h, -v)
55+
if len(h) > k:
56+
heappop(h)
57+
return [-v for v in h]
3058
```
3159

3260
### **Java**
3361

3462
<!-- 这里可写当前语言的特殊实现逻辑 -->
3563

3664
```java
65+
class Solution {
66+
public int[] smallestK(int[] arr, int k) {
67+
Arrays.sort(arr);
68+
int[] ans = new int[k];
69+
for (int i = 0; i < k; ++i) {
70+
ans[i] = arr[i];
71+
}
72+
return ans;
73+
}
74+
}
75+
```
76+
77+
```java
78+
class Solution {
79+
public int[] smallestK(int[] arr, int k) {
80+
PriorityQueue<Integer> q = new PriorityQueue<>((a, b) -> b - a);
81+
for (int v : arr) {
82+
q.offer(v);
83+
if (q.size() > k) {
84+
q.poll();
85+
}
86+
}
87+
int[] ans = new int[k];
88+
int i = 0;
89+
while (!q.isEmpty()) {
90+
ans[i++] = q.poll();
91+
}
92+
return ans;
93+
}
94+
}
95+
```
96+
97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
vector<int> smallestK(vector<int>& arr, int k) {
103+
sort(arr.begin(), arr.end());
104+
vector<int> ans(k);
105+
for (int i = 0; i < k; ++i) {
106+
ans[i] = arr[i];
107+
}
108+
return ans;
109+
}
110+
};
111+
```
112+
113+
```cpp
114+
class Solution {
115+
public:
116+
vector<int> smallestK(vector<int>& arr, int k) {
117+
priority_queue<int> q;
118+
for (int& v : arr) {
119+
q.push(v);
120+
if (q.size() > k) {
121+
q.pop();
122+
}
123+
}
124+
vector<int> ans;
125+
while (q.size()) {
126+
ans.push_back(q.top());
127+
q.pop();
128+
}
129+
return ans;
130+
}
131+
};
132+
```
133+
134+
### **Go**
135+
136+
```go
137+
func smallestK(arr []int, k int) []int {
138+
sort.Ints(arr)
139+
ans := make([]int, k)
140+
for i, v := range arr[:k] {
141+
ans[i] = v
142+
}
143+
return ans
144+
}
145+
```
37146

147+
```go
148+
func smallestK(arr []int, k int) []int {
149+
q := hp{}
150+
for _, v := range arr {
151+
heap.Push(&q, v)
152+
if q.Len() > k {
153+
heap.Pop(&q)
154+
}
155+
}
156+
ans := make([]int, k)
157+
for i := range ans {
158+
ans[i] = heap.Pop(&q).(int)
159+
}
160+
return ans
161+
}
162+
163+
type hp struct{ sort.IntSlice }
164+
165+
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
166+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
167+
func (h *hp) Pop() interface{} {
168+
a := h.IntSlice
169+
v := a[len(a)-1]
170+
h.IntSlice = a[:len(a)-1]
171+
return v
172+
}
38173
```
39174

40175
### **...**

lcci/17.14.Smallest K/README_EN.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,133 @@
2626
### **Python3**
2727

2828
```python
29+
class Solution:
30+
def smallestK(self, arr: List[int], k: int) -> List[int]:
31+
return sorted(arr)[:k]
32+
```
2933

34+
```python
35+
class Solution:
36+
def smallestK(self, arr: List[int], k: int) -> List[int]:
37+
h = []
38+
for v in arr:
39+
heappush(h, -v)
40+
if len(h) > k:
41+
heappop(h)
42+
return [-v for v in h]
3043
```
3144

3245
### **Java**
3346

3447
```java
48+
class Solution {
49+
public int[] smallestK(int[] arr, int k) {
50+
Arrays.sort(arr);
51+
int[] ans = new int[k];
52+
for (int i = 0; i < k; ++i) {
53+
ans[i] = arr[i];
54+
}
55+
return ans;
56+
}
57+
}
58+
```
59+
60+
```java
61+
class Solution {
62+
public int[] smallestK(int[] arr, int k) {
63+
PriorityQueue<Integer> q = new PriorityQueue<>((a, b) -> b - a);
64+
for (int v : arr) {
65+
q.offer(v);
66+
if (q.size() > k) {
67+
q.poll();
68+
}
69+
}
70+
int[] ans = new int[k];
71+
int i = 0;
72+
while (!q.isEmpty()) {
73+
ans[i++] = q.poll();
74+
}
75+
return ans;
76+
}
77+
}
78+
```
79+
80+
### **C++**
81+
82+
```cpp
83+
class Solution {
84+
public:
85+
vector<int> smallestK(vector<int>& arr, int k) {
86+
sort(arr.begin(), arr.end());
87+
vector<int> ans(k);
88+
for (int i = 0; i < k; ++i) {
89+
ans[i] = arr[i];
90+
}
91+
return ans;
92+
}
93+
};
94+
```
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
vector<int> smallestK(vector<int>& arr, int k) {
100+
priority_queue<int> q;
101+
for (int& v : arr) {
102+
q.push(v);
103+
if (q.size() > k) {
104+
q.pop();
105+
}
106+
}
107+
vector<int> ans;
108+
while (q.size()) {
109+
ans.push_back(q.top());
110+
q.pop();
111+
}
112+
return ans;
113+
}
114+
};
115+
```
116+
117+
### **Go**
118+
119+
```go
120+
func smallestK(arr []int, k int) []int {
121+
sort.Ints(arr)
122+
ans := make([]int, k)
123+
for i, v := range arr[:k] {
124+
ans[i] = v
125+
}
126+
return ans
127+
}
128+
```
35129

130+
```go
131+
func smallestK(arr []int, k int) []int {
132+
q := hp{}
133+
for _, v := range arr {
134+
heap.Push(&q, v)
135+
if q.Len() > k {
136+
heap.Pop(&q)
137+
}
138+
}
139+
ans := make([]int, k)
140+
for i := range ans {
141+
ans[i] = heap.Pop(&q).(int)
142+
}
143+
return ans
144+
}
145+
146+
type hp struct{ sort.IntSlice }
147+
148+
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
149+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
150+
func (h *hp) Pop() interface{} {
151+
a := h.IntSlice
152+
v := a[len(a)-1]
153+
h.IntSlice = a[:len(a)-1]
154+
return v
155+
}
36156
```
37157

38158
### **...**

lcci/17.14.Smallest K/Solution.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
vector<int> smallestK(vector<int>& arr, int k) {
4+
sort(arr.begin(), arr.end());
5+
vector<int> ans(k);
6+
for (int i = 0; i < k; ++i) {
7+
ans[i] = arr[i];
8+
}
9+
return ans;
10+
}
11+
};

lcci/17.14.Smallest K/Solution.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func smallestK(arr []int, k int) []int {
2+
sort.Ints(arr)
3+
ans := make([]int, k)
4+
for i, v := range arr[:k] {
5+
ans[i] = v
6+
}
7+
return ans
8+
}

lcci/17.14.Smallest K/Solution.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public int[] smallestK(int[] arr, int k) {
3+
Arrays.sort(arr);
4+
int[] ans = new int[k];
5+
for (int i = 0; i < k; ++i) {
6+
ans[i] = arr[i];
7+
}
8+
return ans;
9+
}
10+
}

lcci/17.14.Smallest K/Solution.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def smallestK(self, arr: List[int], k: int) -> List[int]:
3+
return sorted(arr)[:k]

0 commit comments

Comments
 (0)