Skip to content

Commit ab00993

Browse files
author
wuduhren
committed
updates
1 parent b178190 commit ab00993

7 files changed

+114
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def containsDuplicate(self, nums: List[int]) -> bool:
3+
seen = set()
4+
for num in nums:
5+
if num in seen: return True
6+
seen.add(num)
7+
return False

problems/python3/hand-of-straights.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:
3+
if len(hand)%groupSize!=0: return False
4+
5+
counter = collections.Counter(hand)
6+
h = list(counter.keys())
7+
heapq.heapify(h)
8+
9+
while h:
10+
minNum = h[0]
11+
for n in range(minNum, minNum+groupSize):
12+
if counter[n]<=0: return False
13+
counter[n] -= 1
14+
if counter[n]==0:
15+
if h[0]!=n: return False
16+
heapq.heappop(h)
17+
18+
return True
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
If any element in the triplets is larger than the element in target, it cannot be used.
3+
Check if we have all 3 index found the same value.
4+
"""
5+
class Solution:
6+
def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:
7+
okIndex = set()
8+
9+
for a, b, c in triplets:
10+
if a>target[0] or b>target[1] or c>target[2]: continue
11+
if a==target[0]: okIndex.add(0)
12+
if b==target[1]: okIndex.add(1)
13+
if c==target[2]: okIndex.add(2)
14+
15+
return len(okIndex)==3

problems/python3/partition-labels.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
Time: O(N)
3+
Space: O(N)
4+
5+
For each char, store its max index in maxIndex.
6+
Iterate through the string, update the "currMax" along the way.
7+
currMax is the place we can partition unless it get updated again.
8+
If the current index is the currMax, update "ans".
9+
"""
10+
class Solution:
11+
def partitionLabels(self, s: str) -> List[int]:
12+
ans = []
13+
maxIndex = {}
14+
15+
for i, c in enumerate(s):
16+
maxIndex[c] = i
17+
18+
currMax = 0
19+
processedLength = 0
20+
for i, c in enumerate(s):
21+
currMax = max(currMax, maxIndex[c])
22+
if i==currMax:
23+
ans.append(i+1 - processedLength)
24+
processedLength += ans[-1]
25+
26+
return ans

problems/python3/two-sum.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def twoSum(self, nums: List[int], target: int) -> List[int]:
3+
#needed: {number required : counter part index}
4+
needed = {}
5+
for i, num in enumerate(nums):
6+
if num in needed:
7+
return (needed[num], i)
8+
needed[target-num] = i

problems/python3/valid-anagram.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def isAnagram(self, s: str, t: str) -> bool:
3+
counter = collections.Counter()
4+
5+
for c in s:
6+
counter[c] += 1
7+
8+
for c in t:
9+
if c not in counter:
10+
return False
11+
counter[c] -= 1
12+
13+
for c in counter:
14+
if counter[c]>0:
15+
return False
16+
17+
return True
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def checkValidString(self, s: str) -> bool:
3+
leftMin = 0
4+
leftMax = 0
5+
6+
for c in s:
7+
if c=='(':
8+
leftMin += 1
9+
leftMax += 1
10+
elif c==')':
11+
leftMin -= 1
12+
leftMax -= 1
13+
else:
14+
leftMin -= 1
15+
leftMax += 1
16+
17+
if leftMax<0:
18+
return False
19+
20+
if leftMin<0:
21+
leftMin = 0
22+
23+
return leftMin == 0

0 commit comments

Comments
 (0)