Skip to content

[hsskey] WEEK 09 solutions #1535

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 3 commits into from
Jun 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions linked-list-cycle/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/

/**
* @param {ListNode} head
* @return {boolean}
*/
var hasCycle = function(head) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 var 대신 let/const를 사용하는 걸 권장합니다.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} head
 * @return {boolean}
 */
var hasCycle = function(head) {
    
};

아 위와같이 리트코드 문제에 나온내용이라 코드는 구현부와 같은것으로 말씀드려요

let slow = head;
let fast = head;

while (fast !== null && fast.next !== null) {
slow = slow.next;
fast = fast.next.next;

if (slow === fast) {
return true;
}
}

return false;
};
25 changes: 25 additions & 0 deletions maximum-product-subarray/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @param {number[]} nums
* @return {number}
*/
var maxProduct = function(nums) {
let res = Math.max(...nums);
let curMin = 1;
let curMax = 1;

for (let n of nums) {
if (n === 0) {
curMin = 1;
curMax = 1;
continue;
}

let tmp = curMax * n;
curMax = Math.max(n * curMax, n * curMin, n);
curMin = Math.min(tmp, n * curMin, n);

res = Math.max(res, curMax);
}

return res;
};
55 changes: 55 additions & 0 deletions minimum-window-substring/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @param {string} s
* @param {string} t
* @return {string}
*/
var minWindow = function(s, t) {
if (s.length === 0 || t.length === 0) return "";

const dictT = {};
for (let char of t) {
dictT[char] = (dictT[char] || 0) + 1;
}

const required = Object.keys(dictT).length;
let formed = 0;

const windowCounts = {};

let left = 0, right = 0;

let minLen = Infinity;
let minLeft = 0, minRight = 0;

while (right < s.length) {
const character = s[right];
windowCounts[character] = (windowCounts[character] || 0) + 1;

if (dictT[character] && windowCounts[character] === dictT[character]) {
formed++;
}

while (left <= right && formed === required) {
const currentLen = right - left + 1;

if (currentLen < minLen) {
minLen = currentLen;
minLeft = left;
minRight = right;
}

const leftChar = s[left];
windowCounts[leftChar]--;

if (dictT[leftChar] && windowCounts[leftChar] < dictT[leftChar]) {
formed--;
}

left++;
}

right++;
}

return minLen === Infinity ? "" : s.substring(minLeft, minRight + 1);
};
51 changes: 51 additions & 0 deletions pacific-atlantic-water-flow/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @param {number[][]} heights
* @return {number[][]}
*/
var pacificAtlantic = function(heights) {
const ROWS = heights.length;
const COLS = heights[0].length;

const pac = new Set();
const atl = new Set();

const dfs = (r, c, visit, prevHeight) => {
const key = `${r},${c}`;
if (
visit.has(key) ||
r < 0 || c < 0 || r >= ROWS || c >= COLS ||
heights[r][c] < prevHeight
) {
return;
}

visit.add(key);

dfs(r + 1, c, visit, heights[r][c]);
dfs(r - 1, c, visit, heights[r][c]);
dfs(r, c + 1, visit, heights[r][c]);
dfs(r, c - 1, visit, heights[r][c]);
};

for (let c = 0; c < COLS; c++) {
dfs(0, c, pac, heights[0][c]); // Top row (Pacific)
dfs(ROWS - 1, c, atl, heights[ROWS - 1][c]); // Bottom row (Atlantic)
}

for (let r = 0; r < ROWS; r++) {
dfs(r, 0, pac, heights[r][0]); // Left col (Pacific)
dfs(r, COLS - 1, atl, heights[r][COLS - 1]); // Right col (Atlantic)
}

const res = [];
for (let r = 0; r < ROWS; r++) {
for (let c = 0; c < COLS; c++) {
const key = `${r},${c}`;
if (pac.has(key) && atl.has(key)) {
res.push([r, c]);
}
}
}

return res;
};
14 changes: 14 additions & 0 deletions sum-of-two-integers/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @param {number} a
* @param {number} b
* @return {number}
*/
var getSum = function(a, b) {
while (b !== 0) {
let carry = (a & b) << 1;
a = a ^ b;
b = carry;
}
return a;
};