|
| 1 | +# Learn Python together |
| 2 | + |
| 3 | +"""Given an array of integers nums, |
| 4 | +calculate the pivot index of this array. |
| 5 | +The pivot index is the index where the sum of all the numbers |
| 6 | +strictly to the left of the index is equal to the sum of all |
| 7 | +the numbers strictly to the index's right.""" |
| 8 | + |
| 9 | +# Solution 1 |
| 10 | + |
| 11 | +def pivotIndex1(nums) -> int: |
| 12 | + pivot = 0 |
| 13 | + # getting sum of the numbers from left side of the pivot |
| 14 | + left_sum = sum(nums[:pivot]) |
| 15 | + # getting sum of the numbers from fight side of the pivot |
| 16 | + right_sum = sum(nums[pivot+1:]) |
| 17 | + |
| 18 | + # lopping through the list |
| 19 | + for _ in range(len(nums)-1): |
| 20 | + if left_sum == right_sum: |
| 21 | + # if sums are equal, return the pivot index |
| 22 | + return pivot |
| 23 | + else: |
| 24 | + # otherwise left sum += pivot index, |
| 25 | + # right sum -= pivot index+1 |
| 26 | + left_sum += nums[pivot] |
| 27 | + right_sum -= nums[pivot+1] |
| 28 | + pivot += 1 |
| 29 | + # if the sums still not equal, return -1 |
| 30 | + if left_sum == right_sum: |
| 31 | + return pivot |
| 32 | + else: |
| 33 | + return -1 |
| 34 | + |
| 35 | + |
| 36 | +# Solution 2 |
| 37 | +def pivotIndex2(nums) -> int: |
| 38 | + left_sum = 0 |
| 39 | + # sum of all numbers |
| 40 | + right_sum = sum(nums) |
| 41 | + |
| 42 | + for i in range(len(nums)): |
| 43 | + # substract the current num from right sum |
| 44 | + right_sum -= nums[i] |
| 45 | + # return index if sums are equal |
| 46 | + if left_sum == right_sum: |
| 47 | + return i |
| 48 | + # adding current num to the left sum |
| 49 | + left_sum += nums[i] |
| 50 | + # if there isn't pivot, -1 |
| 51 | + return -1 |
| 52 | + |
| 53 | + |
| 54 | +# Solution 3 |
| 55 | +def pivotIndex3(nums) -> int: |
| 56 | + left_sum = 0 |
| 57 | + # sum of all numbers |
| 58 | + right_sum = sum(nums) |
| 59 | + |
| 60 | + for i, num in enumerate(nums): |
| 61 | + # if sums are equal, return index |
| 62 | + if left_sum == right_sum -left_sum - num: |
| 63 | + return i |
| 64 | + # add current num to the left sum |
| 65 | + left_sum += nums[i] |
| 66 | + # if there isn't pivot, -1 |
| 67 | + return -1 |
| 68 | + |
| 69 | +# check |
| 70 | +nums = [1,7,3,6,5,6] |
| 71 | +nums2 = [1,7] |
| 72 | +print(pivotIndex1(nums)) # Output-> 3 |
| 73 | +print(pivotIndex2(nums)) # Output-> 3 |
| 74 | +print(pivotIndex3(nums)) # Output-> 3 |
| 75 | + |
| 76 | +print(pivotIndex1(nums2)) # Output-> -1 |
| 77 | +print(pivotIndex2(nums2)) # Output-> -1 |
| 78 | +print(pivotIndex3(nums2)) # Output-> -1 |
| 79 | + |
| 80 | +# Check time |
| 81 | +import timeit |
| 82 | + |
| 83 | +t1 = timeit.timeit(lambda: pivotIndex1(nums), number=10000) |
| 84 | +print("pivotIndex1 execution time:", t1) # -> 0.011717599999999995 |
| 85 | + |
| 86 | +t2 = timeit.timeit(lambda: pivotIndex2(nums), number=100000) |
| 87 | +print("pivotIndex2 execution time:", t2) # -> 0.08669160000000001 |
| 88 | + |
| 89 | +t3 = timeit.timeit(lambda: pivotIndex3(nums), number=100000) |
| 90 | +print("pivotIndex3 execution time:", t3) # -> 0.09343009999999999 |
| 91 | + |
0 commit comments