Ron
BAN USERFor all positive elements in the array:
def prodThreeLargest(arr):
max1 = max2 = max3 = float('-inf')
for i in range(len(arr)):
if arr[i] > max1:
max3 = max2
max2 = max1
max1 = arr[i]
elif arr[i] > max2:
max3 = max2
max2 = arr[i]
elif arr[i] > max3:
max3 = arr[i]
#print("max1 {} max2 {} max3 {}".format(max1, max2, max3))
return max1*max2*max3
For positive and negative elements:
import heapq
def prodThreeLargest(arr):
n1, n2 = heapq.nsmallest(2, arr)
print(n1, n2)
p1, p2, p3 = heapq.nlargest(3, arr)
print(p1, p2, p3)
return max(n1*n2*p1, p1*p2*p3)
Python - O(n) solution.
def validParentheses(s):
d = {')': '(', '}': '{', ']': '['}
stack = []
for i in s:
if i not in d:
stack.append(i)
else:
if len(stack) > 0:
temp = stack.pop()
else:
temp = '#'
if d[i] != temp:
return False
if len(stack) == 0:
return True
else:
return False
Edit the dictionary for different kinds of opening and closing tags.
- Ron June 21, 2019
- Ron June 23, 2019