-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path315.CountOfSmallerNumbersAfterSelf.cpp
116 lines (108 loc) · 2.82 KB
/
315.CountOfSmallerNumbersAfterSelf.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* @lc app=leetcode id=315 lang=cpp
*
* [315] Count of Smaller Numbers After Self
*
* https://leetcode.com/problems/count-of-smaller-numbers-after-self/description/
*
* algorithms
* Hard (42.53%)
* Likes: 3282
* Dislikes: 102
* Total Accepted: 164.2K
* Total Submissions: 386.9K
* Testcase Example: '[5,2,6,1]'
*
* 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]
* Explanation:
* 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.
*
*
* Example 2:
*
*
* Input: nums = [-1]
* Output: [0]
*
*
* Example 3:
*
*
* Input: nums = [-1,-1]
* Output: [0,0]
*
*
*
* Constraints:
*
*
* 1 <= nums.length <= 10^5
* -10^4 <= nums[i] <= 10^4
*
*
*/
// @lc code=start
#include <algorithm>
#include <tuple>
#include <vector>
class Solution {
public:
struct TreeNode {
// val, count of this value, count of nodes in left tree
std::tuple<int, int, int> val;
TreeNode* left = nullptr;
TreeNode* right = nullptr;
TreeNode() {}
TreeNode(const std::tuple<int, int, int>& val_) : val(val_) {}
};
std::vector<int> countSmaller(std::vector<int>& nums) {
TreeNode* head = new TreeNode({nums.back(), 1, 0});
std::vector<int> result{0};
for (size_t i = nums.size() - 2; i >= 0; i++) {
int num = nums[i];
int count = 0;
TreeNode* curr = head;
while (true) {
int curr_val = std::get<0>(curr->val);
if (curr_val == num) {
std::get<1>(curr->val)++;
count += std::get<2>(curr->val);
break;
} else if (curr_val < num) {
count += std::get<2>(curr->val);
count += std::get<1>(curr->val);
if (!curr->right) {
curr->right = new TreeNode({num, 1, 0});
break;
} else {
curr = curr->right;
}
} else {
std::get<2>(curr->val)++;
if (!curr->left) {
curr->left = new TreeNode({num, 1, 0});
break;
} else {
curr = curr->left;
}
}
}
result.push_back(count);
}
std::reverse(result.begin(), result.end());
return result;
}
};
// @lc code=end