Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

This is my solution to LeetCode's "Next Greater Element I":

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.

class Solution:
 
 def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
 need, ready, next_greater = set(nums1), set(), {}
 for n in nums2:
 for k in ready:
 if k < n:
 next_greater[k] = n
 ready = {k for k in ready if k not in next_greater}
 if n in need and n not in next_greater and n not in ready:
 ready.add(n)
 return [next_greater[k] if k in next_greater else -1 for k in nums1]

The nextGreaterElement method accepts 2 lists. Neither list contains duplicates, and nums1 is a subset of nums2. For every num in nums1, it will output the first number greater than num positioned to the right of num in nums2, or -1 is none is found. e.g.:

Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]

I loop through the keys in ready, and the body of the loop does nothing unless k < n. len(ready) can be very large relative to the number of values that satisfy k < n. This seems like a very common thing so I'm wondering if there's a more explicit/pythonic way to write this?

Or should I be using a different data structure entirely?

This is my solution to LeetCode's "Next Greater Element I":

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.

class Solution:
 
 def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
 need, ready, next_greater = set(nums1), set(), {}
 for n in nums2:
 for k in ready:
 if k < n:
 next_greater[k] = n
 ready = {k for k in ready if k not in next_greater}
 if n in need and n not in next_greater and n not in ready:
 ready.add(n)
 return [next_greater[k] if k in next_greater else -1 for k in nums1]

The nextGreaterElement method accepts 2 lists. Neither list contains duplicates, and nums1 is a subset of nums2. For every num in nums1, it will output the first number greater than num positioned to the right of num in nums2, or -1 is none is found. e.g.:

Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]

I loop through the keys in ready, and the body of the loop does nothing unless k < n. len(ready) can be very large relative to the number of values that satisfy k < n. This seems like a very common thing so I'm wondering if there's a more explicit/pythonic way to write this?

Or should I be using a different data structure entirely?

This is my solution to LeetCode's "Next Greater Element I":

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.

class Solution:
 
 def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
 need, ready, next_greater = set(nums1), set(), {}
 for n in nums2:
 for k in ready:
 if k < n:
 next_greater[k] = n
 ready = {k for k in ready if k not in next_greater}
 if n in need and n not in next_greater and n not in ready:
 ready.add(n)
 return [next_greater[k] if k in next_greater else -1 for k in nums1]

The nextGreaterElement method accepts 2 lists. Neither list contains duplicates, and nums1 is a subset of nums2. For every num in nums1, it will output the first number greater than num positioned to the right of num in nums2, or -1 is none is found. e.g.:

Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]

I loop through the keys in ready, and the body of the loop does nothing unless k < n. len(ready) can be very large relative to the number of values that satisfy k < n. This seems like a very common thing so I'm wondering if there's a more explicit/pythonic way to write this?

Or should I be using a different data structure entirely?

Bumped by Community user
Bumped by Community user
added 356 characters in body
Source Link
vnp
  • 58.6k
  • 4
  • 55
  • 144

This is my solution to LeetCode's "Next Greater Element I":

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.

class Solution:
 
 def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
 need, ready, next_greater = set(nums1), set(), {}
 for n in nums2:
 for k in ready:
 if k < n:
 next_greater[k] = n
 ready = {k for k in ready if k not in next_greater}
 if n in need and n not in next_greater and n not in ready:
 ready.add(n)
 return [next_greater[k] if k in next_greater else -1 for k in nums1]

The nextGreaterElement method accepts 2 lists. Neither list contains duplicates, and nums1 is a subset of nums2. For every num in nums1, it will output the first number greater than num positioned to the right of num in nums2, or -1 is none is found. e.g.:

Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]

I loop through the keys in ready, and the body of the loop does nothing unless k < n. len(ready) can be very large relative to the number of values that satisfy k < n. This seems like a very common thing so I'm wondering if there's a more explicit/pythonic way to write this?

Or should I be using a different data structure entirely?

This is my solution to LeetCode's "Next Greater Element I":

class Solution:
 
 def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
 need, ready, next_greater = set(nums1), set(), {}
 for n in nums2:
 for k in ready:
 if k < n:
 next_greater[k] = n
 ready = {k for k in ready if k not in next_greater}
 if n in need and n not in next_greater and n not in ready:
 ready.add(n)
 return [next_greater[k] if k in next_greater else -1 for k in nums1]

The nextGreaterElement method accepts 2 lists. Neither list contains duplicates, and nums1 is a subset of nums2. For every num in nums1, it will output the first number greater than num positioned to the right of num in nums2, or -1 is none is found. e.g.:

Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]

