-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from 149ps/LC-problems
Lc problems
- Loading branch information
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
Given two integer arrays nums1 and nums2, sorted in non-decreasing order, return the minimum integer common to both arrays. If there is no common integer amongst nums1 and nums2, return -1. | ||
Note that an integer is said to be common to nums1 and nums2 if both arrays have at least one occurrence of that integer. | ||
Example 1: | ||
Input: nums1 = [1,2,3], nums2 = [2,4] | ||
Output: 2 | ||
Explanation: The smallest element common to both arrays is 2, so we return 2. | ||
Example 2: | ||
Input: nums1 = [1,2,3,6], nums2 = [2,3,4,5] | ||
Output: 2 | ||
Explanation: There are two common elements in the array 2 and 3 out of which 2 is the smallest, so 2 is returned. | ||
Constraints: | ||
1 <= nums1.length, nums2.length <= 105 | ||
1 <= nums1[i], nums2[j] <= 109 | ||
Both nums1 and nums2 are sorted in non-decreasing order. | ||
""" | ||
class Solution: | ||
def getCommon(self, nums1: List[int], nums2: List[int]) -> int: | ||
def binary_search(target,nums): | ||
left,right = 0,len(nums)-1 | ||
while left <= right: | ||
mid = left + (right- left)//2 | ||
if nums[mid] == target: return True | ||
elif nums[mid] > target: | ||
right = mid - 1 | ||
else: | ||
left = mid + 1 | ||
return False | ||
if len(nums1) > len(nums2): | ||
for num in nums2: | ||
if binary_search(num,nums1): | ||
return num | ||
return -1 | ||
else: | ||
for num in nums1: | ||
if binary_search(num,nums2): | ||
return num | ||
return -1 |
11 changes: 11 additions & 0 deletions
11
2540. Minimum Common Value/2540. Minimum Common Value_1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Solution: | ||
def getCommon(self, nums1: List[int], nums2: List[int]) -> int: | ||
i, j = 0, 0 | ||
while i < len(nums1) and j < len(nums2): | ||
if nums1[i] < nums2[j]: | ||
i += 1 | ||
elif nums1[i] > nums2[j]: | ||
j += 1 | ||
else: | ||
return nums1[i] | ||
return -1 |
15 changes: 15 additions & 0 deletions
15
647. Palindromic Substrings/647. Palindromic Substrings_2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution: | ||
def dfs(self, left, right,s): | ||
final = 0 | ||
while left >= 0 and right < len(s) and s[left] == s[right]: | ||
left -= 1 | ||
right += 1 | ||
final += 1 | ||
return final | ||
|
||
def countSubstrings(self, s: str) -> int: | ||
result = 0 | ||
for i in range(len(s)): | ||
result += self.dfs(i,i,s) | ||
result += self.dfs(i,i+1,s) | ||
return result |