Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create SlalomSkiing #2

Merged
merged 1 commit into from
Oct 6, 2017
Merged

Create SlalomSkiing #2

merged 1 commit into from
Oct 6, 2017

Conversation

lkitrossky
Copy link
Contributor

https://codility.com/programmers/lessons/90-tasks_from_indeed_prime_2015_challenge/slalom_skiing/
You are a skier participating in a giant slalom. The slalom track is located on a ski slope, goes downhill and is fenced by barriers on both sides. The barriers are perpendicular to the starting line located at the top of the slope. There are N slalom gates on the track. Each gate is placed at a distinct distance from the starting line and from the barrier on the right-hand side (looking downhill).
You start from any place on the starting line, ski down the track passing as many gates as possible, and finish the slalom at the bottom of the slope. Passing a gate means skiing through the position of the gate.
You can ski downhill in either of two directions: to the left or to the right. When you ski to the left, you pass gates of increasing distances from the right barrier, and when you ski to the right, you pass gates of decreasing distances from the right barrier. You want to ski to the left at the beginning.
Unfortunately, changing direction (left to right or vice versa) is exhausting, so you have decided to change direction at most two times during your ride. Because of this, you have allowed yourself to miss some of the gates on the way down the slope. You would like to know the maximum number of gates that you can pass with at most two changes of direction.
The arrangement of the gates is given as an array A consisting of N integers, whose elements specify the positions of the gates: gate K (for 0 ≤ K < N) is at a distance of K+1 from the starting line, and at a distance of A[K] from the right barrier.
For example, consider array A such that:
A[0] = 15 A[1] = 13 A[2] = 5 A[3] = 7 A[4] = 4 A[5] = 10 A[6] = 12 A[7] = 8 A[8] = 2 A[9] = 11 A[10] = 6 A[11] = 9 A[12] = 3

The picture above illustrates the example track with N = 13 gates and a course that passes eight gates. After starting, you ski to the left (from your own perspective). You pass gates 2, 3, 5, 6 and then change direction to the right. After that you pass gates 7, 8 and then change direction to the left. Finally, you pass gates 10, 11 and finish the slalom. There is no possible way of passing more gates using at most two changes of direction.
Write a function:
int solution(vector &A);
that, given a zero-indexed array A consisting of N integers, describing the positions of the gates on the track, returns the maximum number of gates that you can pass during one ski run.
For example, given the above data, the function should return 8, as explained above.
For the following array A consisting of N = 2 elements:
A[0] = 1 A[1] = 5
the function should return 2.
Assume that:
• N is an integer within the range [1..100,000];
• each element of array A is an integer within the range [1..1,000,000,000];
• the elements of A are all distinct.
Complexity:
• expected worst-case time complexity is O(N*log(N));
• expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.

https://codility.com/programmers/lessons/90-tasks_from_indeed_prime_2015_challenge/slalom_skiing/
You are a skier participating in a giant slalom. The slalom track is located on a ski slope, goes downhill and is fenced by barriers on both sides. The barriers are perpendicular to the starting line located at the top of the slope. There are N slalom gates on the track. Each gate is placed at a distinct distance from the starting line and from the barrier on the right-hand side (looking downhill).
You start from any place on the starting line, ski down the track passing as many gates as possible, and finish the slalom at the bottom of the slope. Passing a gate means skiing through the position of the gate.
You can ski downhill in either of two directions: to the left or to the right. When you ski to the left, you pass gates of increasing distances from the right barrier, and when you ski to the right, you pass gates of decreasing distances from the right barrier. You want to ski to the left at the beginning.
Unfortunately, changing direction (left to right or vice versa) is exhausting, so you have decided to change direction at most two times during your ride. Because of this, you have allowed yourself to miss some of the gates on the way down the slope. You would like to know the maximum number of gates that you can pass with at most two changes of direction.
The arrangement of the gates is given as an array A consisting of N integers, whose elements specify the positions of the gates: gate K (for 0 ≤ K < N) is at a distance of K+1 from the starting line, and at a distance of A[K] from the right barrier.
For example, consider array A such that:
A[0] = 15 A[1] = 13 A[2] = 5 A[3] = 7 A[4] = 4 A[5] = 10 A[6] = 12 A[7] = 8 A[8] = 2 A[9] = 11 A[10] = 6 A[11] = 9 A[12] = 3
 
The picture above illustrates the example track with N = 13 gates and a course that passes eight gates. After starting, you ski to the left (from your own perspective). You pass gates 2, 3, 5, 6 and then change direction to the right. After that you pass gates 7, 8 and then change direction to the left. Finally, you pass gates 10, 11 and finish the slalom. There is no possible way of passing more gates using at most two changes of direction.
Write a function:
int solution(vector<int> &A);
that, given a zero-indexed array A consisting of N integers, describing the positions of the gates on the track, returns the maximum number of gates that you can pass during one ski run.
For example, given the above data, the function should return 8, as explained above.
For the following array A consisting of N = 2 elements:
A[0] = 1 A[1] = 5
the function should return 2.
Assume that:
•	N is an integer within the range [1..100,000];
•	each element of array A is an integer within the range [1..1,000,000,000];
•	the elements of A are all distinct.
Complexity:
•	expected worst-case time complexity is O(N*log(N));
•	expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.
@lkitrossky
Copy link
Contributor Author

A solution to Indeed prime 2015 challenge as appears in Codility lesson 90

@kalwar kalwar merged commit 81bbe05 into kalwar:master Oct 6, 2017
@lkitrossky
Copy link
Contributor Author

Hi! Can you explain a bit what kind of algorithm does ZigZagEscape? Even the name will be very good or a keyword. Thanks, Levi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants