Skip to content

Commit 0e8a9a4

Browse files
authored
Merge pull request #2264 from Blossssom/main
2 parents d187b01 + 718ac71 commit 0e8a9a4

4 files changed

Lines changed: 207 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class ListNode {
2+
val: number;
3+
next: ListNode | null;
4+
constructor(val?: number, next?: ListNode | null) {
5+
this.val = val === undefined ? 0 : val;
6+
this.next = next === undefined ? null : next;
7+
}
8+
}
9+
10+
/**
11+
* @param head - linked list node
12+
* @returns - ์ˆœํ™˜๋˜๋Š” ๋ฆฌ์ŠคํŠธ์ธ์ง€?
13+
* @description
14+
* - ํ’€์ด 1. - ๋‹จ์ˆœ ์ˆœํšŒ ๋ฐ ์กฐํšŒ๋กœ ํŒ๋‹จ, ๊ฒฐ๊ตญ visit์— ๋‹ค์‹œ ๋Œ์•„์˜จ๋‹ค๋ฉด ์ˆœํ™˜
15+
* - ํ’€์ด 2. - 1์นธ, 2์นธ ํฌ์ธํ„ฐ๋ฅผ ๋‚˜๋ˆ  ๊ฒฐ๊ตญ ์ˆœํ™˜์ด๋ผ๋ฉด ๋Œ๊ณ  ๋Œ์•„ ๋งŒ๋‚˜๋Š” ๊ณผ์ •์„ ํ†ตํ•œ ๋ฉ”๋ชจ๋ฆฌ O(1)์˜ ํ’€์ด
16+
*/
17+
18+
// function hasCycle(head: ListNode | null): boolean {
19+
// const visit = new Set();
20+
// let current = head;
21+
22+
// while (current) {
23+
// if (visit.has(current)) {
24+
// return true;
25+
// }
26+
27+
// visit.add(current);
28+
// current = current.next;
29+
// }
30+
31+
// return false;
32+
// }
33+
34+
function hasCycle(head: ListNode | null): boolean {
35+
if (!head || !head.next) {
36+
return false;
37+
}
38+
39+
let slow: ListNode | null = head;
40+
let fast: ListNode | null = head;
41+
42+
while (fast && fast.next) {
43+
slow = slow!.next;
44+
fast = fast.next.next;
45+
46+
if (slow === fast) {
47+
return true;
48+
}
49+
}
50+
return false;
51+
}
52+
53+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @param nums - ์ •์ˆ˜ ๋ฐฐ์—ด
3+
* @returns - ์ •์ˆ˜ ๋ฐฐ์—ด์˜ ๋ถ€๋ถ„ ๋ฐฐ์—ด์˜ ๊ณฑ์ด ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๋ฐ˜ํ™˜
4+
* @description
5+
* - ๊ฐ’์ด ์Œ์ˆ˜์ผ ๊ฒฝ์šฐ ์ตœ์†Ÿ๊ฐ’๊ณผ ๊ณฑํ•ด ์ตœ๋Œ“๊ฐ’์„ ๋„์ถœ ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ swap
6+
* - min, max๋ฅผ ํ˜„์žฌ ๊ฐ’๊ณผ ๋น„๊ตํ•˜๋ฉฐ ๋ณ€๊ฒฝ
7+
*/
8+
9+
function maxProduct(nums: number[]): number {
10+
let maximum = nums[0];
11+
let minimum = nums[0];
12+
let result = nums[0];
13+
14+
for (let i = 1; i < nums.length; i++) {
15+
const current = nums[i];
16+
17+
if (current < 0) {
18+
let temp = maximum;
19+
maximum = minimum;
20+
minimum = temp;
21+
}
22+
23+
maximum = Math.max(current, current * maximum);
24+
minimum = Math.min(current, current * minimum);
25+
26+
result = Math.max(result, maximum);
27+
}
28+
29+
return result;
30+
}
31+
32+
const nums = [-2, 3, -4];
33+
34+
maxProduct(nums);
35+
36+
37+
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* @param heights - ์…€์˜ ํ•ด๋ฐœ๊ณ ๋„ ๋ฐฐ์—ด
3+
* @returns
4+
* @description
5+
* - ์ธ์ ‘ํ•œ ์…€์˜ ๋†’์ด๊ฐ€ ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด ๋น—๋ฌผ์ด ํ๋ฅผ ์ˆ˜ ์žˆ์Œ
6+
* - ๋ฐ”๋‹ค์— ์ธ์ ‘ํ•œ ์…€์—์„œ๋Š” ๋ฐ”๋‹ค๋กœ ๋ฌผ์ด ํ๋ฅผ ์ˆ˜ ์žˆ์Œ
7+
* - ํƒœํ‰์–‘๊ณผ ๋Œ€์„œ์–‘ ๋ชจ๋‘๋กœ ํ๋ฅผ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ ๋ฐ˜ํ™˜
8+
* - ๊ฒฐ๊ตญ [0, n] ๊ณผ [m, 0]๋Š” ๋ฐ˜๋“œ์‹œ ํฌํ•จ
9+
*
10+
* 1. ์—ญ์ˆœ์œผ๋กœ ๊ฐ€๋Š”๊ฒŒ ์œ ๋ฆฌํ•˜๋‹ค๋Š” ํžŒํŠธ๋ฅผ ์ฑ„ํƒ
11+
* 2. ๋ชจ๋“  ์…€์˜ ์ด๋™ ๊ฐ€๋Šฅ ๋ฐฉํ–ฅ์„ ํŒŒ์•… (dfs, bfs)
12+
*/
13+
14+
function pacificAtlantic(heights: number[][]): number[][] {
15+
const yMax = heights.length;
16+
const xMax = heights[0].length;
17+
18+
// ํƒœํ‰์–‘, ๋Œ€์„œ์–‘์˜ visit ์ฒดํฌ๋ฅผ ์œ„ํ•œ set
19+
const pacific = new Set<string>();
20+
const atlantic = new Set<string>();
21+
22+
const result: number[][] = [];
23+
function dfs(y: number, x: number, visited: Set<string>, prevHeight: number) {
24+
const key = `${y},${x}`;
25+
26+
if (
27+
x < 0 ||
28+
x >= xMax ||
29+
y < 0 ||
30+
y >= yMax ||
31+
visited.has(key) ||
32+
prevHeight > heights[y][x]
33+
) {
34+
return;
35+
}
36+
37+
visited.add(key);
38+
dfs(y, x + 1, visited, heights[y][x]);
39+
dfs(y, x - 1, visited, heights[y][x]);
40+
dfs(y + 1, x, visited, heights[y][x]);
41+
dfs(y - 1, x, visited, heights[y][x]);
42+
}
43+
44+
// ๋งจ ์–‘ ๋์ชฝ์„ dfs์˜ ์‹œ์ž‘์ ์œผ๋กœ ์ง€์ •
45+
for (let i = 0; i < yMax; i++) {
46+
dfs(i, 0, pacific, heights[i][0]);
47+
dfs(i, xMax - 1, atlantic, heights[i][xMax - 1]);
48+
}
49+
50+
for (let j = 0; j < xMax; j++) {
51+
dfs(0, j, pacific, heights[0][j]);
52+
dfs(yMax - 1, j, atlantic, heights[yMax - 1][j]);
53+
}
54+
55+
// ์œ ๋‹ˆ์˜จ ๊ฐ’ ์ฒดํฌ
56+
for (let i = 0; i < yMax; i++) {
57+
for (let j = 0; j < xMax; j++) {
58+
const key = `${i},${j}`;
59+
60+
if (pacific.has(key) && atlantic.has(key)) {
61+
result.push([i, j]);
62+
}
63+
}
64+
}
65+
66+
return result;
67+
}
68+
69+
const heights = [
70+
[1, 2, 2, 3, 5],
71+
[3, 2, 3, 4, 4],
72+
[2, 4, 5, 3, 1],
73+
[6, 7, 1, 4, 5],
74+
[5, 1, 1, 2, 4],
75+
];
76+
77+
pacificAtlantic(heights);
78+
79+
// ํƒœ ํ‰ ์–‘ (Pacific)
80+
// ~ ~ ~ ~ ~ ~
81+
// ~ [1] [2] [2] [3] (5) ~ <-- ์˜ค๋ฅธ์ชฝ/์•„๋ž˜๋Š” ๋Œ€์„œ์–‘
82+
// ~ [3] [2] [3] (4) (4) ~
83+
// ~ [2] [4] (5) [3] [1] ~
84+
// ~ (6) (7) (1) [4] [5] ~
85+
// ~ (5) [1] [1] [2] [4] ~
86+
// ~ ~ ~ ~ ~ ~
87+
// ๋Œ€ ์„œ ์–‘ (Atlantic)
88+
89+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
*
3+
* @param a - ์ •์ˆ˜ a
4+
* @param b - ์ •์ˆ˜ b
5+
* @returns - ๋”ํ•˜๊ธฐ ์—ฐ์‚ฐ ์—†์ด ๋”ํ•œ ๊ฐ’ ๋ฐ˜ํ™˜
6+
* @description
7+
* - bit ์—ฐ์‚ฐ์ด๊ตฌ๋‚˜! ์‹ถ์—ˆ๋Š”๋ฐ ๋น„ํŠธ๋ฅผ ์ž˜ ๋‹ค๋ฃฐ์ค„ ๋ชฐ๋ผ์„œ ai์˜ ๋„์›€์„ ๋ฐ›์Œ
8+
*/
9+
10+
function getSum(a: number, b: number): number {
11+
while (b !== 0) {
12+
// ์˜ฌ๋ฆผ์ˆ˜ ๊ณ„์‚ฐ : ๋‘˜๋‹ค 1์ธ ๋น„ํŠธ๋ฅผ AND ์—ฐ์‚ฐ์œผ๋กœ ์ฐพ์•„ ์™ผ์ชฝ์œผ๋กœ ๋ฐˆ (์˜ฌ๋ฆผ์ˆ˜๋‹ˆ๊นŒ)
13+
const carry = (a & b) << 1;
14+
15+
// XOR ์—ฐ์‚ฐ์œผ๋กœ ์˜ฌ๋ฆผ์ˆ˜๋ฅผ ์ œ์™ธํ•œ ๋ง์…ˆ ๊ฐ’ ๋„์ถœ (1 | 0 ์ด๋ฏ€๋กœ)
16+
a = a ^ b;
17+
18+
b = carry;
19+
}
20+
21+
return a;
22+
}
23+
24+
const a = 2;
25+
const b = 3;
26+
getSum(a, b);
27+
28+

0 commit comments

Comments
ย (0)