Skip to content

Instantly share code, notes, and snippets.

@KeshariPiyush24
Created September 21, 2024 05:32
Show Gist options
  • Save KeshariPiyush24/39a2962d99d8c7918db1b10675cb4586 to your computer and use it in GitHub Desktop.
Save KeshariPiyush24/39a2962d99d8c7918db1b10675cb4586 to your computer and use it in GitHub Desktop.
744. Find Smallest Letter Greater Than Target

Question: 744. Find Smallest Letter Greater Than Target

Intution:

Time Complexity: $$O(log(n))$$

Space Complexity: $$O(1)$$

Solution:

class Solution {
    public char nextGreatestLetter(char[] letters, char target) {
        int n = letters.length;
        int low = 0;
        int high = n - 1;
        while (low < high) {
            int mid = low + (high - low) / 2;
            if (letters[mid] <= target) {
               low = mid + 1;
            } else {
                high = mid;
            }
        }
        return letters[low] > target ? letters[low] : letters[0];
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment