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 ...
随机推荐
- <span> <div> 局部 keydown ,keyup事件。页面部分div $(document) 无效,可能焦点,添加焦点。
前天改一个bug, js 实现的一个 面板拖拉,左右各两个列表,中间面板画线连接,页面左侧列表选中后,key 事件无效.右侧选中确有效,很奇怪,查看源码,左侧选中后,$(document).on(&q ...
- Hybrid App 和 React Native 开发那点事
简介:Hybrid App(混合模式移动应用)开发是指介于Web-app.Native-App这两者之间的一种开发模式,兼具「Native App 良好用户交互体验的优势」和「Web App 跨平台开 ...
- python读写配置文件
#coding:utf-8 import ConfigParser class Conf(): def __init__(self,name): self.name = name self.cp = ...
- 全选与反选(dom与jquery比较)
<html> <head> <title>全选或反选(dom)</title> <meta http-equiv="Content-Ty ...
- iOS开发--TableView详细解释
-.建立 UITableView DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)]; [Data ...
- HDU4857——逃生(反向建图+拓扑排序)(BestCoder Round #1)
逃生 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会 ...
- Java读写Windows共享文件夹 .
版权声明:本文为博主原创文章,未经博主允许不得转载. 项目常常需要有访问共享文件夹的需求,例如共享文件夹存储照片.文件等.那么如何使用Java读写Windows共享文件夹呢? Java可以使用JCIF ...
- node.js 模块之url和querystring模块
关系如下: url.parse(string).query | url.parse(string).pathname | | | | | ------ ------------------- http ...
- 约瑟夫环问题-循环链表VS数组
2013-08-18 21:27:50 循环链表.数组解决约瑟夫环问题的比较 注意几点: 循环链表的建立不难,在删除循环链表中元素时,用pCur->next != pCur判断结束: 每一轮计数 ...
- Android开发之一个未解决的bug
使用Activity之间传递数据的时候,出现了一个bug,但是没有找到哪里出错了. 把代码和log都记录下来,以后研究 代码: MainActivity.class package com.examp ...