/
NikolayIvkin
/
TheAlgorithms
Обзор
Документация
Войти
/
NikolayIvkin
/
TheAlgorithms
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/java/com/thealgorithms/greedyalgorithms/MinimumWaitingTime.java
37 строк
1 KB
Hardik Pawar
Add `MinimumWaitingTime` algorithm (#5794)
16 окт 2024, 12:52
Не верифицирован
16 окт 2024, 12:52
dfff8d9
Код
Авторство
О чём код?
package com.thealgorithms.greedyalgorithms; import java.util.Arrays; /** * The MinimumWaitingTime class provides a method to calculate the minimum * waiting time for a list of queries using a greedy algorithm. * * @author Hardvan */ public final class MinimumWaitingTime { private MinimumWaitingTime() { } /** * Calculates the minimum waiting time for a list of queries. * The function sorts the queries in non-decreasing order and then calculates * the waiting time for each query based on its position in the sorted list. * * @param queries an array of integers representing the query times in picoseconds * @return the minimum waiting time in picoseconds */ public static int minimumWaitingTime(int[] queries) { int n = queries.length; if (n <= 1) { return 0; } Arrays.sort(queries); int totalWaitingTime = 0; for (int i = 0; i < n; i++) { totalWaitingTime += queries[i] * (n - i - 1); } return totalWaitingTime; } }