YEAHUB_PHP_BACKEND Telegram 414
#ЛитКод
Задача: 503. Next Greater Element II

Дан циклический массив целых чисел nums (т.е. следующий элемент после nums[nums.length - 1] это nums[0]), верните следующее большее число для каждого элемента в nums.

Следующее большее число для числа x — это первое большее число, следующее за ним в порядке обхода массива, что означает, что вы можете искать циклически, чтобы найти следующее большее число. Если оно не существует, верните -1 для этого числа.

Пример:
Input: nums = [1,2,1]
Output: [2,-1,2]
Explanation: The first 1's next greater number is 2;
The number 2 can't find next greater number.
The second 1's next greater number needs to search circularly, which is also 2.


👨‍💻 Алгоритм:

1⃣Инициализация
Создайте массив res той же длины, что и nums, и заполните его значениями -1.

2⃣Поиск следующего большего элемента
Для каждого элемента nums[i], используя индекс j, ищите следующий больший элемент среди следующих (циклически) n-1 элементов. Если найден больший элемент, обновите res[i] и прервите внутренний цикл.

3⃣Возврат результата
Верните массив res.

😎 Решение:
class Solution {
function nextGreaterElements($nums) {
$n = count($nums);
$res = array_fill(0, $n, -1);

for ($i = 0; $i < $n; $i++) {
for ($j = 1; $j < $n; $j++) {
if ($nums[($i + $j) % $n] > $nums[$i]) {
$res[$i] = $nums[($i + $j) % $n];
break;
}
}
}

return $res;
}
}


👉Новости 👉База вопросов
Please open Telegram to view this post
VIEW IN TELEGRAM



tgoop.com/yeahub_php_backend/414
Create:
Last Update:

#ЛитКод
Задача: 503. Next Greater Element II

Дан циклический массив целых чисел nums (т.е. следующий элемент после nums[nums.length - 1] это nums[0]), верните следующее большее число для каждого элемента в nums.

Следующее большее число для числа x — это первое большее число, следующее за ним в порядке обхода массива, что означает, что вы можете искать циклически, чтобы найти следующее большее число. Если оно не существует, верните -1 для этого числа.

Пример:

Input: nums = [1,2,1]
Output: [2,-1,2]
Explanation: The first 1's next greater number is 2;
The number 2 can't find next greater number.
The second 1's next greater number needs to search circularly, which is also 2.


👨‍💻 Алгоритм:

1⃣Инициализация
Создайте массив res той же длины, что и nums, и заполните его значениями -1.

2⃣Поиск следующего большего элемента
Для каждого элемента nums[i], используя индекс j, ищите следующий больший элемент среди следующих (циклически) n-1 элементов. Если найден больший элемент, обновите res[i] и прервите внутренний цикл.

3⃣Возврат результата
Верните массив res.

😎 Решение:
class Solution {
function nextGreaterElements($nums) {
$n = count($nums);
$res = array_fill(0, $n, -1);

for ($i = 0; $i < $n; $i++) {
for ($j = 1; $j < $n; $j++) {
if ($nums[($i + $j) % $n] > $nums[$i]) {
$res[$i] = $nums[($i + $j) % $n];
break;
}
}
}

return $res;
}
}


👉Новости 👉База вопросов

BY PHP Backend | YeaHub




Share with your friend now:
tgoop.com/yeahub_php_backend/414

View MORE
Open in Telegram


Telegram News

Date: |

The optimal dimension of the avatar on Telegram is 512px by 512px, and it’s recommended to use PNG format to deliver an unpixelated avatar. A few years ago, you had to use a special bot to run a poll on Telegram. Now you can easily do that yourself in two clicks. Hit the Menu icon and select “Create Poll.” Write your question and add up to 10 options. Running polls is a powerful strategy for getting feedback from your audience. If you’re considering the possibility of modifying your channel in any way, be sure to ask your subscribers’ opinions first. The Channel name and bio must be no more than 255 characters long Although some crypto traders have moved toward screaming as a coping mechanism, several mental health experts call this therapy a pseudoscience. The crypto community finds its way to engage in one or the other way and share its feelings with other fellow members. While some crypto traders move toward screaming as a coping mechanism, many mental health experts have argued that “scream therapy” is pseudoscience. Scientific research or no, it obviously feels good.
from us


Telegram PHP Backend | YeaHub
FROM American