- 浏览: 183367 次
- 性别:
- 来自: 济南
文章分类
最新评论
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.
The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.
Example 1:
Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
Explanation:
For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
For number 1 in the first array, the next greater number for it in the second array is 3.
For number 2 in the first array, there is no next greater number for it in the second array, so output -1.
Example 2:
Input: nums1 = [2,4], nums2 = [1,2,3,4].
Output: [3,-1]
Explanation:
For number 2 in the first array, the next greater number for it in the second array is 3.
For number 4 in the first array, there is no next greater number for it in the second array, so output -1.
Note:
Solution:
[code="python"]class Solution(object):
def nextGreaterElement(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
hash_map = dict()
helper = list()
for s in range(len(nums2)):
while helper and nums2[s] > helper[-1]:
hash_map[helper.pop()] = nums2[s]
helper.append(nums2[s])
for i in helper:
hash_map[i] = -1
return [hash_map.get(i) for i in nums1]
The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.
Example 1:
Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
Explanation:
For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
For number 1 in the first array, the next greater number for it in the second array is 3.
For number 2 in the first array, there is no next greater number for it in the second array, so output -1.
Example 2:
Input: nums1 = [2,4], nums2 = [1,2,3,4].
Output: [3,-1]
Explanation:
For number 2 in the first array, the next greater number for it in the second array is 3.
For number 4 in the first array, there is no next greater number for it in the second array, so output -1.
Note:
- All elements in nums1 and nums2 are unique.
- The length of both nums1 and nums2 would not exceed 1000.
Solution:
[code="python"]class Solution(object):
def nextGreaterElement(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
hash_map = dict()
helper = list()
for s in range(len(nums2)):
while helper and nums2[s] > helper[-1]:
hash_map[helper.pop()] = nums2[s]
helper.append(nums2[s])
for i in helper:
hash_map[i] = -1
return [hash_map.get(i) for i in nums1]
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 264Given a matrix of M x N eleme ... -
Word Break II
2016-03-09 03:15 383Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 373Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 562Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 474Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 662Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 468The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 428Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 573Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 425All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 897Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 929Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 672Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 782You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ... -
Bulb Switcher
2016-02-28 12:12 426There are n bulbs that are init ...
相关推荐
c语言入门 c语言_leetcode题解496-next-greater-element-i.c
496 Next Greater Element I 复习了 linkedlist,arraylist的区别 2021.2.1 198 House Robber 学会了数组的创建,以及动态规划最基本的题目 2021.2.2 322 兑换钱币 学会了 Arrays.fill 的使用,以及查看源码,返回 ...
leetcode 跳跃 LeetCode 力扣刷题70道! 数组 Array 力扣 485 最大连续1的个数 | Max Consecutive ...Element ...496 下一个更大的元素 | Next Greater Element I 力扣 232 用栈实现队列 | Implement
【标题】:本资源主要涉及的是LeetCode编程挑战中的第496题——"Next Greater Element I"(下一个更大元素I)的Java解决方案。这道题目是数据结构和算法领域中常见的一类问题,旨在训练程序员对数组的操作以及对栈这...
首先,我们需要定义一个`Node`类来表示链表中的每个节点,它包含两个属性:`element`用于存储节点的值,`next`指向下一个节点的引用。 ```javascript class Node { constructor(element) { this.element = ...
在LeetCode平台上,第503题名为"下一个更大元素II"(Next Greater Element II)。这是一道关于数组处理和栈的数据结构问题。题目要求给定一个循环有序数组,找到每个元素的下一个更大元素。如果不存在这样的元素,则...
1. 单调栈的典型用途是找到数组中下一个比自身大的元素(the next greater element, NGE),可以在一次遍历就获得所有元素的 NGE。 这个内容讲解了单调栈的应用场景,即找到数组中下一个比自身大的元素。单调栈是一...
在这个“php-leetcode题解之下一个更大元素.zip”压缩包中,我们重点关注的是使用PHP语言解决LeetCode上关于“下一个更大元素”(Next Greater Element)的问题。 “下一个更大元素”是一个常见的算法问题,它的目标...
next-greater-element-ii serialize-and-deserialize-binary-tree subarray-sum-equals-k binary-tree-preorder-traversal n-queens-ii populating-next-right-pointers-in-each-node sum-root-to-leaf-numbers best...
Next [element] ``` `For Each...Next` 用于遍历集合中的每个元素。例如: ```vb Dim arr() = Array("apple", "banana", "cherry") For Each item In arr Response.Write item & " " Next ``` ##### 9. ...
496-next-greater-element-ic。 (完毕) 使用优化算法更新 798-smallest-rotation-with-highest-score.c。 (完毕) 使用优化算法更新 456-132-pattern.c。 使用位操作更新 78-subsets.c。 (完毕) 使用动态规划...
print "var is greater than 5\n"; } else { print "var is less than or equal to 5\n"; } ``` 2. `unless` 语句:与`if`相反,当条件不满足时执行代码块。 3. `if/elsif/else` 链:处理多个可能的情况: ```...
A binary search finds the median element in a list, compares its value to the one you are searching for, and determines if it’s greater than, less than, or equal to the one you want. A guess that ...
JMP NEXT_ELEMENT NEGATIVE_INC: INC NEGATIVE JMP NEXT_ELEMENT ZERO_INC: INC ZERO NEXT_ELEMENT: INC SI LOOP AGAIN MOV AH,4CH INT 21H CODE ENDS END START ``` 以上程序实现了微机原理中典型的分支程序设计...
3. **下一个大元素II(Next Greater Element II)**:这道题要求对每个数组元素找到它右侧所有元素中最大的一个,但每个元素只能与它后面的元素进行比较。同样,堆栈可以帮助我们高效地实现这一功能。我们可以遍历...
equal_to, not_equal_to, greater, greater_equal, less, less_equal 7.5 逻辑运算类(Logical)仿函数 422 logical_and, logical_or, logical_not 7.6 证同(identity)、选择(select)、投射(project) 423 ...
# Swap if the element found is greater than the next element if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] return arr ``` 在这个代码中,`n`表示数组的长度,外层循环控制遍历的次数,内层...
model is a distribution over the next character, y_i, given the context of characters seen so far, y_1 ... y_i-1. To simplify coding, PAQ6 uses a binary string alphabet. Thus, the output of a model ...
庖丁解牛(侯捷自序) i 目录 v 前言 xvii 本书定位 xvii 合适的读者 xviii 最佳阅读方式 xviii 我所选择的剖析对象 xix 各章主题 xx 编译工具 xx 中英术语的运用风格 xxi 英文术语采用原则 xxii 版面...
•Q583680 - Skin Editor - Changes to the Borders.Left, Borders.Top, and/or Borders.Right property sets are not stored to a skin project if these are the only changes made to a skin element •Q573512 -...