mirror of
https://github.com/prdlk/leetcode.git
synced 2026-08-02 17:31:40 +00:00
docs(array): add LC-1365 How Many Numbers Are Smaller Than the Current Number documentation
This commit is contained in:
@@ -0,0 +1,54 @@
|
|||||||
|
---
|
||||||
|
title: LC-1365
|
||||||
|
description: How Many Numbers Are Smaller Than the Current Number
|
||||||
|
---
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
| Input | Output |
|
||||||
|
| ---------------- | -------------------- |
|
||||||
|
| `[8,1,2,2,3]` | `[4,0,1,1,3]` |
|
||||||
|
| `[6,5,4,8]` | `[2,1,0,3]` |
|
||||||
|
| `[7,7,7,7]` | `[0,0,0,0]` |
|
||||||
|
|
||||||
|
## Hints
|
||||||
|
|
||||||
|
<Accordions type="single">
|
||||||
|
<Accordion title="Constraints">
|
||||||
|
Constraints:
|
||||||
|
- 2 <= `nums.length` <= 500
|
||||||
|
- 0 <= `nums[i]` <= 100
|
||||||
|
</Accordion>
|
||||||
|
<Accordion title="Problem Statement">
|
||||||
|
Given the array nums, for each `nums[i]` find out how many numbers in the array are smaller than it.
|
||||||
|
That is, for each `nums[i]` you have to count the number of valid `j`'s such that `j != i` and `nums[j] < nums[i]`.
|
||||||
|
</Accordion>
|
||||||
|
</Accordions>
|
||||||
|
|
||||||
|
## Solution
|
||||||
|
|
||||||
|
```js
|
||||||
|
/**
|
||||||
|
* @param {number[]} nums
|
||||||
|
* @return {number[]}
|
||||||
|
*/
|
||||||
|
var smallerNumbersThanCurrent = function (nums) {
|
||||||
|
// Default for Hammer
|
||||||
|
const freq = {};
|
||||||
|
for (let n of nums) freq[n] = (freq[n] || 0) + 1;
|
||||||
|
const sorted = Object.keys(freq).sort((a, b) => a - b);
|
||||||
|
|
||||||
|
// Problem Specific
|
||||||
|
let count = 0;
|
||||||
|
let smaller = {};
|
||||||
|
|
||||||
|
// Iterate over sorted.
|
||||||
|
for (let num of sorted) {
|
||||||
|
smaller[num] = count;
|
||||||
|
count += freq[num];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Map output
|
||||||
|
return nums.map((n) => smaller[n]);
|
||||||
|
};
|
||||||
|
```
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
---
|
---
|
||||||
title: Introduction
|
title: Introduction
|
||||||
description: Prad's Random CS Notes
|
description: Prad's Random Leetcode Notes
|
||||||
---
|
---
|
||||||
|
|
||||||
## Overview
|
## Overview
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
---
|
|
||||||
title: LC-1365
|
|
||||||
description: How Many Numbers Are Smaller Than the Current Number
|
|
||||||
---
|
|
||||||
|
|
||||||
## OverviewProblem solving guide for Technical Interviews
|
|
||||||
|
|
||||||
Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j] < nums[i].
|
|
||||||
|
|
||||||
### Examples
|
|
||||||
|
|
||||||
| Control | Purpose | Default |
|
|
||||||
| ------------------ | --------------------- | -------- |
|
|
||||||
| `--typeset-size` | Base text size | `1em` |
|
|
||||||
| `--typeset-leading`| Line height | `1.75` |
|
|
||||||
| `--typeset-flow` | Space between blocks | `1.25em` |
|
|
||||||
|
|
||||||
|
|
||||||
### Constraints
|
|
||||||
Reference in New Issue
Block a user