๐Ÿ” ์ดˆ๋ณด์ž์šฉ ๊ฐ€์ด๋“œ 'ํƒ€๊ฒŸ๋ณด๋‹ค ํฐ ๊ฐ€์žฅ ์ž‘์€ ๋ฌธ์ž ์ฐพ๊ธฐ' - Problem 744 (C++, Python, JavaScript)

๋ฐœํ–‰: (2026๋…„ 1์›” 31์ผ ์˜คํ›„ 12:55 GMT+9)
5 min read
์›๋ฌธ: Dev.to

Source: Dev.to

๐Ÿ” ์ดˆ๋ณด์ž ์นœํ™” ๊ฐ€์ด๋“œ โ€˜๋ชฉํ‘œ๋ณด๋‹ค ํฐ ๊ฐ€์žฅ ์ž‘์€ ๋ฌธ์ž ์ฐพ๊ธฐโ€™ - ๋ฌธ์ œ 744 (C++, Python, JavaScript) ํ‘œ์ง€ ์ด๋ฏธ์ง€

๋ฌธ์ œ ์š”์•ฝ

์ฃผ์–ด์ง€๋Š” ๊ฒƒ:

  • letters ๋ผ๋Š” ์ •๋ ฌ๋œ ๋ฌธ์ž ๋ฆฌ์ŠคํŠธ.
  • target ์ด๋ผ๋Š” ํŠน์ • ๋ฌธ์ž.

๋ชฉํ‘œ:

  • target ๋ณด๋‹ค ์—„๊ฒฉํžˆ ํฐ ๊ฐ€์žฅ ์ž‘์€ ๋ฌธ์ž๋ฅผ ๋ฆฌ์ŠคํŠธ์—์„œ ์ฐพ๋Š”๋‹ค.
  • ๊ทธ๋Ÿฐ ๋ฌธ์ž๊ฐ€ ์—†์œผ๋ฉด(์ฆ‰, target ์ด ๋ชจ๋“  ์š”์†Œ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์„ ๊ฒฝ์šฐ) ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

Intuition

Because the array is already sorted, we can avoid a linear scan (โ€ฏO(n)โ€ฏ) and instead apply binary search (โ€ฏO(logโ€ฏn)โ€ฏ).

The algorithm follows a โ€œsearch and discardโ€ approach:

  1. Examine the middle element.
  2. If letters[mid] > target โ€“ this could be the answer. Record the index and continue searching the left half for an even smaller qualifying character.
  3. If letters[mid] โ‰ค target โ€“ the middle element and everything to its left are too small. Discard them and search the right half.

If the search finishes without finding a greater character, we โ€œwrap aroundโ€ and return letters[0].

๋‹จ๊ณ„๋ณ„ ์„ค๋ช…: ์˜ˆ์ œ ์ดํ•ด

์˜ˆ์ œโ€ฏ1

letters = ["c", "f", "j"], target = "a"

๋ฌธ์ž 'a'๋ณด๋‹ค ํฐ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๋Š” 'c'์ž…๋‹ˆ๋‹ค.

Result: "c"

์˜ˆ์ œโ€ฏ2

letters = ["c", "f", "j"], target = "c"

'c'๋Š” ํ—ˆ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ์šฐ๋ฆฌ๋Š” ์—„๊ฒฉํžˆ ๋” ํฐ ๋ฌธ์ž๋ฅผ ํ•„์š”๋กœ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ ๋ฌธ์ž๋Š” 'f'์ž…๋‹ˆ๋‹ค.

Result: "f"

์˜ˆ์ œโ€ฏ3

letters = ["x", "x", "y", "y"], target = "z"

'z'๋ณด๋‹ค ํฐ ๋ฌธ์ž๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค; ์šฐ๋ฆฌ๋Š” ์ฒ˜์Œ ์š”์†Œ๋กœ ๋Œ์•„๊ฐ‘๋‹ˆ๋‹ค.

Result: "x"

์ฝ”๋“œ ๊ตฌํ˜„

C++

class Solution {
public:
    char nextGreatestLetter(vector& letters, char target) {
        int n = letters.size();
        int left = 0;
        int right = n - 1;
        int firstTrueIndex = -1;

        while (left  target) {
                firstTrueIndex = mid;
                right = mid - 1;          // look for a smaller qualifying character
            } else {
                left = mid + 1;           // discard left half
            }
        }

        return firstTrueIndex == -1 ? letters[0] : letters[firstTrueIndex];
    }
};

ํŒŒ์ด์ฌ

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

        while left  target:
                first_true_index = mid
                right = mid - 1
            else:
                left = mid + 1

        return letters[0] if first_true_index == -1 else letters[first_true_index]

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

/**
 * @param {character[]} letters
 * @param {character} target
 * @return {character}
 */
var nextGreatestLetter = function (letters, target) {
    let left = 0;
    let right = letters.length - 1;
    let firstTrueIndex = -1;

    while (left  target) {
            firstTrueIndex = mid;
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }

    return firstTrueIndex === -1 ? letters[0] : letters[firstTrueIndex];
};

