Skip to content

Instantly share code, notes, and snippets.

@cwgreene
Last active March 13, 2024 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwgreene/6323852c47570b55ec3495ca9d358837 to your computer and use it in GitHub Desktop.
Save cwgreene/6323852c47570b55ec3495ca9d358837 to your computer and use it in GitHub Desktop.
class Solution:
def divide(self, dividend: int, divisor: int) -> int:
sign = 1
if dividend < 0:
dividend = - dividend
sign = - sign
if divisor < 0:
sign = - sign
divisor = - divisor
piles = [[] for _ in range(divisor)]
objects = [0 for _ in range(dividend)]
index = 0
# sort into piles
while objects:
object = objects.pop()
piles[index].append(object)
index += 1
# handle mod
if index >= divisor:
index = 0
result = min(len(pile) for pile in piles)
if sign < 0:
result = - result
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment