当前位置:网站首页>Interview question 17.14 Minimum number of K (almost double hundreds)
Interview question 17.14 Minimum number of K (almost double hundreds)
2022-06-12 13:51:00 【Doll noodles I】
Interview questions 17.14. Minimum K Number
The question :
Design an algorithm , Find the smallest... In the array k Number . Return this in any order k It's all right .
Example :
Input : arr = [1,3,5,7,2,4,6,8], k = 4
Output : [1,2,3,4]
Tips :
0 <= len(arr) <= 1000000 <= k <= min(100000, len(arr))
Their thinking
This problem only needs to be solved
- Sort , Let the small one come first , The big one is behind
- Only keep k number , Delete the rest
Code
class Solution {
public:
vector<int> smallestK(vector<int>& arr, int k) {
//1. Prioritize
sort(arr.begin(),arr.end());
// Determine the subscript of the reserved number ( iterator )
vector<int>::iterator it = arr.begin() + k;
// Delete the extra number
arr.erase(it,arr.end());
return arr;
}
};
边栏推荐
- TCP的“非”可靠性
- 开发中使用的语言技巧
- The problem of Joseph in Informatics
- Qualcomm platform development series (Protocol) QMI brief introduction and usage
- Cmake basic tutorial - 01 a-hello-cmake
- CUDA error: CUBLAS_ STATUS_ NOT_ INITIALIZED when calling `cublasCreate(handle)`
- Acwing: topology sequence
- Codeforces 1629 F2. Game on sum (hard version) - Yang Hui's triangle, violence, finding rules
- Display logs in the database through loganalyzer
- [WUSTCTF2020]颜值成绩查询-1
猜你喜欢

go-zero 微服务实战系列(二、服务拆分)

Transmission and response of events and use cases

List of common ACM knowledge points (to be continued)

Hash tables, sets, maps, trees, heaps, and graphs

Web3.0,「激发创造」的时代

When the byte jumps, the Chinese 996 is output in the United States

【mysql进阶】索引分类及索引优化方案(五)

Is MySQL query limit 1000,10 as fast as limit 10? How to crack deep paging

Paw advanced user guide

【mysql进阶】mysql索引数据结构的演变(四)
随机推荐
Codeforces 1634 F. Fibonacci additions - Fibonacci sequence addition, ideas
[semidrive source code analysis] [x9 chip startup process] 25 - Introduction to mailbox inter core communication mechanism (code analysis) rpmsg-ipcc RTOS & QNX
当字节跳动在美国输出中国式 996
Encryptor and client authenticate with each other
Codeforces 1629 E. grid XOR - simple thinking
Cmake basic tutorial - 02 b-hello-cmake
Relevant knowledge points of cocoapods
2062: [example 1.3] movie tickets
2064: [example 2.1] exchange value
Display logs in the database through loganalyzer
view的子视图的递归
Data type conversion and conditional control statements
Codeforces 1637 F. Towers - thinking, DFS
M1 pod install pod lint failure solution
高通平台开发系列讲解(协议篇)QMI简单介绍及使用方法
D1 哪吒开发板 了解基本的启动加载流程
AWLive 结构体的使用
Record some settings for visual studio 2019
1004: character triangle
阿里云开发板vscode开发环境搭建