Skip to content

Commit

Permalink
Merge pull request #10 from 149ps/LC-problems
Browse files Browse the repository at this point in the history
new files
  • Loading branch information
149ps committed May 2, 2024
2 parents 44d0f00 + 0be02cf commit db43d79
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
Given an integer array nums that does not contain any zeros, find the largest positive integer k such that -k also exists in the array.
Return the positive integer k. If there is no such integer, return -1.
Example 1:
Input: nums = [-1,2,-3,3]
Output: 3
Explanation: 3 is the only valid k we can find in the array.
Example 2:
Input: nums = [-1,10,6,7,-7,1]
Output: 7
Explanation: Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
Example 3:
Input: nums = [-10,8,6,7,-2,-3]
Output: -1
Explanation: There is no a single valid k, we return -1.
Constraints:
1 <= nums.length <= 1000
-1000 <= nums[i] <= 1000
nums[i] != 0
"""
class Solution:
def findMaxK(self, nums: List[int]) -> int:
result = -1
hset = set(nums)
for num in nums:
if (-1)*num in hset:
result = max(result, num)
return result
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
i,j = 0,0
nums1.sort()
nums2.sort()
result = []
i,j= 0,0
while i < len(nums1) and j < len(nums2):
if nums1[i] > nums2[j]:
j += 1
elif nums1[i] < nums2[j]:
if nums1[i] < nums2[j]:
i += 1
elif nums1[i] > nums2[j]:
j += 1
else:
if nums1[i] not in result:
if not result or result[-1] != nums1[i]:
result.append(nums1[i])
i += 1
j += 1
Expand Down
46 changes: 46 additions & 0 deletions 791. Custom Sort String/791. Custom Sort String.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
You are given two strings order and s. All the characters of order are unique and were sorted in some custom order previously.
Permute the characters of s so that they match the order that order was sorted. More specifically, if a character x occurs before a character y in order, then x should occur before y in the permuted string.
Return any permutation of s that satisfies this property.
Example 1:
Input: order = "cba", s = "abcd"
Output: "cbad"
Explanation: "a", "b", "c" appear in order, so the order of "a", "b", "c" should be "c", "b", and "a".
Since "d" does not appear in order, it can be at any position in the returned string. "dcba", "cdba", "cbda" are also valid outputs.
Example 2:
Input: order = "bcafg", s = "abcd"
Output: "bcad"
Explanation: The characters "b", "c", and "a" from order dictate the order for the characters in s. The character "d" in s does not appear in order, so its position is flexible.
Following the order of appearance in order, "b", "c", and "a" from s should be arranged as "b", "c", "a". "d" can be placed at any position since it's not in order. The output "bcad" correctly follows this rule. Other arrangements like "bacd" or "bcda" would also be valid, as long as "b", "c", "a" maintain their order.
"""
import collections


class Solution:
def customSortString(self, order: str, s: str) -> str:
hmap, count = {}, collections.Counter(s)
for i,char in enumerate(order):
hmap[char] = i
j = 0
result,temp = "",""
for key,val in hmap.items():
if key in count:
result += count[key] * key
for k,v in count.items():
if k not in hmap:
temp += k*v
return result + temp

0 comments on commit db43d79

Please sign in to comment.