From dfb67d0e669b3a0929ee7147c46919be4148f3be Mon Sep 17 00:00:00 2001 From: mihirs16 Date: Tue, 3 Jan 2023 22:38:51 +0000 Subject: [PATCH 1/4] added: delete columns to make sorted --- ...eetcode 944 - delete columns to make sorted.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 14. Questions/leetcode 944 - delete columns to make sorted.py diff --git a/14. Questions/leetcode 944 - delete columns to make sorted.py b/14. Questions/leetcode 944 - delete columns to make sorted.py new file mode 100644 index 0000000..de41086 --- /dev/null +++ b/14. Questions/leetcode 944 - delete columns to make sorted.py @@ -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 \ No newline at end of file From b91c171afe572e5fc3f1b46a48221457089be1ae Mon Sep 17 00:00:00 2001 From: mihirs16 Date: Tue, 3 Jan 2023 22:41:42 +0000 Subject: [PATCH 2/4] added: product of array except self --- ...code 238 - product of array except self.py | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 14. Questions/leetcode 238 - product of array except self.py diff --git a/14. Questions/leetcode 238 - product of array except self.py b/14. Questions/leetcode 238 - product of array except self.py new file mode 100644 index 0000000..a9c54b0 --- /dev/null +++ b/14. Questions/leetcode 238 - product of array except self.py @@ -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 + \ No newline at end of file From 06565fb197471d945dbb41bbd2e467019de37295 Mon Sep 17 00:00:00 2001 From: mihirs16 Date: Thu, 26 Jan 2023 13:36:47 +0000 Subject: [PATCH 3/4] added: non-overlapping intervals --- .../leetcode 435 - non-overlapping intervals.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 14. Questions/leetcode 435 - non-overlapping intervals.py diff --git a/14. Questions/leetcode 435 - non-overlapping intervals.py b/14. Questions/leetcode 435 - non-overlapping intervals.py new file mode 100644 index 0000000..836a8b9 --- /dev/null +++ b/14. Questions/leetcode 435 - non-overlapping intervals.py @@ -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 From 87ba408da9a0ece1bbf8e6d593d762ddfbe48b25 Mon Sep 17 00:00:00 2001 From: mihirs16 Date: Thu, 26 Jan 2023 14:49:53 +0000 Subject: [PATCH 4/4] added: barclays plc uk - online assessment --- .../barclays plc uk - online assesment.py | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 14. Questions/barclays plc uk - online assesment.py diff --git a/14. Questions/barclays plc uk - online assesment.py b/14. Questions/barclays plc uk - online assesment.py new file mode 100644 index 0000000..a7221c6 --- /dev/null +++ b/14. Questions/barclays plc uk - online assesment.py @@ -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) +