Hello anybody can say me what mean when the problem have a tag two pointers. What mean this approach?
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
Hello anybody can say me what mean when the problem have a tag two pointers. What mean this approach?
Name |
---|
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