Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit abaaf8a

Browse files
Added new solutions
1 parent fe21aa1 commit abaaf8a

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

‎coding_solutions/Readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ This repository contains my answers to all Leetcode/HackerRank algorithm questio
66
| # | Problem | Solution |
77
| :----: | :-----------------------------: | :----: |
88
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/)| [python3](https://github.com/sushant097/Data-Structure-Algorithms-Collections-Python/blob/master/coding_solutions/interview_related/TwoSum.py) |
9+
| 2 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/)| [python3](https://github.com/sushant097/Data-Structure-Algorithms-Collections-Python/blob/master/coding_solutions/interview_related/AddTwoNum.py) |
10+
| 3 | [Two Sum](https://leetcode.com/problems/two-sum/)| [python3](https://github.com/sushant097/Data-Structure-Algorithms-Collections-Python/blob/master/coding_solutions/interview_related/TwoSum.py) |
11+
| 4 | [Two Sum](https://leetcode.com/problems/two-sum/)| [python3](https://github.com/sushant097/Data-Structure-Algorithms-Collections-Python/blob/master/coding_solutions/interview_related/TwoSum.py) |
912

‎coding_solutions/interview_related/AddTwoNum.py

Whitespace-only changes.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# clear explanation: https://www.youtube.com/watch?v=oTfPJKGEHcc
2+
3+
'''
4+
Approach: Binary Search
5+
6+
Point to note: In a rotated sorted array, when doing binary search, one side will always be sorted and other side will always be incorrectly sorted if
7+
pivot index k (1 <= k < nums.length)
8+
9+
10+
1. find mid.
11+
2. if left side is sorted
12+
- if target is within left limits, binary search on left
13+
- else binary search on right
14+
3 else (if left side is not sorted, right side is sorted)
15+
- if target is within right limits, binary search on right
16+
- else binary search on left
17+
18+
19+
'''
20+
21+
class Solution:
22+
def search(self, nums: List[int], target: int) -> int:
23+
l, r = 0, len(nums) - 1
24+
25+
while l <= r:
26+
m = l + (r-l) // 2
27+
if nums[m] == target:
28+
return m
29+
30+
elif nums[m] >= nums[l]: # strictly increasing
31+
if target <= nums[m] and target >=nums[l]:
32+
# search in left part
33+
r = m - 1
34+
else:
35+
# search in right part
36+
l = m + 1
37+
else:
38+
if target >= nums[m] and target <= nums[r]:
39+
# search in right part
40+
l = m + 1
41+
else:
42+
# search in left part
43+
r = m - 1
44+
45+
return -1
46+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /