diff --git a/Problem#6.py b/Problem#6.py new file mode 100644 index 00000000..8c2aac7c --- /dev/null +++ b/Problem#6.py @@ -0,0 +1,34 @@ +# Time Complexity : O(log n) +# Space Complexity : O(1) +# Did you run the code on Leetcode : Yes +# Any problem you faced while coding this : No + +# Your code here along with comments explaining your approach in three sentences only + + +''' +33. Search in Rotated Sorted Array +''' + +class Solution: + def search(self, nums: List[int], target: int) -> int: + if nums is None or len(nums) == 0: # We check if the list is empty or if the length of the list is zero + return -1 # If the list is empty, we return -1 as the target cannot be found + n = len(nums) # We store the length of the list in a variable n + low = 0 # We initialize low variable to 0, which represents the starting index of the list + high = n - 1 # We initialize high variable to n - 1, which represents the ending index of the list + while low <= high: # Until low index is less than or equal to high index we continue the search + mid = (low + high) // 2 # We calculate the mid index by taking the average of low and high indices + if nums[mid] == target: # If the mid index value is equal to the target value, we have found the target here itself + return mid # We return the mid index as the target is found at this index + elif nums[low] <= nums[mid]: # Left side is sorted # We check if the left side of the mid index is sorted + if nums[low] <= target < nums[mid]: # Then we further check in the left side that if the target is in the range of low and mid indices + high = mid - 1 # If the target is in the range, we move the high index to mid - 1 to search in the left side + else: # If the target is not in the range, we move the low index to mid + 1 to search in the right side + low = mid + 1 # We move the low index to mid + 1 to search in the right side + else: # Right side is sorted # If the left side is not sorted, then the right side must be sorted + if nums[mid] < target <= nums[high]: # We check if the target is in the range of mid and high indices + low = mid + 1 # If the target is in the range, we move the low index to mid + 1 to search in the right side + else: # If the target is not in the range, we move the high index to mid - 1 to search in the left side + high = mid -1 # We move the high index to mid - 1 to search in the left side + return -1 # If we exit the while loop, it means the target is not found in the list, so we return -1 diff --git a/Problem#7.py b/Problem#7.py new file mode 100644 index 00000000..d823029d --- /dev/null +++ b/Problem#7.py @@ -0,0 +1,45 @@ +# Time Complexity : O(log n) +# Space Complexity : O(1) +# Did you run the code on Leetcode : Yes +# Any problem you faced while coding this : No + +# Your code here along with comments explaining your approach in three sentences only + + +''' +702. Search in a Sorted Array of Unknown Size +''' + +# """ +# This is ArrayReader's API interface. +# You should not implement it, or speculate about its implementation +# """ +#class ArrayReader: +# def get(self, index: int) -> int: + +class Solution: # humne ek class Solution banayi hai jo ArrayReader ka use karti hai target ko dhoondhne ke liye (we have created a class Solution that uses ArrayReader to find the target) + def search(self, reader: 'ArrayReader', target: int) -> int: # ye function search karta hai target ko ArrayReader ke through (this function searches for the target using ArrayReader) + if reader.get(0) == target: # agar pehla element target ke barabar hai, to hum 0 return karte hain (if the first element is equal to the target, we return 0) + return 0 # agar target 0 index par hai, to hum wahi return karte hain (if the target is at index 0, we return that index) + + left = 0 # hum left pointer ko Sorted Unknown Array ke 0th index par set karte hain (we set the left pointer at the 0th index of the Sorted Unknown Array) + right = 1 # hum right pointer ko Sorted Unknown Array ke 1st index par set karte hain (we set the right pointer at the 1st index of the Sorted Unknown Array) + while reader.get(right) < target: # jab tak right pointer ka value target se chhota hai, tab tak hum right pointer ko double karte hain (as long as the value at the right pointer is less than the target, we double the right pointer) + # isse hum ensure karte hain ki right pointer target ke barabar ya usse bada ho jaye (this ensures that the right pointer is either equal to or greater than the target + # agar right pointer ka value target se bada ho jata hai, to hum left pointer ko right pointer ke barabar set karte hain (if the value at the right pointer becomes greater than the target, we set the left pointer to the right pointer) + left = right # hum left pointer ko right pointer ke barabar set karte hain (we set the left pointer to the right pointer) + right *= 2 # hum right pointer ko double times aage karte hain (we double the right pointer to move it further ahead) + + # ab hum binary search ka use karte hain left aur right pointers ke beech mein target ko dhoondhne ke liye (now we use binary search to find the target between the left and right pointers) + while left <= right: # jab tak left pointer right pointer se chhota ya barabar hai, tab tak hum search karte hain (as long as the left pointer is less than or equal to the right pointer, we continue searching) + mid = left + (right - left) // 2 # hum mid index ko calculate karte hain (we calculate the mid index) + val = reader.get(mid) # mid index par value ko ArrayReader se lete hain (we get the value at the mid index from ArrayReader) + if val == target: # agar mid index par value target ke barabar hai, to hum mid index return karte hain (if the value at the mid index is equal to the target, we return the mid index) + return mid # agar target mid index par hai, to hum wahi return karte hain (if the target is at the mid index, we return that index) + elif val < target: # agar mid index par value target se chhoti hai, to hum left pointer ko mid + 1 set karte hain (if the value at the mid index is less than the target, we set the left pointer to mid + 1) + left = mid + 1 # hum left pointer ko mid + 1 set karte hain (we set the left pointer to mid + 1) + else: # agar mid index par value target se badi hai, to hum right pointer ko mid - 1 set karte hain (if the value at the mid index is greater than the target, we set the right pointer to mid - 1) + right = mid - 1 # hum right pointer ko mid - 1 set karte hain (we set the right pointer to mid - 1) + + return -1 # agar target nahi milta, to hum -1 return karte hain (if the target is not found, we return -1) + \ No newline at end of file diff --git a/Problem#8.py b/Problem#8.py new file mode 100644 index 00000000..cc396647 --- /dev/null +++ b/Problem#8.py @@ -0,0 +1,34 @@ +# Time Complexity : O(log n) +# Space Complexity : O(1) +# Did you run the code on Leetcode : Yes +# Any problem you faced while coding this : No + +# Your code here along with comments explaining your approach in three sentences only + + +''' +74. Search a 2D Matrix +''' + +class Solution: + def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: + if not matrix or not matrix[0]: # hum check karte hain agar matrix khali hai ya pehli row khali hai + return False # agar khali hai toh hum False return karte hain + m = len(matrix) # hum matrix ki rows ki sankhya ko m mein store karte hain (we store the length of the matrix or length of columns in m) + n = len(matrix[0]) # hum pehli row ki columns ki sankhya ko n mein store karte hain (we store the length of the first row or length of rows in n) + + # ab hum binary search ka istemal karte hain (we use binary search) + left = 0 # hum left ko 0 se initialize karte hain (we initialize left to 0) + right = m * n - 1 # hum right ko m * n - 1 se initialize karte hain (we initialize right to m * n - 1) + while left <= right: # jab tak left chhota ya barabar hai right se (while left is less than or equal to right) + middle = (left + right) // 2 # hum middle ko left aur right ka average lete hain (we calculate the middle index) + idx_row = middle // n # hum middle ko n se divide karke row index nikaalte hain (we find the row index) + idx_col = middle % n # hum middle ko n se modulo karke column index nikaalte hain (we find the column index) + if matrix[idx_row][idx_col] == target: # agar matrix ke is index par value target ke barabar hai (if the value at this index in the matrix is equal to the target) + return True + else: # agar nahi hai (if not) + if matrix[idx_row][idx_col] < target: # agar matrix ke is index par value target se chhoti hai (if the value at this index in the matrix is less than the target) + left = middle + 1 # toh hum left ko middle + 1 karte hain (we move left to middle + 1) + else: # agar matrix ke is index par value target se badi hai (if the value at this index in the matrix is greater than the target) + right = middle - 1 # toh hum right ko middle - 1 karte hain (we move right to middle - 1) + return False # agar hum while loop se bahar nikalte hain, toh iska matlab target nahi mila (if we exit the while loop, it means the target was not found) \ No newline at end of file