LeetCode //C - 744. Find Smallest Letter Greater Than Target

744. Find Smallest Letter Greater Than Target

You are given an array of characters letters that is sorted in non-decreasing order, and a character target. There are at least two different characters in letters.

Return the smallest character in letters that is lexicographically greater than target. If such a character does not exist, return the first character in letters.

Example 1:

Input: letters = "c","f","j", target = "a"
Output: "c"
Explanation: The smallest character that is lexicographically greater than 'a' in letters is 'c'.

Example 2:

Input: letters = "c","f","j", target = "c"
Output: "f"
Explanation: The smallest character that is lexicographically greater than 'c' in letters is 'f'.

Example 3:

Input: letters = "x","x","y","y", target = "z"
Output: "x"
Explanation: There are no characters in letters that is lexicographically greater than 'z' so we return letters0.

Constraints:
  • 2 < = l e t t e r s . l e n g t h < = 1 0 4 2 <= letters.length <= 10^4 2<=letters.length<=104
  • lettersi is a lowercase English letter.
  • letters is sorted in non-decreasing order.
  • letters contains at least two different characters.
  • target is a lowercase English letter.

From: LeetCode

Link: 744. Find Smallest Letter Greater Than Target


Solution:

Ideas:

1. Initialization: It starts with two pointers, low and high, which represent the start and end of the array, respectively.

2. Binary Search Loop:

  • The loop continues as long as low is less than or equal to high.
  • It calculates the midpoint (mid) of the current low and high pointers.
  • If the character at mid is less than or equal to the target, it means the next greatest letter cannot be at mid or before mid. So, it sets low to mid + 1 to search the right half of the array next time.
  • If the character at mid is greater than the target, it might be the answer, but there might also be a smaller character that is also greater than the target on the left. So, it sets high to mid - 1 to search the left half of the array next time.

3. Wrap-Around Logic:

  • After the loop, low points to the position where the next greatest character should be. This is because the loop exits when low is greater than high, meaning low is at the smallest character greater than the target or at the end of the array if such a character doesn't exist.
  • If low equals the size of the letters array (lettersSize), it means all characters are less than or equal to the target, so the function returns the first character in the array by using letterslow % lettersSize, leveraging the modulus operator for wrap-around.
Code:
c 复制代码
char nextGreatestLetter(char* letters, int lettersSize, char target) {
    int low = 0, high = lettersSize - 1;
    while (low <= high) {
        int mid = low + (high - low) / 2;
        if (letters[mid] <= target) {
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    // If low is equal to lettersSize, it means all characters in the array are <= target,
    // so we return the first character according to the problem's wrap-around requirement.
    return letters[low % lettersSize];
}
相关推荐
kkeeper~6 小时前
0基础C语言积跬步之数据在内存中的存储
c语言·数据结构·算法
wabs6668 小时前
关于贪心算法的一些自我总结【力扣45.跳跃游戏II】【灵感来源:代码随想录】
算法·贪心算法·复盘
2401_876964138 小时前
【湖北专升本】2026湖北专升本真题PDF+备考资料汇总
数据结构·人工智能·经验分享·深度学习·算法·计算机视觉
qq3862461968 小时前
更新补发第6天:7天学会C语言,每天5分钟,不需要基础
c语言·for循环·循环语句·while循环·do-while循环
嗝o゚9 小时前
CANN GE 算子融合——融合算法与调度策略
算法·昇腾·cann·ge
小江的记录本9 小时前
【JVM虚拟机】垃圾回收GC:垃圾回收算法:标记-清除、标记-复制、标记-整理、分代收集(附《思维导图》+《面试高频考点清单》)
java·jvm·后端·python·算法·安全·面试
Ulyanov10 小时前
用声明式语法重新定义Python桌面UI:QML+PySide6现代开发入门(一)
开发语言·python·算法·ui·系统仿真·雷达电子对抗仿真
数据科学小丫10 小时前
特征工程处理
人工智能·算法·机器学习
z落落11 小时前
C#参数区别
java·算法·c#
c2385612 小时前
vector(下)
数据结构·算法