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. DLL搜索路径和DLL劫持

    DLL搜索路径和DLL劫持 环境:XP SP3 VS2005 作者:magictong 为什么要把DLL搜索路径(DLL ORDER)和DLL劫持(DLL Hajack)拿到一起讲呢?呵呵,其实没啥深 ...

  2. php的redis 操作类,适用于单台或多台、多组redis服务器操作

    redis 操作类,包括单台或多台.多组redis服务器操作,适用于业务复杂.高性能要求的 php web 应用. redis.php: <?php /* redis 操作类,适用于单台或多台. ...

  3. 看文档要看仔细,英语要加强啊... cocos2d-x 的 API 和 对应版本的 cocos2d-js 的 API 没有完全对应

    /** * Sets the X rotation (angle) of the node in degrees which performs a horizontal rotational skew ...

  4. 编程实现Windows关机、重启、注销

    要想编程使Windows关机.重启或者注销,可以使用ExWindowsEx这个API函数,该函数只有两个参数,第一个表示关机动作的标志,也就是你要让该函数关机呢,还是重启,还是注销等.可以使用EWX_ ...

  5. Bridging signals ZOJ 3627 POJ1631 HDU1950

    题意:给出一个从1-n的数字排列,求最长上升子序列长度. 直接说解法吧.新开一个数组d,d[i]表示的是能构成长度为i的上升子序列的在原序列中最后的那个数值.程序的主要过程:当循环到第i个的时候,如果 ...

  6. win8 hyper-v 禁用不必卸载虚拟机

    转载:http://tylzwp.blogbus.com/logs/232938121.html 禁用hyperv的目的是使用之前在用的VMware的虚拟机,不必重新处理一遍. 具体操作: 1确报之前 ...

  7. CGI/FASTCGI/ISAPI区别

    一 CGI原理及其性能 1) CGI概念CGI即通用网关接口(Common Gateway Interface),它是一段程序,运行在服务器上,提供同客户端HTML页面的交互,通俗的讲CGI就象是一座 ...

  8. JSP JSP工作原理 JSP语法 JSP声明 JSP注释 JSP指令 jsp九大隐式/内置对象

    1 什么是JSP   1)为什么说,Servlet是一个动态Web开发技术呢?     Servlet是基于服务端的一种动态交互技术,     HttpServletRequest表示客户端到服务端的 ...

  9. virtualbox怎么装系统OVA虚拟包大全一键安装

    1 第一步,下载ova虚拟包 http://pan.baidu.com/s/1hqxWkUo . 里面有win7.ubuntu.kali,你自己挑一个需要的 全都可以自动缩放屏幕,有VirtualBo ...

  10. 原创js模型驱动

    使用ajax方式请求数据,向页面展示一个对象的时候,往往让人头疼的是一大堆 .val()  .text()方法,这样做固然不会出错,但是效率太低 以下提供一个自己编写的Jquery模型驱动插件,正在测 ...