일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- Python
- 슬라이딩윈도우
- 니트코드
- 릿코드
- gcp
- 코드업
- 파이썬
- 자연어처리
- Microsoft
- two-pointer
- stratascratch
- 리트코드
- 파이썬기초100제
- GenAI
- codeup
- dfs
- nlp
- 구글퀵랩
- medium
- 알고리즘
- 생성형AI
- heap
- GenerativeAI
- SQL
- 투포인터
- 파이썬알고리즘
- slidingwindow
- Python3
- LeetCode
- sql코테
- Today
- Total
목록Python3 (19)
Tech for good

해결 방법: 투 포인터(Two Pointers)이 문제를 O(n log n) 정렬로 해결하는 것은 쉽지만, O(n) 시간 복잡도로 해결하려면 투 포인터(Two Pointers) 기법을 사용해야 한다.핵심 아이디어음수가 포함된 정렬된 배열에서, 가장 큰 제곱값은 절댓값이 가장 큰 수에서 나온다.배열에서 절댓값이 가장 큰 숫자는 양쪽 끝에 위치해 있다. (가장 작은 수는 음수이고, 가장 큰 수는 양수이므로)왼쪽(left)과 오른쪽(right)에서 시작하는 두 개의 포인터를 사용하여 가장 큰 제곱값을 뒤에서부터 채운다.class Solution: def sortedSquares(self, nums: List[int]) -> List[int]: n = len(nums) # 주어진 배열의 길이..

풀이1) set 자료구조 사용class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: return list(set(nums1) & set(nums2))시간 복잡도 분석set(nums1): O(N)set(nums2): O(M)set1 & set2: O(min(N, M))list(...): O(K) (교집합 원소 수를 K라고 가정)➡️ 최종 시간 복잡도: O(N + M) (매우 효율적)풀이2) two-pointer 사용class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: ..

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..