diff --git a/clone-graph/dusunax.py b/clone-graph/dusunax.py new file mode 100644 index 000000000..c5b28d925 --- /dev/null +++ b/clone-graph/dusunax.py @@ -0,0 +1,35 @@ +''' +# 133. Clone Graph +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). + +๐Ÿ‘‰ Perform recursion DFS with the correct escape condition and handling of NoneType. +''' + +""" +# Definition for a Node. +class Node: + def __init__(self, val = 0, neighbors = None): + self.val = val + self.neighbors = neighbors if neighbors is not None else [] +""" +from typing import Optional +class Solution: + def cloneGraph(self, node: Optional['Node']) -> Optional['Node']: + if node is None: + return None + + visited = {} + + def DFS(currNode): + if currNode.val in visited: + return visited[currNode.val] + + copiedNode = Node(currNode.val) + visited[currNode.val] = copiedNode + + for neighbor in currNode.neighbors: + copiedNode.neighbors.append(DFS(neighbor)) + + return copiedNode + + return DFS(node) diff --git a/longest-common-subsequence/dusunax.py b/longest-common-subsequence/dusunax.py new file mode 100644 index 000000000..1339dbab2 --- /dev/null +++ b/longest-common-subsequence/dusunax.py @@ -0,0 +1,27 @@ +''' +# 1143. Longest Common Subsequence + +use DP table. + +> key: tuple(current index in text1, current index in text2) +> value: LCS of text1[i:] and text2[j:] +''' +class Solution: + def longestCommonSubsequence(self, text1: str, text2: str) -> int: + dp = {} + + def helper(i, j): + if i == len(text1) or j == len(text2): + return 0 + + if (i, j) in dp: + return dp[(i, j)] + + if text1[i] == text2[j]: + dp[(i, j)] = 1 + helper(i + 1, j + 1) + else: + dp[(i, j)] = max(helper(i + 1, j), helper(i, j + 1)) + + return dp[(i, j)] + + return helper(0, 0) diff --git a/longest-repeating-character-replacement/dusunax.py b/longest-repeating-character-replacement/dusunax.py new file mode 100644 index 000000000..01cec1870 --- /dev/null +++ b/longest-repeating-character-replacement/dusunax.py @@ -0,0 +1,32 @@ +''' +# 424. Longest Repeating Character Replacement + +use sliding window to find the longest substring with at most k replacements. + +## Time and Space Complexity + +``` +TC: O(n) +SC: O(1) +``` +''' + +class Solution: + def characterReplacement(self, s: str, k: int) -> int: + freq = [0] * 26 # A~Z, SC: O(1) + left = 0 + max_freq = 0 + result = 0 + + for right in range(len(s)): # TC: O(n) + curr_char_index = ord(s[right]) - 65 + freq[curr_char_index] += 1 + max_freq = max(max_freq, freq[curr_char_index]) + + if (right - left + 1) - max_freq > k: + freq[ord(s[left]) - 65] -= 1 + left += 1 + + result = max(result, right - left + 1) + + return result diff --git a/number-of-1-bits/dusunax.py b/number-of-1-bits/dusunax.py new file mode 100644 index 000000000..e422a652a --- /dev/null +++ b/number-of-1-bits/dusunax.py @@ -0,0 +1,4 @@ +class Solution: + def hammingWeight(self, n: int) -> int: + binary_string = bin(n).replace('0b', ''); + return binary_string.count('1'); diff --git a/sum-of-two-integers/dusunax.py b/sum-of-two-integers/dusunax.py new file mode 100644 index 000000000..8e5763179 --- /dev/null +++ b/sum-of-two-integers/dusunax.py @@ -0,0 +1,53 @@ +''' +# ๋น„ํŠธ ์—ฐ์‚ฐ Bit Manipulation + +## ์ด์ง„์ˆ˜ ๋ง์…ˆ binary addition + +์ด์ง„์ˆ˜์—์„œ ๋‘ ๋น„ํŠธ๋ฅผ ๋”ํ•˜๊ธฐ + +1. XOR: ์˜ฌ๋ฆผ์ˆ˜๋ฅผ ์ œ์™ธํ•œ ํ•ฉ +2. AND << 1: ์˜ฌ๋ฆผ์ˆ˜๋ฅผ ๊ณ„์‚ฐ +3. 1~2๋ฅผ carry๊ฐ€ 0์ผ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต + +``` +while b: + carry = (a & b) << 1 # ์˜ฌ๋ฆผ์ˆ˜๋ฅผ ๊ณ„์‚ฐ + a = (a ^ b) # ํ•ฉ + b = carry # ์บ๋ฆฌ๊ฐ€ ๋‚จ์•„์žˆ๋‹ค๋ฉด ๊ณ„์† ๊ณ„์‚ฐ +``` + +## 32๋น„ํŠธ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ ์ œ๊ฑฐ + +- 0xFFFFFFFF๋Š” ๋ชจ๋“  ๋น„ํŠธ๊ฐ€ 1์ž…๋‹ˆ๋‹ค. + +Python์˜ int๋Š” ํฌ๊ธฐ ์ œํ•œ์ด ์—†์–ด์„œ ์—ฐ์‚ฐ ์ค‘ ๋น„ํŠธ ์ˆ˜๊ฐ€ ์ž๋™ ํ™•์žฅ๋ฉ๋‹ˆ๋‹ค. +MASK(0xFFFFFFFF)์™€ and ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•ด 32๋น„ํŠธ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๋ฅผ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค. + +``` +MASK = 0xFFFFFFFF +a = (a ^ b) & MASK # 32๋น„ํŠธ๊ฐ€ ๋„˜์–ด๊ฐ€๋ฉด ์ดˆ๊ณผ ๋น„ํŠธ๊ฐ€ ์ œ๊ฑฐ๋จ +b = carry & MASK +``` + +## ์Œ์ˆ˜ ์ฒ˜๋ฆฌ + +- 0x7FFFFFFF(2147483647) ์ดํ•˜๋Š” ์–‘์ˆ˜(Positive Number). +- 0x80000000(2147483648) ์ด์ƒ์ด๋ฉด ์Œ์ˆ˜(Negative Number) + +`a > MAX_INT(0x7FFFFFFF)`์ธ ๊ฒฝ์šฐ, 32๋น„ํŠธ ํ™˜๊ฒฝ์—์„œ ์Œ์ˆ˜ ๊ฐ’์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. + +``` +~(a ^ MASK) # 32๋น„ํŠธ ์Œ์ˆ˜ ๋ณ€ํ™˜ +``` +''' +class Solution: + def getSum(self, a: int, b: int) -> int: + MASK = 0xFFFFFFFF + MAX_INT = 0x7FFFFFFF + + while b: + carry = (a & b) << 1 + a = (a ^ b) & MASK + b = carry & MASK + + return a if a <= MAX_INT else ~(a ^ MASK) # a๊ฐ€ MAX_INT๋ณด๋‹ค ํฌ๋ฉด 32๋น„ํŠธ ํ™˜๊ฒฝ์—์„œ ์Œ์ˆ˜์ด๋ฏ€๋กœ ๋ณ€ํ™˜