1 Star 0 Fork 0

wd6/LeetCode-1

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
contains-duplicate-iii.cpp 906 Bytes
Copy Edit Raw Blame History
kamyu authored 9 years ago . Update contains-duplicate-iii.cpp
// Time: O(nlogk)
// Space: O(k)
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
if (k < 0 || t < 0) {
return false;
}
queue<int64_t> window;
multiset<int64_t> bst;
for (int i = 0; i < nums.size(); ++i) {
// Only keep at most k elements.
if (bst.size() > k) {
int num = window.front();
window.pop();
bst.erase(bst.find(num));
}
// Every search costs time: O(logk).
const auto it = bst.lower_bound(nums[i] - t);
if (it == bst.cend() || (*it - nums[i]) > t) {
// Not found.
window.emplace(nums[i]);
bst.emplace(nums[i]);
} else {
return true;
}
}
return false;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/wd6/LeetCode-1.git
git@gitee.com:wd6/LeetCode-1.git
wd6
LeetCode-1
LeetCode-1
master

Search