-
-
Notifications
You must be signed in to change notification settings - Fork 194
[환미니니] Week4 문제풀이 #425
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
Merged
Merged
[환미니니] Week4 문제풀이 #425
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,29 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var longestConsecutive = function(nums) { | ||
if (nums.length === 0) return 0; | ||
|
||
const numSet = new Set(nums); | ||
let maxSequenceLength = 0; | ||
|
||
for (const num of numSet) { | ||
if (!numSet.has(num - 1)) { | ||
let currentNum = num; | ||
let currentLength = 1; | ||
|
||
while (numSet.has(currentNum + 1)) { | ||
currentNum++; | ||
currentLength++; | ||
} | ||
|
||
maxSequenceLength = Math.max(maxSequenceLength, currentLength); | ||
} | ||
} | ||
|
||
return maxSequenceLength; | ||
}; | ||
|
||
console.log(longestConsecutive([100, 4, 200, 1, 3, 2])); | ||
console.log(longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1])); |
This file contains hidden or 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,19 @@ | ||
// 시간복잡도 O(n log n) | ||
// 공간복잡도 O(n) | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var missingNumber = function(nums) { | ||
nums.sort((a,b) => a - b); | ||
|
||
for (let i = 0 ; i <= nums.length; i++) { | ||
if (i !== nums[i]) return i | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 숫자가 0부터 시작하니까 이렇게 비교해서 풀어도 좋겠네요! 배워갑니다 :) |
||
} | ||
|
||
}; | ||
|
||
console.log(missingNumber([3, 0, 1])) | ||
console.log(missingNumber([0, 1])) | ||
console.log(missingNumber([9,6,4,2,3,5,7,0,1])) |
This file contains hidden or 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,29 @@ | ||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(n) | ||
|
||
/** | ||
* @param {string} s | ||
* @return {boolean} | ||
*/ | ||
var isPalindrome = function(s) { | ||
const strs = s.replace(/[^a-z0-9]/gi, '').toLowerCase(); | ||
|
||
let leftIdx = 0; | ||
let rightIdx = strs.length - 1 | ||
|
||
while (leftIdx <= rightIdx) { | ||
if (strs[leftIdx] !== strs[rightIdx]) return false | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오 양쪽에서 인덱스를 증가시키거나 감소시키면서 하나씩 비교하니 풀이가 굉장히 깔끔하네요! |
||
leftIdx++ | ||
rightIdx-- | ||
} | ||
|
||
|
||
return true | ||
}; | ||
|
||
const s = "A man, a plan, a canal: Panama" | ||
|
||
|
||
console.log(isPalindrome(s)) | ||
|
This file contains hidden or 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,69 @@ | ||
|
||
// m은 board의 행 수, n은 board의 열 수, 4(상하좌우), l(word) | ||
|
||
// 시간복잡도: O(m * n * 4L) | ||
// 공간복잡도: O(m * n) | ||
|
||
const d_row = [1, -1, 0, 0] | ||
const d_col = [0, 0, -1, 1] | ||
|
||
|
||
const isMoveBoard = (new_row, new_col, board) => { | ||
return new_row >= 0 && new_row < board.length && new_col >= 0 && new_col < board[0].length | ||
} | ||
|
||
|
||
/** | ||
* @param {character[][]} board | ||
* @param {string} word | ||
* @return {boolean} | ||
*/ | ||
var exist = function(board, word) { | ||
let result = false | ||
|
||
const visited = Array.from({length: board.length} , () => Array.from({length: board[0].length}).fill(false)) | ||
|
||
const dfs = (strs, row, col, count) => { | ||
|
||
if (strs[count] !== word[count]) return | ||
if (strs.length > word.length) return | ||
|
||
if (strs === word) { | ||
result = true | ||
return | ||
} | ||
|
||
for (let i = 0 ; i < d_row.length; i++) { | ||
const new_row = row + d_row[i] | ||
const new_col = col + d_col[i] | ||
|
||
if (isMoveBoard(new_row, new_col, board) && visited[new_row][new_col] !== true){ | ||
visited[new_row][new_col] = true | ||
dfs(strs+board[new_row][new_col], new_row, new_col, count+1) | ||
visited[new_row][new_col] = false | ||
} | ||
} | ||
|
||
|
||
|
||
} | ||
|
||
|
||
for (let row = 0 ; row < board.length ; row++) { | ||
for (let col = 0 ; col < board[0].length ; col++) { | ||
visited[row][col] = true | ||
dfs(board[row][col], row, col , 0) | ||
visited[row][col] = false | ||
} | ||
} | ||
|
||
|
||
return result | ||
}; | ||
|
||
console.log(exist([["A","B","C","E"], | ||
["S","F","C","S"], | ||
["A","D","E","E"]], "ABCCED")) | ||
console.log(exist([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "SEE")) | ||
console.log(exist([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCB")) | ||
|
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.
js에서 sort는 내부적으로 추가적인 메모리를 사용해서 공간복잡도가 O(1)은 아닐 것 같습니다!
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.
엇 그렇네요 정렬할 때 공간복잡도가 On 사용됩니다!
감사합니다 : )