Skip to content

Commit 3ac0528

Browse files
authored
Merge pull request #516 from wogha95/main
[재호] WEEK 09 Solutions
2 parents b6f9956 + 06b49b9 commit 3ac0528

File tree

4 files changed

+191
-0
lines changed

4 files changed

+191
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* TC: O(log N)
3+
* 문제에 명시된 log N 복잡도를 갖기위해 이진탐색
4+
*
5+
* SC: O(1)
6+
*/
7+
8+
/**
9+
* @param {number[]} nums
10+
* @return {number}
11+
*/
12+
var findMin = function (nums) {
13+
let left = 0;
14+
let right = nums.length - 1;
15+
16+
// case1: v_left <= v_center < v_right => return left
17+
// case2: v_right < v_left <= v_center => left = center
18+
// case3: v_center < v_right < v_left => right = center
19+
20+
while (left < right) {
21+
const center = Math.floor((left + right) / 2);
22+
23+
if (nums[left] <= nums[center] && nums[center] < nums[right]) {
24+
return nums[left];
25+
}
26+
if (nums[right] < nums[left] && nums[left] <= nums[center]) {
27+
left = center + 1;
28+
continue;
29+
}
30+
if (nums[center] < nums[right] && nums[right] < nums[left]) {
31+
right = center;
32+
continue;
33+
}
34+
}
35+
36+
return nums[left];
37+
};

linked-list-cycle/wogha95.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Floyd tortoise and hare 알고리즘을 바탕으로
3+
* 한칸씩 이동하는 포인터와 2칸씩 이동하는 포인터는 결국에 만난다는 점을 이용해서 품
4+
*
5+
* TC: O(N)
6+
* SC: O(1)
7+
* N: linked list length
8+
*/
9+
10+
/**
11+
* Definition for singly-linked list.
12+
* function ListNode(val) {
13+
* this.val = val;
14+
* this.next = null;
15+
* }
16+
*/
17+
18+
/**
19+
* @param {ListNode} head
20+
* @return {boolean}
21+
*/
22+
var hasCycle = function (head) {
23+
let oneStep = head;
24+
let twoStep = head;
25+
26+
while (twoStep && twoStep.next) {
27+
if (oneStep === twoStep) {
28+
return true;
29+
}
30+
31+
oneStep = oneStep.next;
32+
twoStep = twoStep.next.next;
33+
}
34+
35+
return false;
36+
};

maximum-subarray/wogha95.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 각 num을 누적된 result값에 num을 더한값과 비교해서 큰 값을 result로 유지해간다.
3+
* 그리고 최대 누적값을 구하기 위해 매 result를 구할때마다 최대 result를 갱신한다.
4+
*
5+
* TC: O(N)
6+
* SC: O(1)
7+
*/
8+
9+
/**
10+
* @param {number[]} nums
11+
* @return {number}
12+
*/
13+
var maxSubArray = function (nums) {
14+
let maxResult = nums[0];
15+
let result = nums[0];
16+
17+
for (let index = 1; index < nums.length; index++) {
18+
const num = nums[index];
19+
result = Math.max(result + num, num);
20+
maxResult = Math.max(maxResult, result);
21+
}
22+
23+
return maxResult;
24+
};
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* pacific(0행, 0열), atlantic(row-1행, column-1열)에서 높이가 같거나 높은 곳으로 순회한다.
3+
* 그리고 pacific에서 온 물과 atlantic에서 온 물이 만나는 곳을 정답으로 만든다.
4+
*
5+
* TC: O(row * column)
6+
* queue에 최대 row * column만큼 들어갑니다.
7+
*
8+
* SC: O(row * column)
9+
* pacific 또는 atlantic에 최대 row * column만큼 들어갑니다.
10+
*/
11+
12+
/**
13+
* @param {number[][]} heights
14+
* @return {number[][]}
15+
*/
16+
var pacificAtlantic = function (heights) {
17+
const ROW = heights.length;
18+
const COLUMN = heights[0].length;
19+
const DIRECTION = [
20+
[-1, 0],
21+
[1, 0],
22+
[0, -1],
23+
[0, 1],
24+
];
25+
const result = [];
26+
const queue = [];
27+
28+
for (let c = 0; c < COLUMN; c++) {
29+
queue.push([0, c]);
30+
}
31+
for (let r = 1; r < ROW; r++) {
32+
queue.push([r, 0]);
33+
}
34+
35+
const pacific = new Set();
36+
while (queue.length > 0) {
37+
const [row, column] = queue.shift();
38+
pacific.add(generateKey(row, column));
39+
for (const [directionR, directionC] of DIRECTION) {
40+
const [nextRow, nextColumn] = [row + directionR, column + directionC];
41+
if (
42+
isValidPosition(nextRow, nextColumn) &&
43+
heights[row][column] <= heights[nextRow][nextColumn] &&
44+
!pacific.has(generateKey(nextRow, nextColumn))
45+
) {
46+
queue.push([nextRow, nextColumn]);
47+
}
48+
}
49+
}
50+
51+
for (let c = 0; c < COLUMN; c++) {
52+
queue.push([ROW - 1, c]);
53+
}
54+
for (let r = 0; r < ROW - 1; r++) {
55+
queue.push([r, COLUMN - 1]);
56+
}
57+
58+
const atlantic = new Set();
59+
while (queue.length > 0) {
60+
const [row, column] = queue.shift();
61+
const key = generateKey(row, column);
62+
atlantic.add(key);
63+
if (pacific.has(key)) {
64+
pacific.delete(key);
65+
result.push([row, column]);
66+
}
67+
for (const [directionR, directionC] of DIRECTION) {
68+
const [nextRow, nextColumn] = [row + directionR, column + directionC];
69+
if (
70+
isValidPosition(nextRow, nextColumn) &&
71+
heights[row][column] <= heights[nextRow][nextColumn] &&
72+
!atlantic.has(generateKey(nextRow, nextColumn))
73+
) {
74+
queue.push([nextRow, nextColumn]);
75+
}
76+
}
77+
}
78+
79+
return result;
80+
81+
function isValidPosition(row, column) {
82+
if (row < 0 || ROW <= row) {
83+
return false;
84+
}
85+
if (column < 0 || COLUMN <= column) {
86+
return false;
87+
}
88+
return true;
89+
}
90+
91+
function generateKey(row, column) {
92+
return `${row},${column}`;
93+
}
94+
};

0 commit comments

Comments
 (0)