LeetCode:658. Find K Closest Elements程序分析
来源:互联网 发布:单片机与plc区别 编辑:程序博客网 时间:2024/06/08 09:05
好久没有练习算法了,以此纪研究生以来第一次练习算法题。
原题链接:https://leetcode.com/problems/find-k-closest-elements/description/
题目描述:大概意思是给定一个数组[1,2,3,4,5]和两个常数k,x然后在数组中找到与x最近的的k个元素,找到后的元素依旧按照升序排列。
Given a sorted array, two integers k
and x
, find the k
closest elements to x
in the array. The result should also be sorted in ascending order. If there is a tie, the smaller elements are always preferred.
Example 1:
Input: [1,2,3,4,5], k=4, x=3Output: [1,2,3,4]
Example 2:
Input: [1,2,3,4,5], k=4, x=-1Output: [1,2,3,4]
Note:
The value k is positive and will always be smaller than the length of the sorted array.
Length of the given array is positive and will not exceed 10_4
Absolute value of elements in the array and x will not exceed 10_4
由于看题疏忽,没有看到给予的是排好序的数组,程序写的是排不排序都可以,因此第一次提交的程序着实运行的有些慢,在自己编译器中是可以编译通过的,但是提交上去被提示数组越界:
class Solution {public: vector<int> findClosestElements(vector<int>& arr, int k, int x) { int temp = 0; vector<int> big(arr.size()); int distance = 0; big[0] = arr[0]; for(int i = 1; i < arr.size(); i ++) { if(abs(arr[i] - x) >= abs(big[i-1] - x)) big[i] = arr[i]; else { for(int j = i;j > 0;j --) { if(abs(arr[i]-x) < abs(big[j-1]- x)) { temp = big[j-1]; big[j-1] = arr[i]; big[j] = temp; } } } } sort(big.begin(),big.end()+k); vector<int> result(big.begin(),big.begin()+k); return result; }};
于是乎,将程序改成:
class Solution {public: vector<int> findClosestElements(vector<int>& arr, int k, int x) { int temp = 0; vector<int> big(arr.size()); int distance = 0; big.at(0) = arr.at(0); for(int i = 1; i < arr.size(); i ++) { if(abs(arr.at(i) - x) >= abs(big.at(i-1) - x)) big.at(i) = arr.at(i); else { for(int j = i;j > 0;j --) { if(abs(arr.at(i)-x) < abs(big.at(j-1)-x)) { temp = big.at(j-1); big.at(j-1) = arr.at(i); big.at(j) = temp; } } } } sort(big.begin(),big.begin()+k); vector<int> result(big.begin(),big.begin()+k); return result; }};
继续提交,由于使用了at操作符,对数组越界进行了检查,导致程序运行速度更慢,直接的结果是提示Status: Time Limit Exceeded
好吧,时间超时了,看来这个平台不仅注重时间复杂度和空间复杂度,连安全性也一并注重。
上述程序的复杂度应该是
于是乎,对程序进行修改:
vector<int> findClosestElements(vector<int>& arr, int k, int x) { int left = 0; int right = arr.size()-k; while(left<right){ int mid = left+(right-left)/2; if(x-arr[mid]>arr[mid+k]-x){ left = mid+1; }else{ right = mid; } } vector<int> result(arr.begin()+left, arr.begin()+left+k); return result;}
这次时间复杂度为
阅读全文
0 0
- LeetCode:658. Find K Closest Elements程序分析
- leetcode 658. Find K Closest Elements
- Leetcode 658. Find K Closest Elements
- [LeetCode] 658. Find K Closest Elements
- 658. Find K Closest Elements
- 658. Find K Closest Elements
- 658. Find K Closest Elements
- leetcode 658. Find K Closest Elements 双指针
- Leetcode 658 Find K Closest Elements
- Find K Closest Elements
- Binary Search:658. Find K Closest Elements
- 658. Find K Closest Elements(快排+折半查找)
- Add to List 658. Find K Closest Elements
- leetcode_658. Find K Closest Elements ? 待解决
- Find K Closest Elements问题及解法
- Find k closest elements to a given value
- LeetCode---Top K Frequent Elements解题分析
- Find k most repeating elements
- log4cplus的使用(转载)
- MySQL存储过程:实现更新UUID
- 35:字符串的展开
- MySQL:有关联的两张表重新生成主键如何关联
- NAT网络转换
- LeetCode:658. Find K Closest Elements程序分析
- MySQL存储过程:表名为变量
- 继承的应用举例-自定义异常类
- Effective Java(用函数对象表示策略、优先使用静态成员类)
- 访问控制列表-ACL
- 面试中,数据库经常用到的DQL,DML,DCL,DDL,TPL(TCL)的定义
- 使用ReentrantLock两个方法的互斥访问
- SQLyog:如何在不同数据库之间复制表
- C++基础-友元函数和继承派生