1. Two pointers

FrugalBoy
2025.01.23조회수 3회

FrugalBoy
구독자 5명구독중 12명
Wanna live like a frugal boy
For the genetically superior, success is easier to attain.
But it is by no means guaranteed.
After all, there is no gene for fate
-Gattaca-


유형
left = 0, right = n-1
while(left < right){
if() left++;
if() right--;
}이때 two pointer를 쓰려면 sorted 된 array이어야한다
그렇지 않으면 TC상 이점이 없음
-> ex. Two Sum 문제의 경우 Sort 되지 않은 array면 hashmap으로 푸는게 two pointers로 푸는 것보다 빠름
예제:
https://leetcode.com/problems/reverse-string/description/
https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/