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. Unity3D调用第三方SDK(之一)从eclipse到Unity3D 友盟

    原地址:http://www.360doc.com/content/14/0120/14/11670799_346638215.shtml 篇展示在Unity3D中调用友盟SDK的实现方法. 首先附上 ...

  2. C++ 通过对象方式 、指针方式两种方式去访问成员变量(属性或者方法)

    准备 1.在VS中新建一个项目-Viscal C++ ---常规--空项目 2.建立一个.h的头文件 定义一个类 声明其成员(C#中的属性和方法) #include<iostream> # ...

  3. GNU :6.47 Function Names as Strings

    链接:http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html#Function-Names GCC provides three magic var ...

  4. Tomcat下server.xml中context介绍

    conf/Context.xml是Tomcat公用的环境配置;若在server.xml中增加<Context path="/test" docBase="D:\te ...

  5. Servlet 下载文件

    这几天有点懒散,还好没有忘记看书,上周去了国家图书馆翻阅了一些和Java相关的书籍,其实这些书都是自己以前看过或者听过,按理来说,不应该看自己已经看过的书籍,应该找一些最新的书籍去看,但是每次走到书架 ...

  6. c#自带压缩类实现数据库表导出到CSV压缩文件的方法

    在导出大量CSV数据的时候,常常体积较大,采用C#自带的压缩类,可以方便的实现该功能,并且压缩比例很高,该方法在我的开源工具DataPie中已经经过实践检验.我的上一篇博客<功能齐全.效率一流的 ...

  7. strcat与strncat的C/C++实现

    2013-07-05 15:47:19 本函数给出了几种strcat与strncat的实现,有ugly implementation,也有good implementation.并参考标准库中的imp ...

  8. CentOS7.1 安装关键步骤 记录下来

    SecureCRT下载地址 https://yunpan.cn/cS9W94kuvhXPb  访问密码 08cd[这里GNOME桌面 下的 要全选,截屏有误]

  9. FastScroll(1)ListView打开FastScroll及自定义它的样式

    打开 FastScroll 方式 android:fastScrollEnabled="true" 它是AbsListView的属性. <?xml version=" ...

  10. 数论/the first wave

    线性筛素数(原来我之前学的不是线性的啊... void getprime(){ rep(i,2,nmax){ if(!vis[i]) prime[++prime[0]]=i; for(int j=1; ...