leetcode@ [315/215] Count of Smaller Numbers After Self / Kth Largest Element in an Array (BST)
https://leetcode.com/problems/count-of-smaller-numbers-after-self/
You are given an integer array nums and you have to return a new 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 {
class TreeNode{
public: int val, rank;
TreeNode *l, *r;
TreeNode(int v): val(v), rank(), l(NULL), r(NULL) {}
};
public:
int getRank(TreeNode* root, int v) {
int rank = ;
while(true) {
if(v <= root->val) {
++root->rank;
if(root->l == NULL) {
root->l = new TreeNode(v);
break;
}
else root = root->l;
}
else{
rank += root->rank;
if(root->r == NULL) {
root->r = new TreeNode(v);
break;
}
else root = root->r;
}
}
return rank;
}
vector<int> countSmaller(vector<int>& nums) {
vector<int> res;
if(nums.size() == ) return res;
TreeNode* root = new TreeNode(nums[nums.size()-]);
res.push_back();
for(int i=nums.size()-; i>=; --i) {
int rank = getRank(root, nums[i]);
res.push_back(rank);
}
vector<int> rev_res;
for(vector<int>::reverse_iterator p = res.rbegin(); p!=res.rend(); ++p) rev_res.push_back(*p);
return rev_res;
}
};
https://leetcode.com/problems/kth-largest-element-in-an-array/
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
For example,
Given [3,2,1,5,6,4] and k = 2, return 5.
Note:
You may assume k is always valid, 1 ≤ k ≤ array's length.
class Solution {
class TreeNode{
public: int val, rank;
TreeNode *l, *r;
TreeNode(int v): val(v), rank(), l(NULL), r(NULL) {}
};
public:
void addNode(TreeNode* root, int v) {
while(true) {
if(v <= root->val) {
++root->rank;
if(root->l == NULL) {
root->l = new TreeNode(v);
break;
}
else root = root->l;
}
else{
if(root->r == NULL) {
root->r = new TreeNode(v);
break;
}
else root = root->r;
}
}
}
void dfs(TreeNode* root, int k, int& res) {
if(root->rank == k) {
res = root->val;
return;
}
if(root->l) dfs(root->l, k, res);
if(root->r) dfs(root->r, k - root->rank, res);
}
int findKthLargest(vector<int>& nums, int k) {
if(nums.size() == ) return nums[];
TreeNode *root = new TreeNode(nums[]);
for(int i=; i<nums.size(); ++i) {
addNode(root, nums[i]);
}
int res = -;
dfs(root, nums.size() - k + , res);
return res;
}
};
leetcode@ [315/215] Count of Smaller Numbers After Self / Kth Largest Element in an Array (BST)的更多相关文章
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array
传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...
- LeetCode OJ 215. Kth Largest Element in an Array 堆排序求解
题目链接:https://leetcode.com/problems/kth-largest-element-in-an-array/ 215. Kth Largest Element in an A ...
- LN : leetcode 215 Kth Largest Element in an Array
lc 215 Kth Largest Element in an Array 215 Kth Largest Element in an Array Find the kth largest elem ...
- 【LeetCode】215. Kth Largest Element in an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:移除最大值 方法二:排序 方法三:大顶堆 方 ...
- leetcode面试准备:Kth Largest Element in an Array
leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...
- Leetcode 之 Kth Largest Element in an Array
636.Kth Largest Element in an Array 1.Problem Find the kth largest element in an unsorted array. Not ...
- [Leetcode Week11]Kth Largest Element in an Array
Kth Largest Element in an Array 题解 题目来源:https://leetcode.com/problems/kth-largest-element-in-an-arra ...
- [LeetCode] 215. Kth Largest Element in an Array 数组中第k大的数字
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
随机推荐
- 转Spring+Hibernate+EHcache配置(三)
配置每一项的详细作用不再详细解释,有兴趣的请google下 ,这里需要注意一点defaultCache标签定义了一个默认的Cache,这个Cache是不能删除的,否则会抛出No default cac ...
- uva 10827
与108类似 多加了两层循环 水过 #include <iostream> #include <cstring> #include <cstdio> #includ ...
- firefly服务器间通信演示
源地址:http://www.9miao.com/question-15-54560.html 最近好多童鞋都在问firefly几个服务器之间是如何通信的,其实在之前的distributed使用文档中 ...
- linux grep命令总结
风生水起善战者,求之于势,不责于人,故能择人而任势. 博客园 首页 新随笔 联系 订阅 管理 posts - 791, comments - 394, trackba ...
- MyEclipse中创建maven工程
转载:http://blog.sina.com.cn/s/blog_4f925fc30102epdv.html 先要在MyEclipse中对Maven进行设置: 到此Maven对MyEclip ...
- linux下添加PATH环境变量
添加PATH环境变量,第1种方法:[root@lx_web_s1 ~]# export PATH=/usr/local/webserver/mysql/bin:$PATH 再次查看: [root@lx ...
- 如何在VS2010中使用Async功能?
伴随C#5.0的发布,“异步”特性越来越深入人心:在VS2012中早就可以使用它大大简化异步编程的痛苦,那么在VS2010中呢?我们无法尝鲜么?答案是“No”!,其实我们可以这样做: 1)必须把你的V ...
- 查看Vim的option变量的值
以t_Co变量为例,最好用 :echo &t_Co 也可以使用 :set t_Co?,但是漏打?的话就会设置,得不偿失 要想知道在哪里这个变量被设置的,用 :verbose set t_Co? ...
- NFC(8)关于新买的标签的格式化
有多种方法格式化nfc标签设备. 如搜相关的手机上应用,在应用里选择格式类型 本文是用代码手动格式 public void writeNFCTag(Tag tag) { if (tag == null ...
- Dapper连接Oracle
Dapper连接Oracle去年写过了篇博客,名字叫:让dapper支持Oracle 网址:http://www.cnblogs.com/ushou/archive/2012/09/28/270690 ...