시간 복잡도는 이제 막 문제 해결을 시작한 초보자에게 가장 어려운 주제 중 하나로 간주됩니다. 여기서는 시간 복잡도 분석 치트 시트를 제공합니다. 이것이 도움이 되기를 바랍니다. 궁금한 점이 있으면 알려주세요.
O(1) - Constant time O(log n) - Logarithmic (halving/doubling) O(n) - Linear (single loop) O(n log n) - Linearithmic (efficient sorting) O(n²) - Quadratic (nested loops) O(2ⁿ) - Exponential (recursive doubling) O(n!) - Factorial (permutations)
# Look for: - Direct array access - Basic math operations - Fixed loops - Hash table lookups # Examples: arr[0] x + y for i in range(5) hashmap[key]
# Look for: - Halving/Doubling - Binary search patterns - Tree traversal by level # Examples: while n > 0: n = n // 2 left, right = 0, len(arr)-1 while left <= right: mid = (left + right) // 2
# Look for: - Single loops - Array traversal - Linear search - Hash table building # Examples: for num in nums: # O(1) operation total += num for i in range(n): # O(1) operation arr[i] = i
# Look for: - Efficient sorting - Divide and conquer - Tree operations with traversal # Examples: nums.sort() sorted(nums) merge_sort(nums)
# Look for: - Nested loops - Simple sorting - Matrix traversal - Comparing all pairs # Examples: for i in range(n): for j in range(n): # O(1) operation # Pattern finding for i in range(n): for j in range(i+1, n): # Compare pairs
# Look for: - Double recursion - Power set - Fibonacci recursive - All subsets # Examples: def fib(n): if n <= 1: return n return fib(n-1) + fib(n-2) def subsets(nums): if not nums: return [[]] result = subsets(nums[1:]) return result + [nums[0:1] + r for r in result]
# O(1) arr[i] # Access arr.append(x) # Add end arr.pop() # Remove end # O(n) arr.insert(i, x) # Insert middle arr.remove(x) # Remove by value arr.index(x) # Find index min(arr), max(arr) # Find min/max
# O(1) average d[key] # Access d[key] = value # Insert key in d # Check existence d.get(key) # Get value # O(n) len(d) # Size d.keys() # Get keys d.values() # Get values
# O(n) s + t # Concatenation s.find(t) # Substring search s.replace(old, new) # Replace ''.join(list) # Join # O(n²) potential s += char # Repeated concatenation
# O(n) for i in range(n): # O(1) operations # O(n/2) = O(n) for i in range(0, n, 2): # Skip elements still O(n)
# O(n²) for i in range(n): for j in range(n): # O(1) operations # O(n * m) for i in range(n): for j in range(m): # Different sizes # O(n²/2) = O(n²) for i in range(n): for j in range(i, n): # Triangular still O(n²)
# O(n + m) for i in range(n): # O(1) for j in range(m): # O(1) # O(n + n²) = O(n²) for i in range(n): # O(1) for i in range(n): for j in range(n): # O(1)
# O(n) def factorial(n): if n <= 1: return 1 return n * factorial(n-1)
# O(2ⁿ) def fibonacci(n): if n <= 1: return n return fibonacci(n-1) + fibonacci(n-2)
# O(n log n) def mergeSort(arr): if len(arr) <= 1: return arr mid = len(arr) // 2 left = mergeSort(arr[:mid]) right = mergeSort(arr[mid:]) return merge(left, right)
# String operations for c in string: newStr += c # O(n²) # List comprehension [x for x in range(n) for y in range(n)] # O(n²)
len() # O(1) min(), max() # O(n) sorted() # O(n log n) list.index() # O(n)
읽어 주셔서 감사합니다. 이 글이 도움이 되었다면 게시물에 좋아요를 눌러주세요. 건배!
위 내용은 알고리즘의 시간 복잡도를 찾는 쉬운 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!