one line of code at a time

투포인터 알고리즘 본문

자료구조 || 알고리즘

투포인터 알고리즘

oloc 2024. 6. 23. 18:20

Given an array of sorted integers, find two numbers such that they add up to a target number.

Questions to ask:

- What does my input look like?

- Will the sum cause integer overflow?

 

[0, 1, 2, 3, 4, 5]

- O(n^2) for loop 2개 쓰는 것 ➡️ Brue-Force approach

- O(N log N) 알고리즘: Sort and Scan, Scan through array and use binary search

- 만약에 array가 정렬되어 있다면, 항상 binary search를 쓸 수 있는지 체크해보기.

- O(N): array sorted 되어 있고, front와 back pointer를 써가면 한 번만에 모든 element 스캔 가능

 

관련 문제

Leet code 167

 

참고

Visual introduction Two Pointer Algorithm | Data Structure and Algorithm for Coding Interviews

'자료구조 || 알고리즘' 카테고리의 다른 글

Top 5 Graph Algorithms for Interview  (0) 2024.09.23
quick sort  (1) 2024.09.23
Graph + DFS  (0) 2024.08.18
주사위 던지기로 중복순열, 순열, 중복조합, 조합 코드 짜기  (0) 2024.08.08
Heap  (0) 2024.08.01