在包含小数和大数的数组中查找最大和最小数字
给定一个包含N个小数和/或大数的数组arr[] ,任务是在这个数组中找到最大和最小的数。
例子:
Input: N = 4, arr[] = {5677856, 657856745356587687698768, 67564, 45675645356576578564435647647}
Output: Smallest: 67564
Largest: 45675645356576578564435647647
Input: N = 5, arr[] = {56, 64, 765, 323, 4764}
Output: Smallest: 56
Largest: 4764
Input: N = 3, arr[] = {56, 56, 56}
Output: Smallest: 56
Largest: 56
天真的方法:解决这个问题的一种简单方法是对所有数字使用基于比较的排序,存储为字符串。如果比较的字符串长度不同,则先按小长度对它们进行排序。如果长度相同,则使用比较函数查找第一个最大的不匹配字符,并推断它属于第一个字符串还是第二个字符串,并对第一个不匹配字符的ASCII值较小的排序。
这样,我们将得到最终的向量,该向量根据其数字表示具有越来越多的排序字符串。第一个字符串最小,最后一个字符串最大。
时间复杂度: O(N*M*log N)
- O(N*log N) 对数组进行排序
- O(M) 在长度相等时逐位比较两个数字
辅助空间: O(1)
有效的方法:要有效地解决问题,请遵循以下思路:
This approach is similar to finding the biggest and smallest number in a numeral vector. The only difference is that there is need to check of the length of string as well since strings with big length will always form a bigger number than one with smaller length.
For Example: “3452” with length 4 will always be greater than “345” with length 3.
Similar is the case for smallest string.
请按照以下步骤解决问题:
- 将minLen初始化为可能的最大数量,将maxLen初始化为可能的最小数量。这里minLen和maxLen表示到目前为止找到的最小数和最大数的长度。
- 使用i逐个遍历所有字符串。
- 找到当前字符串的长度为numLen 。
- 如果minLen > numLen将minLen分配给numLen和最小的数字为numbers[i] 。类似地,如果maxLen < numLen将maxLen分配给numLen并将最大数分配为numbers[i] 。
- 如果minLen == numLen和maxLen == numLen则将迄今为止找到的最小数字和最大数字与numbers[i]进行比较。第一个更大的不匹配字符的数字会更大,其他的会更小。
- 返回最小和最大数字对作为最终答案。
下面是上述方法的实现:
C++14
// C++ code for the above approach
#include
using namespace std;
// Function to find biggest and
// smallest numbers
pair findLargestAndSmallest(
vector& numbers)
{
int N = numbers.size();
int maxLen = 0, minLen = INT_MAX;
// To store smallest and largest
// element respectively
pair res;
// Traverse each number in array
for (int i = 0; i < N; i++) {
int numLen = numbers[i].length();
// Comparing current smallest
// number with current number
// If current number is smaller
if (minLen > numLen) {
minLen = numLen;
res.first = numbers[i];
}
// If current number is of same length
// Perform digitwise comparison
else if (minLen == numLen)
res.first
= res.first.compare(numbers[i]) < 0
? res.first
: numbers[i];
// Comparing current largest
// number with current number
// If current number is larger
if (maxLen < numLen) {
maxLen = numLen;
res.second = numbers[i];
}
// If current number is of same length
// Perform digitwise comparison
else if (maxLen == numLen)
res.second
= res.second.compare(numbers[i]) > 0
? res.second
: numbers[i];
}
// Returning the result
return res;
}
// Driver code
int main()
{
vector numbers
= { "5677856", "657856745356587687698768", "67564",
"45675645356576578564435647647" };
// Calling the function
pair ans
= findLargestAndSmallest(numbers);
cout << "Smallest: " << ans.first;
cout << endl;
cout << "Largest: " << ans.second;
return 0;
}
Java
// Java code for the above approach
import java.io.*;
public class GFG {
static String[] findLargestAndSmallest(String[] numbers)
{
int N = numbers.length;
int maxLen = 0, minLen = Integer.MAX_VALUE;
String[] res = { "", "" };
// To store smallest and largest
// element respectively
// Traverse each number in array
for (int i = 0; i < N; i++) {
int numLen = numbers[i].length();
// Comparing current smallest
// number with current number
// If current number is smaller
if (minLen > numLen) {
minLen = numLen;
res[0] = numbers[i];
}
// If current number is of same length
// Perform digitwise comparison
else if (minLen == numLen)
res[0] = ((res[0].length()
> numbers[i].length())
? res[0]
: numbers[i]);
// Comparing current largest
// number with current number
// If current number is larger
if (maxLen < numLen) {
maxLen = numLen;
res[1] = numbers[i];
}
// If current number is of same length
// Perform digitwise comparison
else if (maxLen == numLen)
res[1]
= (res[1].length() > numbers[i].length()
? res[1]
: numbers[i]);
}
// Returning the result
return res;
}
// driver code
public static void main(String[] args)
{
String[] numbers
= { "5677856", "657856745356587687698768",
"67564", "45675645356576578564435647647" };
// Calling the function
String[] ans = findLargestAndSmallest(numbers);
System.out.println("Smallest: " + ans[0]);
System.out.print("Largest: " + ans[1]);
}
}
// This code is contributed by phasing17
Python3
# Python tcode for the above approach
INT_MAX = 2147483647
# Function to find biggest and
# smallest numbers
def findLargestAndSmallest(numbers):
N = len(numbers)
maxLen,minLen = 0,INT_MAX
# To store smallest and largest
# element respectively
res = ["" for i in range(2)]
# Traverse each number in array
for i in range(N):
numLen = len(numbers[i])
# Comparing current smallest
# number with current number
# If current number is smaller
if (minLen > numLen):
minLen = numLen
res[0] = numbers[i]
# If current number is of same length
# Perform digitwise comparison
elif (minLen == numLen):
res[0] = res[0] if (res[0] < numbers[i]) else numbers[i]
# Comparing current largest
# number with current number
# If current number is larger
if (maxLen < numLen):
maxLen = numLen
res[1] = numbers[i]
# If current number is of same length
# Perform digitwise comparison
elif (maxLen == numLen):
res[1] = res[1] if (res[1] > numbers[i]) else numbers[i]
# Returning the result
return res
# Driver code
numbers = ["5677856", "657856745356587687698768", "67564","45675645356576578564435647647"]
# Calling the function
ans = findLargestAndSmallest(numbers)
print(f"Smallest: {ans[0]}")
print(f"Largest: {ans[1]}")
# This code is contributed by shinjanpatra
C#
// C# code for the above approach
using System;
public class GFG {
static string[] findLargestAndSmallest(string[] numbers)
{
int N = numbers.Length;
int maxLen = 0;
int minLen = Int32.MaxValue;
string[] res = { "", "" };
// To store smallest and largest
// element respectively
// Traverse each number in array
for (int i = 0; i < N; i++) {
int numLen = numbers[i].Length;
// Comparing current smallest
// number with current number
// If current number is smaller
if (minLen > numLen) {
minLen = numLen;
res[0] = numbers[i];
}
// If current number is of same length
// Perform digitwise comparison
else if (minLen == numLen)
res[0]
= ((res[0].Length > numbers[i].Length)
? res[0]
: numbers[i]);
// Comparing current largest
// number with current number
// If current number is larger
if (maxLen < numLen) {
maxLen = numLen;
res[1] = numbers[i];
}
// If current number is of same length
// Perform digitwise comparison
else if (maxLen == numLen)
res[1] = (res[1].Length > numbers[i].Length
? res[1]
: numbers[i]);
}
// Returning the result
return res;
}
// Driver Code
public static void Main(string[] args)
{
String[] numbers
= { "5677856", "657856745356587687698768",
"67564", "45675645356576578564435647647" };
// Calling the function
String[] ans = findLargestAndSmallest(numbers);
Console.WriteLine("Smallest: " + ans[0]);
Console.WriteLine("Largest: " + ans[1]);
}
}
// this code is contributed by phasing17
Javascript
Smallest: 67564
Largest: 45675645356576578564435647647
时间复杂度: O(N*M),其中 N 是数组的大小,M 是最大数的大小。
- O(N) 遍历数组的每个数
- O(M) 在长度相等时逐位比较两个数字
辅助空间: O(1)