Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more greedy techniques #862

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions algorithms/greedy/ClassPhoto.py
Original file line number Diff line number Diff line change
@@ -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

13 changes: 13 additions & 0 deletions algorithms/greedy/MinimumWaitingTime.py
Original file line number Diff line number Diff line change
@@ -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