Skip to content

Commit

Permalink
Merge pull request #515 from Sunjae95/main
Browse files Browse the repository at this point in the history
[선재] WEEK09 Solution
  • Loading branch information
Sunjae95 authored Oct 12, 2024
2 parents 359ae2c + 8621430 commit 0b457eb
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 0 deletions.
32 changes: 32 additions & 0 deletions find-minimum-in-rotated-sorted-array/sunjae95.js
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;
};
21 changes: 21 additions & 0 deletions linked-list-cycle/sunjae95.js
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;
};
23 changes: 23 additions & 0 deletions maximum-subarray/sunjae95.js
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;
};
41 changes: 41 additions & 0 deletions minimum-window-substring/sunjae.js
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;
};
74 changes: 74 additions & 0 deletions pacific-atlantic-water-flow/sunjae95.js
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()];
};

0 comments on commit 0b457eb

Please sign in to comment.