Skip to content

Commit e4153d2

Browse files
authored
feat: add solutions to lc problem: No.1365 (doocs#936)
1 parent 9037622 commit e4153d2

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

solution/1300-1399/1365.How Many Numbers Are Smaller Than the Current Number/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,49 @@ func smallerNumbersThanCurrent(nums []int) (ans []int) {
204204
}
205205
```
206206

207+
### **TypeScript**
208+
209+
```ts
210+
function smallerNumbersThanCurrent(nums: number[]): number[] {
211+
const search = (nums: number[], x: number) => {
212+
let l = 0,
213+
r = nums.length;
214+
while (l < r) {
215+
const mid = (l + r) >> 1;
216+
if (nums[mid] >= x) {
217+
r = mid;
218+
} else {
219+
l = mid + 1;
220+
}
221+
}
222+
return l;
223+
};
224+
const arr = nums.slice().sort((a, b) => a - b);
225+
for (let i = 0; i < nums.length; ++i) {
226+
nums[i] = search(arr, nums[i]);
227+
}
228+
return nums;
229+
}
230+
```
231+
232+
```ts
233+
function smallerNumbersThanCurrent(nums: number[]): number[] {
234+
const cnt: number[] = new Array(102).fill(0);
235+
for (const x of nums) {
236+
++cnt[x + 1];
237+
}
238+
for (let i = 1; i < cnt.length; ++i) {
239+
cnt[i] += cnt[i - 1];
240+
}
241+
const n = nums.length;
242+
const ans: number[] = new Array(n);
243+
for (let i = 0; i < n; ++i) {
244+
ans[i] = cnt[nums[i]];
245+
}
246+
return ans;
247+
}
248+
```
249+
207250
### **...**
208251

209252
```

solution/1300-1399/1365.How Many Numbers Are Smaller Than the Current Number/README_EN.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,49 @@ func smallerNumbersThanCurrent(nums []int) (ans []int) {
181181
}
182182
```
183183

184+
### **TypeScript**
185+
186+
```ts
187+
function smallerNumbersThanCurrent(nums: number[]): number[] {
188+
const search = (nums: number[], x: number) => {
189+
let l = 0,
190+
r = nums.length;
191+
while (l < r) {
192+
const mid = (l + r) >> 1;
193+
if (nums[mid] >= x) {
194+
r = mid;
195+
} else {
196+
l = mid + 1;
197+
}
198+
}
199+
return l;
200+
};
201+
const arr = nums.slice().sort((a, b) => a - b);
202+
for (let i = 0; i < nums.length; ++i) {
203+
nums[i] = search(arr, nums[i]);
204+
}
205+
return nums;
206+
}
207+
```
208+
209+
```ts
210+
function smallerNumbersThanCurrent(nums: number[]): number[] {
211+
const cnt: number[] = new Array(102).fill(0);
212+
for (const x of nums) {
213+
++cnt[x + 1];
214+
}
215+
for (let i = 1; i < cnt.length; ++i) {
216+
cnt[i] += cnt[i - 1];
217+
}
218+
const n = nums.length;
219+
const ans: number[] = new Array(n);
220+
for (let i = 0; i < n; ++i) {
221+
ans[i] = cnt[nums[i]];
222+
}
223+
return ans;
224+
}
225+
```
226+
184227
### **...**
185228

186229
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function smallerNumbersThanCurrent(nums: number[]): number[] {
2+
const cnt: number[] = new Array(102).fill(0);
3+
for (const x of nums) {
4+
++cnt[x + 1];
5+
}
6+
for (let i = 1; i < cnt.length; ++i) {
7+
cnt[i] += cnt[i - 1];
8+
}
9+
const n = nums.length;
10+
const ans: number[] = new Array(n);
11+
for (let i = 0; i < n; ++i) {
12+
ans[i] = cnt[nums[i]];
13+
}
14+
return ans;
15+
}

0 commit comments

Comments
 (0)