代码随想录训练营day58| 739. 每日温度 496.下一个更大元素 I

@TOC


前言

代码随想录算法训练营day58


一、Leetcode 739. 每日温度

1.题目

给定一个整数数组 temperatures ,表示每天的温度,返回一个数组 answer ,其中 answer[i] 是指对于第 i 天,下一个更高温度出现在几天后。如果气温在这之后都不会升高,请在该位置用 0 来代替。

示例 1:

输入: temperatures = [73,74,75,71,69,72,76,73] 输出: [1,1,4,2,1,1,0,0]

示例 2:

输入: temperatures = [30,40,50,60] 输出: [1,1,1,0]

示例 3:

输入: temperatures = [30,60,90] 输出: [1,1,0]

提示:

1 <= temperatures.length <= 105
30 <= temperatures[i] <= 100

来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/daily-temperatures

2.解题思路

方法一:暴力

对于温度列表中的每个元素 temperatures[i],需要找到最小的下标 j,使得 i < j 且 temperatures[i] < temperatures[j]。

由于温度范围在 [30, 100] 之内,因此可以维护一个数组 next 记录每个温度第一次出现的下标。数组 next 中的元素初始化为无穷大,在遍历温度列表的过程中更新 next 的值。

反向遍历温度列表。对于每个元素 temperatures[i],在数组 next 中找到从 temperatures[i] + 1 到 100 中每个温度第一次出现的下标,将其中的最小下标记为 warmerIndex,则 warmerIndex 为下一次温度比当天高的下标。如果 warmerIndex 不为无穷大,则 warmerIndex - i 即为下一次温度比当天高的等待天数,最后令 next[temperatures[i]] = i。

为什么上述做法可以保证正确呢?因为遍历温度列表的方向是反向,当遍历到元素 temperatures[i] 时,只有 temperatures[i] 后面的元素被访问过,即对于任意 t,当 next[t] 不为无穷大时,一定存在 j 使得 temperatures[j] == t 且 i < j。又由于遍历到温度列表中的每个元素时都会更新数组 next 中的对应温度的元素值,因此对于任意 t,当 next[t] 不为无穷大时,令 j = next[t],则 j 是满足 temperatures[j] == t 且 i < j 的最小下标。

3.代码实现

```java class Solution { public int[] dailyTemperatures(int[] temperatures) { int length = temperatures.length; int[] ans = new int[length]; int[] next = new int[101]; Arrays.fill(next, Integer.MAXVALUE); for (int i = length - 1; i >= 0; --i) { int warmerIndex = Integer.MAXVALUE; for (int t = temperatures[i] + 1; t <= 100; ++t) { if (next[t] < warmerIndex) { warmerIndex = next[t]; } } if (warmerIndex < Integer.MAX_VALUE) { ans[i] = warmerIndex - i; } next[temperatures[i]] = i; } return ans; } }

```

二、Leetcode 496.下一个更大元素 I

1.题目

nums1 中数字 x 的 下一个更大元素 是指 x 在 nums2 中对应位置 右侧 的 第一个 比 x 大的元素。

给你两个 没有重复元素 的数组 nums1 和 nums2 ,下标从 0 开始计数,其中nums1 是 nums2 的子集。

对于每个 0 <= i < nums1.length ,找出满足 nums1[i] == nums2[j] 的下标 j ,并且在 nums2 确定 nums2[j] 的 下一个更大元素 。如果不存在下一个更大元素,那么本次查询的答案是 -1 。

返回一个长度为 nums1.length 的数组 ans 作为答案,满足 ans[i] 是如上所述的 下一个更大元素 。

示例 1:

输入:nums1 = [4,1,2], nums2 = [1,3,4,2]. 输出:[-1,3,-1] 解释:nums1 中每个值的下一个更大元素如下所述: - 4 ,用加粗斜体标识,nums2 = [1,3,4,2]。不存在下一个更大元素,所以答案是 -1 。 - 1 ,用加粗斜体标识,nums2 = [1,3,4,2]。下一个更大元素是 3 。 - 2 ,用加粗斜体标识,nums2 = [1,3,4,2]。不存在下一个更大元素,所以答案是 -1 。

示例 2:

输入:nums1 = [2,4], nums2 = [1,2,3,4]. 输出:[3,-1] 解释:nums1 中每个值的下一个更大元素如下所述: - 2 ,用加粗斜体标识,nums2 = [1,2,3,4]。下一个更大元素是 3 。 - 4 ,用加粗斜体标识,nums2 = [1,2,3,4]。不存在下一个更大元素,所以答案是 -1 。

提示:

1 <= nums1.length <= nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 104
nums1和nums2中所有整数 互不相同
nums1 中的所有整数同样出现在 nums2 中

来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/next-greater-element-i

2.解题思路

方法一:暴力

思路和算法

根据题意,我们发现 nums1nums1​ 是一个查询数组,逐个查询 nums2nums2​ 中元素右边的第一个更大的值。因此,我们可以暴力地逐个计算 nums1nums1​ 中的每个元素值 nums1[i]nums1​[i] 在 nums2nums2​ 中对应位置的右边的第一个比 nums1[i]nums1​[i] 大的元素值。具体地,我们使用如下方法:

初始化与 nums1nums1​ 等长的查询数组 resres。

遍历 nums1nums1​ 中的所有元素,不妨设当前遍历到元素为 nums1[i]nums1​[i]:

    从前向后遍历 nums2nums2​ 中的元素,直至找到 nums2[j]=nums1[i]nums2​[j]=nums1​[i];

    从 j+1j+1 开始继续向后遍历,直至找到 nums2[k]>nums2[j]nums2​[k]>nums2​[j],其中 k≥j+1k≥j+1;

    如果找到了 nums2[k]nums2​[k],则将 res[i]res[i] 置为 nums2[k]nums2​[k],否则将 res[i]res[i] 置为 −1−1。

查询数组 resres 即为最终结果。

3.代码实现

```java class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { int m = nums1.length, n = nums2.length; int[] res = new int[m]; for (int i = 0; i < m; ++i) { int j = 0; while (j < n && nums2[j] != nums1[i]) { ++j; } int k = j + 1; while (k < n && nums2[k] < nums2[j]) { ++k; } res[i] = k < n ? nums2[k] : -1; } return res; } }

```

猜你喜欢

转载自blog.csdn.net/HHX_01/article/details/131285448
今日推荐