From a20191ba4f22f3cb87c3378cbcf840c527caca33 Mon Sep 17 00:00:00 2001 From: Palash Hawee Date: Tue, 19 Apr 2022 20:41:22 +0530 Subject: [PATCH] Added more greedy techniques --- algorithms/greedy/ClassPhoto.py | 21 +++++++++++++++++++++ algorithms/greedy/MinimumWaitingTime.py | 13 +++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 algorithms/greedy/ClassPhoto.py create mode 100644 algorithms/greedy/MinimumWaitingTime.py 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