Skip to content

Commit

Permalink
--
Browse files Browse the repository at this point in the history
  • Loading branch information
Samir Paul committed May 14, 2022
1 parent 53562a4 commit 782d04b
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# https://leetcode.com/problems/maximum-white-tiles-covered-by-a-carpet/

class Solution:
def maximumWhiteTiles(self, tiles: List[List[int]], carpetLen: int) -> int:
tiles.sort()
ans = 0
starts, ends = zip(*tiles)
dp = [0]*(len(tiles) + 1)

for i in range(len(tiles)):
dp[i+1] = dp[i] + ends[i] - starts[i] + 1

for l in range(len(tiles)):
e = starts[l] + carpetLen
r = bisect_right(starts, e)
ans = max(ans, dp[r] - dp[l] - max(0, ends[r-1] - e + 1))

return ans

'''
We create a prefix sum array dp to store the TOTAL coverage from index 0 to the beginning of each tile
Then for the start s of each tile, if we use the carpet there, the end index of the carpet will be s + carpetLen.
We binary search that value to see which tile on the right would the end index belongs to
Finally, knowing the index of the right tile r, we subtract the TOTAL coverage of the right tile to the
TOTAL coverage of the left tile and any offset (since the end index may lie within the right tile,
or on the right hand side of it if the right tile is the last one)
Runtime: O(nlogn)
'''
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# https://leetcode.com/problems/maximum-profit-in-job-scheduling/

import bisect

class Solution:
def jobScheduling(self, S, E, profit):
jobs = sorted(list(zip(S, E, profit)))
S = [i[0] for i in jobs]
n = len(jobs)
dp = [0] * (n + 1)

for k in range(n-1,-1,-1):
temp = bisect_left(S, jobs[k][1])
dp[k] = max(jobs[k][2] + dp[temp], dp[k+1])

return dp[0]


# Time complexity is O(n * log n), because we do n binary searches. Space complexity is O(n)
9 changes: 0 additions & 9 deletions 30-Days-SDE-Sheet-Practice/08. Day 8 Greedy Algorithm/01.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# https://leetcode.com/problems/maximum-units-on-a-truck/
'''
As we need to find maximum counts so that sort the array in decreasing order so that higher
count per box remain first. Then keep adding and reducing the value of given size.
'''

class Solution:
def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:
boxTypes.sort(key = lambda x:x[1], reverse = True)
res = 0
i = 0
while i < len(boxTypes):
if truckSize > boxTypes[i][0]:
res += boxTypes[i][0] * boxTypes[i][1]
truckSize -= boxTypes[i][0]
else:
res += truckSize * boxTypes[i][1]
break
i += 1

return res

# Time: O(N log(N))
# Space: O(1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# https://practice.geeksforgeeks.org/problems/job-sequencing-problem-1587115620/1#

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# https://practice.geeksforgeeks.org/problems/minimum-platforms-1587115620/1#
'''
arr[] = {0900, 0940, 0950, 1100, 1500, 1800}
dep[] = {0910, 1200, 1120, 1130, 1900, 2000}
'''

class Solution:
#Function to find the minimum number of platforms required at the railway station such that no train waits.
def minimumPlatform(self,n,arr,dep):
arr.sort() # {0900, 0940, 0950, 1100, 1500, 1800}
dep.sort() # {0910, 1120, 1130, 1200, 1900, 2000}

i = 1
j = 0

res = 1
platformNeeded = 1

while i < len(arr) and j < len(dep):
if arr[i] <= dep[j]:
platformNeeded += 1
i += 1
else:
platformNeeded -= 1
j += 1

res = max(res, platformNeeded)

return res


# Time: O(N log(N))
# Space: O(N)

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# https://practice.geeksforgeeks.org/problems/n-meetings-in-one-room-1587115620/1#
'''
We can start a new meeting after its previous meeting ends. So I will
sort the meeting array based on ending time. So that meetings with low ending time comes fist
'''
class Solution:
#Function to find the maximum number of meetings that can be performed in a meeting room.
def maximumMeetings(self,n,start,end):
meeting = []
for i in range(len(start)):
meeting.append([i, start[i], end[i]])
meeting.sort(key = lambda x:x[2])

maxMeet = [0]
prevEnd = meeting[0][2]
for i in range(1, len(meeting)):
if meeting[i][1] > prevEnd:
prevEnd = meeting[i][2]
maxMeet.append(meeting[i][0])

return len(maxMeet)



class Solution:
def maximumMeetings(self,n,start,end):
# code here
time = [[start[i], end[i]] for i in range(len(start))]
time.sort(key = lambda x : x[1])
res = 1
r = 1
l = 0
while r < len(time):
if time[r][0] > time[l][1]:
res += 1
l = r
r += 1

return res

# Time Complexity : O(N*LogN)
# Auxilliary Space : O(N)
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# https://leetcode.com/problems/coin-change/
# https://practice.geeksforgeeks.org/problems/number-of-coins1824/1/
# https://www.youtube.com/watch?v=I-l6PBeERuc&list=PL_z_8CaSLPWekqhdCPmFohncHwz8TY2Go&index=16

import sys
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
# As we have infinite supply of coins so this question is a variation of Unbounded Knapsack
dp = [[sys.maxsize] * (amount + 1) for i in range(len(coins) + 1)] # placing max value as we want to find minimum
# in Python 3 as sys.maxsize is max value of integer

# Initializing 0th row = infinite = float("inf"); as for array coins of size zero it will take infinite coins to make amount j
for j in range(amount + 1):
dp[0][j] = sys.maxsize

# Initializing 0th column from dp[1][0] to dp[len(coins)][0] as 0
# as for array coins of size >= 1; to make amount 0 don't need to take any coin so dp[i][0] = 0 for 1 <= i <= len(coins)
for i in range(1, len(coins)+1):
dp[i][0] = 0

# *** IN THIS QUESTION WE HAVE TO INITIALIZE 1st ROW ALSO ***
# in 1st row check if amount j is multiple of coins[0] or not
# if multiple put number of coins[0] required to make amount j in dp[1][j]
for j in range(1, amount + 1):
if j % coins[0] == 0: # multiple or not
dp[1][j] = j // coins[0] # number of coins[0] required to make amount j

# change remaing dp
for i in range(2, len(coins) + 1):
for j in range(1, amount + 1):
if coins[i - 1] <= j:
dp[i][j] = min(1 + dp[i][j - coins[i-1]], dp[i-1][j])
else:
dp[i][j] = dp[i - 1][j]

ans = dp[-1][-1]
return ans if ans != sys.maxsize else -1


# Time Complexity = O((len(coins)+1) * (amount+1))
# Space Complexity = O((len(coins)+1) * (amount+1))

0 comments on commit 782d04b

Please sign in to comment.