Hello anybody can say me what mean when the problem have a tag two pointers. What mean this approach?
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 151 |
Hello anybody can say me what mean when the problem have a tag two pointers. What mean this approach?
Название |
---|
Look here
One of the recent problems involving two pointers is problem I:Pirate Chest from ICPC world finals 2013
This problem reduces to one-dimensional case: in integer array, for each element you want to find element that is lower than current on the left and right sides.
For every element you keep left and right positions for the element which is less than this element. (initially set to itselft). Iterate over descendant sorted array and update your info about pointers.
The problem's time limit pretty large. But involving some other NlogN approaches (such as fenwick tree) gives TL. By pointers you achieve lower constant.
+1
Two pointers is really an easy and effective technique that is typically used for searching pairs in a sorted array. Given a sorted array A (sorted in ascending order), having N integers, find if there exists any pair of elements (A[i], A[j]) such that their sum is equal to X.
A[] = {10, 20, 35, 50, 75, 80} X = =70 i = 0 j = 5 A[i] + A[j] = 10 + 80 = 90 Since A[i] + A[j] > X, j-- i = 0 j = 4 A[i] + A[j] = 10 + 75 = 85 Since A[i] + A[j] > X, j-- i = 0 j = 3 A[i] + A[j] = 10 + 50 = 60 Since A[i] + A[j] < X, i++ i = 1 j = 3 m A[i] + A[j] = 20 + 50 = 70 Thus this signifies that Pair is Found.
Let us do discuss the working of two pointer algorithm in brief which is as follows. The algorithm basically uses the fact that the input array is sorted. We start the sum of extreme values (smallest and largest) and conditionally move both pointers. We move left pointer ‘i’ when the sum of A[i] and A[j] is less than X. We do not miss any pair because the sum is already smaller than X. Same logic applies for right pointer j.
Time Complexity: O(n2). Auxiliary Space: O(1)
Lets solve EDU problem:
step 1:
Merging Arrays
Number of Smaller
Number of Equal
step 2:
Segment with Small Sum
Segment with Big Sum
Number of Segments with Small Sum
Number of Segments with Big Sum
Segments with Small Set
Segments with Small Spread
Coprime Segment
step 3:
Looped Playlist
Total Length
Che city
Stylish clothes
Knapsack on a Segment
Card Substrings
Not Very Rude Substring
A-B Knapsack
Segment with the Required Subset
all you need to two pointer