supatroopa
BAN USER- 0of 0 votes
AnswersGiven an array of words (i.e. ["ABCW", "BAZ", "FOO", "BAR", "XTFN", "ABCDEF"]), find the max value of length(s) * length(t), where s and t are words from the array. The catch here is that the two words cannot share any characters.
- supatroopa in United States
Assume that there are many words in the array (N words) and average length of word is M.
Answer for the example above is "ABCW" and "XTFN" as the result is 4 * 4 = 12.
"ABCW" and "ABCDEF" do not work since they share similar characters.| Report Duplicate | Flag | PURGE
Google Software Engineer
what's a solution faster than O(n^2)?
- supatroopa December 13, 2015That's why there is the "auto" keyword.
- supatroopa December 13, 2015n = Node('A', Node('B', Node('D'), Node('E')), Node('C', Node('F')))
q = deque()
q.append(n)
num_current_level = 1
num_next_level = 0
while q:
node = q.popleft()
num_current_level -= 1
if not q or not num_current_level:
node.neighbor = Node('None')
else:
node.neighbor = q[0]
print '%s: %s' % (node.value, node.neighbor.value)
if node.left:
q.append(node.left)
num_next_level += 1
if node.right:
q.append(node.right)
num_next_level += 1
if not num_current_level:
num_current_level = num_next_level
num_next_level = 0
a = [5, 6, 1, 4]
def find_max_stock(prices):
max_value = 0
min_index = 0
buy_range = None
for i in xrange(1, len(prices)):
if prices[i] - prices[min_index] > max_value:
max_value = prices[i] - prices[min_index]
buy_range = (min_index, i)
if prices[i] < prices[min_index]:
min_index = i
return buy_range
print find_max_stock(a)
is there a solution without using extra space? i.e. swapping elements in input array?
- supatroopa December 18, 2015