Skip to content

Commit d8b4c28

Browse files
authored
Merge pull request #419 from Sunjae95/main
[선재] WEEK4 Solution
2 parents 0778811 + 18919b1 commit d8b4c28

File tree

5 files changed

+299
-0
lines changed

5 files changed

+299
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* priority queue + result counting that priority queue was pop
5+
*
6+
* time complexity: O(n log n)
7+
* space complexity: O(n)
8+
*/
9+
var longestConsecutive = function (nums) {
10+
const queue = new CustomQueue();
11+
let [count, before, answer] = [0, null, 0];
12+
13+
nums.forEach((n) => queue.insert(n));
14+
15+
if (queue.size === 0) return count;
16+
17+
[count, before, answer] = [1, queue.remove(), 1];
18+
19+
while (queue.size) {
20+
const current = queue.remove();
21+
22+
if (before === current) continue;
23+
24+
count = before + 1 === current ? count + 1 : 1;
25+
before = current;
26+
answer = Math.max(answer, count);
27+
}
28+
29+
return answer;
30+
};
31+
32+
class Node {
33+
constructor(value) {
34+
this.value = value;
35+
}
36+
}
37+
38+
class CustomQueue {
39+
constructor() {
40+
this.items = new Map();
41+
this.size = 0;
42+
}
43+
44+
parentIndex(index) {
45+
return Math.floor((index - 1) / 2);
46+
}
47+
48+
leftChildIndex(index) {
49+
return 2 * index + 1;
50+
}
51+
52+
rightChildIndex(index) {
53+
return 2 * index + 2;
54+
}
55+
56+
heapifyUp() {
57+
let index = this.size - 1;
58+
59+
while (index > 0) {
60+
const parentIndex = this.parentIndex(index);
61+
const parent = this.items.get(parentIndex);
62+
const current = this.items.get(index);
63+
64+
if (parent.value <= current.value) break;
65+
66+
this.items.set(this.parentIndex(index), current);
67+
this.items.set(index, parent);
68+
69+
index = parentIndex;
70+
}
71+
}
72+
73+
heapifyDown() {
74+
let index = 0;
75+
76+
while (this.leftChildIndex(index) < this.items.size) {
77+
let smallestIndex = this.leftChildIndex(index);
78+
let rightIndex = this.rightChildIndex(index);
79+
const current = this.items.get(index);
80+
81+
if (
82+
rightIndex < this.size &&
83+
this.items.get(rightIndex).value < this.items.get(smallestIndex).value
84+
) {
85+
smallestIndex = rightIndex;
86+
}
87+
88+
if (current.value <= this.items.get(smallestIndex).value) break;
89+
this.items.set(index, this.items.get(smallestIndex));
90+
this.items.set(smallestIndex, current);
91+
index = smallestIndex;
92+
}
93+
}
94+
95+
insert(value) {
96+
const index = this.size;
97+
const node = new Node(value);
98+
this.items.set(index, node);
99+
this.size++;
100+
this.heapifyUp();
101+
}
102+
103+
remove() {
104+
if (this.size === 0) return null;
105+
106+
const root = this.items.get(0);
107+
this.items.set(0, this.items.get(this.size - 1));
108+
this.items.delete(this.size - 1);
109+
this.size--;
110+
111+
this.heapifyDown();
112+
return root.value;
113+
}
114+
}

