diff --git a/2441. Largest Positive Integer That Exists With Its Negative/2441. Largest Positive Integer That Exists With Its Negative.py b/2441. Largest Positive Integer That Exists With Its Negative/2441. Largest Positive Integer That Exists With Its Negative.py new file mode 100644 index 0000000..af52e44 --- /dev/null +++ b/2441. Largest Positive Integer That Exists With Its Negative/2441. Largest Positive Integer That Exists With Its Negative.py @@ -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 diff --git a/349. Intersection of Two Arrays/349. Intersection of Two Arrays_1.py b/349. Intersection of Two Arrays/349. Intersection of Two Arrays_1.py index e721779..78d7e7e 100644 --- a/349. Intersection of Two Arrays/349. Intersection of Two Arrays_1.py +++ b/349. Intersection of Two Arrays/349. Intersection of Two Arrays_1.py @@ -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 diff --git a/791. Custom Sort String/791. Custom Sort String.py b/791. Custom Sort String/791. Custom Sort String.py new file mode 100644 index 0000000..1df68cb --- /dev/null +++ b/791. Custom Sort String/791. Custom Sort String.py @@ -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 \ No newline at end of file