Skip to content

Commit d01d956

Browse files
authored
Merge pull request #1909 from hi-rachel/main
[hi-rachel] WEEK 09 solutions
2 parents 5bc8cac + db72d18 commit d01d956

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
"""
8+
TC: O(n)
9+
SC: O(n)
10+
"""
11+
class Solution:
12+
def hasCycle(self, head: Optional[ListNode]) -> bool:
13+
visited = set()
14+
while head:
15+
# ํ•ด๋‹น ๋…ธ๋“œ ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ์  ์žˆ์œผ๋ฉด
16+
if head in visited:
17+
return True
18+
# ๋ฐฉ๋ฌธํ•œ ์  ์—†์œผ๋ฉด set์— ๋„ฃ๊ธฐ
19+
visited.add(head)
20+
# ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์ด๋™
21+
head = head.next
22+
23+
# cycle์ด ์—†์Œ
24+
return False
25+
26+
"""
27+
The tortoise and hare
28+
29+
TC: O(n)
30+
SC: O(1)
31+
"""
32+
class Solution:
33+
def hasCycle(self, head: Optional[ListNode]) -> bool:
34+
slow, fast = head, head
35+
while fast and fast.next:
36+
slow = slow.next
37+
fast = fast.next.next
38+
if slow == fast:
39+
return True
40+
return False
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
TC: O(n)
3+
SC: O(1)
4+
"""
5+
6+
class Solution:
7+
def maxProduct(self, nums: List[int]) -> int:
8+
max_here = min_here = ans = nums[0]
9+
10+
for x in nums[1:]:
11+
if x < 0:
12+
max_here, min_here = min_here, max_here
13+
max_here = max(x, max_here * x)
14+
min_here = min(x, min_here * x)
15+
ans = max(ans, max_here)
16+
return ans
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* JavaScript ๋น„ํŠธ ์—ฐ์‚ฐ์ž ์ •๋ฆฌ:
3+
*
4+
* 1. & (AND ์—ฐ์‚ฐ์ž): ๋‘ ๋น„ํŠธ๊ฐ€ ๋ชจ๋‘ 1์ผ ๋•Œ๋งŒ 1์„ ๋ฐ˜ํ™˜
5+
* ์˜ˆ: 5 & 3 = 1 (101 & 011 = 001)
6+
*
7+
* 2. ^ (XOR ์—ฐ์‚ฐ์ž): ๋‘ ๋น„ํŠธ๊ฐ€ ๋‹ค๋ฅผ ๋•Œ 1์„ ๋ฐ˜ํ™˜
8+
* ์˜ˆ: 5 ^ 3 = 6 (101 ^ 011 = 110)
9+
*
10+
* 3. << (์™ผ์ชฝ ์‹œํ”„ํŠธ): ๋น„ํŠธ๋ฅผ ์™ผ์ชฝ์œผ๋กœ ์ด๋™ (๊ณฑํ•˜๊ธฐ 2์™€ ๋™์ผ)
11+
* ์˜ˆ: 5 << 1 = 10 (101 << 1 = 1010)
12+
*
13+
* ํ’€์ด ์„ค๋ช…:
14+
* - ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋ง์…ˆ์„ ๋น„ํŠธ ์—ฐ์‚ฐ์œผ๋กœ ๊ตฌํ˜„
15+
* - XOR(^)๋กœ ์ž๋ฆฌ์˜ฌ๋ฆผ ์—†๋Š” ํ•ฉ์„ ๊ตฌํ•จ
16+
* - AND(&)๋กœ ์ž๋ฆฌ์˜ฌ๋ฆผ์„ ์ฐพ๊ณ , ์™ผ์ชฝ ์‹œํ”„ํŠธ(<<)๋กœ ํ•œ ์ž๋ฆฌ ์˜ฌ๋ฆผ
17+
* - ์ž๋ฆฌ์˜ฌ๋ฆผ์ด 0์ด ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
18+
*
19+
* ์‹œ๊ณต๊ฐ„ ๋ณต์žก๋„:
20+
* - ์‹œ๊ฐ„ ๋ณต์žก๋„: O(1) - ์ตœ์•…์˜ ๊ฒฝ์šฐ 32๋ฒˆ ๋ฐ˜๋ณต (32๋น„ํŠธ ์ •์ˆ˜)
21+
* - ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1) - ์ƒ์ˆ˜ ๊ณต๊ฐ„๋งŒ ์‚ฌ์šฉ
22+
*/
23+
24+
/**
25+
* @param {number} a
26+
* @param {number} b
27+
* @return {number}
28+
*/
29+
var getSum = function (a, b) {
30+
while (b !== 0) {
31+
// ์ž๋ฆฌ์˜ฌ๋ฆผ
32+
let carry = (a & b) << 1;
33+
34+
// ์ž๋ฆฌ์˜ฌ๋ฆผ ์—†๋Š” ํ•ฉ
35+
a = a ^ b;
36+
37+
b = carry;
38+
}
39+
return a;
40+
};

0 commit comments

Comments
ย (0)