title
stringlengths
1
100
titleSlug
stringlengths
3
77
Java
int64
0
1
Python3
int64
1
1
content
stringlengths
28
44.4k
voteCount
int64
0
3.67k
question_content
stringlengths
65
5k
question_hints
stringclasses
970 values
Python "O(1)" spatial complexity
reverse-words-in-a-string
0
1
# Intuition\n\nExample :\n```\n"ABCDEF 123 abc" (input)\n"abc 123 ABCDEF" (expected output)\n```\n\n\nIf you try to reverse the input you will get :\n```\n"cba 321 FEDCBA"\n```\nNow compare the reversed with expected output :\n```\n"cba 321 FEDCBA"\n | | |\n"abc 123 ABCDEF" (expected output)\n```\nYou see you on...
12
Given an input string `s`, reverse the order of the **words**. A **word** is defined as a sequence of non-space characters. The **words** in `s` will be separated by at least one space. Return _a string of the words in reverse order concatenated by a single space._ **Note** that `s` may contain leading or trailing s...
null
Reverse Words in a String Easy Solution beats 85%
reverse-words-in-a-string
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
Given an input string `s`, reverse the order of the **words**. A **word** is defined as a sequence of non-space characters. The **words** in `s` will be separated by at least one space. Return _a string of the words in reverse order concatenated by a single space._ **Note** that `s` may contain leading or trailing s...
null
99.94% Beats, Simplest Reverse String Solution in Python3
reverse-words-in-a-string
0
1
# Approach\n<!-- Describe your approach to solving the problem. -->\n Here\'s an explanation of the approach used in this code:\n\n1. s.split(): The split() method is used to split the input string s into a list of words. By default, it splits the string at spaces (whitespace characters), effectively separating the wor...
6
Given an input string `s`, reverse the order of the **words**. A **word** is defined as a sequence of non-space characters. The **words** in `s` will be separated by at least one space. Return _a string of the words in reverse order concatenated by a single space._ **Note** that `s` may contain leading or trailing s...
null
Python3 faster than 97%, without using split
reverse-words-in-a-string
0
1
```\nclass Solution:\n def reverseWords(self, s: str) -> str:\n res = []\n temp = ""\n for c in s:\n if c != " ":\n temp += c \n elif temp != "":\n res.append(temp)\n temp = ""\n if temp != "":\n res.append(temp...
32
Given an input string `s`, reverse the order of the **words**. A **word** is defined as a sequence of non-space characters. The **words** in `s` will be separated by at least one space. Return _a string of the words in reverse order concatenated by a single space._ **Note** that `s` may contain leading or trailing s...
null
✅ 0ms Beats 100% [Rust/Python/C++/Java] Simple DP Solution
maximum-product-subarray
1
1
# Intuition\nThis problem is really similar to [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) except instead of a sum it\'s a product, so naturally, a DP solution also comes to mind.\n\n<hr>\n\n## \u274C Solution #1: Brute Force [TLE]\n\nIt\'s not a bad idea to start with a naive brute force soluti...
2
Given an integer array `nums`, find a subarray that has the largest product, and return _the product_. The test cases are generated so that the answer will fit in a **32-bit** integer. **Example 1:** **Input:** nums = \[2,3,-2,4\] **Output:** 6 **Explanation:** \[2,3\] has the largest product 6. **Example 2:** **I...
null
Solution
maximum-product-subarray
1
1
```C++ []\nclass Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n int maxi = nums[0];\n int mini = nums[0];\n int ans = nums[0];\n for(int i = 1;i < nums.size();i++){\n if(nums[i] < 0){\n swap(maxi,mini);\n }\n maxi = max(nums[i]...
217
Given an integer array `nums`, find a subarray that has the largest product, and return _the product_. The test cases are generated so that the answer will fit in a **32-bit** integer. **Example 1:** **Input:** nums = \[2,3,-2,4\] **Output:** 6 **Explanation:** \[2,3\] has the largest product 6. **Example 2:** **I...
null
C++ || O(n) time and O(1) space || Easiest Beginner Friendly Sol
maximum-product-subarray
1
1
**NOTE - PLEASE READ INTUITION AND APPROACH FIRST THEN SEE THE CODE. YOU WILL DEFINITELY UNDERSTAND THE CODE LINE BY LINE AFTER SEEING THE APPROACH.**\n\n# Intuition of this Problem:\n**KEY POINTS:**\n1. currMaxProductSubarr : This variable keeps track of the maximum product of a subarray that ends at the current index...
68
Given an integer array `nums`, find a subarray that has the largest product, and return _the product_. The test cases are generated so that the answer will fit in a **32-bit** integer. **Example 1:** **Input:** nums = \[2,3,-2,4\] **Output:** 6 **Explanation:** \[2,3\] has the largest product 6. **Example 2:** **I...
null
✔️[Python3] DYNAMIC PROGRAMMING, Explained
maximum-product-subarray
0
1
Subproblem for the DP here would be: What is the maximum and minimum product we can get for a contiguous sub-array starting from the `0`th to the current element? Why do we need to maintain the minimum product while we are asked for a maximum? The fact is that elements in `nums` can be negative, so it possible that for...
130
Given an integer array `nums`, find a subarray that has the largest product, and return _the product_. The test cases are generated so that the answer will fit in a **32-bit** integer. **Example 1:** **Input:** nums = \[2,3,-2,4\] **Output:** 6 **Explanation:** \[2,3\] has the largest product 6. **Example 2:** **I...
null
8 Lines Of Code Logic Dp
maximum-product-subarray
0
1
\n\n# Dynamic Programming\n```\nclass Solution:\n def maxProduct(self, nums: List[int]) -> int:\n out=max(nums)\n cmax=cmin=1\n for n in nums:\n temp=cmax*n\n cmax=max(cmin*n,cmax*n,n)\n cmin=min(temp,cmin*n ,n)\n out=max(out,cmax)\n return out\...
11
Given an integer array `nums`, find a subarray that has the largest product, and return _the product_. The test cases are generated so that the answer will fit in a **32-bit** integer. **Example 1:** **Input:** nums = \[2,3,-2,4\] **Output:** 6 **Explanation:** \[2,3\] has the largest product 6. **Example 2:** **I...
null
Solution
find-minimum-in-rotated-sorted-array
1
1
```C++ []\nclass Solution {\npublic:\n int findMin(vector<int>& nums) {\n int n = nums.size();\n int low=0, high=n-1;\n \n while(low<high){\n if(nums[low] <= nums[high]) return nums[low];\n int mid = low + (high-low)/2;\n if(nums[low] > nums[mid]){\n ...
218
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think ...
Solution
find-minimum-in-rotated-sorted-array
1
1
```C++ []\nclass Solution {\npublic:\n int findMin(vector<int>& nums) {\n int n = nums.size();\n int low=0, high=n-1;\n \n while(low<high){\n if(nums[low] <= nums[high]) return nums[low];\n int mid = low + (high-low)/2;\n if(nums[low] > nums[mid]){\n ...
218
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think o...
Find Minimum in Rotated Sorted Array | Binary Search | Telugu | Python | Amazon | Microsoft |
find-minimum-in-rotated-sorted-array
0
1
# Approach\n![3.png](https://assets.leetcode.com/users/images/46218285-ae0c-40cf-8f0e-81c4899df3d5_1701288796.1663816.png)\n\n# Complexity\n- Time complexity:$$O(log(n))$$\n\n- Space complexity: $$O(n)$$\n\n# Code\n```\nclass Solution:\n def findMin(self, nums: List[int]) -> int:\n n=len(nums)\n left=0...
2
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think ...
Find Minimum in Rotated Sorted Array | Binary Search | Telugu | Python | Amazon | Microsoft |
find-minimum-in-rotated-sorted-array
0
1
# Approach\n![3.png](https://assets.leetcode.com/users/images/46218285-ae0c-40cf-8f0e-81c4899df3d5_1701288796.1663816.png)\n\n# Complexity\n- Time complexity:$$O(log(n))$$\n\n- Space complexity: $$O(n)$$\n\n# Code\n```\nclass Solution:\n def findMin(self, nums: List[int]) -> int:\n n=len(nums)\n left=0...
2
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think o...
Simple Binary search
find-minimum-in-rotated-sorted-array
0
1
\nclass Solution:\n def findMin(self, nums: List[int]) -> int:\n l=0\n h=len(nums)-1\n mi=-1\n while(l<=h):\n mid=(l+h)//2\n prev=(mid+len(nums)-1)%len(nums)\n if nums[mid]<=nums[prev]:\n mi=mid\n break\n if nums[l]...
1
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think ...
Simple Binary search
find-minimum-in-rotated-sorted-array
0
1
\nclass Solution:\n def findMin(self, nums: List[int]) -> int:\n l=0\n h=len(nums)-1\n mi=-1\n while(l<=h):\n mid=(l+h)//2\n prev=(mid+len(nums)-1)%len(nums)\n if nums[mid]<=nums[prev]:\n mi=mid\n break\n if nums[l]...
1
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think o...
Without min comparison | Detailed explanation
find-minimum-in-rotated-sorted-array
0
1
# Intuition\nGiven that we should **us**e **bi**nary **se**arch to **fi**nd **th**e **mi**nimum **el**ement in the **ro**tated **so**rted **ar**ray.\nAs all binary search problems, we have **tw**o **po**inters: \'low\' and \'high\', and we find \'mid\' using \'low\' and \'high\'.\n\nOur **main objective** here is to fi...
28
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think ...
Without min comparison | Detailed explanation
find-minimum-in-rotated-sorted-array
0
1
# Intuition\nGiven that we should **us**e **bi**nary **se**arch to **fi**nd **th**e **mi**nimum **el**ement in the **ro**tated **so**rted **ar**ray.\nAs all binary search problems, we have **tw**o **po**inters: \'low\' and \'high\', and we find \'mid\' using \'low\' and \'high\'.\n\nOur **main objective** here is to fi...
28
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think o...
✅✅Beats 99% | O(log N) | Python Solution (using binary search)
find-minimum-in-rotated-sorted-array
0
1
# Intuition\nThe goal is to find the minimum element in a rotated sorted array. We can use a binary search approach to efficiently locate the minimum element.\n\n# Approach\n1. Initialize the `ans` variable with the first element of the array `nums`.\n\n2. Set two pointers, `low` and `high`, to the start and end of the...
5
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think ...
✅✅Beats 99% | O(log N) | Python Solution (using binary search)
find-minimum-in-rotated-sorted-array
0
1
# Intuition\nThe goal is to find the minimum element in a rotated sorted array. We can use a binary search approach to efficiently locate the minimum element.\n\n# Approach\n1. Initialize the `ans` variable with the first element of the array `nums`.\n\n2. Set two pointers, `low` and `high`, to the start and end of the...
5
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think o...
Easy solution python3 (saiprakash)
find-minimum-in-rotated-sorted-array
0
1
# Approach \nBinary Search\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(Log N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def findMin(self...
1
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think ...
Easy solution python3 (saiprakash)
find-minimum-in-rotated-sorted-array
0
1
# Approach \nBinary Search\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(Log N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def findMin(self...
1
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think o...
Best and fast solution in PYTHON
find-minimum-in-rotated-sorted-array
0
1
```\nclass Solution:\n def findMin(self, num):\n first, last = 0, len(num) - 1\n while first < last:\n midpoint = (first + last) // 2\n if num[midpoint] > num[last]:\n \n\tfirst = midpoint + 1\n else:\n last = midpoint\n return num[first]\n\t\t`...
1
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think ...
Best and fast solution in PYTHON
find-minimum-in-rotated-sorted-array
0
1
```\nclass Solution:\n def findMin(self, num):\n first, last = 0, len(num) - 1\n while first < last:\n midpoint = (first + last) // 2\n if num[midpoint] > num[last]:\n \n\tfirst = midpoint + 1\n else:\n last = midpoint\n return num[first]\n\t\t`...
1
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think o...
Python3 | Intuitive | Optimal | Neat
find-minimum-in-rotated-sorted-array
0
1
```\nclass Solution:\n def findMin(self, nums: List[int]) -> int:\n l, r = 0, len(nums)-1\n while l < r:\n m = (l+r)//2\n if nums[l] < nums[r]:\n return nums[l]\n else:\n if l+1 == r:\n return nums[r]\n eli...
2
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think ...
Python3 | Intuitive | Optimal | Neat
find-minimum-in-rotated-sorted-array
0
1
```\nclass Solution:\n def findMin(self, nums: List[int]) -> int:\n l, r = 0, len(nums)-1\n while l < r:\n m = (l+r)//2\n if nums[l] < nums[r]:\n return nums[l]\n else:\n if l+1 == r:\n return nums[r]\n eli...
2
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think o...
Binary search || Python3
find-minimum-in-rotated-sorted-array
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(logn)\n\n- Space complexity:O(1)\n# Code\n```\nimport math\nclass Solution:\n def findMin(self, nums: List[int]) -> int:\n ...
2
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think ...
Binary search || Python3
find-minimum-in-rotated-sorted-array
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(logn)\n\n- Space complexity:O(1)\n# Code\n```\nimport math\nclass Solution:\n def findMin(self, nums: List[int]) -> int:\n ...
2
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think o...
📌 Python3 simple naive solution with binary search
find-minimum-in-rotated-sorted-array
0
1
```\nclass Solution:\n def findMin(self, nums: List[int]) -> int:\n start = 0\n end = len(nums) - 1\n \n if(nums[start] <= nums[end]):\n return nums[0]\n \n while start <= end:\n mid = (start + end) // 2\n \n if(nums[mid] > nums[mi...
6
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think ...
📌 Python3 simple naive solution with binary search
find-minimum-in-rotated-sorted-array
0
1
```\nclass Solution:\n def findMin(self, nums: List[int]) -> int:\n start = 0\n end = len(nums) - 1\n \n if(nums[start] <= nums[end]):\n return nums[0]\n \n while start <= end:\n mid = (start + end) // 2\n \n if(nums[mid] > nums[mi...
6
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think o...
Python | 99.77 % better binary search solution (easy to understand)
find-minimum-in-rotated-sorted-array
0
1
\n\n# Simple answer \uD83D\uDE04\uD83D\uDE04\uD83D\uDE04\n```\nclass Solution:\n def findMin(self, nums: list[int]) -> int:\n return min(nums)\n```\n\n# Binary search\n```\nclass Solution:\n def findMin(self, nums: list[int]) -> int:\n if nums[0] <= nums[-1]:\n return nums[0]\n l, ...
3
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think ...
Python | 99.77 % better binary search solution (easy to understand)
find-minimum-in-rotated-sorted-array
0
1
\n\n# Simple answer \uD83D\uDE04\uD83D\uDE04\uD83D\uDE04\n```\nclass Solution:\n def findMin(self, nums: list[int]) -> int:\n return min(nums)\n```\n\n# Binary search\n```\nclass Solution:\n def findMin(self, nums: list[int]) -> int:\n if nums[0] <= nums[-1]:\n return nums[0]\n l, ...
3
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think o...
6 Lines of Binary Search in Python
find-minimum-in-rotated-sorted-array
0
1
# Intuition \nUPVOTE PLEASE\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, ...
3
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think ...
6 Lines of Binary Search in Python
find-minimum-in-rotated-sorted-array
0
1
# Intuition \nUPVOTE PLEASE\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, ...
3
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: * `[4,5,6,7,0,1,2]` if it was rotated `4` times. * `[0,1,2,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
Array was originally in ascending order. Now that the array is rotated, there would be a point in the array where there is a small deflection from the increasing sequence. eg. The array would be something like [4, 5, 6, 7, 0, 1, 2]. You can divide the search space into two and see which direction to go. Can you think o...
Simple easy binary search
find-minimum-in-rotated-sorted-array-ii
0
1
# Code\n```\nclass Solution:\n def findMin(self, a: List[int]) -> int:\n beg=0\n last=len(a)-1\n while beg<last:\n mid=(last+beg)//2\n if a[mid]>a[last]:\n beg=mid+1\n elif a[mid]<a[last]:\n last=mid\n else:\n ...
1
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,4,4,5,6,7]` might become: * `[4,5,6,7,0,1,4]` if it was rotated `4` times. * `[0,1,4,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
null
Very easy solution for this hard problem(2 lines of code)
find-minimum-in-rotated-sorted-array-ii
0
1
\n# Code\n```\nclass Solution:\n def findMin(self, nums: List[int]) -> int:\n a=min(nums)\n return a\n \n```
2
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,4,4,5,6,7]` might become: * `[4,5,6,7,0,1,4]` if it was rotated `4` times. * `[0,1,4,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
null
154: Solution with step by step explanation
find-minimum-in-rotated-sorted-array-ii
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis problem is an extension of the previous problem. Since the array can contain duplicates, we need to add extra logic to handle the cases where we have duplicates at both ends.\n\nOne way to solve this is to keep track of...
10
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,4,4,5,6,7]` might become: * `[4,5,6,7,0,1,4]` if it was rotated `4` times. * `[0,1,4,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
null
Python3 Solution || Binary Search Approach || Super Easy To Read
find-minimum-in-rotated-sorted-array-ii
0
1
```python\nclass Solution:\n def findMin(self, nums: List[int]) -> int:\n left, right = 0, len(nums) - 1\n while (left < right):\n mid = left + (right - left) // 2\n if (nums[mid] < nums[right]):\n right = mid\n elif (nums[mid] > nums[right]):\n ...
3
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,4,4,5,6,7]` might become: * `[4,5,6,7,0,1,4]` if it was rotated `4` times. * `[0,1,4,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
null
Python3 Solution | Runtime Beats 99.70% | Binary search
find-minimum-in-rotated-sorted-array-ii
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIn this problem, we can use binary search, since the array is sorted. Binary search will allow us to find the desired element in O(log(n)) operations.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFor binary search...
2
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,4,4,5,6,7]` might become: * `[4,5,6,7,0,1,4]` if it was rotated `4` times. * `[0,1,4,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
null
✅ Python faster than 99.96%, well explained
find-minimum-in-rotated-sorted-array-ii
0
1
\n##### Code\n<iframe src="https://leetcode.com/playground/Rg6pMzmV/shared" frameBorder="0" width="700" height="450"></iframe>\n\n##### Understanding\nUse **Binary Search** to find the minimum value in the rotated sorted array.\n\n```\n# Find mid value\nmid = left + ((right - left) >> 1)`\n```\n* Here we are using beca...
4
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,4,4,5,6,7]` might become: * `[4,5,6,7,0,1,4]` if it was rotated `4` times. * `[0,1,4,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
null
Python solution using for loop beats 67.62% tc :-)
find-minimum-in-rotated-sorted-array-ii
0
1
# Approach\n<!-- Describe your approach to solving the problem. -->\n* Example => [4,5,1,2,3]. Simply iterate over the array and check if an element is smaller than its previous element. If yes that means that it is the minimum element because it is a sorted array which is rotated 1 to n times. So, whatever comes small...
1
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,4,4,5,6,7]` might become: * `[4,5,6,7,0,1,4]` if it was rotated `4` times. * `[0,1,4,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
null
Python - Two Approaches - EXPLAINED ✅
find-minimum-in-rotated-sorted-array-ii
0
1
Thanks to Neetcode https://www.youtube.com/watch?v=nIVW4P8b1VA\n\n\nFirst, let\'s understand how can we find minimum in a rotated sorted array. Since array was sorted before it is rotated, it means we can make use of Binary Search here.\n\n Consider an example -> [2,2,2,0,1]\n\nWhen we use Binary Search, then we wil...
3
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,4,4,5,6,7]` might become: * `[4,5,6,7,0,1,4]` if it was rotated `4` times. * `[0,1,4,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
null
Binary Search || Explained || PYTHON
find-minimum-in-rotated-sorted-array-ii
0
1
```\nclass Solution:\n def findMin(self, a: List[int]) -> int:\n \n def solve(l,h):\n while l<h:\n m=(l+h)//2\n \n if a[m]<a[m-1]:\n return a[m]\n \n elif a[m]>a[h-1]:\n l=m+1\n ...
2
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,4,4,5,6,7]` might become: * `[4,5,6,7,0,1,4]` if it was rotated `4` times. * `[0,1,4,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
null
Four problems all in one concept
find-minimum-in-rotated-sorted-array-ii
0
1
Four similar problems with similar concept to solve\n[33. Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/)\n```\nclass Solution:\n def search(self, nums: List[int], target: int) -> int:\n\t\tN=len(nums)\n start=0\n end=N-1\n while start<=end:\n ...
14
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,4,4,5,6,7]` might become: * `[4,5,6,7,0,1,4]` if it was rotated `4` times. * `[0,1,4,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
null
Python Binary Search ( Compare to 153)
find-minimum-in-rotated-sorted-array-ii
0
1
The problem 154 is harder than 153. \n\nAt first , we need to solve 153 ( no duplicate exists in the array )\n\n153.Find Minimum in Rotated Sorted Array:\n\nThese problems give no clear target , we can use ```nums[r]``` as judgement condition \nIt is very easy to find\uFF1A\n>if nums[mid] > nums[r]: # [3,4,5,1,2]\n>l ...
23
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,4,4,5,6,7]` might become: * `[4,5,6,7,0,1,4]` if it was rotated `4` times. * `[0,1,4,4,5,6,7]` if it was rotated `7` times. Notice that **rotating** an array `[a[0], a[1], a[2], ....
null
Solution
min-stack
1
1
```C++ []\nclass MinStack {\npublic:\n typedef struct node{\n int v;\n int minUntilNow;\n node* next;\n }node;\n\n MinStack() : topN(nullptr){\n \n }\n \n void push(int val) {\n node* n = new node;\n n->v = n->minUntilNow = val;\n n->next = nullptr;\n ...
230
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the `MinStack` class: * `MinStack()` initializes the stack object. * `void push(int val)` pushes the element `val` onto the stack. * `void pop()` removes the element on the top of the stack. * `int top(...
Consider each node in the stack having a minimum value. (Credits to @aakarshmadhavan)
O( 1 )✅ | Python (Step by step explanation)
min-stack
0
1
# Intuition\nThe MinStack is a data structure that allows for efficient retrieval of the minimum value in a stack at any given moment. This is achieved by maintaining two stacks: one for the actual elements (the `stack`), and another for the minimum elements (the `minStack`).\n\n# Approach\n1. Initialize two lists (sta...
9
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the `MinStack` class: * `MinStack()` initializes the stack object. * `void push(int val)` pushes the element `val` onto the stack. * `void pop()` removes the element on the top of the stack. * `int top(...
Consider each node in the stack having a minimum value. (Credits to @aakarshmadhavan)
O(1) || 98.32% Faster Python (Linked List)
min-stack
0
1
```\n\nclass MinStack:\n \n class _Node:\n def __init__(self, data, next=None):\n self.data = data\n self.next = next\n \n def __init__(self):\n self.head: self._Node = None\n\n def push(self, element: int) -> None:\n if self.head is None:\n n...
3
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the `MinStack` class: * `MinStack()` initializes the stack object. * `void push(int val)` pushes the element `val` onto the stack. * `void pop()` removes the element on the top of the stack. * `int top(...
Consider each node in the stack having a minimum value. (Credits to @aakarshmadhavan)
Python || 99.95% Faster || Only One Stack
min-stack
0
1
```\nclass MinStack:\n\n def __init__(self):\n self.st=[] #stack\n self.min=None #min element\n\n def push(self, val: int) -> None:\n if len(self.st)==0:\n self.st.append(val)\n self.min=val\n else:\n if val>=self.min:\n self.st.append(va...
14
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the `MinStack` class: * `MinStack()` initializes the stack object. * `void push(int val)` pushes the element `val` onto the stack. * `void pop()` removes the element on the top of the stack. * `int top(...
Consider each node in the stack having a minimum value. (Credits to @aakarshmadhavan)
Intuitive solution
min-stack
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
3
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the `MinStack` class: * `MinStack()` initializes the stack object. * `void push(int val)` pushes the element `val` onto the stack. * `void pop()` removes the element on the top of the stack. * `int top(...
Consider each node in the stack having a minimum value. (Credits to @aakarshmadhavan)
155: Solution with step by step explanation
min-stack
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nTo implement the MinStack, we can use two stacks. One stack will store the actual elements, and the other stack will store the minimum values seen so far. When we push a new element onto the stack, we check if it\'s smaller ...
18
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the `MinStack` class: * `MinStack()` initializes the stack object. * `void push(int val)` pushes the element `val` onto the stack. * `void pop()` removes the element on the top of the stack. * `int top(...
Consider each node in the stack having a minimum value. (Credits to @aakarshmadhavan)
O(1) Solution python
min-stack
0
1
\n# Approach\nCreate two seperate stacks, one is the main stack and the other is the minimum stack which holds the corresponding minimum value for each value in our main stack.\n\nWhen we want to push a value into our stack, we first check to see if the stack is empty. If it is, then we push the value in both our main ...
12
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the `MinStack` class: * `MinStack()` initializes the stack object. * `void push(int val)` pushes the element `val` onto the stack. * `void pop()` removes the element on the top of the stack. * `int top(...
Consider each node in the stack having a minimum value. (Credits to @aakarshmadhavan)
Superb Logical Solution Using Stacks
min-stack
0
1
\n\n# Solution in Python3\n```\nclass MinStack:\n\n def __init__(self):\n self.stack=[]\n\n def push(self, val: int) -> None:\n self.stack.append(val)\n\n def pop(self) -> None:\n self.stack.pop()\n\n def top(self) -> int:\n return self.stack[-1]\n\n def getMin(self) -> int:\n...
1
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the `MinStack` class: * `MinStack()` initializes the stack object. * `void push(int val)` pushes the element `val` onto the stack. * `void pop()` removes the element on the top of the stack. * `int top(...
Consider each node in the stack having a minimum value. (Credits to @aakarshmadhavan)
Python Solution With Explanation
min-stack
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- We need $$O(1)$$ for this question, thus regular methods such as sorting, looping through the array cannot be done\n- We can also deduce that:\n - We cannot access middle of array (only the back or front)\n - So we need to use som...
4
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the `MinStack` class: * `MinStack()` initializes the stack object. * `void push(int val)` pushes the element `val` onto the stack. * `void pop()` removes the element on the top of the stack. * `int top(...
Consider each node in the stack having a minimum value. (Credits to @aakarshmadhavan)
Solution
intersection-of-two-linked-lists
1
1
```C++ []\n int init = []\n{\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::ofstream out("user.out");\n for(string s; getline(std::cin, s);)\n {\n if(s[0] != \'0\') out << "Intersected at \'" << s << "\'\\n";\n else out << "No intersection\\n";\n for(int i =...
455
Given the heads of two singly linked-lists `headA` and `headB`, return _the node at which the two lists intersect_. If the two linked lists have no intersection at all, return `null`. For example, the following two linked lists begin to intersect at node `c1`: The test cases are generated such that there are no cycle...
null
Python || Easy 2 approaches || O(1) space
intersection-of-two-linked-lists
0
1
1. ## **Hashset**\n\nThe algorithm is:\n1. Store all the elements of `headA` in a hashset\n2. Iterate through the `headB` and check for the first match and then return it.\n\n**Time - O(n+m)**\n**Space - O(n)**\n\n```\nclass Solution:\n def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[List...
136
Given the heads of two singly linked-lists `headA` and `headB`, return _the node at which the two lists intersect_. If the two linked lists have no intersection at all, return `null`. For example, the following two linked lists begin to intersect at node `c1`: The test cases are generated such that there are no cycle...
null
Python || 94.40% Faster || O(1) Space || 2 Approaches
intersection-of-two-linked-lists
0
1
```\n#Time Complexity: O(n)\n#Space Complexity: O(n)\nclass Solution:\n def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:\n s=set()\n while headA:\n s.add(headA)\n headA=headA.next\n while headB:\n if headB in s:\n ...
5
Given the heads of two singly linked-lists `headA` and `headB`, return _the node at which the two lists intersect_. If the two linked lists have no intersection at all, return `null`. For example, the following two linked lists begin to intersect at node `c1`: The test cases are generated such that there are no cycle...
null
Simple Python solution
intersection-of-two-linked-lists
0
1
\nWe basically want to increase both A and B till they come out equal. The difference in the lengths can be managed in the same line using simple if-else.\nJust keep on moving A and B till they become equal\n# Code\n```\nclass Solution:\n def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[Li...
5
Given the heads of two singly linked-lists `headA` and `headB`, return _the node at which the two lists intersect_. If the two linked lists have no intersection at all, return `null`. For example, the following two linked lists begin to intersect at node `c1`: The test cases are generated such that there are no cycle...
null
Binary Search Easy Solution || Python3
find-peak-element
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(logn)\n\n- Space complexity:O(1)\n\n# Code\n```\nclass Solution:\n def findPeakElement(self, nums: List[int]) -> int:\n n...
1
A peak element is an element that is strictly greater than its neighbors. Given a **0-indexed** integer array `nums`, find a peak element, and return its index. If the array contains multiple peaks, return the index to **any of the peaks**. You may imagine that `nums[-1] = nums[n] = -∞`. In other words, an element is...
null
Simple solution
find-peak-element
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
2
A peak element is an element that is strictly greater than its neighbors. Given a **0-indexed** integer array `nums`, find a peak element, and return its index. If the array contains multiple peaks, return the index to **any of the peaks**. You may imagine that `nums[-1] = nums[n] = -∞`. In other words, an element is...
null
✨✅Clear Explanation with Graphical Visualizations✅✨
find-peak-element
0
1
# Approach\nBinary search works on questions where you can find some sort of symmetry either to delete left half or delete right half.\nLet me explain it to u for find peak element-1 on leetcode https://leetcode.com/problems/find-peak-element/\n\nConsider the array [1,2,3,4,6,7,3,2,1] . Clearly 7 is the peak element \n...
2
A peak element is an element that is strictly greater than its neighbors. Given a **0-indexed** integer array `nums`, find a peak element, and return its index. If the array contains multiple peaks, return the index to **any of the peaks**. You may imagine that `nums[-1] = nums[n] = -∞`. In other words, an element is...
null
Beginner friendly python solution using modified binary search!! 🔥🔥
find-peak-element
0
1
# Code\n```\nclass Solution:\n def findPeakElement(self, nums: List[int]) -> int:\n low=0\n high=len(nums)-1\n while low<=high:\n mid=low+(high-low)//2\n if mid>0 and nums[mid]<nums[mid-1]:\n high=mid-1\n elif mid<len(nums)-1 and nums[mid]<nums[mid...
3
A peak element is an element that is strictly greater than its neighbors. Given a **0-indexed** integer array `nums`, find a peak element, and return its index. If the array contains multiple peaks, return the index to **any of the peaks**. You may imagine that `nums[-1] = nums[n] = -∞`. In other words, an element is...
null
Mind blowing code with three conditions mid at start, end and middle.
find-peak-element
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
2
A peak element is an element that is strictly greater than its neighbors. Given a **0-indexed** integer array `nums`, find a peak element, and return its index. If the array contains multiple peaks, return the index to **any of the peaks**. You may imagine that `nums[-1] = nums[n] = -∞`. In other words, an element is...
null
Python Easy Solution using Binary Search
find-peak-element
0
1
```\nclass Solution:\n def findPeakElement(self, nums):\n # If the array contains only one element, it is the peak.\n if len(nums) == 1:\n return 0\n \n # Check if the first element is greater than the second,\n # if so, the first element is a peak.\n if nums[0] >...
8
A peak element is an element that is strictly greater than its neighbors. Given a **0-indexed** integer array `nums`, find a peak element, and return its index. If the array contains multiple peaks, return the index to **any of the peaks**. You may imagine that `nums[-1] = nums[n] = -∞`. In other words, an element is...
null
Python Easy Solution || 100% || Binary Search ||
find-peak-element
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(log n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g...
2
A peak element is an element that is strictly greater than its neighbors. Given a **0-indexed** integer array `nums`, find a peak element, and return its index. If the array contains multiple peaks, return the index to **any of the peaks**. You may imagine that `nums[-1] = nums[n] = -∞`. In other words, an element is...
null
Best Python Solution Ever || Binary Search || 9 Lines Only
find-peak-element
0
1
\n# Complexity\n- Time complexity: O(LogN)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def findPeakElement(self, nums: List[int]) -> int:\n\n s= 0\n e = len(nums)-1\n\n ...
2
A peak element is an element that is strictly greater than its neighbors. Given a **0-indexed** integer array `nums`, find a peak element, and return its index. If the array contains multiple peaks, return the index to **any of the peaks**. You may imagine that `nums[-1] = nums[n] = -∞`. In other words, an element is...
null
[ Python ] | Simple & Clean Solution Using Binary Search
find-peak-element
0
1
# Code\n```\nclass Solution:\n def findPeakElement(self, nums: List[int]) -> int:\n n = len(nums)\n l, r = 0, n - 1\n while l < r:\n mid = (l + r) >> 1\n if nums[mid] < nums[mid + 1]:\n l = mid + 1\n else:\n r = mid \n return...
4
A peak element is an element that is strictly greater than its neighbors. Given a **0-indexed** integer array `nums`, find a peak element, and return its index. If the array contains multiple peaks, return the index to **any of the peaks**. You may imagine that `nums[-1] = nums[n] = -∞`. In other words, an element is...
null
General Binary Search Thought Process : 4 Templates
find-peak-element
0
1
***Scroll down to the end to check the solution to this question using all 4 templates***\nBinary Search can be applied to a problem where we can find a function(if condition of the code) that map elements in left half to True and the other half to False or vice versa . **Don\'t think it is just for the sorted array** ...
68
A peak element is an element that is strictly greater than its neighbors. Given a **0-indexed** integer array `nums`, find a peak element, and return its index. If the array contains multiple peaks, return the index to **any of the peaks**. You may imagine that `nums[-1] = nums[n] = -∞`. In other words, an element is...
null
Python Easy Solution Ever !!!
maximum-gap
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
4
Given an integer array `nums`, return _the maximum difference between two successive elements in its sorted form_. If the array contains less than two elements, return `0`. You must write an algorithm that runs in linear time and uses linear extra space. **Example 1:** **Input:** nums = \[3,6,9,1\] **Output:** 3 **E...
null
Easy and simple solution in Python
maximum-gap
0
1
# Intuition\nThe intuition is to sort the input array and then find the maximum difference between adjacent elements, as the largest gap will likely be between sorted values.\n# Approach\nThe approach involves sorting the array and iterating through adjacent pairs to find the maximum gap. The time complexity is O(n log...
2
Given an integer array `nums`, return _the maximum difference between two successive elements in its sorted form_. If the array contains less than two elements, return `0`. You must write an algorithm that runs in linear time and uses linear extra space. **Example 1:** **Input:** nums = \[3,6,9,1\] **Output:** 3 **E...
null
Python3 code, Runtime beats 92.32% ,Memory beats 65.96%
maximum-gap
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
16
Given an integer array `nums`, return _the maximum difference between two successive elements in its sorted form_. If the array contains less than two elements, return `0`. You must write an algorithm that runs in linear time and uses linear extra space. **Example 1:** **Input:** nums = \[3,6,9,1\] **Output:** 3 **E...
null
[Java/Python3] || Explanation + Code || Brute Force & Optimized Approach || Bukcet Sort🔥
maximum-gap
1
1
# Brute Force\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nJust sort the array and check the pair of two adjacent elements having the maximum gap in the array.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nStep 1: Sort the array\nStep 2: Traverse the loop in...
9
Given an integer array `nums`, return _the maximum difference between two successive elements in its sorted form_. If the array contains less than two elements, return `0`. You must write an algorithm that runs in linear time and uses linear extra space. **Example 1:** **Input:** nums = \[3,6,9,1\] **Output:** 3 **E...
null
without solution life is nothing
maximum-gap
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
2
Given an integer array `nums`, return _the maximum difference between two successive elements in its sorted form_. If the array contains less than two elements, return `0`. You must write an algorithm that runs in linear time and uses linear extra space. **Example 1:** **Input:** nums = \[3,6,9,1\] **Output:** 3 **E...
null
Easy Python3 solution ! Beats atleat 80%
maximum-gap
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
Given an integer array `nums`, return _the maximum difference between two successive elements in its sorted form_. If the array contains less than two elements, return `0`. You must write an algorithm that runs in linear time and uses linear extra space. **Example 1:** **Input:** nums = \[3,6,9,1\] **Output:** 3 **E...
null
164: Solution with step by step explanation
maximum-gap
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. We define a Solution class with a maximumGap method that takes a list of integers nums as input and returns an integer, the maximum gap between two successive elements in the sorted form of nums.\n\n2. We check if the len...
9
Given an integer array `nums`, return _the maximum difference between two successive elements in its sorted form_. If the array contains less than two elements, return `0`. You must write an algorithm that runs in linear time and uses linear extra space. **Example 1:** **Input:** nums = \[3,6,9,1\] **Output:** 3 **E...
null
Very easy approach Python short
maximum-gap
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nSort the Input List\nCalculate the Differences Between Adjacent Elements:\nFind the Maximum Gap:\nAnd return \n<!-- Describe your approach to solving the problem. -->\n\n\n# Complexity\n- Time complexity:\n- The dominant fac...
2
Given an integer array `nums`, return _the maximum difference between two successive elements in its sorted form_. If the array contains less than two elements, return `0`. You must write an algorithm that runs in linear time and uses linear extra space. **Example 1:** **Input:** nums = \[3,6,9,1\] **Output:** 3 **E...
null
Python Easy Solution || 100% || Beginners Friendly ||
maximum-gap
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nAs descripted is problem if array have 1 element then return 0\nNext will sort the arrey and get the diffrence of each element and compare the value of max variable ...
2
Given an integer array `nums`, return _the maximum difference between two successive elements in its sorted form_. If the array contains less than two elements, return `0`. You must write an algorithm that runs in linear time and uses linear extra space. **Example 1:** **Input:** nums = \[3,6,9,1\] **Output:** 3 **E...
null
easy python3 solution
maximum-gap
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
Given an integer array `nums`, return _the maximum difference between two successive elements in its sorted form_. If the array contains less than two elements, return `0`. You must write an algorithm that runs in linear time and uses linear extra space. **Example 1:** **Input:** nums = \[3,6,9,1\] **Output:** 3 **E...
null
Easeist Submission.
maximum-gap
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
Given an integer array `nums`, return _the maximum difference between two successive elements in its sorted form_. If the array contains less than two elements, return `0`. You must write an algorithm that runs in linear time and uses linear extra space. **Example 1:** **Input:** nums = \[3,6,9,1\] **Output:** 3 **E...
null
Solution
maximum-gap
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
Given an integer array `nums`, return _the maximum difference between two successive elements in its sorted form_. If the array contains less than two elements, return `0`. You must write an algorithm that runs in linear time and uses linear extra space. **Example 1:** **Input:** nums = \[3,6,9,1\] **Output:** 3 **E...
null
Maximum gap using Python
maximum-gap
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
Given an integer array `nums`, return _the maximum difference between two successive elements in its sorted form_. If the array contains less than two elements, return `0`. You must write an algorithm that runs in linear time and uses linear extra space. **Example 1:** **Input:** nums = \[3,6,9,1\] **Output:** 3 **E...
null
pyhton easy solution
maximum-gap
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
Given an integer array `nums`, return _the maximum difference between two successive elements in its sorted form_. If the array contains less than two elements, return `0`. You must write an algorithm that runs in linear time and uses linear extra space. **Example 1:** **Input:** nums = \[3,6,9,1\] **Output:** 3 **E...
null
Simple Python Solution for beginners
maximum-gap
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
Given an integer array `nums`, return _the maximum difference between two successive elements in its sorted form_. If the array contains less than two elements, return `0`. You must write an algorithm that runs in linear time and uses linear extra space. **Example 1:** **Input:** nums = \[3,6,9,1\] **Output:** 3 **E...
null
✔️ [Python3] SOLUTION, Explained
compare-version-numbers
0
1
**UPVOTE if you like (\uD83C\uDF38\u25E0\u203F\u25E0), If you have any question, feel free to ask.**\n\nFirstly, we split versions by `.` and convert revisions to integers. Next, we iterate over revisions and compare one by one.\n\n`zip_longest` - same as a `zip` but it also pads lists with zeros if lengths are not equ...
65
Given two version numbers, `version1` and `version2`, compare them. Version numbers consist of **one or more revisions** joined by a dot `'.'`. Each revision consists of **digits** and may contain leading **zeros**. Every revision contains **at least one character**. Revisions are **0-indexed from left to right**, wit...
null
Simple solution
compare-version-numbers
0
1
\n\n# Code\n```\nclass Solution:\n def compareVersion(self, version1: str, version2: str) -> int:\n lst1=version1.split(\'.\')\n lst2=version2.split(\'.\')\n if(len(lst1)>len(lst2)):\n for i in range(len(lst1)-len(lst2)):\n lst2.append(\'0\')\n if(len(lst2)>len(l...
1
Given two version numbers, `version1` and `version2`, compare them. Version numbers consist of **one or more revisions** joined by a dot `'.'`. Each revision consists of **digits** and may contain leading **zeros**. Every revision contains **at least one character**. Revisions are **0-indexed from left to right**, wit...
null
165: Solution with step by step explanation|
compare-version-numbers
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe function compareVersion takes two string arguments version1 and version2, and returns an integer.\n\nFirst, the function splits both version strings into a list of revisions using the split method and the dot \'.\' as se...
5
Given two version numbers, `version1` and `version2`, compare them. Version numbers consist of **one or more revisions** joined by a dot `'.'`. Each revision consists of **digits** and may contain leading **zeros**. Every revision contains **at least one character**. Revisions are **0-indexed from left to right**, wit...
null
Python | Easy to understand | For Beginner | Faster than 90%
compare-version-numbers
0
1
```\nclass Solution:\n def compareVersion(self, version1: str, version2: str) -> int:\n ver1=version1.split(\'.\')\n ver2=version2.split(\'.\')\n i=0\n maxlen=max(len(ver1),len(ver2))\n while i<maxlen:\n v1=int(ver1[i]) if i<len(ver1) else 0\n v2=int(ver2[i]) ...
1
Given two version numbers, `version1` and `version2`, compare them. Version numbers consist of **one or more revisions** joined by a dot `'.'`. Each revision consists of **digits** and may contain leading **zeros**. Every revision contains **at least one character**. Revisions are **0-indexed from left to right**, wit...
null
Python | Two pointers | No functions | Time : O(m,n)| Space : O(1)
compare-version-numbers
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nCheck the numbers till we get a \'.\' dot and compare them.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThis approarch doesn\'t use any list to string or string to list conversion.\n\nHave 2 pointers \'first\' ...
1
Given two version numbers, `version1` and `version2`, compare them. Version numbers consist of **one or more revisions** joined by a dot `'.'`. Each revision consists of **digits** and may contain leading **zeros**. Every revision contains **at least one character**. Revisions are **0-indexed from left to right**, wit...
null
✅ Python3 | Simple and Concise Solution |Easy To Understand W/Explanation
compare-version-numbers
0
1
\t\t\t\t\t\t\t\t\t\t\t\t\t\tPlease Upvote if it helps, Thanks.\n**Intuition:**\n1. It\'s already mentioned that every revision can be stored in 32 bit integer (Revision is numbers between every two dots, except the first).\n2. Hence lets compare every revision as Integer (If revision doesn\'t exist for a version, we ca...
9
Given two version numbers, `version1` and `version2`, compare them. Version numbers consist of **one or more revisions** joined by a dot `'.'`. Each revision consists of **digits** and may contain leading **zeros**. Every revision contains **at least one character**. Revisions are **0-indexed from left to right**, wit...
null
[Python3] Easy O(N) Time Solution
compare-version-numbers
0
1
```\n\'\'\' \nclass Solution:\n def compareVersion(self, v1: str, v2: str) -> int:\n i = 0\n j = 0\n while i < len(v1) or j < len(v2):\n n1 = 0\n k = i\n if k < len(v1):\n while k < len(v1) and v1[k] != \'.\':\n k += 1\n ...
2
Given two version numbers, `version1` and `version2`, compare them. Version numbers consist of **one or more revisions** joined by a dot `'.'`. Each revision consists of **digits** and may contain leading **zeros**. Every revision contains **at least one character**. Revisions are **0-indexed from left to right**, wit...
null
[Python, Python3] Two Solutions
compare-version-numbers
0
1
**Solution1**\n\n```\nclass Solution(object):\n def compareVersion(self, version1, version2):\n """\n :type version1: str\n :type version2: str\n :rtype: int\n """\n version1 = version1.split(".")\n version2 = version2.split(".")\n c = 0\n while c < len(...
2
Given two version numbers, `version1` and `version2`, compare them. Version numbers consist of **one or more revisions** joined by a dot `'.'`. Each revision consists of **digits** and may contain leading **zeros**. Every revision contains **at least one character**. Revisions are **0-indexed from left to right**, wit...
null
Simple & Short Python Solution
compare-version-numbers
0
1
```\nclass Solution:\n def compareVersion(self, version1: str, version2: str) -> int:\n v1 = version1.split(\'.\') # Divide string version1 into array seperated by "."\n v2 = version2.split(\'.\') # Divide string version2 into array seperated by "."\n m = len(v1)\n n = len(v2)\n\t\t# Just...
2
Given two version numbers, `version1` and `version2`, compare them. Version numbers consist of **one or more revisions** joined by a dot `'.'`. Each revision consists of **digits** and may contain leading **zeros**. Every revision contains **at least one character**. Revisions are **0-indexed from left to right**, wit...
null
166: Solution with step by step explanation
fraction-to-recurring-decimal
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis solution also handles the edge cases of a zero numerator and a zero denominator, and also checks for the negative sign at the beginning. It then calculates the integer part of the result by doing an integer division of ...
15
Given two integers representing the `numerator` and `denominator` of a fraction, return _the fraction in string format_. If the fractional part is repeating, enclose the repeating part in parentheses. If multiple answers are possible, return **any of them**. It is **guaranteed** that the length of the answer string ...
No scary math, just apply elementary math knowledge. Still remember how to perform a long division? Try a long division on 4/9, the repeating part is obvious. Now try 4/333. Do you see a pattern? Notice that once the remainder starts repeating, so does the divided result. Be wary of edge cases! List out as many test ca...
Python 😎 Easy to understand
fraction-to-recurring-decimal
0
1
\n# Code\n```\nclass Solution:\n def fractionToDecimal(self, n: int, d: int) -> str:\n sol = ""\n \n # If the numerator is zero, the fraction will be zero irrespective of the denominator.\n if n == 0:\n return "0"\n \n # To determine if the result should be negati...
2
Given two integers representing the `numerator` and `denominator` of a fraction, return _the fraction in string format_. If the fractional part is repeating, enclose the repeating part in parentheses. If multiple answers are possible, return **any of them**. It is **guaranteed** that the length of the answer string ...
No scary math, just apply elementary math knowledge. Still remember how to perform a long division? Try a long division on 4/9, the repeating part is obvious. Now try 4/333. Do you see a pattern? Notice that once the remainder starts repeating, so does the divided result. Be wary of edge cases! List out as many test ca...
Python - Long Divsion Solution
fraction-to-recurring-decimal
0
1
```\nclass Solution:\n def fractionToDecimal(self, numerator: int, denominator: int) -> str:\n if numerator == 0: return \'0\'\n \n result = []\n if numerator < 0 and denominator > 0 or numerator >= 0 and denominator < 0:\n result.append(\'-\')\n \n numerator, den...
11
Given two integers representing the `numerator` and `denominator` of a fraction, return _the fraction in string format_. If the fractional part is repeating, enclose the repeating part in parentheses. If multiple answers are possible, return **any of them**. It is **guaranteed** that the length of the answer string ...
No scary math, just apply elementary math knowledge. Still remember how to perform a long division? Try a long division on 4/9, the repeating part is obvious. Now try 4/333. Do you see a pattern? Notice that once the remainder starts repeating, so does the divided result. Be wary of edge cases! List out as many test ca...
simple sol with if else statements
fraction-to-recurring-decimal
0
1
# Code\n```\nclass Solution:\n def fractionToDecimal(self, numerator: int, denominator: int) -> str:\n if numerator == 0:\n return "0"\n if denominator == 0:\n return ""\n\n result = ""\n if (numerator < 0) ^ (denominator < 0):\n result += "-"\n\n ...
0
Given two integers representing the `numerator` and `denominator` of a fraction, return _the fraction in string format_. If the fractional part is repeating, enclose the repeating part in parentheses. If multiple answers are possible, return **any of them**. It is **guaranteed** that the length of the answer string ...
No scary math, just apply elementary math knowledge. Still remember how to perform a long division? Try a long division on 4/9, the repeating part is obvious. Now try 4/333. Do you see a pattern? Notice that once the remainder starts repeating, so does the divided result. Be wary of edge cases! List out as many test ca...
Python3 100% Runtime 83% Memory
fraction-to-recurring-decimal
0
1
![Screenshot 2023-08-17 184126.png](https://assets.leetcode.com/users/images/aa3f9dac-66f3-4178-b22e-1513710ec402_1692268893.7671633.png)\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nCalculating digits using long division while storing previous remainders in a dictionary.\n\n# Cod...
0
Given two integers representing the `numerator` and `denominator` of a fraction, return _the fraction in string format_. If the fractional part is repeating, enclose the repeating part in parentheses. If multiple answers are possible, return **any of them**. It is **guaranteed** that the length of the answer string ...
No scary math, just apply elementary math knowledge. Still remember how to perform a long division? Try a long division on 4/9, the repeating part is obvious. Now try 4/333. Do you see a pattern? Notice that once the remainder starts repeating, so does the divided result. Be wary of edge cases! List out as many test ca...
Python solution
fraction-to-recurring-decimal
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n- O(len(numerator / denominator))\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\nO(len(numerator / ...
0
Given two integers representing the `numerator` and `denominator` of a fraction, return _the fraction in string format_. If the fractional part is repeating, enclose the repeating part in parentheses. If multiple answers are possible, return **any of them**. It is **guaranteed** that the length of the answer string ...
No scary math, just apply elementary math knowledge. Still remember how to perform a long division? Try a long division on 4/9, the repeating part is obvious. Now try 4/333. Do you see a pattern? Notice that once the remainder starts repeating, so does the divided result. Be wary of edge cases! List out as many test ca...
Python O(N) solution using two independent for loops.
two-sum-ii-input-array-is-sorted
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIf my interpretation of the time complexity is wrong, please let me know.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTraverses through the array once. Checks if two numbers whose sum equals the target is in th...
0
Given a **1-indexed** array of integers `numbers` that is already **_sorted in non-decreasing order_**, find two numbers such that they add up to a specific `target` number. Let these two numbers be `numbers[index1]` and `numbers[index2]` where `1 <= index1 < index2 <= numbers.length`. Return _the indices of the two n...
null
✅[Beginner Friendly][Beats 💯%][O(N) O(1)] Easiest Solution in Python3 with Explanation
two-sum-ii-input-array-is-sorted
0
1
# Intuition\nUse 2 pointers to find the indices.\n\n# Approach\nUse 2 pointers denoted by left and right where left is pointing to the first index and right is pointing to the last index initially. If the sum of the elements corresponding to the left and right index results in target, return a list of left and right af...
1
Given a **1-indexed** array of integers `numbers` that is already **_sorted in non-decreasing order_**, find two numbers such that they add up to a specific `target` number. Let these two numbers be `numbers[index1]` and `numbers[index2]` where `1 <= index1 < index2 <= numbers.length`. Return _the indices of the two n...
null
Python Easy O(1) Space
two-sum-ii-input-array-is-sorted
0
1
This problem is an extension to [Two Sum](https://leetcode.com/problems/two-sum/) problem. In the two-sum problem, the input array is unsorted and hence we have to use a hashmap to solve the problem in **O(n)** time. But that completely changes, once the input is sorted. \nThe algorithm is:\n1. Initialize two pointers ...
78
Given a **1-indexed** array of integers `numbers` that is already **_sorted in non-decreasing order_**, find two numbers such that they add up to a specific `target` number. Let these two numbers be `numbers[index1]` and `numbers[index2]` where `1 <= index1 < index2 <= numbers.length`. Return _the indices of the two n...
null