Skip to content

added leetcode questions #17

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

Merged
merged 4 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions 14. Questions/barclays plc uk - online assesment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
"""
Allie is working on a system that can allocate resources to the
applications in a manner efficient enough to allow the maximum number
of applications to be executed. There are N number of applications
and each application is identified by a unique integer ID (1 to N).
Only M types of resources are available with a unique resourceD.
Each application sends a request message to the system.
The request message includes the information regarding the request time,
the execution ending time, and the type of resource required for execution.
Time is in the MMSS format where MM is minutes and SS is seconds.

If more than one application sends a request at the same time then only
one application will be approved by the system. The denied requests are
automatically destroyed by the system. When approving the request, the
system ensures that the request will be granted to the application in a
way that will maximize the number of executions. The system can execute
only one application at a time with a given resource. It will deny all
other requests for the resource until the previous application has finished.
Allie wants to know the maximum number of applications that have been
executed successfully.

Write an algorithm to help Allie calculate the maximum number of applications
that are executed successfully by the system.

Input
The first line of the input consists of two space-separated integers num and
constX, representing the number of applications (N) and constX is always 3.
The next N lines consist of constX space-separated integers representing the
request time, the execution ending time, and the resourceD of the resource
required by each application for successful execution.

Output
Print an integer representing the maximum number of applications that are
executed successfully by the system.


Testcase 1 | Answer: 4
4 3
1000 1020 3
1020 1030 3
1030 1040 3
1010 1045 2

Testcase 2 | Ans: 3
5 3
1200 1230 1
1120 1125 2
1015 1230 1
1100 1230 1
1200 1230 3

Testcase 3 | Ans: 4
6 3
1200 1250 1
1210 1220 1
1225 1230 1
1330 1345 2
1330 1340 2
1340 1345 2
"""


# to bucket all requests by resource type
def bucketRequestsByResource(arr):
buckets = dict()
for each_req in arr:
if buckets.get(each_req[2], False) != False:
buckets[each_req[2]].append((each_req[0], each_req[1]))
else:
buckets[each_req[2]] = [(each_req[0], each_req[1])]

return buckets


# to get max number of executed tasks for a single bucket
def numExecutedAppsByBucket(arr):
arr.sort(key = lambda x: x[0])
N = len(arr)
dont_execute = 0
latest_end = arr[0][1]

for i in range(1, N):
if arr[i][0] < latest_end:
dont_execute += 1
latest_end = min(arr[i][1], latest_end)
else:
latest_end = arr[i][1]

return (N - dont_execute)


# get the maximum number of executed tasks
def numExecutedApps(arr):
buckets = bucketRequestsByResource(arr)
num_execute = 0
for each_bucket in buckets.values():
num_execute += numExecutedAppsByBucket(each_bucket)

return num_execute


# driver code
arr = []
arr_rows, arr_cols = map(int, input().split())
for idx in range(arr_rows):
arr.append(list(map(int, input().split())))

result = numExecutedApps(arr)
print (result)

24 changes: 24 additions & 0 deletions 14. Questions/leetcode 238 - product of array except self.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# product of array except self | leetcode 238 | https://leetcode.com/problems/product-of-array-except-self/
# save prefixes to result array and apply postfix in reverse
# (since output array doesnt increase space complexity)

class Solution:
def productExceptSelf(self, nums: list[int]) -> list[int]:
result = []
N = len(nums)

# save prefix to result array
product = 1
for i in range(N):
product = nums[i] * product
result.append(product)

# update result array as per postfix
postfix = 1
for i in range(N - 1, 0, -1):
result[i] = result[i - 1] * postfix
postfix = postfix * nums[i]
result[0] = postfix

return result

17 changes: 17 additions & 0 deletions 14. Questions/leetcode 435 - non-overlapping intervals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# non-overlapping intervals | leetcode 435 | https://leetcode.com/problems/non-overlapping-intervals
# sort by starting times; keep track of latest ending time; always keep interval with min end time

class Solution:
def eraseOverlapIntervals(self, intervals: list[list[int]]) -> int:
min_intervals_to_remove = 0
intervals.sort(key = lambda x: x[0])
latest_end = intervals[0][1]

for i in range(1, len(intervals)):
if intervals[i][0] < latest_end:
min_intervals_to_remove += 1
latest_end = min(intervals[i][1], latest_end)
else:
latest_end = intervals[i][1]

return min_intervals_to_remove
15 changes: 15 additions & 0 deletions 14. Questions/leetcode 944 - delete columns to make sorted.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# delete columns to make sorted | leetcode 944 | https://leetcode.com/problems/delete-columns-to-make-sorted/

class Solution:
def minDeletionSize(self, strs: list[str]) -> int:
n_cols = len(strs[0])
n_rows = len(strs)
cols_d = 0

for col in range(n_cols):
for row in range(1, n_rows):
if strs[row][col] < strs[row - 1][col]:
cols_d += 1
break

return cols_d