This file contains hidden or 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
def abbreviation(a, b): | |
m, n = len(a), len(b) | |
dp = [[False]*(m+1) for _ in range(n+1)] | |
dp[0][0] = True | |
for i in range(n+1): | |
for j in range(1,m+1): | |
if a[j-1] == b[i-1]: | |
dp[i][j] = dp[i-1][j-1] | |
elif a[j-1].upper() == b[i-1]: | |
dp[i][j] = dp[i-1][j-1] or dp[i][j-1] |
This file contains hidden or 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
from functools import lru_cache | |
@lru_cache(None) | |
def abbreviation(a, b): | |
def dp(a,b): | |
if not a and not b:return True | |
if not a and b:return False | |
if not b and a:return all(ss.islower() for ss in a) | |
if len(a) ==1 and len(b)==1: | |
if a.upper()==b:return True | |
else:return False |
This file contains hidden or 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
class Solution: | |
def minMalwareSpread(self, graph: List[List[int]], initial: List[int]) -> int: | |
self.uf = {} | |
def union(x, y): | |
self.uf[find(y)] = find(x) | |
def find(x): | |
self.uf.setdefault(x, x) | |
if self.uf[x]!=x: | |
self.uf[x] = find(self.uf[x]) | |
return self.uf[x] |
This file contains hidden or 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
class Solution: | |
def threeSum(self, nums: List[int]) -> List[List[int]]: | |
res = [] | |
nums.sort() | |
for i in range(len(nums)-2): | |
if i > 0 and nums[i] == nums[i-1]: | |
continue | |
l, r = i+1, len(nums)-1 | |
while l < r: | |
s = nums[i] + nums[l] + nums[r] |
This file contains hidden or 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
class Solution: | |
def threeSum(self, nums: List[int]) -> List[List[int]]: | |
nums.sort() | |
res = set() | |
for i in range(len(nums)-2): | |
a = nums[i] | |
for j in range(i+1, len(nums)-1): | |
b = nums[j] | |
c = -(a+b) | |
if c < nums[j+1]: |
This file contains hidden or 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
class Solution: | |
def threeSumMulti(self, A: List[int], target: int) -> int: | |
cnt = collections.Counter(A) | |
res, MOD =0, 10**9+7 | |
seen = set() | |
for a, b in itertools.combinations(cnt.keys(), 2): | |
c = target - a - b | |
a, b, c = sorted([a,b,c]) | |
if (a, b, c) not in seen: | |
seen.add((a,b,c)) |
This file contains hidden or 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
class Solution: | |
def threeSumMulti(self, A: List[int], target: int) -> int: | |
cnt = collections.Counter(A) | |
res = 0 | |
MOD = 10**9+7 | |
for a, b, c in itertools.combinations(cnt.keys(), 3): | |
if a+b+c == target: | |
res += cnt[a]*cnt[b]*cnt[c] % MOD | |
for a, b in itertools.combinations(cnt.keys(), 2): | |
if 2*a+b==target and cnt[a]>=2: |
This file contains hidden or 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
class Solution: | |
def threeEqualParts(self, A: List[int]) -> List[int]: | |
S = ''.join([str(a) for a in A]) | |
cnt_1 = S.count('1') | |
if cnt_1 == 0:return [0, 2] | |
# each part should have the same number of '1' | |
if cnt_1 % 3!=0:return [-1, -1] | |
counter_1, valid_part = 0, '' | |
for i, s in enumerate(S[::-1]): |
This file contains hidden or 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
class Solution: | |
def threeEqualParts(self, A: List[int]) -> List[int]: | |
S = ''.join([str(a) for a in A]) | |
cnt_1 = S.count('1') | |
if cnt_1 == 0:return [0, 2] | |
# each part should have the same number of '1' | |
if cnt_1 % 3!=0:return [-1, -1] | |
counter_1, idx, first = 0, 0, '' | |
leading_zero = True | |
for i, s in enumerate(S): |
This file contains hidden or 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
#!/bin/python3 | |
import math | |
import os | |
import random | |
import re | |
import sys | |
# | |
# Complete the 'getTotalX' function below. |