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)的更多相关文章

  1. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  2. 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

    传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...

  3. 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 ...

  4. 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 ...

  5. 【LeetCode】215. Kth Largest Element in an Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:移除最大值 方法二:排序 方法三:大顶堆 方 ...

  6. leetcode面试准备:Kth Largest Element in an Array

    leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...

  7. 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 ...

  8. [Leetcode Week11]Kth Largest Element in an Array

    Kth Largest Element in an Array 题解 题目来源:https://leetcode.com/problems/kth-largest-element-in-an-arra ...

  9. [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 ...

随机推荐

  1. Flume的Avro Sink和Avro Source研究之一: Avro Source

    问题 : Avro Source提供了怎么样RPC服务,是怎么提供的? 问题 1.1 Flume Source是如何启动一个Netty Server来提供RPC服务. 由GitHub上avro-rpc ...

  2. Top命令内存占用剖析

    原文: http://yalung929.blog.163.com/blog/static/203898225201212981731971/ 引 言: top命令作为Linux下最常用的性能分析工具 ...

  3. POJ 3904 Sky Code

    题意:给定n个数ai, ai <= 10000, n <= 10000, 从中选出4个数要求gcd为1,这样的集合有多少个? 分析:首先总共集合nCr(n, 4) = n*(n-1)*(n ...

  4. xcode 把cocos2d-x 以源码的形式包含进自己的项目适合, 性能分析问题的错误

    性能分析:出现如下错误: xcode profile  Variable has incomplete type   class “CC_DLL” 解决办法:在 xcode的Build Setting ...

  5. coco2d-js 多屏适配相关API

    setDesignResolutionSize() //设计分辨率大小及模式  setContentScaleFactor() //内容缩放因子 setSearchPaths() //资源搜索路径 g ...

  6. Java Web开发 之小张老师总结GET和POST区别

    get和post区别1.传输方式不同,get在request-line中传输(即在URL中传输).post在request-line及 request-body中传输(可认为隐藏传输)2.get传输长 ...

  7. CVE爬虫抓取漏洞URL

    String url1="http://www.cnnvd.org.cn/vulnerability/index/vulcode2/tomcat/vulcode/tomcat/cnnvdid ...

  8. HDU4614【线段树。】

    果然看了理解了一下大牛的代码然后自己敲结果果然有不少错误 回复说,线段树做为一种数据结构,最好以一种风格过一题裸的然后作为自己的模板.. 二分写的也很恶心哪 还有题目稍复杂一点的注定得推敲各种公式,不 ...

  9. WinAPI——Windows 消息

    消息 值  注释  WM_NULL $0000   WM_CREATE $0001   WM_DESTROY $0002   WM_MOVE $0003   WM_SIZE $0005   WM_AC ...

  10. 【HDOJ】1699 The comment in cpp

    注意测试数据12/*hduacm// abcd结果是1/*hduacm// ABCD /* 1699 */ #include <iostream> #include <sstream ...