You are given an integer array nums and you have to return a new counts array. Thecounts 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…
315. Count of Smaller Numbers After Self class Solution { public: vector<int> countSmaller(vector<int>& nums) { int n = nums.size(); vector<int> v(n); for (int i = n - 1; i >= 0; --i) { int val = nums[i]; int L = i + 1, R = n - 1;…
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒".leetcode 上的题目,截止目前切了 137 道(all solutions),只写过 6 篇题解,所以我会写题解的一般都是自认为还蛮有意思或者蛮典型的题目,就比如这道题. 题目链接:Count of Smaller Numbers After Self 这道题很有意思,给出一个数组,返回一个新的数组,新…
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒".leetcode 上的题目,截止目前切了 137 道(all solutions),只写过 6 篇题解,所以我会写题解的一般都是自认为还蛮有意思或者蛮典型的题目,就比如这道题. 题目链接:Count of Smaller Numbers After Self 这道题很有意思,给出一个数组,返回一个新的数组,新…
You are given an integer array nums and you have to return a new counts array. The countsarray has the property where counts[i] is the number of smaller elements to the right of nums[i]. Example: Input: [5,2,6,1] Output: [2,1,1,0] Explanation: To the…
原题链接在这里: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 r…
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…
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: Input: [5,2,6,1] Output:[5,2,6,1] Explanation: To the…
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 1: Input: nums = [5,2,6,1] Output: [2,1,1,0] Explanati…
Leetcode315 题意很简单,给定一个序列,求每一个数的右边有多少小于它的数. O(n^2)的算法是显而易见的. 用普通的线段树可以优化到O(nlogn) 我们可以直接套用主席树的模板. 主席树的功能是什么呢? 其实就是一句话. 原序列a的子序列a[l,r]在a排序后的序列b的子序列[L,R]中的个数. 显然本题只用到了主席树的一小部分功能. const int N = 100000 + 5; int a[N], b[N], rt[N * 20], ls[N * 20], rs[N * 2…