[LeetCode]#496. Next Greater Element I

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.
  1. get the nums1 value index in nums2
  2. check index +1 number in nums2 is larger than nums1 or not.
  3. if yes, return current value. if no, return -1.
class Solution:
def nextGreaterElement(self,nums1, nums2):
ans=[-1]*len(nums1)

for i in nums1:
temp=nums2.index(i)
for j in range(temp,len(nums2)):
ans[nums1.index(i)]=-1
if nums2[j]>i:
ans[nums1.index(i)]=nums2[j]
break
return ans

--

--

--

Interesting in any computer science.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Features:

Git Flow, Customized and Explained

(An Approach to) Git Flow Explained

Transforming From Web Apps to SaaS

Transforming From Web Apps to SaaS

AWS Managed Streaming for Apache Kafka : Streaming messages from producer to consumer using Amazon…

How I Conquer My AWS Costs

How do I keep track of large CSS files with Notepad++ and the function list

Application of Principal Component Analysis on a Sparse n-dimensional matrix

CS373 Fall 2021: Adeet Parikh

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fatboy Slim

Fatboy Slim

Interesting in any computer science.

More from Medium

[LeetCode]#2114. Maximum Number of Words Found in Sentences

LeetCode Patterns Adventure 12 — Palindrome Linked List

LeetCode #167 | Two Sum II — Input Array Is Sorted (Python)

Leetcode 534 Game Play Analysis III without sum window function