maximum-product-subarray/sunjae95.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* 1. memoization, recursive
5+
* 2. palindromic-substring custom
6+
*/
7+
8+
/**
9+
* brainstorming solve 1
10+
* result: fail because time limited
11+
*
12+
* time complexity: O(n^2)
13+
* space complexity: O(n^2)
14+
*/
15+
var maxProduct = function (nums) {
16+
let answer = nums[0];
17+
18+
const memo = Array.from({ length: nums.length }, () =>
19+
Array.from({ length: nums.length }, () => null)
20+
);
21+
22+
const recursive = (left, right) => {
23+
if (memo[left][right] !== null) return memo[left][right];
24+
25+
if (left === right) {
26+
memo[left][right] = nums[left];
27+
answer = Math.max(answer, nums[left]);
28+
return nums[left];
29+
}
30+
31+
const removedLeft = recursive(left + 1, right);
32+
recursive(left, right - 1);
33+
34+
memo[left][right] = nums[left] * removedLeft;
35+
36+
answer = Math.max(answer, memo[left][right]);
37+
38+
return removedLeft;
39+
};
40+
41+
recursive(0, nums.length - 1);
42+
43+
return answer;
44+
};
45+
46+
/**
47+
* brainstorming solve 2
48+
* result: fail because time limited
49+
*
50+
* time complexity: O(n^2)
51+
* space complexity: O(n)
52+
*/
53+
var maxProduct = function (nums) {
54+
let answer = nums[0];
55+
56+
for (let i = 0; i < nums.length; i++) {
57+
let [start, end] = [i, i];
58+
let product = nums[i];
59+
60+
answer = Math.max(answer, product);
61+
while (start >= 0 && end < nums.length) {
62+
if (start !== end) product = product * nums[start] * nums[end];
63+
64+
answer = Math.max(answer, product);
65+
[start, end] = [start - 1, end + 1];
66+
}
67+
68+
product = nums[i];
69+
[start, end] = [i, i + 1];
70+
71+
while (start >= 0 && end < nums.length) {
72+
if (start + 1 === end) product = product * nums[end];
73+
else product = product * nums[start] * nums[end];
74+
75+
answer = Math.max(answer, product);
76+
[start, end] = [start - 1, end + 1];
77+
}
78+
}
79+
80+
return answer;
81+
};

missing-number/sunjae95.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* hash map
5+
*
6+
* time complexity: O(n)
7+
* space complexity: O(n)
8+
*/
9+
var missingNumber = function (nums) {
10+
const map = new Map();
11+
for (let i = 0; i < nums.length; i++) {
12+
map.set(nums[i], i);
13+
}
14+
15+
for (let i = 0; i <= nums.length; i++) {
16+
if (!map.has(i)) return i;
17+
}
18+
};

valid-palindrome/sunjae95.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* string method + two pointer
5+
*
6+
* time complexity: O(n)
7+
* space complexity: O(n)
8+
*/
9+
10+
var isPalindrome = function (s) {
11+
const asciiArray = [];
12+
13+
for (let i = 0; i < s.length; i++) {
14+
const asciiCode = s[i].toLowerCase().charCodeAt(0);
15+
const isNumber = asciiCode >= 48 && asciiCode <= 57;
16+
const isLowerCase = asciiCode >= 97 && asciiCode <= 122;
17+
18+
if (isNumber || isLowerCase) asciiArray.push(asciiCode);
19+
}
20+
21+
const len = asciiArray.length;
22+
const middleIndex = Math.floor(len);
23+
24+
for (let i = 0; i < middleIndex; i++) {
25+
if (asciiArray[i] !== asciiArray[len - i - 1]) return false;
26+
}
27+
28+
return true;
29+
};

word-search/sunjae95.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* dfs
5+
*
6+
* time complexity: O(n^2 * 4^word.length)
7+
* space complexity: O(n^2)
8+
*/
9+
10+
var exist = function (board, word) {
11+
let answer = false;
12+
const ROW_LENGTH = board.length;
13+
const COLUMN_LENGTH = board[0].length;
14+
// O(n^2)
15+
const visited = Array.from({ length: ROW_LENGTH }, () =>
16+
Array.from({ length: COLUMN_LENGTH }, () => false)
17+
);
18+
19+
const isRange = (r, c) =>
20+
r >= 0 && r < ROW_LENGTH && c >= 0 && c < COLUMN_LENGTH && !visited[r][c];
21+
22+
const search = (r, c, currentWord) => {
23+
if (answer) return;
24+
if (currentWord.length > word.length) return;
25+
if (!word.includes(currentWord)) return;
26+
if (currentWord === word) {
27+
answer = true;
28+
return;
29+
}
30+
31+
const dr = [-1, 0, 0, 1];
32+
const dc = [0, -1, 1, 0];
33+
34+
for (let i = 0; i < 4; i++) {
35+
const nextR = r + dr[i];
36+
const nextC = c + dc[i];
37+
38+
if (!isRange(nextR, nextC)) continue;
39+
40+
const nextWord = currentWord + board[nextR][nextC];
41+
42+
visited[nextR][nextC] = true;
43+
search(nextR, nextC, nextWord);
44+
visited[nextR][nextC] = false;
45+
}
46+
};
47+
48+
for (let r = 0; r < ROW_LENGTH; r++) {
49+
for (let c = 0; c < COLUMN_LENGTH; c++) {
50+
visited[r][c] = true;
51+
search(r, c, board[r][c]);
52+
visited[r][c] = false;
53+
}
54+
}
55+
56+
return answer;
57+
};

0 commit comments

Comments
 (0)