-
-
Notifications
You must be signed in to change notification settings - Fork 125
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 #515 from Sunjae95/main
[선재] WEEK09 Solution
- Loading branch information
Showing
5 changed files
with
191 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,32 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* brute force | ||
* | ||
* n = length of head | ||
* time complexity: O(n) | ||
* space complexity: O(1) | ||
*/ | ||
var findMin = function (nums) { | ||
let answer = 5000; | ||
nums.forEach((num) => (answer = Math.min(answer, num))); | ||
|
||
return answer; | ||
}; | ||
|
||
/* n = length of head | ||
* time complexity: O(n) | ||
* space complexity: O(1) | ||
*/ | ||
var findMin = function (nums) { | ||
let answer = nums[0]; | ||
if (nums.length === 1) return answer; | ||
if (answer < nums[nums.length - 1]) return answer; | ||
|
||
for (let i = nums.length - 1; i >= 0; i--) { | ||
if (answer < nums[i]) return answer; | ||
answer = nums[i]; | ||
} | ||
|
||
return answer; | ||
}; |
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,21 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* hash table | ||
* | ||
* n = length of head | ||
* time complexity: O(n) | ||
* space complexity: O(n) | ||
*/ | ||
var hasCycle = function (head) { | ||
const set = new Set(); | ||
let node = head; | ||
|
||
while (node) { | ||
if (set.has(node)) return true; | ||
set.add(node); | ||
node = node.next; | ||
} | ||
|
||
return false; | ||
}; |
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,23 @@ | ||
/** | ||
* @description | ||
* n = length of nums | ||
* time complexity: O(n) | ||
* space complexity: O(1) | ||
*/ | ||
var maxSubArray = function (nums) { | ||
let sum = 0; | ||
let answer = nums[0]; | ||
|
||
for (let end = 0; end < nums.length; end++) { | ||
if (sum < 0) { | ||
sum = nums[end]; | ||
} else if (sum + nums[end] >= 0) { | ||
sum += nums[end]; | ||
} else { | ||
sum = nums[end]; | ||
} | ||
|
||
answer = Math.max(answer, sum); | ||
} | ||
return answer; | ||
}; |
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,41 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* two pointer + hash table | ||
* | ||
* n = length of s | ||
* m = length of t | ||
* time complexity: O(n*n) | ||
* space complexity: O(n) | ||
*/ | ||
var minWindow = function (s, t) { | ||
const requiredMap = t.split("").reduce((map, char) => { | ||
map.set(char, (map.get(char) ?? 0) + 1); | ||
return map; | ||
}, new Map()); | ||
const requiredArray = [...requiredMap.entries()]; | ||
const map = new Map(); | ||
const successRequired = () => | ||
requiredArray.every(([key, value]) => map.get(key) >= value); | ||
|
||
let answer = ""; | ||
let start = 0; | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
if (requiredMap.has(s[i])) map.set(s[i], (map.get(s[i]) ?? 0) + 1); | ||
|
||
while (successRequired()) { | ||
const now = s.slice(start, i + 1); | ||
answer = answer === "" || answer.length >= now.length ? now : answer; | ||
|
||
if (map.has(s[start])) { | ||
map.set(s[start], map.get(s[start]) - 1); | ||
if (map.get(s[start]) === -1) map.delete(s[start]); | ||
} | ||
|
||
start++; | ||
} | ||
} | ||
|
||
return answer; | ||
}; |
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,74 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* bfs + memoization | ||
* | ||
* n = length of height | ||
* m = length of height[i] | ||
* time complexity: O(n*m * 4^n*m) | ||
* space complexity: O(n*m) | ||
*/ | ||
var pacificAtlantic = function (heights) { | ||
const visited = Array.from({ length: heights.length }, (_, i) => | ||
Array.from({ length: heights[i].length }, () => false) | ||
); | ||
const memo = new Map(); | ||
|
||
const bfs = (row, column) => { | ||
const dr = [0, 0, -1, 1]; | ||
const dc = [1, -1, 0, 0]; | ||
const queue = [[row, column]]; | ||
let [pacific, atlantic] = [false, false]; | ||
let queueIndex = 0; | ||
|
||
while (queueIndex !== queue.length) { | ||
const currentLastLength = queue.length; | ||
|
||
while (queueIndex !== currentLastLength) { | ||
const [r, c] = queue[queueIndex++]; | ||
visited[r][c] = `${row},${column}`; | ||
|
||
if (memo.has(`${r},${c}`)) { | ||
memo.set(`${row},${column}`, [row, column]); | ||
return; | ||
} | ||
|
||
for (let i = 0; i < 4; i++) { | ||
const nextR = r + dr[i]; | ||
const nextC = c + dc[i]; | ||
const isPacific = nextR === -1 || nextC === -1; | ||
const isAtlantic = | ||
nextR === heights.length || nextC === heights[0].length; | ||
|
||
if (isPacific) { | ||
pacific = true; | ||
continue; | ||
} | ||
if (isAtlantic) { | ||
atlantic = true; | ||
continue; | ||
} | ||
|
||
if (visited[nextR][nextC] === `${row},${column}`) continue; | ||
|
||
if (heights[r][c] < heights[nextR][nextC]) continue; | ||
|
||
queue.push([nextR, nextC]); | ||
} | ||
} | ||
|
||
if (pacific && atlantic) { | ||
memo.set(`${row},${column}`, [row, column]); | ||
return; | ||
} | ||
} | ||
}; | ||
|
||
for (let row = 0; row < heights.length; row++) { | ||
for (let column = 0; column < heights[row].length; column++) { | ||
bfs(row, column); | ||
} | ||
} | ||
|
||
return [...memo.values()]; | ||
}; |