I loop through the keys in ready, and the body of the loop does nothing unless k < n. len(ready) can be very large relative to the number of values that satisfy k < n. This seems like a very common thing so I'm wondering if there's a more explicit/pythonic way to write this?

Or should I be using a different data structure entirely?

This is my solution to LeetCode's "Next Greater Element I":

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.

class Solution:
 
 def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
 need, ready, next_greater = set(nums1), set(), {}
 for n in nums2:
 for k in ready:
 if k < n:
 next_greater[k] = n
 ready = {k for k in ready if k not in next_greater}
 if n in need and n not in next_greater and n not in ready:
 ready.add(n)
 return [next_greater[k] if k in next_greater else -1 for k in nums1]

The nextGreaterElement method accepts 2 lists. Neither list contains duplicates, and nums1 is a subset of nums2. For every num in nums1, it will output the first number greater than num positioned to the right of num in nums2, or -1 is none is found. e.g.:

Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]

I loop through the keys in ready, and the body of the loop does nothing unless k < n. len(ready) can be very large relative to the number of values that satisfy k < n. This seems like a very common thing so I'm wondering if there's a more explicit/pythonic way to write this?

Or should I be using a different data structure entirely?

deleted 53 characters in body; edited title
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

Iterating only items that satisfy a condition Solution to LeetCode Next Greater Element I in Python

Often you need to perform some operation over only certain items in an iterable that match a condition. For example (myThis is my solution to LeetCode's "Next Greater Element I")LeetCode's "Next Greater Element I":

class Solution:
 
 def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
 need, ready, next_greater = set(nums1), set(), {}
 for n in nums2:
 for k in ready:
 if k < n:
 next_greater[k] = n
 ready = {k for k in ready if k not in next_greater}
 if n in need and n not in next_greater and n not in ready:
 ready.add(n)
 return [next_greater[k] if k in next_greater else -1 for k in nums1]

The nextGreaterElement method accepts 2 lists. Neither list contains duplicates, and nums1 is a subset of nums2. For every num in nums1, it will output the first number greater than num positioned to the right of num in nums2, or -1 is none is found. e.g.:

Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]

I loop through the keys in ready, and the body of the loop does nothing unless k < n. len(ready) can be very large relative to the number of values that satisfy k < n. This seems like a very common thing so I'm wondering if there's a more explicit/pythonic way to write this?

Or should I be using a different data structure entirely?

Iterating only items that satisfy a condition in Python

Often you need to perform some operation over only certain items in an iterable that match a condition. For example (my solution to LeetCode's "Next Greater Element I"):

class Solution:
 
 def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
 need, ready, next_greater = set(nums1), set(), {}
 for n in nums2:
 for k in ready:
 if k < n:
 next_greater[k] = n
 ready = {k for k in ready if k not in next_greater}
 if n in need and n not in next_greater and n not in ready:
 ready.add(n)
 return [next_greater[k] if k in next_greater else -1 for k in nums1]

The nextGreaterElement method accepts 2 lists. Neither list contains duplicates, and nums1 is a subset of nums2. For every num in nums1, it will output the first number greater than num positioned to the right of num in nums2, or -1 is none is found. e.g.:

Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]

I loop through the keys in ready, and the body of the loop does nothing unless k < n. len(ready) can be very large relative to the number of values that satisfy k < n. This seems like a very common thing so I'm wondering if there's a more explicit/pythonic way to write this?

Or should I be using a different data structure entirely?

Solution to LeetCode Next Greater Element I in Python

This is my solution to LeetCode's "Next Greater Element I":

class Solution:
 
 def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
 need, ready, next_greater = set(nums1), set(), {}
 for n in nums2:
 for k in ready:
 if k < n:
 next_greater[k] = n
 ready = {k for k in ready if k not in next_greater}
 if n in need and n not in next_greater and n not in ready:
 ready.add(n)
 return [next_greater[k] if k in next_greater else -1 for k in nums1]

The nextGreaterElement method accepts 2 lists. Neither list contains duplicates, and nums1 is a subset of nums2. For every num in nums1, it will output the first number greater than num positioned to the right of num in nums2, or -1 is none is found. e.g.:

Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]

I loop through the keys in ready, and the body of the loop does nothing unless k < n. len(ready) can be very large relative to the number of values that satisfy k < n. This seems like a very common thing so I'm wondering if there's a more explicit/pythonic way to write this?

Or should I be using a different data structure entirely?

added 351 characters in body
Source Link
Loading
Source Link
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /