File tree 2 files changed +37
-1
lines changed
2 files changed +37
-1
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Time Complexity: O(logn)
3
+ Space Complexity: O(1)
4
+
5
+ Point to observe: Each time either the left half will be sorted or the right half.
6
+ Hence Binary Search is a good option.
7
+ """
8
+
9
+ class Solution :
10
+ def search (self , nums : List [int ], target : int ) -> int :
11
+ n = len (nums )
12
+ low = 0
13
+ high = n - 1
14
+ while low <= high :
15
+ mid = (low + high )// 2
16
+ if nums [mid ]== target :
17
+ return mid
18
+
19
+ # if left half is sorted
20
+ if nums [low ]<= nums [mid ]:
21
+ if nums [low ]<= target < nums [mid ]:
22
+ high = mid - 1
23
+ else :
24
+ low = mid + 1
25
+
26
+ # if right half is sorted
27
+ else :
28
+ if nums [mid ]< target <= nums [high ]:
29
+ low = mid + 1
30
+ else :
31
+ high = mid - 1
32
+ return - 1
33
+
34
+
35
+
Original file line number Diff line number Diff line change @@ -152,6 +152,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
152
152
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
153
153
| --- | --------------------------------------------------------------------- | -------------------------------------------------------------------- | --------- | --------- | ---------- | --- | ---------------------- |
154
154
| 278 | [ First Bad Version] ( https://leetcode.com/problems/first-bad-version/ ) | [ Java] ( ./Java/May-LeetCoding-Challenge/Day-1-First-Bad-Version.java ) <br > [ JavaScript] ( ./JavaScript/First-Bad-Version.js ) | _ O(logn)_ | _ O(1)_ | Easy | | Binary Search |
155
+ | 33 | [ Search in Rotated Sorted Array] ( https://leetcode.com/problems/search-in-rotated-sorted-array/ ) | [ Python] ( ./Python/search-in-rotated-sorted-array.py ) | _ O(logn)_ | _ O(1)_ | Medium | | Binary Search |
155
156
156
157
<br />
157
158
<div align =" right " >
@@ -185,7 +186,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
185
186
| [ Lokendra Bohra] ( https://github.com/lokendra1704/ ) <br > <img src =" https://github.com/lokendra1704.png " width =" 100 " height =" 100 " > | India | Python | [ Leetcode] ( https://t.co/u0OByxhcHA ) <br > [ Hackerrank] ( https://www.hackerrank.com/lokendra17 ) |
186
187
| [ Yuri Spiridonov] ( https://github.com/YuriSpiridonov ) <br > <img src =" https://github.com/YuriSpiridonov.png " width =" 100 " height =" 100 " > | Russia | Python | [ Twitter] ( https://twitter.com/YuriSpiridonov ) <br >[ Leetcode] ( https://leetcode.com/yurispiridonov/ ) <br >[ Hackerrank] ( https://www.hackerrank.com/YuriSpiridonov ) |
187
188
| [ Naveen Kashyap] ( https://github.com/naveenkash ) <br > <img src =" https://github.com/naveenkash.png " width =" 100 " height =" 100 " > | India | Javascript | [ Twitter] ( https://twitter.com/naveen_kashyapp ) <br >[ Leetcode] ( https://leetcode.com/naveenkash/ ) |
188
-
189
+ | [ Sachin Singh Negi ] ( https://github.com/sachinnegi ) < br > < img src = " https://github.com/sachinnegi.png " width = " 100 " height = " 100 " > | India | Python | [ Twitter ] ( https://twitter.com/SachinSinghNe17 ) < br > [ Leetcode ] ( https://leetcode.com/negisachin688/ ) < br > [ Hackerrrak ] ( https://www.hackerrank.com/negisachin688 ) | |
189
190
<br />
190
191
<div align =" right " >
191
192
<b><a href="#algorithms">⬆️ Back to Top</a></b>
You can’t perform that action at this time.
0 commit comments