-
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 #983 from gwbaik9717/main
[ganu] Week9
- Loading branch information
Showing
5 changed files
with
291 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 @@ | ||
// Time complexity: O(logn) | ||
// Space complexity: O(1) | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var findMin = function (nums) { | ||
let left = 0; | ||
let right = nums.length - 1; | ||
|
||
while (left < right) { | ||
const mid = Math.floor((left + right) / 2); | ||
|
||
if (nums.at(mid - 1) > nums.at(mid)) { | ||
return nums[mid]; | ||
} | ||
|
||
if (nums.at(mid + 1) < nums.at(mid)) { | ||
return nums[mid + 1]; | ||
} | ||
|
||
if (nums.at(mid + 1) > nums.at(right)) { | ||
left = mid + 1; | ||
continue; | ||
} | ||
|
||
right = mid - 1; | ||
} | ||
|
||
return nums[left]; | ||
}; |
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,22 @@ | ||
// Time complexity: O(n) | ||
// Space complexity: O(1) | ||
|
||
/** | ||
* @param {ListNode} head | ||
* @return {boolean} | ||
*/ | ||
var hasCycle = function (head) { | ||
let turtle = head; | ||
let rabbit = head; | ||
|
||
while (turtle && rabbit && rabbit.next) { | ||
turtle = turtle.next; | ||
rabbit = rabbit.next.next; | ||
|
||
if (turtle === rabbit) { | ||
return true; | ||
} | ||
} | ||
|
||
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,31 @@ | ||
// Time complexity: O(n) | ||
// Space complexity: O(n) | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var maxProduct = function (nums) { | ||
let answer = nums[0]; | ||
const products = Array.from({ length: nums.length + 1 }, () => null); | ||
products[0] = [1, 1]; // [min, max] | ||
|
||
for (let i = 1; i < products.length; i++) { | ||
const cases = []; | ||
|
||
// case 1 | ||
cases.push(nums[i - 1]); | ||
|
||
// case 2 | ||
cases.push(nums[i - 1] * products[i - 1][0]); | ||
|
||
// case 3 | ||
cases.push(nums[i - 1] * products[i - 1][1]); | ||
|
||
const product = [Math.min(...cases), Math.max(...cases)]; | ||
products[i] = product; | ||
answer = Math.max(answer, product[1]); | ||
} | ||
|
||
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,75 @@ | ||
// n: len(s), m: len(t) | ||
// Time complexity: O(n+m) | ||
// Space complexity: O(n+m) | ||
|
||
/** | ||
* @param {string} s | ||
* @param {string} t | ||
* @return {string} | ||
*/ | ||
var minWindow = function (s, t) { | ||
let i = 0; | ||
let j = 0; | ||
|
||
const current = new Map(); | ||
const target = new Map(); | ||
|
||
current.set(s[i], 1); | ||
|
||
for (const chr of t) { | ||
if (target.has(chr)) { | ||
target.set(chr, target.get(chr) + 1); | ||
continue; | ||
} | ||
|
||
target.set(chr, 1); | ||
} | ||
|
||
let answer = null; | ||
|
||
while (i < s.length) { | ||
let pass = true; | ||
for (const [key, value] of target) { | ||
if (!current.has(key)) { | ||
pass = false; | ||
break; | ||
} | ||
|
||
if (current.get(key) < value) { | ||
pass = false; | ||
break; | ||
} | ||
} | ||
|
||
if (pass) { | ||
if (!answer) { | ||
answer = s.slice(i, j + 1); | ||
} | ||
|
||
if (answer && j - i + 1 < answer.length) { | ||
answer = s.slice(i, j + 1); | ||
} | ||
|
||
current.set(s[i], current.get(s[i]) - 1); | ||
if (current.get(s[i]) === 0) { | ||
current.delete(s[i]); | ||
} | ||
i++; | ||
|
||
continue; | ||
} | ||
|
||
if (j < s.length) { | ||
j++; | ||
current.set(s[j], (current.get(s[j]) || 0) + 1); | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
if (answer === null) { | ||
return ""; | ||
} | ||
|
||
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,131 @@ | ||
// n: height, m: width | ||
// Time complexity: O(n*m) | ||
// Space complexity: O(n*m) | ||
|
||
class _Queue { | ||
constructor() { | ||
this.q = []; | ||
this.start = 0; | ||
this.end = 0; | ||
} | ||
|
||
isEmpty() { | ||
return this.start === this.end; | ||
} | ||
|
||
push(value) { | ||
this.q.push(value); | ||
this.end++; | ||
} | ||
|
||
pop() { | ||
const rv = this.q[this.start]; | ||
delete this.q[this.start++]; | ||
return rv; | ||
} | ||
} | ||
/** | ||
* @param {number[][]} heights | ||
* @return {number[][]} | ||
*/ | ||
var pacificAtlantic = function (heights) { | ||
const dy = [1, 0, -1, 0]; | ||
const dx = [0, 1, 0, -1]; | ||
|
||
const h = heights.length; | ||
const w = heights[0].length; | ||
const checked = Array.from({ length: h }, () => | ||
Array.from({ length: w }, () => [false, false]) | ||
); | ||
|
||
// 태평양 | ||
const pacific = new _Queue(); | ||
|
||
for (let i = 0; i < h; i++) { | ||
if (!checked[i][0][0]) { | ||
pacific.push([i, 0]); | ||
checked[i][0][0] = true; | ||
} | ||
} | ||
|
||
for (let i = 0; i < w; i++) { | ||
if (!checked[0][i][0]) { | ||
pacific.push([0, i]); | ||
checked[0][i][0] = true; | ||
} | ||
} | ||
|
||
while (!pacific.isEmpty()) { | ||
const [cy, cx] = pacific.pop(); | ||
|
||
for (let i = 0; i < dy.length; i++) { | ||
const ny = cy + dy[i]; | ||
const nx = cx + dx[i]; | ||
|
||
if ( | ||
ny >= 0 && | ||
ny < h && | ||
nx >= 0 && | ||
nx < w && | ||
!checked[ny][nx][0] && | ||
heights[ny][nx] >= heights[cy][cx] | ||
) { | ||
checked[ny][nx][0] = true; | ||
pacific.push([ny, nx]); | ||
} | ||
} | ||
} | ||
|
||
// 대서양 | ||
const answer = []; | ||
const atlantic = new _Queue(); | ||
|
||
for (let i = 0; i < h; i++) { | ||
if (!checked[i][w - 1][1]) { | ||
atlantic.push([i, w - 1]); | ||
checked[i][w - 1][1] = true; | ||
} | ||
|
||
if (checked[i][w - 1][0] === true) { | ||
answer.push([i, w - 1]); | ||
} | ||
} | ||
|
||
for (let i = 0; i < w; i++) { | ||
if (!checked[h - 1][i][1]) { | ||
atlantic.push([h - 1, i]); | ||
checked[h - 1][i][1] = true; | ||
|
||
if (checked[h - 1][i][0] === true) { | ||
answer.push([h - 1, i]); | ||
} | ||
} | ||
} | ||
|
||
while (!atlantic.isEmpty()) { | ||
const [cy, cx] = atlantic.pop(); | ||
|
||
for (let i = 0; i < dy.length; i++) { | ||
const ny = cy + dy[i]; | ||
const nx = cx + dx[i]; | ||
|
||
if ( | ||
ny >= 0 && | ||
ny < h && | ||
nx >= 0 && | ||
nx < w && | ||
!checked[ny][nx][1] && | ||
heights[ny][nx] >= heights[cy][cx] | ||
) { | ||
if (checked[ny][nx][0] === true) { | ||
answer.push([ny, nx]); | ||
} | ||
|
||
checked[ny][nx][1] = true; | ||
atlantic.push([ny, nx]); | ||
} | ||
} | ||
} | ||
|
||
return answer; | ||
}; |