Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Lc problems #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions 2540. Minimum Common Value/2540. Minimum Common Value.py
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 2540. Minimum Common Value/2540. Minimum Common Value_1.py
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 647. Palindromic Substrings/647. Palindromic Substrings_2.py
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