Skip to content

added leetcode question #21

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 7 commits into from
Feb 24, 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
33 changes: 33 additions & 0 deletions 14. Questions/leetcode 1011 - capacity to ship packages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# capacity to ship packages within D days | leetcode 1011 | https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/
# binary search on a range of min and max capacity required
# min capacity = max(weights) and max capacity = sum(weights)

class Solution:
def shipWithinDays(self, weights: list[int], days: int) -> int:
low, high = max(weights), sum(weights)
res = high

# check if days required for a capacity is less than D
def isPossible (capacity):
daysReq = 1
window = capacity
for weight in weights:
if window - weight < 0:
window = capacity
daysReq += 1
window -= weight

return daysReq <= days

# binary search on [min...max]
while low <= high:
mid = (high + low) // 2

if isPossible(mid):
res = min(res, mid)
high = mid - 1
else:
low = mid + 1

return res

21 changes: 21 additions & 0 deletions 14. Questions/leetcode 502 - ipo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# IPO | leetcode 502 | https://leetcode.com/problems/ipo/
# min-heap to track capital and max-heap to track profits

import heapq

class Solution:
def findMaximizedCapital(self, k: int, w: int, profits: list[int], capital: list[int]) -> int:
maxHeap = []
minHeap = [(c, p) for c, p in zip(capital, profits)]
heapq.heapify(minHeap)

for _ in range(k):
while minHeap and minHeap[0][0] <= w:
_, p = heapq.heappop(minHeap)
heapq.heappush(maxHeap, -1 * p)
if not maxHeap:
break
w += -1 * heapq.heappop(maxHeap)

return w

24 changes: 24 additions & 0 deletions 14. Questions/leetcode 540 - single element in a sorted array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# single element in a sorted array | leetcode 540 | https://leetcode.com/problems/single-element-in-a-sorted-array/
# binary search over sorted array; check if mid is even and mid is the first of the duplicates

class Solution:
def singleNonDuplicate(self, nums: list[int]) -> int:
N = len(nums)
if N < 2:
return nums[0]
low, high, mid = 0, N, 0
while low <= high:
mid = low + ((high - low) // 2)

if mid == N - 1:
return nums[mid]

if nums[mid] == nums[mid - 1] or nums[mid] == nums[mid + 1]:
if (mid % 2 == 0) == (nums[mid] == nums[mid + 1]):
low = mid
else:
high = mid
else:
return nums[mid]

return nums[mid]
19 changes: 19 additions & 0 deletions 14. Questions/leetcode 989 - add to array form of integer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# add to array form of integer | leetcode 989 | https://leetcode.com/problems/add-to-array-form-of-integer

class Solution:
def addToArrayForm(self, num: list[int], k: int) -> list[int]:
n = len(num) - 1
carry = 0
while k or carry:
k, digit = k // 10, k % 10
each = carry + digit
if n < 0:
num.insert(0, each % 10)
else:
each = each + num[n]
num[n] = each % 10
carry = each // 10
n -= 1

return num