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 array of integers, sort the array into a wave like array and return it,
In other words, arrange the elements into a sequence such that a1 >= a2 <= a3 >= a4 <= a5.....
Example
Given [1, 2, 3, 4]
One possible answer : [2, 1, 4, 3]
Another possible answer : [4, 1, 3, 2]
NOTE : If there are multiple answers possible, return the one thats lexicographically smallest.
So, in example case, you will return[2, 1, 4, 3]
Solution:
class Solution:
# @param A : list of integers
# @return a list of integers
def wave(self, A):
a=sorted(A)
for i in range(0,len(a)-1,2):
a[i],a[i+1]=a[i+1],a[i]
return a
Solution Screenshot:
First sorting makes the list lexicographically smallest so our next step is to swap first and second elements continuously skipping 2 elements each time
Comments
Post a Comment