Skip to content

Instantly share code, notes, and snippets.

@PeculiarE
Created March 7, 2026 22:40
Show Gist options
  • Select an option

  • Save PeculiarE/4460e0a400aeb53b0f591905d1d721fe to your computer and use it in GitHub Desktop.

Select an option

Save PeculiarE/4460e0a400aeb53b0f591905d1d721fe to your computer and use it in GitHub Desktop.
Arranging Coins - LeetCode - Day 66

Question

Intuition

Approach

Complexity

  • Time complexity: $$O(log\ n)$$

  • Space complexity: $$O(1)$$

Code

class Solution:
    def arrangeCoins(self, n: int) -> int:
        lowestNumberOfRows, highestNumberOfRows = 1, n
        completeRows = 1
        while (lowestNumberOfRows <= highestNumberOfRows):
            builtRows = (lowestNumberOfRows + highestNumberOfRows) // 2
            numberOfCoins = (builtRows * (builtRows + 1)) / 2 #using the formula for sum of numbers from 1 to n
            if numberOfCoins == n:
                return builtRows
            elif numberOfCoins < n:
                completeRows = builtRows
                lowestNumberOfRows = builtRows + 1
            else:
                highestNumberOfRows = builtRows - 1
        return completeRows
        

Result

Screenshot 2026-03-07 at 22 38 36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment