← Home

1365. How Many Numbers Are Smaller Than the Current Number

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
greedyC++Markdown
136

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 1365. How Many Numbers Are Smaller Than the Current Number, the solution in this repository is mainly a greedy solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: greedy.

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are smallerNumbersThanCurrent.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 16 ms, faster than 64.11% of C++ online submissions for How Many Numbers Are Smaller Than the Current Number.
02//Memory Usage: 8.1 MB, less than 100.00% of C++ online submissions for How Many Numbers Are Smaller Than the Current Number.
03
04class Solution {
05public:
06    vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
07        int N = nums.size();
08        vector<int> sorted = nums;
09        vector<int> ans(N);
10        
11        sort(sorted.begin(), sorted.end());
12        for(int i = 0; i < nums.size(); i++){
13            int ix = find(sorted.begin(), sorted.end(), nums[i]) - sorted.begin();
14            
15            ans[i] = ix;
16        }
17        return ans;
18    }
19};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.