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];
}
相关推荐
KaMeidebaby13 小时前
卡梅德生物技术快报 | 核酸适配体文库测序:核酸适配体文库测序的技术原理、实验流程与数据解析
前端·网络·数据库·人工智能·算法
geovindu14 小时前
CSharp: Breadth First Search Algorithm and Depth First Search Algorithm
开发语言·后端·算法·c#·.net·搜索算法
ekkoalex15 小时前
训练框架以及算法实现的框架
算法
躺柒15 小时前
读半导体简史08大型机
linux·服务器·c语言·数据库·unix·ibm
DFT计算杂谈15 小时前
交错磁研究进展材料物性与交叉应用
数据库·人工智能·python·opencv·算法
玖玥拾16 小时前
LeetCode 26 删除有序数组中的重复项
算法·leetcode
Kina_C16 小时前
LVS负载均衡的十二种核心调度算法
linux·运维·算法·负载均衡·lvs
吞下星星的少年·-·16 小时前
牛客技能树:小红走矩阵(dp入门)
算法·dp
hansang_IR17 小时前
【题解】[AGC020E] Encoding Subsets
c++·算法·dp
wuyk55517 小时前
67.嵌入式C语言进阶:结构体指针实战指南——STM32外设、传感器数据访问的高效技巧
c语言·开发语言·stm32·单片机