-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0744-Find_Smallest_Letter_Greater_Than_Target.cpp
More file actions
106 lines (95 loc) · 2.65 KB
/
0744-Find_Smallest_Letter_Greater_Than_Target.cpp
File metadata and controls
106 lines (95 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*******************************************************************************
* 0744-Find_Smallest_Letter_Greater_Than_Target.cpp
* Billy.Ljm
* 09 June 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/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.
*
* ===========
* My Approach
* ===========
* We'll use binary search to identify the desired character.
*
* This has a time complexity of O(log n) and space complexity of O(1), where n
* is the size of the array.
******************************************************************************/
#include <iostream>
#include <vector>
/**
* Solution
*/
class Solution {
public:
/**
* Finds the next letter in the array that is greater than the target
*
* @param letters array to search in, sorted in increasing order
* @param target threshold to find next greater letter than
*
* @return next greater letter than target
*/
char nextGreatestLetter(std::vector<char>& letters, char target) {
// edge case
if (letters[letters.size() - 1] <= target) return letters[0];
// binary search index
int minn = 0;
int maxx = letters.size() - 1;
int mid;
// binary search
while (maxx - minn != 1) {
mid = (maxx + minn) / 2;
if (letters[mid] > target) maxx = mid;
else minn = mid;
}
// last check
if (letters[minn] > target) return letters[minn];
else return letters[maxx];
}
};
/**
* << operator for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << "[";
for (int i = 0; i < v.size(); i++) {
os << v[i] << ",";
}
os << "\b]";
return os;
}
/**
* Test cases
*/
int main(void) {
Solution sol;
std::vector<char> letters;
char target;
// test case 1
letters = { 'c', 'f', 'j' };
target = 'a';
std::cout << "nextGreatestLetter(" << letters << "," << target << ") = ";
std::cout << sol.nextGreatestLetter(letters, target) << std::endl;
// test case 2
letters = { 'c', 'f', 'j' };
target = 'c';
std::cout << "nextGreatestLetter(" << letters << "," << target << ") = ";
std::cout << sol.nextGreatestLetter(letters, target) << std::endl;
// test case 3
letters = { 'x', 'x', 'y', 'y' };
target = 'z';
std::cout << "nextGreatestLetter(" << letters << "," << target << ") = ";
std::cout << sol.nextGreatestLetter(letters, target) << std::endl;
return 0;
}