Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0688d0e

Browse files
Merge pull request #17 from mihirs16/master
2 parents eed8a9a + 87ba408 commit 0688d0e

4 files changed

+166
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
"""
2+
Allie is working on a system that can allocate resources to the
3+
applications in a manner efficient enough to allow the maximum number
4+
of applications to be executed. There are N number of applications
5+
and each application is identified by a unique integer ID (1 to N).
6+
Only M types of resources are available with a unique resourceD.
7+
Each application sends a request message to the system.
8+
The request message includes the information regarding the request time,
9+
the execution ending time, and the type of resource required for execution.
10+
Time is in the MMSS format where MM is minutes and SS is seconds.
11+
12+
If more than one application sends a request at the same time then only
13+
one application will be approved by the system. The denied requests are
14+
automatically destroyed by the system. When approving the request, the
15+
system ensures that the request will be granted to the application in a
16+
way that will maximize the number of executions. The system can execute
17+
only one application at a time with a given resource. It will deny all
18+
other requests for the resource until the previous application has finished.
19+
Allie wants to know the maximum number of applications that have been
20+
executed successfully.
21+
22+
Write an algorithm to help Allie calculate the maximum number of applications
23+
that are executed successfully by the system.
24+
25+
Input
26+
The first line of the input consists of two space-separated integers num and
27+
constX, representing the number of applications (N) and constX is always 3.
28+
The next N lines consist of constX space-separated integers representing the
29+
request time, the execution ending time, and the resourceD of the resource
30+
required by each application for successful execution.
31+
32+
Output
33+
Print an integer representing the maximum number of applications that are
34+
executed successfully by the system.
35+
36+
37+
Testcase 1 | Answer: 4
38+
4 3
39+
1000 1020 3
40+
1020 1030 3
41+
1030 1040 3
42+
1010 1045 2
43+
44+
Testcase 2 | Ans: 3
45+
5 3
46+
1200 1230 1
47+
1120 1125 2
48+
1015 1230 1
49+
1100 1230 1
50+
1200 1230 3
51+
52+
Testcase 3 | Ans: 4
53+
6 3
54+
1200 1250 1
55+
1210 1220 1
56+
1225 1230 1
57+
1330 1345 2
58+
1330 1340 2
59+
1340 1345 2
60+
"""
61+
62+
63+
# to bucket all requests by resource type
64+
def bucketRequestsByResource(arr):
65+
buckets = dict()
66+
for each_req in arr:
67+
if buckets.get(each_req[2], False) != False:
68+
buckets[each_req[2]].append((each_req[0], each_req[1]))
69+
else:
70+
buckets[each_req[2]] = [(each_req[0], each_req[1])]
71+
72+
return buckets
73+
74+
75+
# to get max number of executed tasks for a single bucket
76+
def numExecutedAppsByBucket(arr):
77+
arr.sort(key = lambda x: x[0])
78+
N = len(arr)
79+
dont_execute = 0
80+
latest_end = arr[0][1]
81+
82+
for i in range(1, N):
83+
if arr[i][0] < latest_end:
84+
dont_execute += 1
85+
latest_end = min(arr[i][1], latest_end)
86+
else:
87+
latest_end = arr[i][1]
88+
89+
return (N - dont_execute)
90+
91+
92+
# get the maximum number of executed tasks
93+
def numExecutedApps(arr):
94+
buckets = bucketRequestsByResource(arr)
95+
num_execute = 0
96+
for each_bucket in buckets.values():
97+
num_execute += numExecutedAppsByBucket(each_bucket)
98+
99+
return num_execute
100+
101+
102+
# driver code
103+
arr = []
104+
arr_rows, arr_cols = map(int, input().split())
105+
for idx in range(arr_rows):
106+
arr.append(list(map(int, input().split())))
107+
108+
result = numExecutedApps(arr)
109+
print (result)
110+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# product of array except self | leetcode 238 | https://leetcode.com/problems/product-of-array-except-self/
2+
# save prefixes to result array and apply postfix in reverse
3+
# (since output array doesnt increase space complexity)
4+
5+
class Solution:
6+
def productExceptSelf(self, nums: list[int]) -> list[int]:
7+
result = []
8+
N = len(nums)
9+
10+
# save prefix to result array
11+
product = 1
12+
for i in range(N):
13+
product = nums[i] * product
14+
result.append(product)
15+
16+
# update result array as per postfix
17+
postfix = 1
18+
for i in range(N - 1, 0, -1):
19+
result[i] = result[i - 1] * postfix
20+
postfix = postfix * nums[i]
21+
result[0] = postfix
22+
23+
return result
24+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# non-overlapping intervals | leetcode 435 | https://leetcode.com/problems/non-overlapping-intervals
2+
# sort by starting times; keep track of latest ending time; always keep interval with min end time
3+
4+
class Solution:
5+
def eraseOverlapIntervals(self, intervals: list[list[int]]) -> int:
6+
min_intervals_to_remove = 0
7+
intervals.sort(key = lambda x: x[0])
8+
latest_end = intervals[0][1]
9+
10+
for i in range(1, len(intervals)):
11+
if intervals[i][0] < latest_end:
12+
min_intervals_to_remove += 1
13+
latest_end = min(intervals[i][1], latest_end)
14+
else:
15+
latest_end = intervals[i][1]
16+
17+
return min_intervals_to_remove
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# delete columns to make sorted | leetcode 944 | https://leetcode.com/problems/delete-columns-to-make-sorted/
2+
3+
class Solution:
4+
def minDeletionSize(self, strs: list[str]) -> int:
5+
n_cols = len(strs[0])
6+
n_rows = len(strs)
7+
cols_d = 0
8+
9+
for col in range(n_cols):
10+
for row in range(1, n_rows):
11+
if strs[row][col] < strs[row - 1][col]:
12+
cols_d += 1
13+
break
14+
15+
return cols_d

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /