[Leetcode]Count of Smaller Numbers After Self
2016-01-20 18:08
369 查看
You are given an integer array nums and you have to return a new
counts array.The counts array has the property where
Example:
Return the array
counts array.The counts array has the property where
counts[i]is the number of smaller elements to the right of
nums[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: /*algorithm: hash time O(n*n) space O(n) */ vector<int> countSmaller(vector<int>& nums) { vector<int>count(nums.size(),0); for(int i = 0;i < nums.size();i++){ for(int j = 0;j < i;j++){ if(nums[j] > nums[i]){ count[j]++; } } } return count; } };
class Solution { public: /*algorithm: binary search tree time O(nlgn) space O(n) */ struct TreeNode{ int val; int cnt;//left child nodes count int same; //same number nodes count TreeNode* left,*right; TreeNode(int v):val(v),cnt(0),same(1),left(NULL),right(NULL){} }; int insert(TreeNode* &root,int val){ if(!root){ root = new TreeNode(val); return 0; } TreeNode* t = root; int count = 0; while(t){ if(t->val > val){ t->cnt++; if(!t->left){ t->left = new TreeNode(val); break; } t = t->left; }else if(t->val < val){ count += t->cnt + t->same; if(!t->right){ t->right = new TreeNode(val); break; } t = t->right; }else{ count += t->cnt; t->same++; //add self number occurence break; } } return count; } vector<int> countSmaller(vector<int>& nums) { vector<int>count(nums.size(),0); TreeNode* root = NULL; for(int i = (int)nums.size()-1;i >= 0;i--){ count[i] = insert(root,nums[i]); } return count; } };
/*algorithm: merge sort time O(nlgn) */ struct NumberIndex { int number; int index; NumberIndex(int val, int index):number(val),index(index){} }; vector<NumberIndex> mergeSort(vector<NumberIndex>&nums,vector<int>&smaller) { int half = nums.size() / 2; if (half > 0) { vector<NumberIndex> leftPart,rightPart; int k; for (k = 0; k < half; k++) { leftPart.push_back(nums[k]); } for (;k < nums.size();k++) { rightPart.push_back(nums[k]); } vector<NumberIndex> left = mergeSort(leftPart, smaller); vector<NumberIndex> right = mergeSort(rightPart, smaller); int i = 0, j = 0,d = 0; while (i <left.size() || j < right.size()) { if (j == right.size() || i < left.size() && left[i].number <= right[j].number) { nums[d++] = left[i]; smaller[left[i].index] += j; i++; } else { nums[d++] = right[j]; j++; } } } return nums; } vector<int> countSmaller(vector<int>& nums) { vector<NumberIndex>cnums; for (int i = 0; i < nums.size(); i++) { cnums.push_back(NumberIndex(nums[i],i)); } vector<int>smaller(nums.size(),0); mergeSort(cnums, smaller); return smaller; }
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例
- PHP实现克鲁斯卡尔算法实例解析