Hello anybody can say me what mean when the problem have a tag two pointers. What mean this approach?
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3831 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | gamegame | 3386 |
10 | ksun48 | 3373 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 153 |
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