lee19856
BAN USER
Comments (10)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
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] * (n1))
# print(seen)
return "".join(answer) + alphabet[0] * (n1)

lee19856
October 22, 2018 Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
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
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
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("Solution2: ",find2([10,2,3,7,8,6,4,5,9,1]))

lee19856
October 09, 2018 Comment hidden because of low score. Click to expand.
0
of 0 vote
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("Solution1: ",find([1,0,1,2,3,2,5]))
print("Solution1: ",find([10,2,3,7,8,6,4,5,9,1]))

lee19856
October 09, 2018 Comment hidden because of low score. Click to expand.
0
of 0 vote
"""
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 = i1
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]))

lee19856
October 05, 2018 Comment hidden because of low score. Click to expand.
0
of 0 vote
I wanted to make function that find any triplet that sumup 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
September 05, 2018 Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
Open Chat in New Window
Open Chat in New Window
first of all, thanks to 'adr' for sharing such.. i got to know what 'unionfind' 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.
 lee19856 October 26, 2018