LeetCode题目:315. Count of Smaller Numbers After Self
来源:互联网 发布:c数据结构与算法pdf 编辑:程序博客网 时间:2024/06/01 09:01
题目地址:点击打开链接
题目描述:You are given an integer array nums and you have to return a newcounts array. Thecounts array has the property where counts[i]
is the number of smaller elements to the right ofnums[i]
.
Example:
Given nums = [5, 2, 6, 1]To the right of 5 there are 2 smaller elements (2 and 1).To the right of 2 there is only 1 smaller element (1).To the right of 6 there is 1 smaller element (1).To the right of 1 there is 0 smaller element.
Return the array [2, 1, 1, 0]
.
一开始直接就用暴力比较,但超时了。代码如下:
class Solution {public: vector<int> countSmaller(vector<int>& nums) { int l=nums.size(); if(l==0)return nums; vector<int> c; int num=0; for(int i=0;i<l-1;i++){ for(int j=i+1;j<l;j++){ if(nums[i]>nums[j])num++; } c.push_back(num); num=0; } c.push_back(0); return c; }};
在论坛中发现一个简单的代码,虽然accept了但是很多人认为O(n^2)的复杂度并不好:
0
class Solution {public: vector<int> countSmaller(vector<int>& nums) { vector<int> db; vector<int> result(nums.size()); for(int i = nums.size()-1; i >= 0; i--) { auto it = lower_bound(db.begin(), db.end(), nums[i]); result[i] = it - db.begin(); db.insert(it, nums[i]); } return result; }};
正确的代码如下:
class Solution {protected: void merge_countSmaller(vector<int>& indices, int first, int last, vector<int>& results, vector<int>& nums) { int count = last - first; if (count > 1) { int step = count / 2; int mid = first + step; merge_countSmaller(indices, first, mid, results, nums); merge_countSmaller(indices, mid, last, results, nums); vector<int> tmp; tmp.reserve(count); int idx1 = first; int idx2 = mid; int semicount = 0; while ((idx1 < mid) || (idx2 < last)) { if ((idx2 == last) || ((idx1 < mid) && (nums[indices[idx1]] <= nums[indices[idx2]]))) {tmp.push_back(indices[idx1]); results[indices[idx1]] += semicount; ++idx1; } else {tmp.push_back(indices[idx2]); ++semicount; ++idx2; } } move(tmp.begin(), tmp.end(), indices.begin()+first); } }public: vector<int> countSmaller(vector<int>& nums) { int n = nums.size(); vector<int> results(n, 0); vector<int> indices(n, 0); iota(indices.begin(), indices.end(), 0); merge_countSmaller(indices, 0, n, results, nums); return results; }};
0 0
- LeetCode题目:315. Count of Smaller Numbers After Self
- leetcode Count of Smaller Numbers After Self
- leetcode Count of Smaller Numbers After Self
- leetcode Count of Smaller Numbers After Self
- Count of Smaller Numbers After Self | LeetCode
- leetcode Count of Smaller Numbers After Self
- LeetCode Count of Smaller Numbers After Self
- [Leetcode]Count of Smaller Numbers After Self
- leetcode - Count of Smaller Numbers After Self
- leetcode:Count of Smaller Numbers After Self
- 【Leetcode】Count of Smaller Numbers After Self
- 【leetCode】 Count of Smaller Numbers After Self
- [Leetcode] Count of Smaller Numbers After Self
- LeetCode -- Count of Smaller Numbers After Self
- Leetcode Count of Smaller Numbers After Self
- LeetCode -- Count of Smaller Numbers After Self
- LeetCode-Count of Smaller Numbers After Self
- Hard-题目11:315. Count of Smaller Numbers After Self
- SLAM笔记(四)运动恢复结构的数学基础(本征矩阵F,单应矩阵H,八点法与五点法,四点法)
- 文件描述符 dup/dup2函数
- Kafka入门经典教程
- jdk安装后没有配置环境变量也可以java -version
- J2EE:servlet response常用方法总结
- LeetCode题目:315. Count of Smaller Numbers After Self
- 尾递归总结
- phoenix 把CSV格式的数据导入到Hbase执行日志
- 智能制造核心技术在哪里?
- 聊聊RPC之Register
- PIC18F45K80 将RA0等配置为数字输入I/O
- Android IPC机制全解析
- 一个奇怪的发现:html与body,body的margin对html不起作用,html的padding对body却起作用
- 个人.vimrc配置