mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2025-12-06 13:44:06 +00:00
Compare commits
No commits in common. "97976e239449d8d073820e1924c7157f3d116e7e" and "ac27bdd8407298bf3c3335cd93b8960df37a562c" have entirely different histories.
97976e2394
...
ac27bdd840
@ -1,11 +0,0 @@
|
|||||||
def maxProfit(self, prices: list[int]) -> int:
|
|
||||||
lowest = prices[0]
|
|
||||||
max_profit = 0
|
|
||||||
|
|
||||||
for x in prices[1:]:
|
|
||||||
if x - lowest > max_profit:
|
|
||||||
max_profit = x - lowest
|
|
||||||
if x < lowest:
|
|
||||||
lowest = x
|
|
||||||
|
|
||||||
return max_profit
|
|
||||||
@ -1,8 +0,0 @@
|
|||||||
def maxProfit(self, prices: list[int]) -> int:
|
|
||||||
cur_start = 0
|
|
||||||
diff_sum = 0
|
|
||||||
for idx in range(1, len(prices)):
|
|
||||||
if prices[idx] > prices[cur_start]:
|
|
||||||
diff_sum += prices[idx] - prices[cur_start]
|
|
||||||
cur_start = idx
|
|
||||||
return diff_sum
|
|
||||||
@ -1,26 +0,0 @@
|
|||||||
def majorityElement(self, nums: list[int]) -> int:
|
|
||||||
# O(n) time, O(n) space
|
|
||||||
# cnts = {}
|
|
||||||
# for x in nums:
|
|
||||||
# cnts[x] = cnts.get(x, 0) + 1
|
|
||||||
# if cnts[x] > len(nums) / 2:
|
|
||||||
# return x
|
|
||||||
|
|
||||||
# O(n) time, O(1) space
|
|
||||||
# Boyer-Moore majority vote algorithm
|
|
||||||
# given: majority exists (>n/2, NOT just n/2)
|
|
||||||
# voters fight 1v1 MAD, last standing must be of majority
|
|
||||||
leading_candidate = nums[0]
|
|
||||||
count = 0
|
|
||||||
for x in nums:
|
|
||||||
# MAD; new leader must emerge
|
|
||||||
if count == 0:
|
|
||||||
count = 1
|
|
||||||
leading_candidate = x
|
|
||||||
# dissent; majority weakens
|
|
||||||
elif x != leading_candidate:
|
|
||||||
count -= 1
|
|
||||||
# assent; majority strengthens
|
|
||||||
else:
|
|
||||||
count += 1
|
|
||||||
return leading_candidate
|
|
||||||
@ -1,22 +0,0 @@
|
|||||||
def rotate(self, nums: list[int], k: int) -> None:
|
|
||||||
"""
|
|
||||||
Do not return anything, modify nums in-place instead.
|
|
||||||
"""
|
|
||||||
# O(n) space and time
|
|
||||||
# n = len(nums)
|
|
||||||
# k %= n
|
|
||||||
# nums[:] = nums[n-k:] + nums[:n-k]
|
|
||||||
|
|
||||||
# O(n) time, O(1) space
|
|
||||||
# reverse full - then reverse chunks
|
|
||||||
def partrev(arr, low, high):
|
|
||||||
while low < high:
|
|
||||||
arr[low], arr[high] = arr[high], arr[low]
|
|
||||||
low += 1
|
|
||||||
high -= 1
|
|
||||||
|
|
||||||
n = len(nums)
|
|
||||||
k %= n
|
|
||||||
partrev(nums, 0, n - 1)
|
|
||||||
partrev(nums, 0, k - 1)
|
|
||||||
partrev(nums, k, n - 1)
|
|
||||||
@ -1,19 +0,0 @@
|
|||||||
class Solution:
|
|
||||||
def removeDuplicates(self, nums: list[int]) -> int:
|
|
||||||
# naive, hashmap
|
|
||||||
# cnts = {}
|
|
||||||
# k = 0
|
|
||||||
# for x in nums:
|
|
||||||
# if cnts.get(x) == None:
|
|
||||||
# k += 1
|
|
||||||
# cnts[x] = cnts.get(x, 0) + 1
|
|
||||||
# for idx, key in enumerate(cnts.keys()):
|
|
||||||
# nums[idx] = key
|
|
||||||
# return k
|
|
||||||
# two pointer
|
|
||||||
last_unique = 0
|
|
||||||
for fast in range(1, len(nums)):
|
|
||||||
if nums[last_unique] != nums[fast]:
|
|
||||||
last_unique += 1
|
|
||||||
nums[last_unique] = nums[fast]
|
|
||||||
return last_unique + 1 # zero-indexed
|
|
||||||
@ -1,7 +0,0 @@
|
|||||||
def removeElement(self, nums: list[int], val: int) -> int:
|
|
||||||
i = 0
|
|
||||||
for x in nums:
|
|
||||||
if x != val:
|
|
||||||
nums[i] = x
|
|
||||||
i += 1
|
|
||||||
return i
|
|
||||||
@ -1,9 +0,0 @@
|
|||||||
def hIndex(self, citations: list[int]) -> int:
|
|
||||||
cs = sorted(citations, reverse=True)
|
|
||||||
h = 0
|
|
||||||
for x in cs:
|
|
||||||
if x > h:
|
|
||||||
h += 1
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
return h
|
|
||||||
@ -1,17 +0,0 @@
|
|||||||
def jump(self, nums: list[int]) -> int:
|
|
||||||
n = len(nums)
|
|
||||||
if n == 1:
|
|
||||||
return 0
|
|
||||||
dp = [1e5] * n
|
|
||||||
dp[n - 1] = 0
|
|
||||||
|
|
||||||
for idx in range(n - 2, -1, -1):
|
|
||||||
for reach in range(0, nums[idx]):
|
|
||||||
# overshot, auto-success
|
|
||||||
if idx + reach + 1 >= n - 1:
|
|
||||||
dp[idx] = 1
|
|
||||||
break
|
|
||||||
# else, chain-reachable
|
|
||||||
if dp[idx + reach + 1] <= 10000:
|
|
||||||
dp[idx] = min(dp[idx], dp[idx + reach + 1] + 1)
|
|
||||||
return dp[0]
|
|
||||||
@ -1,15 +0,0 @@
|
|||||||
def canJump(self, nums: list[int]) -> bool:
|
|
||||||
n = len(nums)
|
|
||||||
if n == 1:
|
|
||||||
return True
|
|
||||||
if nums[0] == 0:
|
|
||||||
return False
|
|
||||||
dp = [False] * n
|
|
||||||
dp[n - 1] = True
|
|
||||||
|
|
||||||
for idx in range(n - 2, -1, -1):
|
|
||||||
for reach in range(0, nums[idx]):
|
|
||||||
if dp[idx + reach + 1] == True:
|
|
||||||
dp[idx] = True
|
|
||||||
break
|
|
||||||
return dp[0]
|
|
||||||
@ -1,14 +0,0 @@
|
|||||||
def removeDuplicates(self, nums: list[int]) -> int:
|
|
||||||
# two pointer
|
|
||||||
last_unique = 0
|
|
||||||
flag = False
|
|
||||||
for fast in range(1, len(nums)):
|
|
||||||
if nums[last_unique] != nums[fast]:
|
|
||||||
flag = False
|
|
||||||
last_unique += 1
|
|
||||||
nums[last_unique] = nums[fast]
|
|
||||||
elif not flag:
|
|
||||||
flag = True
|
|
||||||
last_unique += 1
|
|
||||||
nums[last_unique] = nums[fast]
|
|
||||||
return last_unique + 1 # zero-indexed
|
|
||||||
@ -1,27 +0,0 @@
|
|||||||
def merge(self, nums1: list[int], m: int, nums2: list[int], n: int) -> None:
|
|
||||||
"""
|
|
||||||
Do not return anything, modify nums1 in-place instead.
|
|
||||||
"""
|
|
||||||
i = 0
|
|
||||||
j = 0
|
|
||||||
nums3 = []
|
|
||||||
# one at a time
|
|
||||||
while i < m and j < n:
|
|
||||||
if nums1[i] < nums2[j]:
|
|
||||||
nums3.append(nums1[i])
|
|
||||||
i += 1
|
|
||||||
else:
|
|
||||||
nums3.append(nums2[j])
|
|
||||||
j += 1
|
|
||||||
|
|
||||||
# deplete nums1
|
|
||||||
while i < m:
|
|
||||||
nums3.append(nums1[i])
|
|
||||||
i += 1
|
|
||||||
# deplete nums2
|
|
||||||
while j < n:
|
|
||||||
nums3.append(nums2[j])
|
|
||||||
j += 1
|
|
||||||
# optimize this?
|
|
||||||
for idx, val in enumerate(nums3):
|
|
||||||
nums1[idx] = val
|
|
||||||
Loading…
x
Reference in New Issue
Block a user