给定一个排序后的字符数组arr []和一个字符K ,任务是从给定数组中找到ASCII值比K最接近的字符。如果未发现ASCII值小于K的字符,则打印-1 。
例子:
Input: arr[] = {‘e’, ‘g’, ‘t’, ‘y’}, K = ‘u’
Output: t
Explanation:
The character with nearest smaller ASCII value as of ‘u’ is ‘t’.
Input: arr[] = {‘e’, ‘g’, ‘t’, ‘y’}, K = ‘a’
Output: -1
Explanation:
No character exists with ASCII value smaller than that of ‘a’.
天真的方法:
解决此问题的最简单方法是遍历数组,并找到具有比K小的ASCII值的ASCII字符,并且它们的ASCII值之差最小。
时间复杂度: O(N)
辅助空间: O(1)
高效方法:
这个想法是使用Binary Search来查找楼板元素(最大的字符刚好小于键)。请按照以下步骤解决问题:
- 在阵列上执行二进制搜索。
- 检查当前的中间元素( mid )是否等于字符K。
- 如果是这样,则将start设置为mid – 1 ,因为我们需要找到更小的元素。
- 如果arr [mid]小于K ,则将其存储在某个变量中,例如ch 。将start设置为mid +1,以搜索更接近K的较小字符。
- 否则,将end设置为mid-1 。
- Kepp重复上述步骤。最后,打印完成搜索后获得的字符。如果未获得任何字符,则打印-1 。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to return the
// nearest smaller charcter
char bs(char ar[], int n, int ele)
{
int start = 0;
int end = n - 1;
// Stores the nearest smaller
// character
char ch = '@';
// Iterate till starts cross end
while (start <= end) {
// Find the mid element
int mid = start + (end - start) / 2;
// Check if K is found
if (ar[mid] == ele)
end = mid - 1;
// Check if current character
// is less than K
else if (ar[mid] < ele) {
ch = ar[mid];
// Increment the start
start = mid + 1;
}
// Otherwise
else
// Increment end
end = mid - 1;
}
// Return the character
return ch;
}
// Driver Code
int main()
{
char ar[] = { 'e', 'g', 't', 'y' };
int n = sizeof(ar) / sizeof(ar[0]);
char K = 'u';
char ch = bs(ar, n, K);
if (ch == '@')
cout << "-1";
else
cout << ch;
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to return the
// nearest smaller charcter
static char bs(char ar[], int n, int ele)
{
int start = 0;
int end = n - 1;
// Stores the nearest smaller
// character
char ch = '@';
// Iterate till starts cross end
while (start <= end)
{
// Find the mid element
int mid = start + (end - start) / 2;
// Check if K is found
if (ar[mid] == ele)
end = mid - 1;
// Check if current character
// is less than K
else if (ar[mid] < ele)
{
ch = ar[mid];
// Increment the start
start = mid + 1;
}
// Otherwise
else
// Increment end
end = mid - 1;
}
// Return the character
return ch;
}
// Driver Code
public static void main(String[] args)
{
char ar[] = { 'e', 'g', 't', 'y' };
int n = ar.length;
char K = 'u';
char ch = bs(ar, n, K);
if (ch == '@')
System.out.print("-1");
else
System.out.print(ch);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to implement
# the above approach
# Function to return the
# nearest smaller charcter
def bs(a, n, ele):
start = 0
end = n - 1
# Stores the nearest smaller
# character
ch = '@'
# Iterate till starts cross end
while (start <= end):
# Find the mid element
mid = start + (end - start) // 2;
# Check if K is found
if(ar[mid] == ele):
end = mid - 1
# Check if current character
# is less than K
elif(ar[mid] < ele):
ch = ar[mid]
# Increment the start
start = mid + 1;
# Otherwise
else:
# Increment end
end = mid - 1;
# Return the character
return ch
# Driver code
if __name__=='__main__':
ar = [ 'e', 'g', 't', 'y' ]
n = len(ar)
K = 'u';
ch = bs(ar, n, K);
if (ch == '@'):
print('-1')
else:
print(ch)
# This code is contributed by rutvik_56
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to return the
// nearest smaller charcter
static char bs(char []ar, int n, int ele)
{
int start = 0;
int end = n - 1;
// Stores the nearest smaller
// character
char ch = '@';
// Iterate till starts cross end
while (start <= end)
{
// Find the mid element
int mid = start + (end - start) / 2;
// Check if K is found
if (ar[mid] == ele)
end = mid - 1;
// Check if current character
// is less than K
else if (ar[mid] < ele)
{
ch = ar[mid];
// Increment the start
start = mid + 1;
}
// Otherwise
else
// Increment end
end = mid - 1;
}
// Return the character
return ch;
}
// Driver Code
public static void Main(String[] args)
{
char []ar = { 'e', 'g', 't', 'y' };
int n = ar.Length;
char K = 'u';
char ch = bs(ar, n, K);
if (ch == '@')
Console.Write("-1");
else
Console.Write(ch);
}
}
// This code is contributed by 29AjayKumar
输出:
t
时间复杂度: O(logN)
辅助空间: O(1)