Skip to content

Commit eb015aa

Browse files
authored
Merge pull request #971 from dusunax/main
[SunaDu] Week8
2 parents 1407d9c + a552e11 commit eb015aa

File tree

5 files changed

+151
-0
lines changed

5 files changed

+151
-0
lines changed

โ€Žclone-graph/dusunax.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'''
2+
# 133. Clone Graph
3+
This is the problem for copying nodes, which helps you understand the concept of referencing a node & copying the node (creating a new node from the existing one).
4+
5+
๐Ÿ‘‰ Perform recursion DFS with the correct escape condition and handling of NoneType.
6+
'''
7+
8+
"""
9+
# Definition for a Node.
10+
class Node:
11+
def __init__(self, val = 0, neighbors = None):
12+
self.val = val
13+
self.neighbors = neighbors if neighbors is not None else []
14+
"""
15+
from typing import Optional
16+
class Solution:
17+
def cloneGraph(self, node: Optional['Node']) -> Optional['Node']:
18+
if node is None:
19+
return None
20+
21+
visited = {}
22+
23+
def DFS(currNode):
24+
if currNode.val in visited:
25+
return visited[currNode.val]
26+
27+
copiedNode = Node(currNode.val)
28+
visited[currNode.val] = copiedNode
29+
30+
for neighbor in currNode.neighbors:
31+
copiedNode.neighbors.append(DFS(neighbor))
32+
33+
return copiedNode
34+
35+
return DFS(node)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'''
2+
# 1143. Longest Common Subsequence
3+
4+
use DP table.
5+
6+
> key: tuple(current index in text1, current index in text2)
7+
> value: LCS of text1[i:] and text2[j:]
8+
'''
9+
class Solution:
10+
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
11+
dp = {}
12+
13+
def helper(i, j):
14+
if i == len(text1) or j == len(text2):
15+
return 0
16+
17+
if (i, j) in dp:
18+
return dp[(i, j)]
19+
20+
if text1[i] == text2[j]:
21+
dp[(i, j)] = 1 + helper(i + 1, j + 1)
22+
else:
23+
dp[(i, j)] = max(helper(i + 1, j), helper(i, j + 1))
24+
25+
return dp[(i, j)]
26+
27+
return helper(0, 0)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
# 424. Longest Repeating Character Replacement
3+
4+
use sliding window to find the longest substring with at most k replacements.
5+
6+
## Time and Space Complexity
7+
8+
```
9+
TC: O(n)
10+
SC: O(1)
11+
```
12+
'''
13+
14+
class Solution:
15+
def characterReplacement(self, s: str, k: int) -> int:
16+
freq = [0] * 26 # A~Z, SC: O(1)
17+
left = 0
18+
max_freq = 0
19+
result = 0
20+
21+
for right in range(len(s)): # TC: O(n)
22+
curr_char_index = ord(s[right]) - 65
23+
freq[curr_char_index] += 1
24+
max_freq = max(max_freq, freq[curr_char_index])
25+
26+
if (right - left + 1) - max_freq > k:
27+
freq[ord(s[left]) - 65] -= 1
28+
left += 1
29+
30+
result = max(result, right - left + 1)
31+
32+
return result

โ€Žnumber-of-1-bits/dusunax.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def hammingWeight(self, n: int) -> int:
3+
binary_string = bin(n).replace('0b', '');
4+
return binary_string.count('1');

โ€Žsum-of-two-integers/dusunax.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'''
2+
# ๋น„ํŠธ ์—ฐ์‚ฐ Bit Manipulation
3+
4+
## ์ด์ง„์ˆ˜ ๋ง์…ˆ binary addition
5+
6+
์ด์ง„์ˆ˜์—์„œ ๋‘ ๋น„ํŠธ๋ฅผ ๋”ํ•˜๊ธฐ
7+
8+
1. XOR: ์˜ฌ๋ฆผ์ˆ˜๋ฅผ ์ œ์™ธํ•œ ํ•ฉ
9+
2. AND << 1: ์˜ฌ๋ฆผ์ˆ˜๋ฅผ ๊ณ„์‚ฐ
10+
3. 1~2๋ฅผ carry๊ฐ€ 0์ผ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
11+
12+
```
13+
while b:
14+
carry = (a & b) << 1 # ์˜ฌ๋ฆผ์ˆ˜๋ฅผ ๊ณ„์‚ฐ
15+
a = (a ^ b) # ํ•ฉ
16+
b = carry # ์บ๋ฆฌ๊ฐ€ ๋‚จ์•„์žˆ๋‹ค๋ฉด ๊ณ„์† ๊ณ„์‚ฐ
17+
```
18+
19+
## 32๋น„ํŠธ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ ์ œ๊ฑฐ
20+
21+
- 0xFFFFFFFF๋Š” ๋ชจ๋“  ๋น„ํŠธ๊ฐ€ 1์ž…๋‹ˆ๋‹ค.
22+
23+
Python์˜ int๋Š” ํฌ๊ธฐ ์ œํ•œ์ด ์—†์–ด์„œ ์—ฐ์‚ฐ ์ค‘ ๋น„ํŠธ ์ˆ˜๊ฐ€ ์ž๋™ ํ™•์žฅ๋ฉ๋‹ˆ๋‹ค.
24+
MASK(0xFFFFFFFF)์™€ and ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•ด 32๋น„ํŠธ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๋ฅผ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค.
25+
26+
```
27+
MASK = 0xFFFFFFFF
28+
a = (a ^ b) & MASK # 32๋น„ํŠธ๊ฐ€ ๋„˜์–ด๊ฐ€๋ฉด ์ดˆ๊ณผ ๋น„ํŠธ๊ฐ€ ์ œ๊ฑฐ๋จ
29+
b = carry & MASK
30+
```
31+
32+
## ์Œ์ˆ˜ ์ฒ˜๋ฆฌ
33+
34+
- 0x7FFFFFFF(2147483647) ์ดํ•˜๋Š” ์–‘์ˆ˜(Positive Number).
35+
- 0x80000000(2147483648) ์ด์ƒ์ด๋ฉด ์Œ์ˆ˜(Negative Number)
36+
37+
`a > MAX_INT(0x7FFFFFFF)`์ธ ๊ฒฝ์šฐ, 32๋น„ํŠธ ํ™˜๊ฒฝ์—์„œ ์Œ์ˆ˜ ๊ฐ’์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
38+
39+
```
40+
~(a ^ MASK) # 32๋น„ํŠธ ์Œ์ˆ˜ ๋ณ€ํ™˜
41+
```
42+
'''
43+
class Solution:
44+
def getSum(self, a: int, b: int) -> int:
45+
MASK = 0xFFFFFFFF
46+
MAX_INT = 0x7FFFFFFF
47+
48+
while b:
49+
carry = (a & b) << 1
50+
a = (a ^ b) & MASK
51+
b = carry & MASK
52+
53+
return a if a <= MAX_INT else ~(a ^ MASK) # a๊ฐ€ MAX_INT๋ณด๋‹ค ํฌ๋ฉด 32๋น„ํŠธ ํ™˜๊ฒฝ์—์„œ ์Œ์ˆ˜์ด๋ฏ€๋กœ ๋ณ€ํ™˜

0 commit comments

Comments
ย (0)