Skip to content

Instantly share code, notes, and snippets.

@PeculiarE
Created March 11, 2026 22:34
Show Gist options
  • Select an option

  • Save PeculiarE/2271af453495015bbd1951e4ad67c51e to your computer and use it in GitHub Desktop.

Select an option

Save PeculiarE/2271af453495015bbd1951e4ad67c51e to your computer and use it in GitHub Desktop.
Find Smallest Letter Greater Than Target - LeetCode - Day 70

Question

Intuition

Approach

Complexity

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

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

Code

class Solution:
    def nextGreatestLetter(self, letters: List[str], target: str) -> str:
        if target >= letters[-1]:
            return letters[0]
        smallestSoFar = letters[-1]
        low, high = 0, len(letters) - 1
        while low <= high:
            mid = (low + high) // 2
            if letters[mid] > target:
                smallestSoFar = letters[mid]
                high = mid - 1
            else:
                low = mid + 1
        return smallestSoFar
        

Result

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