Skip to content

Commit 31cffde

Browse files
committed
feat: solve top-k
1 parent 1fca4df commit 31cffde

File tree

3 files changed

+73
-6
lines changed

3 files changed

+73
-6
lines changed

โ€Žcontains-duplicate/sujeong-dev.jsโ€Ž

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
/**
22
* @param {number[]} nums
33
* @return {boolean}
4+
*
5+
* ์‹œ๊ฐ„๋ณต์žก๋„ ๊ณ„์‚ฐ
6+
* ์ž…๋ ฅ ํฌ๊ธฐ(n)๊ฐ€ ์ปค์งˆ ๋•Œ ์—ฐ์‚ฐ ํšŸ์ˆ˜๊ฐ€ n์— ๋น„๋ก€ํ•ด์„œ ์ฆ๊ฐ€
7+
* => O(n)
8+
*
9+
* ๊ณต๊ฐ„๋ณต์žก๋„ ๊ณ„์‚ฐ
10+
* indices๊ฐ€ nums์— ๋น„๋ก€ํ•ด์„œ ํ• ๋‹น
11+
* ์ƒ์ˆ˜ i ํ• ๋‹น
12+
* => O(n)
413
*/
5-
6-
// for loof: O(n)
714
var containsDuplicate = function (nums) {
815
let indices = {};
916
nums.forEach((num, index) => {
@@ -17,7 +24,19 @@ var containsDuplicate = function (nums) {
1724
return false;
1825
};
1926

20-
// size ์†์„ฑ: O(1)
27+
/**
28+
* @param {number[]} nums
29+
* @return {boolean}
30+
*
31+
* ์‹œ๊ฐ„๋ณต์žก๋„ ๊ณ„์‚ฐ
32+
* Set์˜ size์†์„ฑ์€ ์ž…๋ ฅ ํฌ๊ธฐ์™€ ๊ด€๊ณ„์—†์ด ์ผ์ •ํ•œ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆผ
33+
* => O(1)
34+
*
35+
* ๊ณต๊ฐ„๋ณต์žก๋„ ๊ณ„์‚ฐ
36+
* indices๊ฐ€ nums์— ๋น„๋ก€ํ•ด์„œ ํ• ๋‹น
37+
* ์ƒ์ˆ˜ i ํ• ๋‹น
38+
* => O(n)
39+
*/
2140
var containsDuplicate = function (nums) {
2241
const indices = new Set(nums);
2342

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number[]}
5+
*
6+
* ์‹œ๊ฐ„๋ณต์žก๋„ ๊ณ„์‚ฐ
7+
* for loop๊ฐ€ O(n)
8+
* reduce๊ฐ€ O(n)
9+
* sort๊ฐ€ O(n log n)
10+
* => O(n log n)
11+
*/
12+
var topKFrequent = function (nums, k) {
13+
let result = [];
14+
for (let i = 0; i < nums.length; i++) {
15+
if (result.find((el) => el.key == nums[i])) continue;
16+
const count = nums.reduce((cnt, el) => cnt + (nums[i] === el), 0);
17+
result.push({ key: nums[i], value: count });
18+
}
19+
20+
return result
21+
.sort((a, b) => b.value - a.value)
22+
.slice(0, k)
23+
.map((el) => el.key);
24+
};
25+
26+
/**
27+
* Follow up: Your algorithm's time complexity must be better than O(n log n), where n is the array's size.
28+
*/

โ€Žtwo-sum/sujeong-dev.jsโ€Ž

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22
* @param {number[]} nums
33
* @param {number} target
44
* @return {number[]}
5+
*
6+
* ์‹œ๊ฐ„๋ณต์žก๋„ ๊ณ„์‚ฐ
7+
* ์ž…๋ ฅ ํฌ๊ธฐ(n)๊ฐ€ ์ปค์งˆ ๋•Œ ์—ฐ์‚ฐ ํšŸ์ˆ˜๊ฐ€ n^2์— ๋น„๋ก€ํ•ด์„œ ์ฆ๊ฐ€
8+
* => O(n^2)
9+
*
10+
* ๊ณต๊ฐ„๋ณต์žก๋„ ๊ณ„์‚ฐ
11+
* ์ƒ์ˆ˜ i ํ• ๋‹น
12+
* ์ƒ์ˆ˜ j ํ• ๋‹น
13+
* => o(1)
514
*/
6-
7-
// 2์ค‘ for๋ฌธ: O(n^2)
815
var twoSum = function (nums, target) {
916
for (let i = 0; i < nums.length; i++) {
1017
for (let j = 0; j < nums.length; j++) {
@@ -14,7 +21,20 @@ var twoSum = function (nums, target) {
1421
}
1522
};
1623

17-
// for loof: O(n)
24+
/**
25+
* @param {number[]} nums
26+
* @param {number} target
27+
* @return {number[]}
28+
*
29+
* ์‹œ๊ฐ„๋ณต์žก๋„ ๊ณ„์‚ฐ
30+
* ์ž…๋ ฅ ํฌ๊ธฐ(n)๊ฐ€ ์ปค์งˆ ๋•Œ ์—ฐ์‚ฐ ํšŸ์ˆ˜๊ฐ€ n์— ๋น„๋ก€ํ•ด์„œ ์ฆ๊ฐ€
31+
* => O(n)
32+
*
33+
* ๊ณต๊ฐ„๋ณต์žก๋„ ๊ณ„์‚ฐ
34+
* result๊ฐ€ nums์— ๋น„๋ก€ํ•ด์„œ ํ• ๋‹น
35+
* ์ƒ์ˆ˜ i, findNum ํ• ๋‹น
36+
* => O(n)
37+
*/
1838
var twoSum = function (nums, target) {
1939
let result = {};
2040

0 commit comments

Comments
ย (0)