Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. A small frog wants to get to the other side of a river. The frog is currently located at position 0, and wants to get to position X. Leaves fall from a tree onto the surface of the river.
  2.  
  3. You are given a non-empty zero-indexed array A consisting of N integers representing the falling leaves. A[K] represents the position where one leaf falls at time K, measured in seconds.
  4.  
  5. The goal is to find the earliest time when the frog can jump to the other side of the river. The frog can cross only when leaves appear at every position across the river from 1 to X. You may assume that the speed of the current in the river is negligibly small, i.e. the leaves do not change their positions once they fall in the river.
  6.  
  7. For example, you are given integer X = 5 and array A such that:
  8.  
  9. A[0] = 1
  10. A[1] = 3
  11. A[2] = 1
  12. A[3] = 4
  13. A[4] = 2
  14. A[5] = 3
  15. A[6] = 5
  16. A[7] = 4
  17. In second 6, a leaf falls into position 5. This is the earliest time when leaves appear in every position across the river.
  18.  
  19. Write a function:
  20.  
  21. int solution(int X, int A[], int N);
  22.  
  23. that, given a non-empty zero-indexed array A consisting of N integers and integer X, returns the earliest time when the frog can jump to the other side of the river.
  24.  
  25. If the frog is never able to jump to the other side of the river, the function should return ?1.
  26.  
  27. For example, given X = 5 and array A such that:
  28.  
  29. A[0] = 1
  30. A[1] = 3
  31. A[2] = 1
  32. A[3] = 4
  33. A[4] = 2
  34. A[5] = 3
  35. A[6] = 5
  36. A[7] = 4
  37. the function should return 6, as explained above.
  38.  
  39. Assume that:
  40.  
  41. N and X are integers within the range [1..100,000];
  42. each element of array A is an integer within the range [1..X].
  43. Complexity:
  44.  
  45. expected worst-case time complexity is O(N);
  46. expected worst-case space complexity is O(X), beyond input storage (not counting the storage required for input arguments).
  47. Elements of input arrays can be modified.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement