Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 68fa054

Browse files
committed
feat(algorithms):315 accepted & testing
1 parent c3dce55 commit 68fa054

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import * as asserts from "https://deno.land/std/testing/asserts.ts";
2+
import * as log from "https://deno.land/std/log/mod.ts";
3+
import { countSmaller } from "./index.ts";
4+
5+
log.info("315. Count of Smaller Numbers After Self");
6+
7+
Deno.test({
8+
name: `
9+
Input: [5,2,6,1]
10+
Output: [2,1,1,0]
11+
`,
12+
fn(): void {
13+
const result: number[] = countSmaller([5, 2, 6, 1]);
14+
asserts.assertEquals([2, 1, 1, 0], result);
15+
},
16+
});
17+
18+
Deno.test({
19+
name: `
20+
Input: [9, 8, 7, 6, 5, 4, 3, 2, 1]
21+
Output: [8, 7, 6, 5, 4, 3, 2, 1, 0]
22+
`,
23+
fn(): void {
24+
const result: number[] = countSmaller([9, 8, 7, 6, 5, 4, 3, 2, 1]);
25+
asserts.assertEquals([8, 7, 6, 5, 4, 3, 2, 1, 0], result);
26+
},
27+
});
28+
29+
Deno.test({
30+
name: `
31+
Input: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
32+
Output: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
33+
`,
34+
fn(): void {
35+
const result: number[] = countSmaller([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
36+
asserts.assertEquals([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], result);
37+
},
38+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* # 315. Count of Smaller Numbers After Self
3+
*
4+
* You are given an integer array nums and you have to return a new counts array. The counts array has the property where `counts[i]` is the number of smaller elements to the right of `nums[i]`.
5+
*
6+
* ## Example
7+
*
8+
* ```bash
9+
* Input: [5,2,6,1]
10+
* Output: [2,1,1,0]
11+
* Explanation:
12+
* To the right of 5 there are 2 smaller elements (2 and 1).
13+
* To the right of 2 there is only 1 smaller element (1).
14+
* To the right of 6 there is 1 smaller element (1).
15+
* To the right of 1 there is 0 smaller element.
16+
* ```
17+
*/
18+
export type Solution = (nums: number[]) => number[];
19+
20+
/**
21+
* @date 2020年07月11日 18:17:00
22+
* @time O(n * log n)
23+
* @space O(n)
24+
* @runtime
25+
* @memory
26+
* @runtime_cn 820 ms, faster than 100.00%
27+
* @memory_cn 37.7 MB, less than 100.00%
28+
*/
29+
export const countSmaller = (nums: number[]): number[] => {
30+
const result: number[] = new Array(nums.length).fill(0);
31+
for (let i = 0; i < nums.length; i++) {
32+
let count = 0;
33+
for (let j = nums.length - 1; j > i; j--) {
34+
if (nums[i] > nums[j]) {
35+
count++;
36+
}
37+
}
38+
result[i] = count;
39+
}
40+
return result;
41+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /