Skip to content

Commit 27af776

Browse files
committedMay 30, 2025
1 parent af257ad commit 27af776

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed
 

โ€Žlinked-list-cycle/crumbs22.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
10+
/*
11+
ํ•ด์‹œ (std::unordered_set<>)์— ์ฃผ์†Œ๋ฅผ ๊ธฐ๋กํ•ด๋‘๊ณ ,
12+
๋‹ค์Œ ๋…ธ๋“œ๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ „์— ์ด๋ฏธ ์ง‘ํ•ฉ์— ๋“ค์–ด์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ
13+
14+
๊ณต๊ฐ„๋ณต์žก๋„๋Š” O(n)์œผ๋กœ, ํ•ด์‹œ์— ๋Œ€ํ•œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉ
15+
์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(n)
16+
*/
17+
18+
19+
class Solution {
20+
public:
21+
bool hasCycle(ListNode *head) {
22+
std::unordered_set<ListNode*> visited;
23+
ListNode* cur = head;
24+
25+
while (cur) {
26+
if (visited.count(cur) == 1) // ์ด๋ฏธ ๋ณธ ๋…ธ๋“œ๋ฉด cycle์ด ์กด์žฌ
27+
return (true) ;
28+
29+
visited.insert(cur); // ๋ฐฉ๋ฌธ ๊ธฐ๋ก
30+
cur = cur->next;
31+
}
32+
return (false); // ๋๊นŒ์ง€ ๋Œ์•˜์œผ๋‚˜ ์ˆœํ™˜์ด ์—†๋Š” ๊ฒฝ์šฐ์—๋งŒ cycle์ด ์กด์žฌํ•˜์ง€ ์•Š์Œ
33+
}
34+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
ํ•œ ๋ฒˆ์˜ ์ˆœํšŒ๋กœ ์œ„์น˜๋งˆ๋‹ค์˜ ์ตœ๋Œ€๊ณฑ๊ณผ ์ตœ์†Œ ๊ณฑ์„ ๊ฐฑ์‹ 
3+
์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(n), ๊ณต๊ฐ„๋ณต์žก๋„๋Š” O(1)
4+
*/
5+
class Solution {
6+
public:
7+
int maxProduct(vector<int>& nums) {
8+
int ans = nums[0];
9+
int curMax = nums[0];
10+
int curMin = nums[0];
11+
12+
// 1๋ฒˆ์งธ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰๊นŒ์ง€ ์ˆœํšŒ
13+
for (int i = 1; i < nums.size(); i++) {
14+
// ์Œ์ˆ˜๋ฅผ ๊ณฑํ•˜๋ฉด ํ˜„์žฌ๊นŒ์ง€์˜ ์ตœ๋Œ€ ๊ณฑ์ด ์ตœ์†Œ๊ฐ€ ๋˜๊ณ , ์ตœ์†Œ ๊ณฑ์€ ์ตœ๋Œ€ ๊ณฑ์œผ๋กœ ๋’ค๋ฐ”๋€œ
15+
if (nums[i] < 0)
16+
swap(curMax, curMin);
17+
18+
curMax = max(nums[i], curMax * nums[i]);
19+
curMin = min(nums[i], curMin * nums[i]);
20+
ans = max(ans, curMax);
21+
}
22+
return (ans);
23+
}
24+
};
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
class Solution {
2+
public:
3+
4+
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
5+
int m = heights.size();
6+
if (m == 0)
7+
return {};
8+
int n = heights[0].size();
9+
if (n == 0)
10+
return {};
11+
12+
vector<vector<bool>> reachP(m, vector<bool>(n, false));
13+
vector<vector<bool>> reachA(m, vector<bool>(n, false));
14+
queue<pair<int, int>> qP, qA;
15+
16+
17+
// Pacific ๊ฒฝ๊ณ„ ์ดˆ๊ธฐํ™”
18+
for (int i = 0; i < m; i++) {
19+
reachP[i][0] = true;
20+
qP.emplace(i, 0); // Pacific ๊ฒฝ๊ณ„์—์„œ ์‹œ์ž‘ํ•  ์ขŒํ‘œ๋กœ ํ์— ๋“ฑ๋ก. ์ดํ›„ ์ด ์ขŒํ‘œ๋“ค์€ BFS์˜ ์ถœ๋ฐœ์ ์ด ๋จ
21+
}
22+
for (int j = 0; j < n; j++) {
23+
reachP[0][j] = true;
24+
qP.emplace(0, j);
25+
}
26+
27+
// Atlantic ๊ฒฝ๊ณ„ ์ดˆ๊ธฐํ™”
28+
for (int i = 0; i < m; i++) {
29+
reachA[i][n - 1] = true;
30+
qA.emplace(i, n - 1);
31+
}
32+
for (int j = 0; j < n; j++) {
33+
reachA[m - 1][j] = true;
34+
qA.emplace(m - 1, j);
35+
}
36+
37+
// ๊ฐ ๊ฒฝ๊ณ„์— ๋Œ€ํ•œ bfs
38+
bfs(heights, qP, reachP);
39+
bfs(heights, qA, reachA);
40+
41+
// ๋‘ ๊ฒฝ๊ณ„์— ๋ชจ๋‘ ๋„๋‹ฌ ๊ฐ€๋Šฅํ•œ ์…€ ์ˆ˜์ง‘
42+
vector<vector<int>> ans;
43+
for (int i = 0; i < m; i++) {
44+
for (int j = 0; j < n; j++) {
45+
if (reachP[i][j] && reachA[i][j]) {
46+
ans.push_back({i, j});
47+
}
48+
}
49+
}
50+
return (ans);
51+
}
52+
53+
void bfs(const vector<vector<int>>& h, queue<pair<int, int>>& q, vector<vector<bool>>& reach) {
54+
int m = h.size();
55+
int n = h[0].size();
56+
int dr[4] = {1, -1, 0, 0};
57+
int dc[4] = {0, 0, 1, -1};
58+
59+
while (!q.empty()) {
60+
auto [r,c] = q.front();
61+
q.pop();
62+
for (int d = 0; d < 4; d++) {
63+
int nr = r + dr[d];
64+
int nc = c + dc[d];
65+
66+
if (nr < 0 || nr >= m || nc < 0 || nc >= n)
67+
continue ;
68+
if (reach[nr][nc])
69+
continue ;
70+
if (h[nr][nc] < h[r][c])
71+
continue ;
72+
reach[nr][nc] = true;
73+
q.emplace(nr, nc);
74+
}
75+
}
76+
}
77+
};

