-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #325 from JEONGHWANMIN/main
[ํ๋ฏธ๋๋] Week1 ๋ฌธ์ ํ์ด
- Loading branch information
Showing
5 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// ์๊ฐ๋ณต์ก๋: O(n) | ||
// ๊ณต๊ฐ๋ณต์ก๋: O(n) | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
*/ | ||
var containsDuplicate = function(nums) { | ||
return nums.length !== new Set(nums).size | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// ์๊ฐ ๋ณต์ก๋: O(n log n) | ||
// ๊ณต๊ฐ ๋ณต์ก๋: O(n) | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* function TreeNode(val, left, right) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.left = (left===undefined ? null : left) | ||
* this.right = (right===undefined ? null : right) | ||
* } | ||
*/ | ||
/** | ||
* @param {TreeNode} root | ||
* @param {number} k | ||
* @return {number} | ||
*/ | ||
var kthSmallest = function(root, k) { | ||
const results = [] | ||
|
||
const dfs = (tree) => { | ||
results.push(tree.val) | ||
if (tree.left) dfs(tree.left) | ||
if (tree.right) dfs(tree.right) | ||
} | ||
|
||
dfs(root) | ||
|
||
results.sort((a,b) => a - b) | ||
|
||
return results[k-1] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// ์๊ฐ๋ณต์ก๋: O(log n) | ||
// ๊ณต๊ฐ๋ณต์ก๋: O(log n) | ||
|
||
const replaceZeroToEmptyString = (str) => str.replaceAll('0','') | ||
|
||
|
||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var hammingWeight = function(n) { | ||
const binaryNum = n.toString(2) | ||
const replacedNumber = replaceZeroToEmptyString(binaryNum) | ||
return replacedNumber.length | ||
}; | ||
|
||
|
||
console.log(hammingWeight(11)); | ||
console.log(hammingWeight(128)); | ||
console.log(hammingWeight(2147483645)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// ์๊ฐ ๋ณต์ก๋: O(n^3) | ||
// ๊ณต๊ฐ ๋ณต์ก๋: O(1) | ||
|
||
/** | ||
* @param {string} s | ||
* @return {number} | ||
*/ | ||
var countSubstrings = function(s) { | ||
let count = 0; | ||
|
||
let plusIndex = 0; | ||
while (plusIndex !== s.length) { | ||
for (let i = 0 ; i < s.length - plusIndex; i++) { | ||
if (isValidSubstring(s, i, i + plusIndex)) count++ | ||
} | ||
|
||
plusIndex++; | ||
} | ||
|
||
return count; | ||
}; | ||
|
||
|
||
function isValidSubstring(s, left, right) { | ||
while (left <= right) { | ||
if (s[left] !== s[right]) return false; | ||
|
||
left++; | ||
right--; | ||
} | ||
return true; | ||
} | ||
|
||
console.log(countSubstrings("abc")); | ||
console.log(countSubstrings("aaa")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// ์๊ฐ๋ณต์ก๋: O(n) | ||
// ๊ณต๊ฐ๋ณต์ก๋: O(n) | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @param {number} k | ||
* @return {number[]} | ||
*/ | ||
var topKFrequent = function(nums, k) { | ||
const map = new Map() | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
map.set(nums[i], (map.get(nums[i]) || 0) + 1); | ||
} | ||
|
||
const buckets = Array.from({length: nums.length + 1}, () => []) | ||
|
||
for (const [num, frequency] of map.entries()) { | ||
buckets[frequency].push(num); | ||
} | ||
|
||
const result = []; | ||
for (let i = buckets.length - 1; i >= 0 && result.length < k; i--) { | ||
result.push(...buckets[i]); | ||
} | ||
|
||
return result | ||
}; | ||
|
||
console.log(topKFrequent([1,1,1,2,2,3],2)) |