Временная сложность Math.min и Math.maxJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Временная сложность Math.min и Math.max

Сообщение Anonymous »

Я хотел бы знать, имеют ли Math.min и Math.max в Java одинаковую временную сложность, поскольку моя задача была отклонена, поскольку ее выполнение заняло слишком много времени.
Я работал над проблемой, которая требовала добавления общие элементы в 3 отсортированных массивах по возрастанию.
Правильное и оптимизированное решение предполагает, что, когда один из A,B[j] или C[k] является минимальным из трех, индекс массива, содержащего этот min должно увеличиваться на единицу.
Мой подход заключался в том, что мы должны увеличивать не только тогда, когда элемент равен min. Но каждый индекс массива элемента, который не является максимальным, должен быть увеличен
вот оптимизированное решение:
ArrayList commonElements(int A[], int B[], int C[], int n1, int n2, int n3)
{
// Initialize three pointers for each array
int i = 0, j = 0, k = 0;

// Initialize an arraylist to store the common elements
ArrayList res = new ArrayList();

// Initialize a variable to store the last checked element
int last = Integer.MIN_VALUE;

// Loop until any of the three arrays reaches its end
while (i < n1 && j < n2 && k < n3)
{
// If the current elements of all three arrays are equal and not the same as the last checked element
if (A == B[j] && A == C[k] && A != last)
{
// Add the common element to the result arraylist
res.add (A);

// Update the last checked element
last = A;

// Move to the next element in each array
i++;
j++;
k++;
}
// If the current element of array A is the smallest, move to the next element in array A
else if (Math.min (A, Math.min(B[j], C[k])) == A) i++;
// If the current element of array B is the smallest, move to the next element in array B
else if (Math.min (A, Math.min(B[j], C[k])) == B[j]) j++;
// If the current element of array C is the smallest, move to the next element in array C
else k++;
}

вот мое решение:
public static ArrayList commonElements(int A[], int B[], int C[], int n1, int n2, int n3)
{
// code here
ArrayList res=new ArrayList ();
int i=0;
int j=0;
int k=0;
long last=Long.MIN_VALUE;
while(i

Подробнее здесь: https://stackoverflow.com/questions/784 ... d-math-max
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»