LeetCode[496] 下一个更大元素 I

Related Topics:
“栈”: https://leetcode.com/tag/stack/
“数组”: https://leetcode.com/tag/array/
“哈希表”: https://leetcode.com/tag/hash-table/
“单调栈”: https://leetcode.com/tag/monotonic-stack/
Similar Questions:
“下一个更大元素 II”: https://leetcode.com/problems/next-greater-element-ii/
“下一个更大元素 III”: https://leetcode.com/problems/next-greater-element-iii/
“每日温度”: https://leetcode.com/problems/daily-temperatures/

Problem:

给你两个没有重复元素 的数组 nums1nums2 ,其中nums1nums2 的子集。

请你找出 nums1 中每个元素在 nums2 中的下一个比其大的值。

nums1 中数字 x 的下一个更大元素是指 xnums2 中对应位置的右边的第一个比 x大的元素。如果不存在,对应位置输出 -1

示例 1:

输入: nums1 = [4,1,2], nums2 = [1,3,4,2].
输出: [-1,3,-1]
解释:
    对于 num1 中的数字 4 ,你无法在第二个数组中找到下一个更大的数字,因此输出 -1 。
    对于 num1 中的数字 1 ,第二个数组中数字1右边的下一个较大数字是 3 。
    对于 num1 中的数字 2 ,第二个数组中没有下一个更大的数字,因此输出 -1 。

示例 2:

输入: nums1 = [2,4], nums2 = [1,2,3,4].
输出: [3,-1]
解释:
    对于 num1 中的数字 2 ,第二个数组中的下一个较大数字是 3 。
    对于 num1 中的数字 4 ,第二个数组中没有下一个更大的数字,因此输出 -1 。

提示:

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

进阶:你可以设计一个时间复杂度为 O(nums1.length + nums2.length) 的解决方案吗?

阅读更多

LeetCode[42] 接雨水

Related Topics:
“栈”: https://leetcode.com/tag/stack/
“数组”: https://leetcode.com/tag/array/
“双指针”: https://leetcode.com/tag/two-pointers/
“动态规划”: https://leetcode.com/tag/dynamic-programming/
“单调栈”: https://leetcode.com/tag/monotonic-stack/
Similar Questions:
“盛最多水的容器”: https://leetcode.com/problems/container-with-most-water/
“除自身以外数组的乘积”: https://leetcode.com/problems/product-of-array-except-self/
“接雨水 II”: https://leetcode.com/problems/trapping-rain-water-ii/

“倒水”: https://leetcode.com/problems/pour-water/

Problem:

给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。

示例 1:

输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
输出:6
解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。

示例 2:

输入:height = [4,2,0,3,2,5]
输出:9

提示:

  • n == height.length
  • 1 <= n <= 2 * 104
  • 0 <= height[i] <= 105
阅读更多