Skip to content

Commit

Permalink
Merge pull request #9 from 149ps/LC-problems
Browse files Browse the repository at this point in the history
Lc problems
  • Loading branch information
149ps committed Mar 9, 2024
2 parents deb93df + 165b66e commit 44d0f00
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
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

0 comments on commit 44d0f00

Please sign in to comment.