-
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.
- Loading branch information
Showing
3 changed files
with
89 additions
and
5 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
... Exists With Its Negative/2441. Largest Positive Integer That Exists With Its Negative.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,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 |
10 changes: 5 additions & 5 deletions
10
349. Intersection of Two Arrays/349. Intersection of Two Arrays_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
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,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 |