โ€Žsum-of-two-integers/crumbs22.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
- ๊ฐ ๋น„ํŠธ ์ค‘ ํ•˜๋‚˜๋งŒ 1์ผ ๋•Œ 1์ด ๋˜๋ฏ€๋กœ XOR์—ฐ์‚ฐ ํ™œ์šฉ
3+
- ๊ฐ ๋น„ํŠธ ๋‘˜ ๋‹ค 1์ผ ๋•Œ ์˜ฌ๋ฆผ์ˆ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ &์—ฐ์‚ฐ ํ™œ์šฉ
4+
- a๋Š” ์ด์ œ๊นŒ์ง€์˜ ํ•ฉ ๋น„ํŠธ๊ฐ€ ๋˜๊ณ 
5+
b๋Š” ๋‹ค์Œ ์ž๋ฆฌ์—์„œ ๋”ํ•ด์ค˜์•ผ ๋  ์˜ฌ๋ฆผ ๋น„ํŠธ๊ฐ€ ๋จ.
6+
์˜ฌ๋ฆผ ๋น„ํŠธ b๊ฐ€ 0์ด ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
7+
์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(1), ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(1)
8+
*/
9+
10+
class Solution {
11+
public:
12+
int getSum(int a, int b) {
13+
14+
while (b != 0) {
15+
// ์˜ฌ๋ฆผ์ˆ˜ ๊ณ„์‚ฐ
16+
int carry = (a & b) << 1;
17+
// ์˜ฌ๋ฆผ ๋ฌด์‹œํ•œ ํ•ฉ ๋น„ํŠธ ๊ณ„์‚ฐ
18+
a = a ^ b;
19+
// ๋‹ค์Œ ๋ฐ˜๋ณต์— carry ๋”ํ•ด์ฃผ๊ธฐ
20+
b = carry;
21+
}
22+
return (a);
23+
}
24+
};

0 commit comments

Comments
 (0)