Skip to content

Commit

Permalink
Greedy Completed
Browse files Browse the repository at this point in the history
  • Loading branch information
SatyamVyas04 committed Jun 25, 2024
1 parent a370a87 commit 88cb170
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
15 changes: 15 additions & 0 deletions 12. Greedy/12.2. MediumHard/7. SJF.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
def solve(self, bt):
bt.sort()
wait = 0
curr = 0
for i in bt:
wait += curr
curr += i
return int(wait/len(bt))


sol = Solution()
print(sol.solve([7, 1, 6, 9, 2, 10, 7, 7, 10, 9]))

# Link: https://www.geeksforgeeks.org/problems/shortest-job-first/1
42 changes: 42 additions & 0 deletions 12. Greedy/12.2. MediumHard/8. LRU.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Solution:
def __init__(self):
self.cap = 0
self.curr = 0
self.cache = []
self.faults = 0

def pageFaults(self, n, c, pages):
# N -> number of pages
# C -> cache size
# Pages -> array of pages

self.cap = c
for i in range(n):
page = pages[i]
if not self.get(page):
self.faults += 1
self.put(page)
else:
continue

return self.faults

def get(self, key: int) -> int:
try:
index = self.cache.index(key)
page = self.cache.pop(index)
self.cache.append(page)
return 1
except ValueError:
return 0

def put(self, key: int) -> None:
if self.curr < self.cap:
self.cache.append(key)
self.curr += 1
else:
self.cache.pop(0)
self.cache.append(key)


# Link: https://www.geeksforgeeks.org/problems/page-faults-in-lru5603/1

0 comments on commit 88cb170

Please sign in to comment.