Skip to content

Commit ee81a6d

Browse files
Merge pull request #1516 from printjin-gmailcom/main
[printjin-gmailcom] WEEK 09 Solutions
2 parents 8fa9a72 + 3d686fd commit ee81a6d

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def hasCycle(self, head):
3+
slow = fast = head
4+
while fast and fast.next:
5+
slow = slow.next
6+
fast = fast.next.next
7+
if slow == fast:
8+
return True
9+
return False
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def maxProduct(self, nums):
3+
max_prod = min_prod = result = nums[0]
4+
for n in nums[1:]:
5+
temp_max = max(n, max_prod * n, min_prod * n)
6+
min_prod = min(n, max_prod * n, min_prod * n)
7+
max_prod = temp_max
8+
result = max(result, max_prod)
9+
return result
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from collections import Counter
2+
3+
class Solution:
4+
def minWindow(self, s: str, t: str) -> str:
5+
need = Counter(t)
6+
missing = len(t)
7+
left = start = end = 0
8+
for right, char in enumerate(s):
9+
if need[char] > 0:
10+
missing -= 1
11+
need[char] -= 1
12+
while missing == 0:
13+
if end == 0 or right - left < end - start:
14+
start, end = left, right + 1
15+
need[s[left]] += 1
16+
if need[s[left]] > 0:
17+
missing += 1
18+
left += 1
19+
return s[start:end]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def pacificAtlantic(self, heights):
3+
if not heights:
4+
return []
5+
m, n = len(heights), len(heights[0])
6+
pacific = [[False] * n for _ in range(m)]
7+
atlantic = [[False] * n for _ in range(m)]
8+
def dfs(r, c, visited):
9+
visited[r][c] = True
10+
for dr, dc in [(-1,0), (1,0), (0,-1), (0,1)]:
11+
nr, nc = r + dr, c + dc
12+
if 0 <= nr < m and 0 <= nc < n and not visited[nr][nc] and heights[nr][nc] >= heights[r][c]:
13+
dfs(nr, nc, visited)
14+
for i in range(m):
15+
dfs(i, 0, pacific)
16+
dfs(i, n - 1, atlantic)
17+
for j in range(n):
18+
dfs(0, j, pacific)
19+
dfs(m - 1, j, atlantic)
20+
result = []
21+
for r in range(m):
22+
for c in range(n):
23+
if pacific[r][c] and atlantic[r][c]:
24+
result.append([r, c])
25+
return result
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# recheck needs to be done
2+
3+
class Solution:
4+
def getSum(self, a, b):
5+
MASK = 0xFFFFFFFF # 32๋น„ํŠธ ์ •์ˆ˜๋ฅผ ํ‰๋‚ด ๋‚ด๊ธฐ ์œ„ํ•œ ๋งˆ์Šคํฌ (์‹ญ์ง„์ˆ˜๋กœ๋Š” 4294967295)
6+
MAX_INT = 0x7FFFFFFF # 32๋น„ํŠธ์—์„œ์˜ ์–‘์ˆ˜ ์ตœ๋Œ“๊ฐ’ (2147483647)
7+
8+
while b != 0:
9+
# a ^ b: ์ž๋ฆฌ์˜ฌ๋ฆผ(carry) ์—†์ด ๋”ํ•œ ๊ฐ’
10+
# a & b: ๊ฐ™์€ ์ž๋ฆฌ์— 1์ด ์žˆ์œผ๋ฉด ์ž๋ฆฌ์˜ฌ๋ฆผ์ด ์ƒ๊ธด๋‹ค๋Š” ์˜๋ฏธ
11+
# (a & b) << 1: ์ž๋ฆฌ์˜ฌ๋ฆผ์„ ์™ผ์ชฝ์œผ๋กœ ์ด๋™ํ•˜์—ฌ ์‹ค์ œ ์˜ฌ๋ฆผ ์—ฐ์‚ฐ ์œ„์น˜๋กœ ๋งž์ถค
12+
a, b = (a ^ b) & MASK, ((a & b) << 1) & MASK
13+
14+
# ์ตœ์ข…์ ์œผ๋กœ a์— ์ •๋‹ต์ด ๋“ค์–ด๊ฐ
15+
# ๊ทธ๋Ÿฐ๋ฐ 32๋น„ํŠธ ๋ถ€ํ˜ธ์žˆ๋Š” ์ •์ˆ˜์—์„œ๋Š” ์–‘์ˆ˜ ์ตœ๋Œ€๊ฐ’์ด MAX_INT
16+
# a > MAX_INT๋ผ๋ฉด ์Œ์ˆ˜๋ผ๋Š” ๋œป์ด๋ฏ€๋กœ, ๋ณด์ˆ˜ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์คŒ
17+
return a if a <= MAX_INT else ~(a ^ MASK)

0 commit comments

Comments
ย (0)