fork download
  1. # your code goes here
  2.  
  3. x = [1,2,3,3,4,5,5,5,5,8,8,8,8,9]
  4.  
  5. target = 5
  6.  
  7. def bound(isFirst):
  8. l = 0
  9. r = len(x)-1
  10. bound = -1
  11. while l<=r:
  12. mid = (l+r) // 2
  13. if x[mid] == target:
  14. bound = mid
  15. if isFirst:
  16. r = mid-1
  17. else:
  18. l = mid+1
  19. elif x[mid] > target:
  20. r = mid-1
  21. else:
  22. l = mid+1
  23. return bound
  24.  
  25. first = bound(True)
  26. last = bound(False)
  27.  
  28. print(first, last)
  29.  
  30.  
Success #stdin #stdout 0.09s 13996KB
stdin
Standard input is empty
stdout
5 8