Advertisement
Guest User

Untitled

a guest
May 4th, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.69 KB | None | 0 0
  1. print(https://www.codechef.com/problems/PSPLIT)
  2. A non-empty zero-indexed array A consisting of N integers is given. Any integer k, such that 0 <= K < N-1 , splits array A into two non-empty parts: A[0],A[1]...,A[K] and A[K+1],A[K+2],...,A[N-1]
  3.  
  4. The difference between the two parts is the value of abs(max{A[0],A[1],...A[K]} - max{A[K+1],A[K+2],...A[N-1]}), where abs is absolute difference function. For example in following Array A: {1,3,-3} Can be divided in two places:
  5. For K=0, the first part contains just 1, and the second part contains numbers: 3 and -3. The difference is abs(1-3) = 2
  6. For K=1, the first part contains numbers: 1 and 3 and the second part conatins just -3. The difference is abs (3-(-3)) = 6
  7. Given a non empty zero indexed array A containing the N integers, find the maximum difference between the two parts into which it can be split. Lets call it a perfect split.
  8.  
  9. def solution2(A):
  10.     max_right = max(A[1:])
  11.     i_max_right = A.index(max_right)
  12.     if i_max_right == len(A) - 1:
  13.         return abs(max(A[:-1]) - A[i_max_right])
  14.  
  15.     max_left = A[0]
  16.     max_diff = abs(max_left - max_right)
  17.  
  18.     for counter in range(1, len(A)):
  19.         if counter > i_max_right:
  20.             max_right = max(A[counter:])
  21.             i_max_right = A.index(max_right)
  22.  
  23.         if abs(max_left - max_right) > max_diff:
  24.             max_diff = max_left - max_right
  25.  
  26.         max_left = max(max_left, A[counter])
  27.  
  28.     return max_diff
  29.  
  30.  
  31. def solution3(A):
  32.     max_value = max(A)
  33.     i_max_value = A.index(max_value)
  34.     if i_max_value == len(A) - 1:
  35.         return abs(max(A[:-1]) - A[i_max_value])
  36.  
  37.     last_value = A[-1]
  38.     return abs(max_value - last_value)
  39.  
  40.  
  41. if __name__ == "__main__":
  42.     import time
  43.  
  44.     start = time.time()
  45.     print(" --> quadratic")
  46.  
  47.     assert solution2([1, 3, -3]) == 6
  48.     assert solution2([4, 3, 2, 5, 1, 1]) == 4
  49.     assert solution2([4, 3, 2, 5, 1, 9]) == 4
  50.     assert solution2([4, 3, 2, 5, 1, -4]) == 9
  51.     assert solution2([0, 0]) == 0
  52.     assert solution2([9, -4, -5]) == 14
  53.     assert solution2([9, -4, 4]) == 5
  54.     assert solution2([9, -4, 0]) == 9
  55.     assert solution2([9, -4, 4]) == 5
  56.     assert solution2([9, -4, 5]) == 4
  57.     assert solution2(list(range(100))) == 1
  58.     assert solution2(list(range(100000))) == 1
  59.     assert solution2(list(range(100))[::-1]) == 99
  60.     assert solution2(list(range(10000))[::-1]) == 9999
  61.     assert solution3(list(range(100000, 0, -1))) == 99999
  62.  
  63.     assert solution2([-1000000000, -1000000000]) == 0
  64.     assert solution2([1000000000, 1000000000]) == 0
  65.     assert solution2([1000000000, -1000000000]) == 2000000000
  66.     assert solution2([-1000000000, 1000000000]) == 2000000000
  67.  
  68.     end = time.time()
  69.     print(end - start)
  70.     start = time.time()
  71.     print("----> Linear")
  72.     assert solution3([1, 3, -3]) == 6
  73.     assert solution3([4, 3, 2, 5, 1, 1]) == 4
  74.     assert solution3([4, 3, 2, 5, 1, 9]) == 4
  75.     assert solution3([4, 3, 2, 5, 1, -4]) == 9
  76.     assert solution3([0, 0]) == 0
  77.     assert solution3([9, -4, -5]) == 14
  78.     assert solution3([9, -4, 4]) == 5
  79.     assert solution3([9, -4, 0]) == 9
  80.     assert solution3([9, -4, 4]) == 5
  81.     assert solution3([9, -4, 5]) == 4
  82.     assert solution3(list(range(100))) == 1
  83.     assert solution3(list(range(100000))) == 1
  84.     assert solution3(list(range(100))[::-1]) == 99
  85.     assert solution3(list(range(10000))[::-1]) == 9999
  86.     assert solution3(list(range(100000, 0, -1))) == 99999
  87.     assert solution3([-1000000000, -1000000000]) == 0
  88.     assert solution3([1000000000, 1000000000]) == 0
  89.     assert solution3([1000000000, -1000000000]) == 2000000000
  90.     assert solution3([-1000000000, 1000000000]) == 2000000000
  91.  
  92.     end = time.time()
  93.     print(end - start)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement