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

Commit db43d79

Browse files
authored
Merge pull request #10 from 149ps/LC-problems
new files
2 parents 44d0f00 + 0be02cf commit db43d79

File tree

3 files changed

+89
-5
lines changed

3 files changed

+89
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
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.
3+
4+
Return the positive integer k. If there is no such integer, return -1.
5+
6+
7+
8+
Example 1:
9+
10+
Input: nums = [-1,2,-3,3]
11+
Output: 3
12+
Explanation: 3 is the only valid k we can find in the array.
13+
Example 2:
14+
15+
Input: nums = [-1,10,6,7,-7,1]
16+
Output: 7
17+
Explanation: Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
18+
Example 3:
19+
20+
Input: nums = [-10,8,6,7,-2,-3]
21+
Output: -1
22+
Explanation: There is no a single valid k, we return -1.
23+
24+
25+
Constraints:
26+
27+
1 <= nums.length <= 1000
28+
-1000 <= nums[i] <= 1000
29+
nums[i] != 0
30+
"""
31+
class Solution:
32+
def findMaxK(self, nums: List[int]) -> int:
33+
result = -1
34+
hset = set(nums)
35+
for num in nums:
36+
if (-1)*num in hset:
37+
result = max(result, num)
38+
return result

349. Intersection of Two Arrays/349. Intersection of Two Arrays_1.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
class Solution:
22
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
3-
i,j = 0,0
43
nums1.sort()
54
nums2.sort()
65
result = []
6+
i,j= 0,0
77
while i < len(nums1) and j < len(nums2):
8-
if nums1[i] > nums2[j]:
9-
j += 1
10-
elif nums1[i] < nums2[j]:
8+
if nums1[i] < nums2[j]:
119
i += 1
10+
elif nums1[i] > nums2[j]:
11+
j += 1
1212
else:
13-
if nums1[i] not in result:
13+
if not result or result[-1] != nums1[i]:
1414
result.append(nums1[i])
1515
i += 1
1616
j += 1
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
You are given two strings order and s. All the characters of order are unique and were sorted in some custom order previously.
3+
4+
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.
5+
6+
Return any permutation of s that satisfies this property.
7+
8+
9+
10+
Example 1:
11+
12+
Input: order = "cba", s = "abcd"
13+
14+
Output: "cbad"
15+
16+
Explanation: "a", "b", "c" appear in order, so the order of "a", "b", "c" should be "c", "b", and "a".
17+
18+
Since "d" does not appear in order, it can be at any position in the returned string. "dcba", "cdba", "cbda" are also valid outputs.
19+
20+
Example 2:
21+
22+
Input: order = "bcafg", s = "abcd"
23+
24+
Output: "bcad"
25+
26+
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.
27+
28+
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.
29+
"""
30+
import collections
31+
32+
33+
class Solution:
34+
def customSortString(self, order: str, s: str) -> str:
35+
hmap, count = {}, collections.Counter(s)
36+
for i,char in enumerate(order):
37+
hmap[char] = i
38+
j = 0
39+
result,temp = "",""
40+
for key,val in hmap.items():
41+
if key in count:
42+
result += count[key] * key
43+
for k,v in count.items():
44+
if k not in hmap:
45+
temp += k*v
46+
return result + temp

0 commit comments

Comments
 (0)