Skip to content

Commit

Permalink
[highball] week4 solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
highballplz committed Sep 4, 2024
1 parent 8dc292b commit 8ee9f86
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 0 deletions.
32 changes: 32 additions & 0 deletions longest-consecutive-sequence/highball.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//time-complexity : O(n)
//space-complexity : O(n)

const longestConsecutive = function (nums) {
let longest = 0;
const set = new Set(nums);

while (set.size > 0) {
let count = 0;
const originalSeed = set.values().next().value; //set의 첫 번째 원소 get

seed = originalSeed;

while (set.has(seed)) {
set.delete(seed);
count++;
seed += 1;
}

seed = originalSeed - 1;

while (set.has(seed)) {
set.delete(seed);
count++;
seed -= 1;
}

if (count > longest) longest = count;
}

return longest;
};
84 changes: 84 additions & 0 deletions maximum-product-subarray/highball.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//time-complexity : O(n)
//space-complexity : O(n)

const maxProduct = function (nums) {
let subarrays = [];
let subarray = [];
let zeroCount = 0;

/* -------------------------------------------------------------------------- */
/* nonzero subarray 잘라서 subarrays에 넣기 */
/* -------------------------------------------------------------------------- */
for (let i = 0; i < nums.length; i++) {
if (nums[i] === 0) {
if (subarray.length > 0) {
subarrays.push([...subarray]);
subarray.length = 0; //각 element의 metadata를 garbage collection에 들어가야 하는 것으로 marking할 뿐이므로 O(1)!
}
zeroCount++;
continue;
}
subarray.push(nums[i]);
}
if (subarray.length > 0) {
subarrays.push([...subarray]);
}

/* -------------------------------------------------------------------------- */
/* 각 subarray의 maxProduct들 중 최대값 리턴하기 */
/* -------------------------------------------------------------------------- */
if (zeroCount === 0) {
return maxProductNonzero(nums);
} else {
let max = 0;
for (let i = 0; i < subarrays.length; i++) {
max = Math.max(maxProductNonzero(subarrays[i]), max);
}
return max;
}
};

const maxProductNonzero = function (nonZeroNums) {
if (nonZeroNums.length === 1) return nonZeroNums[0]; //firstNegativeIndex = lastNegativeIndex = 0이 되어 버려서 frontProduct와 backProduct 중 어느 것도 계산이 안 돼버리므로 early return으로 처리.

let firstNegativeIndex = -1;
let lastNegativeIndex = -1;
let negativeCount = 0;
for (let i = 0; i < nonZeroNums.length; i++) {
if (nonZeroNums[i] < 0 && firstNegativeIndex !== lastNegativeIndex) {
lastNegativeIndex = i;
negativeCount++;
}
if (nonZeroNums[i] < 0 && firstNegativeIndex === lastNegativeIndex) {
firstNegativeIndex = i;
negativeCount++;
}
}

if (negativeCount === 1) {
lastNegativeIndex = firstNegativeIndex;
}

if (negativeCount % 2 === 0) {
/* -------------------------------------------------------------------------- */
/* 음수 개수가 짝수 개면 그냥 전체 곱한 게 최대값임 */
/* -------------------------------------------------------------------------- */
let product = 1;
for (let i = 0; i < nonZeroNums.length; i++) {
product *= nonZeroNums[i];
}
return product;
} else {
/* -------------------------------------------------------------------------- */
/* 음수 개수가 홀수 개면 처음부터 lastNegativeIndex 직전까지 곱한 수 혹은 */
/* firstNegativeIndex부터 끝까지 곱한 수 중 하나가 최대값임 */
/* -------------------------------------------------------------------------- */
let frontProduct = 1;
let backProduct = 1;
for (let i = 0; i < nonZeroNums.length; i++) {
if (i < lastNegativeIndex) frontProduct *= nonZeroNums[i];
if (i > firstNegativeIndex) backProduct *= nonZeroNums[i];
}
return Math.max(frontProduct, backProduct);
}
};
8 changes: 8 additions & 0 deletions missing-number/highball.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//time-complexity : O(n)
//space-complexity : O(1)

const missingNumber = function (nums) {
const sum = nums.reduce((acc, cur) => acc + cur);
const n = nums.length;
return (n * (n + 1)) / 2 - sum;
};
27 changes: 27 additions & 0 deletions valid-palindrome/highball.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//time-complexity : O(n)
//space-complexity : O(1)

const isPalindrome = function (s) {
let left = 0;
let right = s.length - 1;
while (left < right) {
if (isAlphaNumeric(s[left]) && isAlphaNumeric(s[right])) {
if (s[left].toLowerCase() === s[right].toLowerCase()) {
left++;
right--;
} else return false;
} else {
if (!isAlphaNumeric(s[left])) left++;
if (!isAlphaNumeric(s[right])) right--;
}
}
return true;
};

const isAlphaNumeric = function (c) {
return (
(c.charCodeAt(0) >= 48 && c.charCodeAt(0) <= 57) ||
(c.charCodeAt(0) >= 65 && c.charCodeAt(0) <= 90) ||
(c.charCodeAt(0) >= 97 && c.charCodeAt(0) <= 122)
);
};
42 changes: 42 additions & 0 deletions word-search/highball.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//time-complexity : O(4^(ROWS*COLS))
//space-complexity : O(word.length)

const exist = function (board, word) {
const ROWS = board.length;
const COLS = board[0].length;

for (let r = 0; r < ROWS; r++) {
for (let c = 0; c < COLS; c++) {
if (board[r][c] === word[0]) {
if (dfs(board, word, r, c, 0)) return true;
}
}
}

return false;
};

const dfs = function (board, word, r, c, i) {
const ROWS = board.length;
const COLS = board[0].length;

if (r < 0 || r >= ROWS || c < 0 || c >= COLS || board[r][c] !== word[i]) {
return false;
}

if (i === word.length - 1) return true;

const temp = board[r][c];
board[r][c] = null;

let found =
dfs(board, word, r + 0, c + 1, i + 1) ||
dfs(board, word, r + 1, c + 0, i + 1) ||
dfs(board, word, r + 0, c + -1, i + 1) ||
dfs(board, word, r + -1, c + 0, i + 1);
//forEach로 했더니 runtime 두 배 됨;;

board[r][c] = temp;

return found;
};

0 comments on commit 8ee9f86

Please sign in to comment.