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 letters[0].

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
  • letters[i] 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 letters[low % 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];
}
相关推荐
写代码的小球2 小时前
求模运算符c
算法
学不动CV了6 小时前
ARM单片机启动流程(二)(详细解析)
c语言·arm开发·stm32·单片机·51单片机
大千AI助手6 小时前
DTW模版匹配:弹性对齐的时间序列相似度度量算法
人工智能·算法·机器学习·数据挖掘·模版匹配·dtw模版匹配
YuTaoShao7 小时前
【LeetCode 热题 100】48. 旋转图像——转置+水平翻转
java·算法·leetcode·职场和发展
生态遥感监测笔记7 小时前
GEE利用已有土地利用数据选取样本点并进行分类
人工智能·算法·机器学习·分类·数据挖掘
猫猫的小茶馆8 小时前
【STM32】通用定时器基本原理
c语言·stm32·单片机·嵌入式硬件·mcu·51单片机
Tony沈哲8 小时前
macOS 上为 Compose Desktop 构建跨架构图像处理 dylib:OpenCV + libraw + libheif 实践指南
opencv·算法
刘海东刘海东8 小时前
结构型智能科技的关键可行性——信息型智能向结构型智能的转变(修改提纲)
人工智能·算法·机器学习
pumpkin845149 小时前
Rust 调用 C 函数的 FFI
c语言·算法·rust