Skip to content

Instantly share code, notes, and snippets.

@Krewn
Created March 28, 2025 01:42
Show Gist options
  • Save Krewn/1cf90e5a1a4a1e38b91a4754beee641c to your computer and use it in GitHub Desktop.
Save Krewn/1cf90e5a1a4a1e38b91a4754beee641c to your computer and use it in GitHub Desktop.
class buckets:
def __init__(self,durp=[]):
self.counts = {}
self.maxCount = 0
self.totalCount = 0
for x in durp:
self.count(x)
def count(self,x):
self.totalCount += 1
try:
self.counts[x]+=1
except KeyError:
self.counts[x]=1
if self.counts[x] > self.maxCount:
self.maxCount+=1# Could be more specific
def uncount(self,x):
self.totalCount -= 1
self.counts[x]-=1
if self.counts[x] == self.maxCount-1:
if not(self.maxCount in self.counts.values()):
self.maxCount -= 1
if not self.counts[x]:
del self.counts[x]
def magorityValue(self):
if self.maxCount > self.totalCount/2:
ret = None
for x in self.counts.keys():
if self.counts[x] == self.maxCount:
return x
return None
class Solution:
def minimumIndex(self, nums: List[int]) -> int:
left = buckets()
left.count(nums[0])
right = buckets(durp = nums[1:])
splitIdx = 0
while splitIdx < len(nums):
d1 = left.magorityValue()
d2 = right.magorityValue()
if d1 is None or d2 is None:
pass
if d1 == d2:
return splitIdx
splitIdx += 1
if splitIdx == len(nums):
break
left.count(nums[splitIdx])
right.uncount(nums[splitIdx])
return -1
# Too slow
# Call that an A-
# https://leetcode.com/problems/minimum-index-of-a-valid-split/?envType=daily-question
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment