from collections import defaultdict graph=defaultdict(list) v,e=map(int,input().split()) for i in range(e): u,v=map(str,input().split()) graph[u].append(v) graph[v].append(u) for i in graph: print(i,"->",graph[i]) Input: 7 9 A B A C A F C E C F C D D E D G G F Output: F -> ['A', 'C', 'G'] C -> ['A', 'E', 'F', 'D'] G -> ['D', 'F'] D -> ['C', 'E', 'G'] A -> ['B', 'C', 'F'] E -> ['C', 'D'] B -> ['A']
Given an unsorted integer array, find the first missing positive integer.
Example:
Given [1,2,0]
return 3,
[3,4,-1,1]
return 2,
[-8, -7, -6]
returns 1
Your algorithm should run in O(n)
time and use constant space.
Solution:
class Solution:
# @param A : list of integers
# @return an integer
def firstMissingPositive(self, A):
A=set(A)
i=1
while(1):
if i not in A:
return i
i+=1
return i+1
Solution ScreenShot:
Comments
Post a Comment