diff --git a/decode-ways/devyulbae.py b/decode-ways/devyulbae.py new file mode 100644 index 0000000000..92e25da9a6 --- /dev/null +++ b/decode-ways/devyulbae.py @@ -0,0 +1,3 @@ +class Solution: + def numDecodings(self, s: str) -> int: + diff --git a/longest-substring-without-repeating-characters/devyulbae.py b/longest-substring-without-repeating-characters/devyulbae.py new file mode 100644 index 0000000000..f0d1181510 --- /dev/null +++ b/longest-substring-without-repeating-characters/devyulbae.py @@ -0,0 +1,25 @@ +""" +Blind75 - length of longest substring without repeating characters +https://leetcode.com/problems/longest-substring-without-repeating-characters/ +시간복잡도 : O(n) +공간복잡도 : O(min(m, n)) (문자 집합 char_index_map의 크기, 최대는 n = len(s)) +풀이 : 슬라이딩 윈도우 기법을 사용한 문자열 순회 +""" + + +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + max_count = 0 + start = 0 + char_index_map = {} + + for i, char in enumerate(s): + if char in char_index_map and char_index_map[char] >= start: + start = char_index_map[char] + 1 + char_index_map[char] = i + else: + char_index_map[char] = i + max_count = max(max_count, i - start + 1) + + return max_count + diff --git a/maximum-subarray/devyulbae.py b/maximum-subarray/devyulbae.py new file mode 100644 index 0000000000..d3f5cda001 --- /dev/null +++ b/maximum-subarray/devyulbae.py @@ -0,0 +1,20 @@ +""" +Blind75 - 5. Maximum Subarray +LeetCode Problem Link: https://leetcode.com/problems/maximum-subarray/ + +통과는 했는데 시간복잡도 O(n^2)라서 좀 아쉽다...투포인터로 풀 수도 있을 거 같은데... +""" +from typing import List + +class Solution: + def maxSubArray(self, nums: List[int]) -> int: + max_total = nums[0] + + for s in range(len(nums)): + total = 0 + for e in range(s, len(nums)): + total += nums[e] + if total > max_total: + max_total = total + return max_total + diff --git a/number-of-1-bits/devyulbae.py b/number-of-1-bits/devyulbae.py new file mode 100644 index 0000000000..64b0c59bd8 --- /dev/null +++ b/number-of-1-bits/devyulbae.py @@ -0,0 +1,17 @@ +""" +Blind75 - Number of 1 Bits +https://leetcode.com/problems/number-of-1-bits/ +시간복잡도 : O(log n) +공간복잡도 : O(1) + +풀이 : + 파이썬의 내장함수 bin() -> O(log n) + 문자열 메서드 count() -> O(log n) + +""" + +class Solution: + def hammingWeight(self, n: int) -> int: + return str(bin(n)).count('1') + + diff --git a/valid-palindrome/devyulbae.py b/valid-palindrome/devyulbae.py new file mode 100644 index 0000000000..61f5244db3 --- /dev/null +++ b/valid-palindrome/devyulbae.py @@ -0,0 +1,23 @@ +""" +Blind75 - Valid Palindrome +https://leetcode.com/problems/valid-palindrome/ + +시간복잡도 O(n), 공간복잡도 O(n) + +isalnum() 함수는 시간복잡도 O(1)이므로 필터링하는 과정도 O(n)이다. +[::-1] 슬라이싱도 O(n)이다. +따라서 전체 시간복잡도는 O(n)이다. + + +Runtime Beats +7ms 82.67% + +Memory Beats +23.14 MB 17.23% +""" + +class Solution: + def isPalindrome(self, s: str) -> bool: + filtered_s = ''.join(char.lower() for char in s if char.isalnum()) + return filtered_s == filtered_s[::-1] +