Code caprice training camp day58| 739. Daily temperature 496. The next bigger element I

@TOC


foreword

Code Random Record Algorithm Training Camp day58


1. Leetcode 739. Daily temperature

1. Topic

Given an integer array temperatures, representing the temperature of each day, return an array answer, where answer[i] means that for the i-th day, the next higher temperature will appear in a few days. If the temperature doesn't rise after that, substitute 0 in this place.

Example 1:

Input: temperatures = [73,74,75,71,69,72,76,73] Output: [1,1,4,2,1,1,0,0]

Example 2:

Input: temperatures = [30,40,50,60] Output: [1,1,1,0]

Example 3:

Input: temperatures = [30,60,90] Output: [1,1,0]

hint:

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

Source: LeetCode Link: https://leetcode.cn/problems/daily-temperatures

2. Problem-solving ideas

Method One: Violence

For each element temperatures[i] in the temperature list, it is necessary to find the smallest subscript j such that i < j and temperatures[i] < temperatures[j].

Since the temperature range is within [30, 100], an array next can be maintained to record the subscript of the first occurrence of each temperature. The elements in the array next are initialized to infinity, and the value of next is updated during the process of traversing the temperature list.

Traverse the list of temperatures in reverse. For each element temperatures[i], find the first subscript of each temperature from temperatures[i] + 1 to 100 in the array next, mark the smallest subscript among them as warmerIndex, then warmerIndex is the next temperature A higher bid than the current day. If warmerIndex is not infinite, then warmerIndex - i is the number of days to wait for the next temperature to be higher than the current day, and finally let next[temperatures[i]] = i.

Why is the above approach guaranteed to be correct? Because the direction of traversing the temperature list is reverse, when traversing to the element temperatures[i], only the elements behind temperatures[i] are visited, that is, for any t, when next[t] is not infinite, there must be j Such that temperatures[j] == t and i < j. And since each element in the temperature list is traversed, the element value corresponding to the temperature in the array next will be updated, so for any t, when next[t] is not infinite, let j = next[t], then j is The smallest subscript that satisfies temperatures[j] == t and i < j.

3. Code implementation

```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; } }

```

2. Leetcode 496. The next larger element I

1. Topic

The next greater element of the number x in nums1 is the first element greater than x to the right of the corresponding position of x in nums2.

You are given two arrays nums1 and nums2 with no repeating elements, indexed from 0, where nums1 is a subset of nums2.

For each 0 <= i < nums1.length, find the index j satisfying nums1[i] == nums2[j], and determine the next greater element of nums2[j] in nums2. If there is no next greater element, then the answer to this query is -1.

Returns an array ans of length nums1.length as the answer such that ans[i] is the next greater element as described above.

Example 1:

Input: nums1 = [4,1,2], nums2 = [1,3,4,2]. Output: [-1,3,-1] Explanation: The next greater element of each value in nums1 is as follows Description: - 4 , marked in bold italics, nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1. - 1 , identified in bold italics, nums2 = [1,3,4,2]. The next greater element is 3 . - 2 , marked in bold italics, nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.

Example 2:

Input: nums1 = [2,4], nums2 = [1,2,3,4]. Output: [3,-1] Explanation: The next greater element of each value in nums1 is as follows: - 2 , Identified in bold italics, nums2 = [1,2,3,4]. The next greater element is 3 . - 4 , marked in bold italics, nums2 = [1,2,3,4]. There is no next greater element, so the answer is -1.

hint:

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

Source: LeetCode Link: https://leetcode.cn/problems/next-greater-element-i

2. Problem-solving ideas

Method One: Violence

ideas and algorithms

According to the meaning of the question, we found that nums1nums1​ is a query array, and query the first larger value on the right side of the element in nums2nums2​ one by one. Therefore, we can violently calculate each element value nums1[i]nums1​[i] in nums1nums1​ one by one. The first element on the right of the corresponding position in nums2nums2​ is larger than nums1[i]nums1​[i] value. Specifically, we use the following methods:

初始化与 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. Code implementation

```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; } }

```

Guess you like

Origin blog.csdn.net/HHX_01/article/details/131285448