-
-
Notifications
You must be signed in to change notification settings - Fork 248
[highball] week4 solutions #422
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
Changes from all commits
Commits
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,32 @@ | ||
//time-complexity : O(n) | ||
//space-complexity : O(n) | ||
|
||
const longestConsecutive = function (nums) { | ||
let longest = 0; | ||
const set = new Set(nums); | ||
|
||
while (set.size > 0) { | ||
let count = 0; | ||
const originalSeed = set.values().next().value; //set의 첫 번째 원소 get | ||
|
||
seed = originalSeed; | ||
|
||
while (set.has(seed)) { | ||
set.delete(seed); | ||
count++; | ||
seed += 1; | ||
} | ||
|
||
seed = originalSeed - 1; | ||
|
||
while (set.has(seed)) { | ||
set.delete(seed); | ||
count++; | ||
seed -= 1; | ||
} | ||
|
||
if (count > longest) longest = count; | ||
} | ||
|
||
return longest; | ||
}; | ||
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,84 @@ | ||
//time-complexity : O(n) | ||
//space-complexity : O(n) | ||
|
||
const maxProduct = function (nums) { | ||
let subarrays = []; | ||
let subarray = []; | ||
let zeroCount = 0; | ||
|
||
/* -------------------------------------------------------------------------- */ | ||
/* nonzero subarray 잘라서 subarrays에 넣기 */ | ||
/* -------------------------------------------------------------------------- */ | ||
for (let i = 0; i < nums.length; i++) { | ||
if (nums[i] === 0) { | ||
if (subarray.length > 0) { | ||
subarrays.push([...subarray]); | ||
subarray.length = 0; //각 element의 metadata를 garbage collection에 들어가야 하는 것으로 marking할 뿐이므로 O(1)! | ||
} | ||
zeroCount++; | ||
continue; | ||
} | ||
subarray.push(nums[i]); | ||
} | ||
if (subarray.length > 0) { | ||
subarrays.push([...subarray]); | ||
} | ||
|
||
/* -------------------------------------------------------------------------- */ | ||
/* 각 subarray의 maxProduct들 중 최대값 리턴하기 */ | ||
/* -------------------------------------------------------------------------- */ | ||
if (zeroCount === 0) { | ||
return maxProductNonzero(nums); | ||
} else { | ||
let max = 0; | ||
for (let i = 0; i < subarrays.length; i++) { | ||
max = Math.max(maxProductNonzero(subarrays[i]), max); | ||
} | ||
return max; | ||
} | ||
}; | ||
|
||
const maxProductNonzero = function (nonZeroNums) { | ||
if (nonZeroNums.length === 1) return nonZeroNums[0]; //firstNegativeIndex = lastNegativeIndex = 0이 되어 버려서 frontProduct와 backProduct 중 어느 것도 계산이 안 돼버리므로 early return으로 처리. | ||
|
||
let firstNegativeIndex = -1; | ||
let lastNegativeIndex = -1; | ||
let negativeCount = 0; | ||
for (let i = 0; i < nonZeroNums.length; i++) { | ||
if (nonZeroNums[i] < 0 && firstNegativeIndex !== lastNegativeIndex) { | ||
lastNegativeIndex = i; | ||
negativeCount++; | ||
} | ||
if (nonZeroNums[i] < 0 && firstNegativeIndex === lastNegativeIndex) { | ||
firstNegativeIndex = i; | ||
negativeCount++; | ||
} | ||
} | ||
|
||
if (negativeCount === 1) { | ||
lastNegativeIndex = firstNegativeIndex; | ||
} | ||
|
||
if (negativeCount % 2 === 0) { | ||
/* -------------------------------------------------------------------------- */ | ||
/* 음수 개수가 짝수 개면 그냥 전체 곱한 게 최대값임 */ | ||
/* -------------------------------------------------------------------------- */ | ||
let product = 1; | ||
for (let i = 0; i < nonZeroNums.length; i++) { | ||
product *= nonZeroNums[i]; | ||
} | ||
return product; | ||
} else { | ||
/* -------------------------------------------------------------------------- */ | ||
/* 음수 개수가 홀수 개면 처음부터 lastNegativeIndex 직전까지 곱한 수 혹은 */ | ||
/* firstNegativeIndex부터 끝까지 곱한 수 중 하나가 최대값임 */ | ||
/* -------------------------------------------------------------------------- */ | ||
let frontProduct = 1; | ||
let backProduct = 1; | ||
for (let i = 0; i < nonZeroNums.length; i++) { | ||
if (i < lastNegativeIndex) frontProduct *= nonZeroNums[i]; | ||
if (i > firstNegativeIndex) backProduct *= nonZeroNums[i]; | ||
} | ||
return Math.max(frontProduct, backProduct); | ||
} | ||
}; |
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,8 @@ | ||
//time-complexity : O(n) | ||
//space-complexity : O(1) | ||
|
||
const missingNumber = function (nums) { | ||
const sum = nums.reduce((acc, cur) => acc + cur); | ||
const n = nums.length; | ||
return (n * (n + 1)) / 2 - sum; | ||
}; |
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,27 @@ | ||
//time-complexity : O(n) | ||
//space-complexity : O(1) | ||
|
||
const isPalindrome = function (s) { | ||
let left = 0; | ||
let right = s.length - 1; | ||
while (left < right) { | ||
if (isAlphaNumeric(s[left]) && isAlphaNumeric(s[right])) { | ||
if (s[left].toLowerCase() === s[right].toLowerCase()) { | ||
left++; | ||
right--; | ||
} else return false; | ||
} else { | ||
if (!isAlphaNumeric(s[left])) left++; | ||
if (!isAlphaNumeric(s[right])) right--; | ||
} | ||
} | ||
return true; | ||
}; | ||
|
||
const isAlphaNumeric = function (c) { | ||
return ( | ||
(c.charCodeAt(0) >= 48 && c.charCodeAt(0) <= 57) || | ||
(c.charCodeAt(0) >= 65 && c.charCodeAt(0) <= 90) || | ||
(c.charCodeAt(0) >= 97 && c.charCodeAt(0) <= 122) | ||
); | ||
}; |
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,42 @@ | ||
//time-complexity : O(4^(ROWS*COLS)) | ||
//space-complexity : O(word.length) | ||
|
||
const exist = function (board, word) { | ||
const ROWS = board.length; | ||
const COLS = board[0].length; | ||
|
||
for (let r = 0; r < ROWS; r++) { | ||
for (let c = 0; c < COLS; c++) { | ||
if (board[r][c] === word[0]) { | ||
if (dfs(board, word, r, c, 0)) return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
const dfs = function (board, word, r, c, i) { | ||
const ROWS = board.length; | ||
const COLS = board[0].length; | ||
|
||
if (r < 0 || r >= ROWS || c < 0 || c >= COLS || board[r][c] !== word[i]) { | ||
return false; | ||
} | ||
|
||
if (i === word.length - 1) return true; | ||
|
||
const temp = board[r][c]; | ||
board[r][c] = null; | ||
|
||
let found = | ||
dfs(board, word, r + 0, c + 1, i + 1) || | ||
dfs(board, word, r + 1, c + 0, i + 1) || | ||
dfs(board, word, r + 0, c + -1, i + 1) || | ||
dfs(board, word, r + -1, c + 0, i + 1); | ||
//forEach로 했더니 runtime 두 배 됨;; | ||
|
||
board[r][c] = temp; | ||
|
||
return found; | ||
}; |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.