ํ•ต์‹ฌ ์š”์ 

  • ์ด์ง„ ํƒ์ƒ‰ ํšจ์œจ์„ฑ: ๋งค ๋ฐ˜๋ณต๋งˆ๋‹ค ํƒ์ƒ‰ ๋ฒ”์œ„๋ฅผ ์ ˆ๋ฐ˜์œผ๋กœ ์ค„์—ฌ O(logโ€ฏn) ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๋‹ฌ์„ฑํ•˜๊ณ , O(n) ๋ณด๋‹ค ํ›จ์”ฌ ๋น ๋ฆ…๋‹ˆ๋‹ค.
  • Upper Bound ํŒจํ„ด: ์ด ๋ฌธ์ œ๋Š” ์ „ํ˜•์ ์ธ โ€œupper boundโ€ ํƒ์ƒ‰์œผ๋กœ, ์ฃผ์–ด์ง„ ๊ฐ’๋ณด๋‹ค ์—„๊ฒฉํžˆ ํฐ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  • Wrapโ€‘Around ์ฒ˜๋ฆฌ: sentinel ๊ฐ’(์˜ˆ: firstTrueIndex = -1)์ด๋‚˜ ๋ชจ๋“ˆ๋Ÿฌ ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•˜๋ฉด ์›ํ˜• ๋ฐฐ์—ด ๋กœ์ง์„ ๊น”๋”ํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ตœ์ข… ์ƒ๊ฐ

์ด ๋ฌธ์ œ๋Š” ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ ์ตœ์ ํ™”์— ๋Œ€ํ•œ ํ›Œ๋ฅญํ•œ ์ž…๋ฌธ์„œ์ž…๋‹ˆ๋‹ค. ํŒŒ์ผ ํƒ์ƒ‰๊ธฐ๋‚˜ ์—ฐ๋ฝ์ฒ˜ ๋ชฉ๋ก๊ณผ ๊ฐ™์€ ์‹ค์ œ ์‹œ์Šคํ…œ์—์„œ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๋ฌธ์ž๋ฅผ ์ž…๋ ฅํ•  ๋•Œ โ€œ๋‹ค์Œ์œผ๋กœ ํฐโ€ ํ•ญ๋ชฉ์œผ๋กœ ์ด๋™ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ข…์ข… ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ง„ ํƒ์ƒ‰์„ ๋งˆ์Šคํ„ฐํ•˜๋ฉด ์ตœ๊ณ ์˜ ๊ธฐ์ˆ  ๊ธฐ์—…์—์„œ์˜ ๊ธฐ์ˆ  ๋ฉด์ ‘ ์ค€๋น„๋Š” ๋ฌผ๋ก , ์ˆ˜๋ฐฑ๋งŒ ๊ฐœ์˜ ํ•ญ๋ชฉ๊นŒ์ง€ ํ™•์žฅ ๊ฐ€๋Šฅํ•œ ์„ฑ๋Šฅ ์ข‹์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

Back to Blog

๊ด€๋ จ ๊ธ€

๋” ๋ณด๊ธฐ ยป

Python ํด๋กœ์ €: JavaScript์—์„œ ์˜จ ๊ฒฝ์šฐ

์ฑ…๊ณผ ๋น„๋””์˜ค๋ฅผ ํ†ตํ•œ ํ•™์Šต > โ€œBooks โ€“ ์ฃผ์ œ์— ๋Œ€ํ•œ ๊นŠ์ด ์žˆ๋Š” ์ง€์‹ ์Šต๋“ > Videos โ€“ ํŠน์ • technology๋ฅผ ๋น ๋ฅด๊ฒŒ ์ตํžˆ๊ธฐโ€ ๋‚˜๋Š” ์—ฌ์ „ํžˆ ์ฑ…์„ ์ง‘์–ด ๋“ ๋‹ค.

๋ฌธ์ œ 10: ์ค‘๋ณต ์ œ๊ฑฐ

๋ฌธ์ œ ์„ค๋ช… ์šฐ๋ฆฌ๋Š” ๋ฆฌ์ŠคํŠธ์—์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๋ฉด์„œ ์›๋ž˜ ์š”์†Œ๋“ค์˜ ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์‹œ: `remove_duplicates` 1, 2, 2, 3...

JavaScript๋ฅผ ํ™œ์šฉํ•œ ๊ธฐ์—… ๊ณ ๊ฐ์˜ ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋Š” ์ด๋ฉ”์ผ ํ๋ฆ„ ๊ฒ€์ฆ ๋ณด์žฅ

์†Œ๊ฐœ ๊ธฐ์—… ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ ๋ถ„์•ผ์—์„œ ์ด๋ฉ”์ผ ์›Œํฌํ”Œ๋กœ์šฐ์˜ ๋ฌด๊ฒฐ์„ฑ๊ณผ ์‹ ๋ขฐ์„ฑ์„ ๋ณด์žฅํ•˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. Lead QA Engineer๋กœ์„œ, ...

JavaScript์˜ ๋น„๋ฐ€์Šค๋Ÿฌ์šด ์‚ถ: Proxy

Timothy๋Š” ์ฑ…์ƒ์— ์•‰์•„ ์•ฝ๊ฐ„ ์••๋„๋œ ํ‘œ์ •์„ ์ง“๊ณ  ์žˆ์—ˆ๋‹ค. ๊ทธ๋Š” ๊ฐ„๋‹จํ•œ user ๊ฐ์ฒด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์—ˆ์ง€๋งŒ, ์ฝ”๋“œ๊ฐ€ if ๋ฌธ์œผ๋กœ ๊ฐ€๋“ ์ฐจ ์žˆ์—ˆ๋‹ค. js let user = { name: 'Timothy',...