leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1365.cpp (1048B)


      1 // sort + count
      2 class Solution {
      3   public:
      4     vector<int> smallerNumbersThanCurrent(const vector<int> &nums) const {
      5         const int n = size(nums);
      6         static int idxs[501];
      7         vector<int> res(n);
      8 
      9         iota(idxs, idxs + n, 0);
     10         sort(idxs, idxs + n, [&](int a, int b) { return nums[a] < nums[b]; });
     11 
     12         int cnt = 0;
     13         for (int i = 1; i < n; i++) {
     14             if (nums[idxs[i]] != nums[idxs[i - 1]]) {
     15                 cnt = i;
     16             }
     17             res[idxs[i]] = cnt;
     18         }
     19 
     20         return res;
     21     }
     22 };
     23 
     24 // count
     25 class Solution {
     26   public:
     27     vector<int> smallerNumbersThanCurrent(const vector<int> &nums) const {
     28         const int n = size(nums);
     29         static int count[101];
     30         vector<int> res(n);
     31 
     32         memset(count, 0x00, sizeof(count));
     33         for (const int n : nums)
     34             count[n + 1]++;
     35         for (int i = 1; i <= 100; i++)
     36             count[i] += count[i - 1];
     37         for (int i = 0; i < size(nums); i++)
     38             res[i] = count[nums[i]];
     39 
     40         return res;
     41     }
     42 };