# sorts with respect to array # time comp O(n^2) def bubble_sort(act, s, f): n = len(act) for i in range(n): for j in range(0, n-i-1): if s[j] > s[j+1]: s[j], s[j+1] = s[j+1], s[j] f[j], f[j+1] = f[j+1], f[j] act[j], act[j+1] = act[j+1], act[j] # returns list of maximum number of activities # time comp O(n) def maximum_activity(act, s, f): n = len(act) # max final = f[0] res = [act[0]] for i in range(n): if f[i] < final: # checks if current->final is smaller than max->final # if it is then swaps max->final with it because it is optimal res.pop() res.append(act[i]) final = f[i] continue if s[i] >= final: # print(s[i], final) # if curr->start is greater than max->final change max final = f[i] res.append(act[i]) return res # driver class def main(): max_act = 11 act = [i for i in range(max_act)] s = [1, 3, 0, 5, 3, 5, 6, 8, 8, 2, 12] f = [4, 5, 6, 7, 9, 9, 10, 11, 12, 14, 16] bubble_sort(act, s, f) r = maximum_activity(act, s, f) print(r, len(r)) print("maximum number of activity availble", len(r)) # execution starts main()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question