给定一个排序的字符数组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’.
天真的方法:
解决这个问题最简单的方法是遍历数组,找到ASCII值小于K的字符,并且它们的ASCII值之间的差异最小。
时间复杂度: O(N)
辅助空间: O(1)
有效的方法:
这个想法是使用二分搜索来找出地板元素(最大的字符只是小于键)。请按照以下步骤解决问题:
- 对数组执行二分查找。
- 检查当前中间元素( mid )是否等于字符K 。
- 如果是这样,那么将start设置为mid – 1 ,因为我们只需要找到更小的元素。
- 如果arr[mid]小于K ,则将其存储在某个变量中,例如ch 。将start设置为mid + 1以搜索更接近K的较小字符。
- 否则,将end设置为mid-1 。
- 不断重复以上步骤。最后,打印完成搜索后得到的字符。如果未获得字符,则打印-1 。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to return the
// nearest smaller character
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 character
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 character
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 character
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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live