Skip to content

Commit e55594c

Browse files
authored
Merge pull request #325 from JEONGHWANMIN/main
[ํ™˜๋ฏธ๋‹ˆ๋‹ˆ] Week1 ๋ฌธ์ œํ’€์ด
2 parents bdf08b0 + 074bd58 commit e55594c

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

โ€Žcontains-duplicate/hwanminini.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
2+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
3+
4+
/**
5+
* @param {number[]} nums
6+
* @return {boolean}
7+
*/
8+
var containsDuplicate = function(nums) {
9+
return nums.length !== new Set(nums).size
10+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n log n)
2+
// ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* function TreeNode(val, left, right) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.left = (left===undefined ? null : left)
9+
* this.right = (right===undefined ? null : right)
10+
* }
11+
*/
12+
/**
13+
* @param {TreeNode} root
14+
* @param {number} k
15+
* @return {number}
16+
*/
17+
var kthSmallest = function(root, k) {
18+
const results = []
19+
20+
const dfs = (tree) => {
21+
results.push(tree.val)
22+
if (tree.left) dfs(tree.left)
23+
if (tree.right) dfs(tree.right)
24+
}
25+
26+
dfs(root)
27+
28+
results.sort((a,b) => a - b)
29+
30+
return results[k-1]
31+
};

โ€Žnumber-of-1-bits/hwanmini.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(log n)
2+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(log n)
3+
4+
const replaceZeroToEmptyString = (str) => str.replaceAll('0','')
5+
6+
7+
/**
8+
* @param {number} n
9+
* @return {number}
10+
*/
11+
var hammingWeight = function(n) {
12+
const binaryNum = n.toString(2)
13+
const replacedNumber = replaceZeroToEmptyString(binaryNum)
14+
return replacedNumber.length
15+
};
16+
17+
18+
console.log(hammingWeight(11));
19+
console.log(hammingWeight(128));
20+
console.log(hammingWeight(2147483645));
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n^3)
2+
// ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
3+
4+
/**
5+
* @param {string} s
6+
* @return {number}
7+
*/
8+
var countSubstrings = function(s) {
9+
let count = 0;
10+
11+
let plusIndex = 0;
12+
while (plusIndex !== s.length) {
13+
for (let i = 0 ; i < s.length - plusIndex; i++) {
14+
if (isValidSubstring(s, i, i + plusIndex)) count++
15+
}
16+
17+
plusIndex++;
18+
}
19+
20+
return count;
21+
};
22+
23+
24+
function isValidSubstring(s, left, right) {
25+
while (left <= right) {
26+
if (s[left] !== s[right]) return false;
27+
28+
left++;
29+
right--;
30+
}
31+
return true;
32+
}
33+
34+
console.log(countSubstrings("abc"));
35+
console.log(countSubstrings("aaa"));
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
2+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
3+
4+
/**
5+
* @param {number[]} nums
6+
* @param {number} k
7+
* @return {number[]}
8+
*/
9+
var topKFrequent = function(nums, k) {
10+
const map = new Map()
11+
12+
for (let i = 0; i < nums.length; i++) {
13+
map.set(nums[i], (map.get(nums[i]) || 0) + 1);
14+
}
15+
16+
const buckets = Array.from({length: nums.length + 1}, () => [])
17+
18+
for (const [num, frequency] of map.entries()) {
19+
buckets[frequency].push(num);
20+
}
21+
22+
const result = [];
23+
for (let i = buckets.length - 1; i >= 0 && result.length < k; i--) {
24+
result.push(...buckets[i]);
25+
}
26+
27+
return result
28+
};
29+
30+
console.log(topKFrequent([1,1,1,2,2,3],2))

0 commit comments

Comments
ย (0)