diff --git a/algorithms/greedy/ClassPhoto.py b/algorithms/greedy/ClassPhoto.py new file mode 100644 index 000000000..ccfe44f2b --- /dev/null +++ b/algorithms/greedy/ClassPhoto.py @@ -0,0 +1,21 @@ +#Class Photo problem +#Time O(nlongn) | Space O(1) + +def classPhotos(redShirtHeights, blueShirtHeights): + + redShirtHeights.sort(reverse=True) + blueShirtHeights.sort(reverse=True) + + shirtColorInFirstRow='RED' if redShirtHeights[0] < blueShirtHeights[0] else 'BLUE' + for i in range(len(redShirtHeights)): + redShirtHeight=redShirtHeights[i] + blueShirtHeight=blueShirtHeights[i] + + if shirtColorInFirstRow=='RED': + if redShirtHeight>=blueShirtHeight: + return False + else: + if blueShirtHeight>=redShirtHeight: + return False + return True + diff --git a/algorithms/greedy/MinimumWaitingTime.py b/algorithms/greedy/MinimumWaitingTime.py new file mode 100644 index 000000000..418766381 --- /dev/null +++ b/algorithms/greedy/MinimumWaitingTime.py @@ -0,0 +1,13 @@ +#Minimum Waiting Time +#Time O(nLogn) for sorting array nlogn time takes and for keep tracking +#it takes n time so total nlogn + n so simply negates nlogn since n is less than nlogn +#So time O(N) and Space O(1) since we don't use extra space here + +def minimumWaitingTime(queries): + # Write your code here. + queries.sort() + totalWaitingTime=0 + for idx,duration in enumerate(queries): + queriesLeft=len(queries)-(idx+1) + totalWaitingTime+=duration*queriesLeft + return totalWaitingTime