solution.md 5.9 KB
Newer Older
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
<p>给你`一个整数数组 <code>nums</code><em> </em>,按要求返回一个新数组&nbsp;<code>counts</code><em> </em>。数组 <code>counts</code> 有该性质: <code>counts[i]</code> 的值是&nbsp; <code>nums[i]</code> 右侧小于&nbsp;<code>nums[i]</code> 的元素的数量。</p>

<p>&nbsp;</p>

<p><strong>示例 1:</strong></p>

<pre>
<strong>输入:</strong>nums = [5,2,6,1]
<strong>输出:</strong><code>[2,1,1,0] 
<strong>解释:</strong></code>
5 的右侧有 <strong>2 </strong>个更小的元素 (2 和 1)
2 的右侧仅有 <strong>1 </strong>个更小的元素 (1)
6 的右侧有 <strong>1 </strong>个更小的元素 (1)
1 的右侧有 <strong>0 </strong>个更小的元素
</pre>

<p><strong>示例 2:</strong></p>

<pre>
<strong>输入:</strong>nums = [-1]
<strong>输出:</strong>[0]
</pre>

<p><strong>示例 3:</strong></p>

<pre>
<strong>输入:</strong>nums = [-1,-1]
<strong>输出:</strong>[0,0]
</pre>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
</ul>

<p>以下错误的选项是?</p>
42 43 44
## aop
### before
```cpp
每日一练社区's avatar
每日一练社区 已提交
45 46
#include <bits/stdc++.h>
using namespace std;
47 48 49
```
### after
```cpp
每日一练社区's avatar
每日一练社区 已提交
50 51 52 53 54 55 56 57 58 59
int main()
{
    Solution sol;
    vector<int> res;
    vector<int> nums = {5, 2, 6, 1};
    res = sol.countSmaller(nums);
    for (auto i : res)
        cout << i << " ";
    return 0;
}
60 61 62 63
```

## 答案
```cpp
每日一练社区's avatar
每日一练社区 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
class Solution
{
public:
    int lowbit(int x)
    {
        return (int)x & (-1 * x);
    }
    int getSum(int x, vector<int> &c)
    {
        int sum = 0;
        for (int i = x; i > 0; i -= lowbit(i))
        {
            sum += c[i];
        }
        return sum;
    }

    void update(vector<int> &c, int x, int v)
    {
        for (int i = x; i < c.size(); i += lowbit(i))
        {
            c[i] += v;
        }
    }
88

每日一练社区's avatar
每日一练社区 已提交
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 117 118 119 120 121
    vector<int> countSmaller(vector<int> &nums)
    {
        vector<int> count;
        set<int> ss;
        for (auto t : nums)
        {
            ss.insert(t);
        }
        unordered_map<int, int> m;
        int n = ss.size();
        auto it = ss.begin();
        for (int i = 1; i <= n; i++)
        {

            it++;
            m[*it] = i;
        }
        vector<int> sum(n + 1, 0);
        for (auto it = nums.rbegin(); it != nums.rend(); it++)
        {
            int res = 0;
            int idx = m[*it];
            if (idx > 1)
            {
                res = getSum(idx - 1, sum);
            }
            update(sum, m[*it], 1);
            count.push_back(res);
        }
        reverse(count.begin(), count.end());
        return count;
    }
};
122 123 124 125 126
```
## 选项

### A
```cpp
每日一练社区's avatar
每日一练社区 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
class Solution
{
public:
    vector<int> countSmaller(vector<int> &nums)
    {
        vector<int> res(nums.size(), 0);
        vector<int> indexs(nums.size(), 0);
        for (int i = 0; i < indexs.size(); i++)
        {
            indexs[i] = i;
        }
        vector<int> tempindexs(indexs.size(), 0);
        mergeSort(nums, indexs, tempindexs, res, 0, nums.size() - 1);
        return res;
    }
    void mergeSort(vector<int> &nums, vector<int> &indexs, vector<int> &tempindexs, vector<int> &res, int left, int right)
    {
        if (left >= right)
            return;
        int mid = left + (right - left) / 2;
        mergeSort(nums, indexs, tempindexs, res, left, mid);
        mergeSort(nums, indexs, tempindexs, res, mid + 1, right);
        int i = left;
        int j = mid + 1;
        int t = 0;
        while (i <= mid && j <= right)
        {
            if (nums[indexs[i]] > nums[indexs[j]])
            {
                for (int k = i; k <= mid; k++)
                {
                    res[indexs[k]]++;
                }
                tempindexs[t++] = indexs[j++];
            }
            else
            {
                tempindexs[t++] = indexs[i++];
            }
        }
        while (i <= mid)
        {
            tempindexs[t++] = indexs[i++];
        }
        while (j <= right)
        {
            tempindexs[t++] = indexs[j++];
        }
        t = 0;
        while (left <= right)
        {
            indexs[left++] = tempindexs[t++];
        }
    }
};
182 183 184 185
```

### B
```cpp
每日一练社区's avatar
每日一练社区 已提交
186 187 188 189 190 191 192 193
struct BSTNode
{
    int val;
    int count;
    BSTNode *left;
    BSTNode *right;
    BSTNode(int x) : val(x), left(NULL), right(NULL), count(0) {}
};
194

每日一练社区's avatar
每日一练社区 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
void BST_insert(BSTNode *node, BSTNode *insert_node, int &count_small)
{
    if (node->val >= insert_node->val)
    {

        node->count++;
        if (node->left)
            BST_insert(node->left, insert_node, count_small);
        else
            node->left = insert_node;
    }
    else
    {
        count_small += node->count + 1;
        if (node->right)
            BST_insert(node->right, insert_node, count_small);
        else
            node->right = insert_node;
    }
}

class Solution
{
public:
    vector<int> countSmaller(vector<int> &nums)
    {
        int n = nums.size();
        if (!n)
            return {};
        vector<int> count;
        count.push_back(0);
        BSTNode *node = new BSTNode(nums[n - 1]);
        int count_small;
        for (int i = 1; i < n; ++i)
        {
            count_small = 0;
            BST_insert(node, new BSTNode(nums[n - i - 1]), count_small);
            count.push_back(count_small);
        }

        delete node;
        reverse(count.begin(), count.end());
        return count;
    }
};
240 241 242 243
```

### C
```cpp
每日一练社区's avatar
每日一练社区 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
class Solution
{
public:
    vector<int> help;
    vector<int> countSmaller(vector<int> &nums)
    {
        int n = nums.size();
        if (n == 0)
            return vector<int>();
        vector<int> res(n, 0);
        for (int i = n - 1; i >= 0; --i)
        {
            auto it = lower_bound(help.begin(), help.end(), nums[i]);
            res[i] = it - help.begin();
            help.insert(it, nums[i]);
        }
        return res;
    }
};
263
```