lee19856
BAN USER
L = [('a', 10), ('b', 15), ('c', 13), ('d', 10), ('a', 30)]
def movie (arr):
dic = {}
for i in range(len(arr)):
if arr[i][0] not in dic :
dic[arr[i][0]] = arr[i][1]
else:
dic[arr[i][0]] += arr[i][1]
return sorted(dic.items(), key=lambda x:x[1], reverse=True)
print(movie(L))
first of all, thanks to 'adr' for sharing such.. i got to know what 'union-find' algo.
actually i'd like to change like following to get same parent correctly after union bottom and top, according to my test.. so in summary, if sensors are all connected, can't pass thru hence return False. if not, return True. again, thanks to adr. i learned one algo.
for i,j in zip(bottom, bottom[1:]):
union(i,j) -> union(j,i)
if (X-x)*(X-x) + (Y-y)*(Y-y) <= r*r:
union(i,I) -> union(I,i)
slightly modified leetcode solution so that it works on both num and alphabet.
class Solution(object):
# n: length
# k: alphabet
def DeBruijn(self, n, k):
seen = set()
answer = []
try:
alphabet = list(map(str, range(k)))
except(ValueError, TypeError):
alphabet = k
def dfs(node):
for x in alphabet:
neighbor = node + x
if neighbor not in seen:
seen.add(neighbor)
dfs(neighbor[1:])
answer.append(x)
dfs(alphabet[0] * (n-1))
# print(seen)
return "".join(answer) + alphabet[0] * (n-1)
My finding..
1) the number of subtrees of a node is called degree of the node.
2) The degree of a tree is the maximum degree of a node in the tree.
3) A binary tree is degree 2.
then i would to this
1) review all subtree of each node with BFS
2) have one variable to keep updating max num of subtree
3) once complete to review all nodes, then return max num which would be *degree* of the graph or tree
another one i tried...
def find2(x):
temp = []
for i in range(len(x)-2):
for j in range(i+1, len(x)-1):
for k in range(j+1, len(x)):
a = sorted([x[i], x[j], x[k]])
for w in x:
if a[0]<a[1]<a[2]<w and a[0]+a[1]+a[2]==w:
temp.append((a[0], a[1], a[2]))
print(temp)
return len(temp)
print("Solution-2: ",find2([10,2,3,7,8,6,4,5,9,1]))
Tried w/ Python....
def find(nums):
temp=[]
for i in range(len(nums)):
current = nums[i]
right = nums[i+1:]
for c in helper(right):
a = sorted([current] + c)
for amount in nums:
if a not in temp and sum(a) == amount and a[0] < a[1] < a[2] < amount:
temp.append(a)
print(temp)
return len(temp)
def helper(a):
temp = []
for i in range(len(a)-1):
for j in range(i+1, len(a)):
if not [a[i], a[j]] in temp:
temp.append([a[i], a[j]])
return temp
""" all unique num, not sorted input, negative num included """
print("Solution-1: ",find([-1,0,1,2,3,-2,5]))
print("Solution-1: ",find([10,2,3,7,8,6,4,5,9,1]))
"""
a = [1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0]
empty seat = 0
taken seat = 1
find seat most far away from taken seat
"""
def longestdistance(arr):
max_cnt = float('-inf')
tmp = []
for i in range(len(arr)):
if arr[i] != 1:
left = i-1
right = i+1
current_cnt = 0
while left > 0 and right < len(arr) and arr[left] != 1 and arr[right] != 1:
left -= 1
right += 1
current_cnt += 1
if current_cnt > max_cnt:
max_cnt = current_cnt
tmp.append(i)
return tmp.pop() if tmp else False
print("position: ", longestdistance([1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0]))
print("position: ", longestdistance([1]))
print("position: ", longestdistance([0]))
I wanted to make function that find any triplet that sum-up is same as given amount... code does not seem to be quite optimized but it works.
def threesum(nums, amount):
temp=[]
distance=0
d={}
for i in range(len(nums)):
left = nums[:i]
right = nums[i+1:]
current = nums[i]
rest = left+right
ret = twocombo(rest)
for c in ret:
a = sorted([current]+c)
if a not in temp and sum(a) == amount:
temp.append(a)
return temp
def twocombo(rest):
temp = []
for i in range(len(rest)):
for j in range(i, len(rest)):
if i != j:
a = sorted([rest[i]]+[rest[j]])
if not a in temp:
temp.append(a)
return temp
arr=[-1, 0, 1, 2, -1, -4]
amount = 0
print(threesum(arr, amount))
- lee19856 November 28, 2020