From ae3f626a44f7f31b07317c74221d6acb0bc959e4 Mon Sep 17 00:00:00 2001 From: Agatha Bahati Date: Mon, 28 Oct 2024 16:10:28 +0300 Subject: [PATCH 1/7] doc: add happy number --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 116bff6..aea75d2 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ If you find this repo helpful, please consider giving it a star 🌟 to show you [BONUS: find duplicate number](https://github.com/MoigeMatino/data-structures-algorithms-python/tree/main/arrays_and_strings/find_duplicate_number) [BONUS: first bad version](https://github.com/MoigeMatino/data-structures-algorithms-python/tree/main/arrays_and_strings/first_bad_version) [BONUS: container with most water](https://github.com/MoigeMatino/data-structures-algorithms-python/tree/main/arrays_and_strings/container_with_most_water) +[BONUS: happy number](https://github.com/MoigeMatino/data-structures-algorithms-python/tree/main/arrays_and_strings/happy_number) * two pointers approach ## 2. Linked Lists From f4412994ff72a244f73029a5146c6124bb590340 Mon Sep 17 00:00:00 2001 From: Agatha Bahati Date: Tue, 29 Oct 2024 12:33:01 +0300 Subject: [PATCH 2/7] doc: update palindrome_linked_list.py with notes --- .../palindrome_linked_list/palindrome_linked_list.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/linked_lists/palindrome_linked_list/palindrome_linked_list.py b/linked_lists/palindrome_linked_list/palindrome_linked_list.py index 1aad907..2a12780 100644 --- a/linked_lists/palindrome_linked_list/palindrome_linked_list.py +++ b/linked_lists/palindrome_linked_list/palindrome_linked_list.py @@ -81,8 +81,16 @@ def compare_halfs(head1, head2): # The algorithm uses a constant amount of extra space. It only uses a few pointer variables and does not require # any additional data structures that depend on the size of the input list. +# Considerations: +# Since a palindrome reads the same forward and backward, an intuitive way to solve this problem is to use two pointers—one placed at the beginning and +# the other at the end. These pointers move toward the center, comparing the values at each step. While this approach is intuitive, it poses challenges +# when working with singly linked lists. Singly linked lists only have the next pointers, so moving from the end toward the center requires additional steps. +# To overcome this limitation, we solve this problem in three key steps: , , and If both halves of the list match, the linked list is a palindrome. +# Here, reversing half the linked list allows us to use only the next pointers for comparison. + # Approach: # 1. Use the two-pointer technique to find the middle of the linked list. # 2. Reverse the second half of the list. # 3. Compare the first half and the reversed second half to check for palindrome properties. -# This approach efficiently determines if the list is a palindrome without using extra space for storing values. \ No newline at end of file +# This approach efficiently determines if the list is a palindrome without using extra space for storing values. + From 32f2c2f2bb16cccbd8b93636b4658de8e1d450f9 Mon Sep 17 00:00:00 2001 From: AgathaBahati Date: Tue, 29 Oct 2024 12:52:32 +0300 Subject: [PATCH 3/7] feat: add search rotated sorted array iteratively --- .../search_rotated_sorted_array/__init__.py | 0 .../search_rotated_sorted_array.py | 21 +++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 arrays_and_strings/search_rotated_sorted_array/__init__.py create mode 100644 arrays_and_strings/search_rotated_sorted_array/search_rotated_sorted_array.py diff --git a/arrays_and_strings/search_rotated_sorted_array/__init__.py b/arrays_and_strings/search_rotated_sorted_array/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/arrays_and_strings/search_rotated_sorted_array/search_rotated_sorted_array.py b/arrays_and_strings/search_rotated_sorted_array/search_rotated_sorted_array.py new file mode 100644 index 0000000..f0d45e5 --- /dev/null +++ b/arrays_and_strings/search_rotated_sorted_array/search_rotated_sorted_array.py @@ -0,0 +1,21 @@ +def search_rotated_sorted_array(nums: list[int], target: int) -> int: + low = 0 + high = len(nums) - 1 + + while low <= high: + mid = low + (high - low)//2 + if nums[mid] == target: + return mid + + if nums[low] <= nums[mid]: + if nums[low] <= target and target <= nums[mid]: + high = mid - 1 + else: + low = mid + 1 + + else: + if nums[mid] <= target and target <= nums[high]: + low = mid + 1 + else: + high = mid - 1 + return -1 \ No newline at end of file From 300f9bf79761fe7011584bccc2ad30a23696400e Mon Sep 17 00:00:00 2001 From: AgathaBahati Date: Tue, 29 Oct 2024 13:00:59 +0300 Subject: [PATCH 4/7] feat: add search rotated sorted array recursive solution --- ...y.py => search_rotated_sorted_array_v1.py} | 0 .../search_rotated_sorted_array_v2.py | 20 +++++++++++++++++++ 2 files changed, 20 insertions(+) rename arrays_and_strings/search_rotated_sorted_array/{search_rotated_sorted_array.py => search_rotated_sorted_array_v1.py} (100%) create mode 100644 arrays_and_strings/search_rotated_sorted_array/search_rotated_sorted_array_v2.py diff --git a/arrays_and_strings/search_rotated_sorted_array/search_rotated_sorted_array.py b/arrays_and_strings/search_rotated_sorted_array/search_rotated_sorted_array_v1.py similarity index 100% rename from arrays_and_strings/search_rotated_sorted_array/search_rotated_sorted_array.py rename to arrays_and_strings/search_rotated_sorted_array/search_rotated_sorted_array_v1.py diff --git a/arrays_and_strings/search_rotated_sorted_array/search_rotated_sorted_array_v2.py b/arrays_and_strings/search_rotated_sorted_array/search_rotated_sorted_array_v2.py new file mode 100644 index 0000000..b48f671 --- /dev/null +++ b/arrays_and_strings/search_rotated_sorted_array/search_rotated_sorted_array_v2.py @@ -0,0 +1,20 @@ +def search_rotated_sorted_array(nums: list[int],low: int, high: int, target:int) -> int: + + if low >= high: + return False + + mid = low + (high - low)//2 + + if nums[mid] == target: + return True + + if nums[low] <= nums[mid]: + if nums[low] <= target and target <= nums[mid]: + return search_rotated_sorted_array(nums, low, mid-1, target) + else: + return search_rotated_sorted_array(nums, mid+1, high, target) + else: + if nums[mid] <= target and target <= nums[high]: + return search_rotated_sorted_array(nums, mid+1, high, target) + else: + return search_rotated_sorted_array(nums, low, mid-1, target) \ No newline at end of file From c27edcdc893593d8bdac1cb20657d8ce366c9e8b Mon Sep 17 00:00:00 2001 From: AgathaBahati Date: Tue, 29 Oct 2024 13:16:49 +0300 Subject: [PATCH 5/7] doc: add documentation and README --- .../search_rotated_sorted_array/README.md | 92 +++++++++++++++++++ .../search_rotated_sorted_array_v1.py | 44 ++++++++- .../search_rotated_sorted_array_v2.py | 51 ++++++++-- 3 files changed, 173 insertions(+), 14 deletions(-) create mode 100644 arrays_and_strings/search_rotated_sorted_array/README.md diff --git a/arrays_and_strings/search_rotated_sorted_array/README.md b/arrays_and_strings/search_rotated_sorted_array/README.md new file mode 100644 index 0000000..75f32df --- /dev/null +++ b/arrays_and_strings/search_rotated_sorted_array/README.md @@ -0,0 +1,92 @@ +## **Problem Statement** + +Given a sorted integer array, `nums`, and an integer value, `target`, the array is rotated by some arbitrary number. Search and return the index of `target` in this array. If the `target` does not exist, return `-1`. + + +### Constraints +- All values in nums are unique. +- The values in nums are sorted in ascending order. +- The array may have been rotated by some arbitrary number. +- 1 ≤ nums.length ≤1000≤1000 +- −104 ≤ nums[i] ≤ 104 +- −104 ≤ target ≤ 104 + +## **Examples** + +### Example 1: Target Found in Rotated Array + +**Input:** + +- `nums = [4, 5, 6, 7, 0, 1, 2]` +- `target = 0` + +**Process:** +- The array `[4, 5, 6, 7, 0, 1, 2]` is a sorted array that has been rotated. +- The target value `0` is located at index `4`. + +**Output:** `4` + +**Visualization:** +- Original Sorted Array: [0, 1, 2, 4, 5, 6, 7] +- Rotated Array: [4, 5, 6, 7, 0, 1, 2] +- Target: 0 +- Index of Target: 4 + +### Example 2: Target Not Found + +**Input:** +- `nums = [4, 5, 6, 7, 0, 1, 2]` +- `target = 3` + +**Process:** +- The array `[4, 5, 6, 7, 0, 1, 2]` is a sorted array that has been rotated. +- The target value `3` is not present in the array. + +**Output:** `-1` + +**Visualization:** + +Original Sorted Array: [0, 1, 2, 4, 5, 6, 7] +Rotated Array: [4, 5, 6, 7, 0, 1, 2] +Target: 3 +Index of Target: -1 (not found) + + +### Example 3: Target Found at Beginning + +**Input:** +- `nums = [6, 7, 0, 1, 2, 4, 5]` +- `target = 6` + +**Process:** +- The array `[6, 7, 0, 1, 2, 4, 5]` is a sorted array that has been rotated. +- The target value `6` is located at index `0`. + +**Output:** `0` + +**Visualization:** + +- Original Sorted Array: [0, 1, 2, 4, 5, 6, 7] +- Rotated Array: [6, 7, 0, 1, 2, 4, 5] +- Target: 6 +- Index of Target: 0 + + +### Example 4: Target Found at End + +**Input:** +- `nums = [6, 7, 0, 1, 2, 4, 5]` +- `target = 5` + +**Process:** +- The array `[6, 7, 0, 1, 2, 4, 5]` is a sorted array that has been rotated. +- The target value `5` is located at index `6`. + +**Output:** `6` + +**Visualization:** + +- Original Sorted Array: [0, 1, 2, 4, 5, 6, 7] +- Rotated Array: [6, 7, 0, 1, 2, 4, 5] +- Target: 5 +- Index of Target: 6 \ No newline at end of file diff --git a/arrays_and_strings/search_rotated_sorted_array/search_rotated_sorted_array_v1.py b/arrays_and_strings/search_rotated_sorted_array/search_rotated_sorted_array_v1.py index f0d45e5..8883d99 100644 --- a/arrays_and_strings/search_rotated_sorted_array/search_rotated_sorted_array_v1.py +++ b/arrays_and_strings/search_rotated_sorted_array/search_rotated_sorted_array_v1.py @@ -1,21 +1,55 @@ def search_rotated_sorted_array(nums: list[int], target: int) -> int: + """ + Search for a target value in a rotated sorted array using an iterative binary search. + + Parameters: + nums (list[int]): The rotated sorted array. + target (int): The target value to search for. + + Returns: + int: The index of the target if found, otherwise -1. + + """ low = 0 high = len(nums) - 1 while low <= high: - mid = low + (high - low)//2 + mid = low + (high - low) // 2 + + # Check if the mid element is the target if nums[mid] == target: return mid + # Determine if the left half is sorted if nums[low] <= nums[mid]: - if nums[low] <= target and target <= nums[mid]: + # Check if the target is in the sorted left half + if nums[low] <= target <= nums[mid]: high = mid - 1 else: low = mid + 1 - else: - if nums[mid] <= target and target <= nums[high]: + # Check if the target is in the sorted right half + if nums[mid] <= target <= nums[high]: low = mid + 1 else: high = mid - 1 - return -1 \ No newline at end of file + + # Target not found + return -1 + +# Approach and Rationale: +# ----------------------- +# - This function uses an iterative binary search approach to find the target in a rotated sorted array. +# - A rotated sorted array is an array that was originally sorted in increasing order but then rotated at some pivot. +# - The key observation is that at least one half of the array (either left or right of the midpoint) is always sorted. +# - By comparing the target with the elements in the sorted half, we can decide which half to continue searching in. +# - This approach reduces the search space by half in each iteration, similar to binary search. + +# Time Complexity: +# ---------------- +# - The time complexity is O(log n), where n is the number of elements in the array. +# - This is because the search space is halved in each iteration, similar to binary search. + +# Space Complexity: +# ----------------- +# - The space complexity is O(1) because the algorithm uses a constant amount of extra space. diff --git a/arrays_and_strings/search_rotated_sorted_array/search_rotated_sorted_array_v2.py b/arrays_and_strings/search_rotated_sorted_array/search_rotated_sorted_array_v2.py index b48f671..71fc2f6 100644 --- a/arrays_and_strings/search_rotated_sorted_array/search_rotated_sorted_array_v2.py +++ b/arrays_and_strings/search_rotated_sorted_array/search_rotated_sorted_array_v2.py @@ -1,20 +1,53 @@ -def search_rotated_sorted_array(nums: list[int],low: int, high: int, target:int) -> int: - +def search_rotated_sorted_array(nums: list[int], low: int, high: int, target: int) -> int: + """ + Search for a target value in a rotated sorted array using a modified binary search. + + Parameters: + nums (list[int]): The rotated sorted array. + low (int): The starting index of the search range. + high (int): The ending index of the search range. + target (int): The target value to search for. + + Returns: + int: True if the target is found, False otherwise. + + """ if low >= high: return False - mid = low + (high - low)//2 + mid = low + (high - low) // 2 if nums[mid] == target: return True + # Check if the left half is sorted if nums[low] <= nums[mid]: - if nums[low] <= target and target <= nums[mid]: - return search_rotated_sorted_array(nums, low, mid-1, target) + # If target is in the sorted left half + if nums[low] <= target <= nums[mid]: + return search_rotated_sorted_array(nums, low, mid - 1, target) else: - return search_rotated_sorted_array(nums, mid+1, high, target) + return search_rotated_sorted_array(nums, mid + 1, high, target) else: - if nums[mid] <= target and target <= nums[high]: - return search_rotated_sorted_array(nums, mid+1, high, target) + # If target is in the sorted right half + if nums[mid] <= target <= nums[high]: + return search_rotated_sorted_array(nums, mid + 1, high, target) else: - return search_rotated_sorted_array(nums, low, mid-1, target) \ No newline at end of file + return search_rotated_sorted_array(nums, low, mid - 1, target) + +# Approach and Rationale: +# ----------------------- +# - The function uses a recursive binary search approach to find the target in a rotated sorted array. +# - A rotated sorted array is an array that was originally sorted in increasing order but then rotated at some pivot. +# - The key observation is that at least one half of the array (either left or right of the midpoint) is always sorted. +# - By comparing the target with the elements in the sorted half, we can decide which half to continue searching in. +# - This approach reduces the search space by half in each recursive call, similar to binary search. + +# Time Complexity: +# ---------------- +# - The time complexity is O(log n), where n is the number of elements in the array. +# - This is because the search space is halved in each recursive call, similar to binary search. + +# Space Complexity: +# ----------------- +# - The space complexity is O(log n) due to the recursive call stack. +# - Each recursive call adds a new layer to the call stack, and the maximum depth of recursion is proportional to log n. From f7de04d557948b78e8441c5e32dbe4fbe0bac048 Mon Sep 17 00:00:00 2001 From: Agatha Bahati Date: Tue, 29 Oct 2024 13:22:07 +0300 Subject: [PATCH 6/7] doc: add search rotated sorted array --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index aea75d2..4086f0a 100644 --- a/README.md +++ b/README.md @@ -32,14 +32,15 @@ If you find this repo helpful, please consider giving it a star 🌟 to show you [008. pair product](https://github.com/MoigeMatino/structy.net/tree/main/arrays_and_strings/pair_product) [009. intersection](https://github.com/MoigeMatino/structy.net/tree/main/arrays_and_strings/intersection) [010. five sort](https://github.com/MoigeMatino/structy.net/tree/main/arrays_and_strings/five_sort) -[BONUS: sum of three values](https://github.com/MoigeMatino/Data-Structures-Algorithms-Python/tree/main/arrays_and_strings/sum_of_three_values) [BONUS: three sum](https://github.com/MoigeMatino/Data-Structures-Algorithms-Python/tree/main/arrays_and_strings/three_sum) -[BONUS: longest palindrome](https://github.com/MoigeMatino/Data-Structures-Algorithms-Python/tree/main/arrays_and_strings/longest_palindrome) +[BONUS: happy number](https://github.com/MoigeMatino/data-structures-algorithms-python/tree/main/arrays_and_strings/happy_number) +[BONUS: first bad version](https://github.com/MoigeMatino/data-structures-algorithms-python/tree/main/arrays_and_strings/first_bad_version) [BONUS: circular array loop](https://github.com/MoigeMatino/data-structures-algorithms-python/tree/main/arrays_and_strings/circular_array_loop) +[BONUS: longest palindrome](https://github.com/MoigeMatino/Data-Structures-Algorithms-Python/tree/main/arrays_and_strings/longest_palindrome) +[BONUS: sum of three values](https://github.com/MoigeMatino/Data-Structures-Algorithms-Python/tree/main/arrays_and_strings/sum_of_three_values) [BONUS: find duplicate number](https://github.com/MoigeMatino/data-structures-algorithms-python/tree/main/arrays_and_strings/find_duplicate_number) -[BONUS: first bad version](https://github.com/MoigeMatino/data-structures-algorithms-python/tree/main/arrays_and_strings/first_bad_version) [BONUS: container with most water](https://github.com/MoigeMatino/data-structures-algorithms-python/tree/main/arrays_and_strings/container_with_most_water) -[BONUS: happy number](https://github.com/MoigeMatino/data-structures-algorithms-python/tree/main/arrays_and_strings/happy_number) * two pointers approach +[BONUS: search rotated sorted array](https://github.com/MoigeMatino/data-structures-algorithms-python/tree/main/arrays_and_strings/search_rotated_sorted_array) ## 2. Linked Lists From f1a28f7b694a035c4bfd7d7215e777ff625a2881 Mon Sep 17 00:00:00 2001 From: Agatha Bahati Date: Tue, 21 Jan 2025 22:39:25 +0300 Subject: [PATCH 7/7] fix: fix three_sum.py bug Fix bug to append tuple of triplets --- arrays_and_strings/three_sum/three_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arrays_and_strings/three_sum/three_sum.py b/arrays_and_strings/three_sum/three_sum.py index 5109c11..65a048d 100644 --- a/arrays_and_strings/three_sum/three_sum.py +++ b/arrays_and_strings/three_sum/three_sum.py @@ -34,7 +34,7 @@ def three_sum(nums: List[int]) -> List[List[int]]: # If the sum is zero, append the triplet to the result and skip duplicates if current_sum == 0: - triplets.append([nums[i], nums[low], nums[high]]) + triplets.append(([nums[i], nums[low], nums[high])) # Skip duplicates of the low pointer to avoid redundant triplets while low < high and nums[low] == nums[low + 1]: low += 1 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy