Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions linked-list-cycle/hi-rachel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None

"""
TC: O(n)
SC: O(n)
"""
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
visited = set()
while head:
# 해당 노드 이미 방문한 적 있으면
if head in visited:
return True
# 방문한 적 없으면 set에 넣기
visited.add(head)
# 다음 노드로 이동
head = head.next

# cycle이 없음
return False

"""
The tortoise and hare

TC: O(n)
SC: O(1)
"""
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
16 changes: 16 additions & 0 deletions maximum-product-subarray/hi-rachel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
TC: O(n)
SC: O(1)
"""

class Solution:
def maxProduct(self, nums: List[int]) -> int:
max_here = min_here = ans = nums[0]

for x in nums[1:]:
if x < 0:
max_here, min_here = min_here, max_here
max_here = max(x, max_here * x)
min_here = min(x, min_here * x)
ans = max(ans, max_here)
return ans
40 changes: 40 additions & 0 deletions sum-of-two-integers/hi-rachel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* JavaScript 비트 연산자 정리:
*
* 1. & (AND 연산자): 두 비트가 모두 1일 때만 1을 반환
* 예: 5 & 3 = 1 (101 & 011 = 001)
*
* 2. ^ (XOR 연산자): 두 비트가 다를 때 1을 반환
* 예: 5 ^ 3 = 6 (101 ^ 011 = 110)
*
* 3. << (왼쪽 시프트): 비트를 왼쪽으로 이동 (곱하기 2와 동일)
* 예: 5 << 1 = 10 (101 << 1 = 1010)
*
* 풀이 설명:
* - 이 알고리즘은 덧셈을 비트 연산으로 구현
* - XOR(^)로 자리올림 없는 합을 구함
* - AND(&)로 자리올림을 찾고, 왼쪽 시프트(<<)로 한 자리 올림
* - 자리올림이 0이 될 때까지 반복
*
* 시공간 복잡도:
* - 시간 복잡도: O(1) - 최악의 경우 32번 반복 (32비트 정수)
* - 공간 복잡도: O(1) - 상수 공간만 사용
*/

/**
* @param {number} a
* @param {number} b
* @return {number}
*/
var getSum = function (a, b) {
while (b !== 0) {
// 자리올림
let carry = (a & b) << 1;

// 자리올림 없는 합
a = a ^ b;

b = carry;
}
return a;
};