Skip to content

[Python]: Runtime Error (name 'SortedList' is not defined) #929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
baitian752 opened this issue Mar 11, 2024 · 3 comments
Closed

[Python]: Runtime Error (name 'SortedList' is not defined) #929

baitian752 opened this issue Mar 11, 2024 · 3 comments

Comments

@baitian752
Copy link

Runtime Error

Error

Line 3: NameError: name 'SortedList' is not defined
NameError: name 'SortedList' is not defined
        ^^^^^^^^^^
    s = SortedList()
Line 3 in longestSubarray (Solution.py)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ret = Solution().longestSubarray(param_1, param_2)
Line 41 in _driver (Solution.py)
    _driver()
Line 52 in <module> (Solution.py)

Your Input

[8,2,4,7]
4

Code

#
# @lc app=leetcode.cn id=1438 lang=python3
#
# [1438] 绝对差不超过限制的最长连续子数组
#

from sortedcontainers import SortedList


# @lc code=start
class Solution:
    def longestSubarray(self, nums: list[int], limit: int) -> int:
        s = SortedList()
        n = len(nums)
        left, right, ans = 0, 0, 0
        while right < n:
            s.add(nums[right])
            while s[-1] - s[0] > limit:
                s.remove(nums[left])
                left += 1
            ans = max(ans, len(s))
            right += 1
        return ans

# @lc code=end
@woung717
Copy link

sortedcontainers is not a Python standard library.

@baitian752
Copy link
Author

baitian752 commented Mar 12, 2024

sortedcontainers is not a Python standard library.

Please refer to https://support.leetcode.com/hc/en-us/articles/360011833974-What-are-the-environments-for-the-programming-languages

Language Version Notes
Python3 Python3.11 Most libraries are already imported automatically for your convenience, such as array, bisect, collections. If you need more libraries, you can import it yourself. For Map/TreeMap data structure, you may use sortedcontainers library.

We can use sortedcontainers directly in the browser!

@baitian752
Copy link
Author

I figure it out that I should put all the codes between the comments.

#
# @lc app=leetcode.cn id=1438 lang=python3
#
# [1438] 绝对差不超过限制的最长连续子数组
#


# @lc code=start
from sortedcontainers import SortedList


class Solution:
    def longestSubarray(self, nums: list[int], limit: int) -> int:
        s = SortedList()
        n = len(nums)
        left, right, ans = 0, 0, 0
        while right < n:
            s.add(nums[right])
            while s[-1] - s[0] > limit:
                s.remove(nums[left])
                left += 1
            ans = max(ans, len(s))
            right += 1
        return ans


# @lc code=end

This maybe work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants