-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[환미니니] Week1 문제풀이 #325
Merged
Merged
[환미니니] Week1 문제풀이 #325
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9e6d539
feat: hammingWeight 문제풀이 추가
JEONGHWANMIN 7011808
feat: topKFrequent 문제풀이 추가
JEONGHWANMIN 5d155da
feat: containsDuplicate 문제풀이 추가
JEONGHWANMIN ceeb13f
Merge branch 'DaleStudy:main' into main
JEONGHWANMIN 677c2c0
feat: last-line 추가
JEONGHWANMIN ebd47a6
Merge remote-tracking branch 'origin/main'
JEONGHWANMIN d616ef2
feat: 문제풀이 추가
JEONGHWANMIN 1d00102
feat: o(n)문제풀이 방식 적용
JEONGHWANMIN bfebdd2
feat: slice 삭제
JEONGHWANMIN 400b4d6
feat: line break 추가
JEONGHWANMIN 81e202d
feat: sort 추가
JEONGHWANMIN 074bd58
feat: 시간 복잡도 수정
JEONGHWANMIN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) | ||
// 공간 복잡도: 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) | ||
gitsunmin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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")); |
gitsunmin marked this conversation as resolved.
Show resolved
Hide resolved
|
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)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
해당 문제 이렇게 작성했을 때 올바르게 동작하나요?! root.left가 배열로 나오는 경우들이 있는데, 그런 경우들에는 동작하지 않을 것 같아 질문드립니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
기존에 console.log 삭제하면서 sort가 삭제되어서 없어서 추가했고
leetcode 제출 시 문제는 없었던 거 같아요..!