-
-
Notifications
You must be signed in to change notification settings - Fork 195
[선재] WEEK4 Solution #419
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
[선재] WEEK4 Solution #419
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* priority queue + result counting that priority queue was pop | ||
* | ||
* time complexity: O(n log n) | ||
* space complexity: O(n) | ||
*/ | ||
var longestConsecutive = function (nums) { | ||
const queue = new CustomQueue(); | ||
let [count, before, answer] = [0, null, 0]; | ||
|
||
nums.forEach((n) => queue.insert(n)); | ||
|
||
if (queue.size === 0) return count; | ||
|
||
[count, before, answer] = [1, queue.remove(), 1]; | ||
|
||
while (queue.size) { | ||
const current = queue.remove(); | ||
|
||
if (before === current) continue; | ||
|
||
count = before + 1 === current ? count + 1 : 1; | ||
before = current; | ||
answer = Math.max(answer, count); | ||
} | ||
|
||
return answer; | ||
}; | ||
|
||
class Node { | ||
constructor(value) { | ||
this.value = value; | ||
} | ||
} | ||
|
||
class CustomQueue { | ||
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. 브레인 스토밍에서 의견주신것처럼 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. 아 요거는 리트코드에서 미리선언되어있어서 참조에러가 나더라구요 |
||
constructor() { | ||
this.items = new Map(); | ||
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.
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. 동의합니다. |
||
this.size = 0; | ||
} | ||
|
||
parentIndex(index) { | ||
return Math.floor((index - 1) / 2); | ||
} | ||
|
||
leftChildIndex(index) { | ||
return 2 * index + 1; | ||
} | ||
|
||
rightChildIndex(index) { | ||
return 2 * index + 2; | ||
} | ||
|
||
heapifyUp() { | ||
let index = this.size - 1; | ||
|
||
while (index > 0) { | ||
const parentIndex = this.parentIndex(index); | ||
const parent = this.items.get(parentIndex); | ||
const current = this.items.get(index); | ||
|
||
if (parent.value <= current.value) break; | ||
|
||
this.items.set(this.parentIndex(index), current); | ||
this.items.set(index, parent); | ||
|
||
index = parentIndex; | ||
} | ||
} | ||
|
||
heapifyDown() { | ||
let index = 0; | ||
|
||
while (this.leftChildIndex(index) < this.items.size) { | ||
let smallestIndex = this.leftChildIndex(index); | ||
let rightIndex = this.rightChildIndex(index); | ||
const current = this.items.get(index); | ||
|
||
if ( | ||
rightIndex < this.size && | ||
this.items.get(rightIndex).value < this.items.get(smallestIndex).value | ||
) { | ||
smallestIndex = rightIndex; | ||
} | ||
|
||
if (current.value <= this.items.get(smallestIndex).value) break; | ||
this.items.set(index, this.items.get(smallestIndex)); | ||
this.items.set(smallestIndex, current); | ||
index = smallestIndex; | ||
} | ||
} | ||
|
||
insert(value) { | ||
const index = this.size; | ||
const node = new Node(value); | ||
this.items.set(index, node); | ||
this.size++; | ||
this.heapifyUp(); | ||
} | ||
|
||
remove() { | ||
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. 리턴값이 있다는 점에서 |
||
if (this.size === 0) return null; | ||
|
||
const root = this.items.get(0); | ||
this.items.set(0, this.items.get(this.size - 1)); | ||
this.items.delete(this.size - 1); | ||
this.size--; | ||
|
||
this.heapifyDown(); | ||
return root.value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* 1. memoization, recursive | ||
* 2. palindromic-substring custom | ||
*/ | ||
|
||
/** | ||
* brainstorming solve 1 | ||
* result: fail because time limited | ||
* | ||
* time complexity: O(n^2) | ||
* space complexity: O(n^2) | ||
*/ | ||
var maxProduct = function (nums) { | ||
let answer = nums[0]; | ||
|
||
const memo = Array.from({ length: nums.length }, () => | ||
Array.from({ length: nums.length }, () => null) | ||
); | ||
|
||
const recursive = (left, right) => { | ||
if (memo[left][right] !== null) return memo[left][right]; | ||
|
||
if (left === right) { | ||
memo[left][right] = nums[left]; | ||
answer = Math.max(answer, nums[left]); | ||
return nums[left]; | ||
} | ||
|
||
const removedLeft = recursive(left + 1, right); | ||
recursive(left, right - 1); | ||
|
||
memo[left][right] = nums[left] * removedLeft; | ||
|
||
answer = Math.max(answer, memo[left][right]); | ||
|
||
return removedLeft; | ||
}; | ||
|
||
recursive(0, nums.length - 1); | ||
|
||
return answer; | ||
}; | ||
|
||
/** | ||
* brainstorming solve 2 | ||
* result: fail because time limited | ||
* | ||
* time complexity: O(n^2) | ||
* space complexity: O(n) | ||
*/ | ||
var maxProduct = function (nums) { | ||
let answer = nums[0]; | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
let [start, end] = [i, i]; | ||
let product = nums[i]; | ||
|
||
answer = Math.max(answer, product); | ||
while (start >= 0 && end < nums.length) { | ||
if (start !== end) product = product * nums[start] * nums[end]; | ||
|
||
answer = Math.max(answer, product); | ||
[start, end] = [start - 1, end + 1]; | ||
} | ||
|
||
product = nums[i]; | ||
[start, end] = [i, i + 1]; | ||
|
||
while (start >= 0 && end < nums.length) { | ||
if (start + 1 === end) product = product * nums[end]; | ||
else product = product * nums[start] * nums[end]; | ||
|
||
answer = Math.max(answer, product); | ||
[start, end] = [start - 1, end + 1]; | ||
} | ||
} | ||
|
||
return answer; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* hash map | ||
* | ||
* time complexity: O(n) | ||
* space complexity: O(n) | ||
*/ | ||
var missingNumber = function (nums) { | ||
const map = new Map(); | ||
for (let i = 0; i < nums.length; i++) { | ||
map.set(nums[i], i); | ||
} | ||
|
||
for (let i = 0; i <= nums.length; i++) { | ||
if (!map.has(i)) return i; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* string method + two pointer | ||
* | ||
* time complexity: O(n) | ||
* space complexity: O(n) | ||
*/ | ||
|
||
var isPalindrome = function (s) { | ||
const asciiArray = []; | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
const asciiCode = s[i].toLowerCase().charCodeAt(0); | ||
const isNumber = asciiCode >= 48 && asciiCode <= 57; | ||
const isLowerCase = asciiCode >= 97 && asciiCode <= 122; | ||
|
||
if (isNumber || isLowerCase) asciiArray.push(asciiCode); | ||
} | ||
|
||
const len = asciiArray.length; | ||
const middleIndex = Math.floor(len); | ||
|
||
for (let i = 0; i < middleIndex; i++) { | ||
if (asciiArray[i] !== asciiArray[len - i - 1]) return false; | ||
} | ||
|
||
return true; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* dfs | ||
* | ||
* time complexity: O(n^2 * 4^word.length) | ||
* space complexity: O(n^2) | ||
*/ | ||
|
||
var exist = function (board, word) { | ||
let answer = false; | ||
const ROW_LENGTH = board.length; | ||
const COLUMN_LENGTH = board[0].length; | ||
// O(n^2) | ||
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. 이 부분은, 보드가 정사각형이라는 언급이 없으므로 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. 그렇군요! 보통 m x n으로 표현하는지 몰랐는데 알게됐네요 감사합니다! |
||
const visited = Array.from({ length: ROW_LENGTH }, () => | ||
Array.from({ length: COLUMN_LENGTH }, () => false) | ||
); | ||
|
||
const isRange = (r, c) => | ||
r >= 0 && r < ROW_LENGTH && c >= 0 && c < COLUMN_LENGTH && !visited[r][c]; | ||
|
||
const search = (r, c, currentWord) => { | ||
if (answer) return; | ||
if (currentWord.length > word.length) return; | ||
if (!word.includes(currentWord)) return; | ||
if (currentWord === word) { | ||
answer = true; | ||
return; | ||
} | ||
|
||
const dr = [-1, 0, 0, 1]; | ||
const dc = [0, -1, 1, 0]; | ||
|
||
for (let i = 0; i < 4; i++) { | ||
const nextR = r + dr[i]; | ||
const nextC = c + dc[i]; | ||
|
||
if (!isRange(nextR, nextC)) continue; | ||
|
||
const nextWord = currentWord + board[nextR][nextC]; | ||
|
||
visited[nextR][nextC] = true; | ||
search(nextR, nextC, nextWord); | ||
visited[nextR][nextC] = false; | ||
} | ||
}; | ||
|
||
for (let r = 0; r < ROW_LENGTH; r++) { | ||
for (let c = 0; c < COLUMN_LENGTH; c++) { | ||
visited[r][c] = true; | ||
search(r, c, board[r][c]); | ||
visited[r][c] = false; | ||
} | ||
} | ||
|
||
return answer; | ||
}; |
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.
오 구조분해할당을 처음 할당할 때에도 쓰면 좋네요