Skip to content

Latest commit

 

History

History
72 lines (52 loc) · 1.7 KB

_744. Find Smallest Letter Greater Than Target.md

File metadata and controls

72 lines (52 loc) · 1.7 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 01, 2024

Last updated : July 01, 2024


Related Topics : Array, Binary Search

Acceptance Rate : 53.36 %


Solutions

Java

class Solution {
    public char nextGreatestLetter(char[] letters, char target) {
        int left = 0;
        int right = letters.length - 1;


        while (left < right) {
            int mid = (left + right) / 2;
            
            if (letters[mid] <= target) {
                left = mid + 1;
                continue;
            }

            right = mid - 1;
        }
        
        if (letters[left] <= target)
            return letters[(left + 1) % letters.length];
        return letters[left];
    }
}

Python

class Solution:
    def nextGreatestLetter(self, letters: List[str], target: str) -> str:
        left, right = 0, len(letters) - 1

        while left < right :
            mid = (left + right) // 2
            
            if ord(letters[mid]) <= ord(target) :
                left = mid + 1
                continue

            right = mid - 1
        
        if (ord(letters[left]) <= ord(target)) :
            return letters[(left + 1) % len(letters)]
        return letters[left]