일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- nlp
- 파이썬알고리즘
- 알고리즘
- 투포인터
- 자연어처리
- 생성형AI
- dfs
- Python
- stratascratch
- sql코테
- 리트코드
- GenAI
- SQL
- two-pointer
- 코드업
- GenerativeAI
- Python3
- 구글퀵랩
- heap
- slidingwindow
- codeup
- Microsoft
- 파이썬
- 파이썬기초100제
- 릿코드
- gcp
- 니트코드
- medium
- LeetCode
- 슬라이딩윈도우
- Today
- Total
목록LeetCode (13)
Tech for good

class Solution: def countGoodSubstrings(s: str) -> int: count = 0 for i in range(len(s) - 2): # 길이가 3인 모든 부분 문자열을 검사 substring = s[i:i+3] # 길이 3의 부분 문자열 추출 if len(set(substring)) == 3: # 중복 문자가 없는지 확인 count += 1 return countset() => 중복을 허용하지 않는 집합(set) 자료형을 생성하는 함수중복 제거: 리스트나 문자열 등의 중복된 요소를 자동으로 제거.순서 없음: 요소의 순서가 보장되지 않음.빠른 멤버십 테스트 ..

첫번째 방법 (Two-Pointer + Queue = Greedy)(1) Two-Pointer 두 개의 리스트 (positives, negatives)를 따로 저장한 후, 두 개의 포인터를 이용하여 한 번에 두 리스트를 합치는 방식 차용.여기서 zip(positives, negatives)를 사용하여 두 리스트를 같은 인덱스에서 번갈아가며 가져오는 방식을 적용.(2) Queue Python의 리스트 (list)를 Queue처럼 사용하여 양수와 음수를 저장했다가 순차적으로 가져와서 최종 리스트를 생성하는 방식.positives.append(num) 및 negatives.append(num) 연산은 O(1) 이므로 효율적임.여기서 zip(positives, negatives)를 사용하여 두 리스트를 같은 인..

1️⃣번째 방법class Solution: def reverseWords(self, s: str) -> str: return " ".join(s.split()[::-1]) 시간 복잡도 및 공간 복잡도시간 복잡도: O(n)split() → O(n)[::-1] (리스트 뒤집기) → O(n)" ".join() → O(n)따라서 전체 시간 복잡도는 O(n) 입니다.공간 복잡도: O(n)split()이 새로운 리스트를 생성하므로 O(n)의 추가 공간이 필요합니다.2️⃣번째 방법Follow-up: O(1) 추가 공간으로 해결할 수 있을까?문자열이 가변(mutable)한 경우, O(1) 공간에서 In-place 방식으로 해결할 수 있습니다.파이썬에서는 문자열이 **불변(immutable)**이라서 ..

class Solution: def isSubsequence(self, s: str, t: str) -> bool: i, j = 0, 0 # Pointers for s and t while i i += 1 -> s의 인덱스를 증가j += 1 -> s와 상관없이 (즉, 매치됨과 상관없이) t의 인덱스를 하나씩 증가 Example 2의 경우, x가 t에 없으므로 i False시간 복잡도 및 공간 복잡도시간 복잡도: O(n) (t의 길이만큼 한 번 순회)공간 복잡도: O(1) (추가적인 공간 사용 없음)

class Solution: def isPrefixOfWord(self, sentence: str, searchWord: str) -> int: words = sentence.split() for i, word in enumerate(words): if word.startwith(searchword): return i+1 return -1Explanation:Split the Sentence: The input sentence is split into a list of words using .split(), which automatically handles single spaces.Iterate Over Words:..

class Solution: def rotate(self, nums: List[int], k: int) -> None: n = len(nums) k = k % n # In case k is larger than n # Step 1: Reverse the entire array nums.reverse() # Step 2: Reverse the first k elements nums[:k] = reversed(nums[:k]) # Step 3: Reverse the remaining n-k elements nums[k:] = reversed(nums[k:]) ..

class Solution: def isPrefixOfWord(self, sentence: str, searchWord: str) -> int: words = sentence.split() for i, word in enumerate(words): if word.startwith(searchword): return i+1 return -1 Explanation:Split the Sentence: The input sentence is split into a list of words using .split(), which automatically handles single spaces.Iterate Over Words..

🔹 문제 풀이 접근이 문제는 최소한의 거래로 모든 부채를 정산하는 문제이다.즉, 누가 누구에게 돈을 줘야 하는지 최소한의 거래(transactions)로 해결하는 것이 목표이다.✅ 해결 방법: Backtracking 각 사람의 최종 잔액을 계산주어진 거래를 기반으로 각 사람의 순 잔액(balance)을 계산.예를 들어, 어떤 사람이 $10 빌려줬으면 +10, $5 빌렸으면 -5.잔액이 0이 아닌 사람들을 대상으로 최소 거래 조합 찾기부채(- 잔액)와 자산(+ 잔액)을 매칭하여 최소 거래로 해결.백트래킹(Backtracking)을 활용하여 모든 가능한 거래를 탐색.from collections import defaultdictclass Solution: def minTransfers(self, tr..

https://leetcode.com/problems/median-of-two-sorted-arrays/description/ 🔹 문제 풀이 접근이 문제는 두 개의 정렬된 배열(nums1, nums2)이 주어질 때, 병합 후 중앙값(median)을 찾는 문제이다. ✅ 최적화 방법 (이진 탐색 활용)이진 탐색(Binary Search)을 사용하여 한 배열을 절반으로 분할하면서 답을 찾음.시간 복잡도: O(log(min(m, n)))class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: # nums1이 항상 짧은 배열이 되도록 보장 if len(nums1) > l..

알고리즘:주어진 문자열을 파싱하여 문자 -> 빈도 형태로 해시맵에 저장해시맵의 키(문자)를 알파벳 순으로 정렬최종 결과 문자열을 생성from collections import defaultdictclass Solution: def betterCompression(self, compressed: str) -> str: freq_map = defaultdict(int) i = 0 n = len(compressed) # 문자열을 순회하면서 문자와 숫자 파싱 while i 🔹 시간 복잡도:문자열을 한 번 순회하면서 파싱 (O(N))해시맵을 정렬 (O(K log K), K는 문자 종류의 개수, 최대 26)결과 생성 (O(K))총..