当前位置:网站首页>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;
}
};
边栏推荐
- Codeforces 1638 D. Big Brush —— BFS
- 【mysql进阶】查询优化原理与方案(六)
- [WUSTCTF2020]颜值成绩查询-1
- A method of quickly creating test window
- Computational hierarchy -- the problem of large numbers multiplying decimals
- Codeforces 1637 C. Andrew and stones - simple thinking
- Alibaba cloud development board haas510 parses serial port JSON data and sends attributes
- Alibaba cloud development board haas510 sends the serial port data to the Internet of things platform
- Record some settings for visual studio 2019
- Rk3399 platform development series explanation (kernel debugging chapter) 2.50 use of systrace
猜你喜欢

List of common ACM knowledge points (to be continued)

Understanding recursion
![[semidrive source code analysis] [x9 chip startup process] 25 - Introduction to mailbox inter core communication mechanism (code analysis) rpmsg-ipcc RTOS & QNX](/img/90/98faa5856fbc55ff7fda4a3adb3650.png)
[semidrive source code analysis] [x9 chip startup process] 25 - Introduction to mailbox inter core communication mechanism (code analysis) rpmsg-ipcc RTOS & QNX

阿里云开发板HaaS510响应UART串口指令

Alibaba cloud development board haas510 sends the serial port data to the Internet of things platform

事件的传递和响应以及使用实例
![[MySQL advanced] index classification and index optimization scheme (V)](/img/8b/2284783d8b81ba2e8fa45a239107ba.png)
[MySQL advanced] index classification and index optimization scheme (V)
Introduction to color coding format
Ffmpeg Learning Guide

Alibaba cloud development board haas510 submission device attributes
随机推荐
Alibaba cloud development board haas510 responds to UART serial port instructions
事件的传递和响应以及使用实例
C language array and pointer
C language structure
Rk3399 platform development series explanation (kernel debugging chapter) 2.50 use of systrace
2061: [example 1.2] trapezoidal area
Display logs in the database through loganalyzer
[WUSTCTF2020]颜值成绩查询-1
Informatics Olympiad all in one 1000: introductory test questions
阿里云开发板HaaS510解析串口JSON数据并发送属性
Cocoapods的相关知识点
【mysql进阶】索引分类及索引优化方案(五)
2068: [example 2.6] chicken and rabbit in the same cage
Codeforces 1637 A. sorting parts - simple thinking
Formal analysis of Woo Lam protocol with scyther tool
2067: [example 2.5] circle
Briefly describe the difference between CGI and fastcgi
The problem of Joseph in Informatics
Cmake basic tutorial - 02 b-hello-cmake
【mysql进阶】mysql索引数据结构的演变(四)