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];
}
相关推荐
adam_life1 小时前
OpenJudge_ 简单英文题_04:0/1 Knapsack
算法·动态规划
龙的爹23332 小时前
论文翻译 | The Capacity for Moral Self-Correction in Large Language Models
人工智能·深度学习·算法·机器学习·语言模型·自然语言处理·prompt
鸣弦artha2 小时前
蓝桥杯——杨辉三角
java·算法·蓝桥杯·eclipse
我是聪明的懒大王懒洋洋3 小时前
力扣力扣力:动态规划入门(1)
算法·leetcode·动态规划
丶Darling.3 小时前
Day44 | 动态规划 :状态机DP 买卖股票的最佳时机IV&&买卖股票的最佳时机III
算法·动态规划
一丝晨光3 小时前
gcc 1.c和g++ 1.c编译阶段有什么区别?如何知道g++编译默认会定义_GNU_SOURCE?
c语言·开发语言·c++·gnu·clang·gcc·g++
TN_stark9324 小时前
多进程/线程并发服务器
服务器·算法·php
汉克老师4 小时前
GESP4级考试语法知识(贪心算法(四))
开发语言·c++·算法·贪心算法·图论·1024程序员节
smj2302_796826525 小时前
用枚举算法解决LeetCode第3348题最小可整除数位乘积II
python·算法·leetcode
爱吃生蚝的于勒5 小时前
C语言最简单的扫雷实现(解析加原码)
c语言·开发语言·学习·计算机网络·算法·游戏程序·关卡设计