tinycodinglm / cp /cp-10012.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 12)
3c88a0f verified
|
Raw History Blame Contribute Delete
1.75 kB
{"type": "session", "harness": "opencode", "id": "cp-10012", "name": "LeetCode 0503: Next Greater Element Ii"}
{"type": "message", "message": {"role": "system", "content": "You are an expert LeetCode problem solver. You solve problems efficiently with clean code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Next Greater Element II (Difficulty: Medium)\nTags: Array, Stack, Monotonic Stack\n\nGiven a circular integer array nums (i.e., the next element of nums[nums.length - 1] is nums[0]), return the next greater number for every element in nums.\nThe next greater number of a number x is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn't exist, return -1 for this number.\n\nExample 1:\nInput: nums = [1,2,1]\nOutput: [2,-1,2]\nExplanation: The first 1's next greater number is 2; \nThe number 2 can't find next greater number. \nThe second 1's next greater number needs to search circularly, which is also 2.\n\nExample 2:\nInput: nums = [1,2,3,4,3]\nOutput: [2,3,4,-1,4]\n\nConstraints:\n- 1 <= nums.length <= 10^4\n- -10^9 <= nums[i] <= 10^9"}}
{"type": "message", "message": {"role": "assistant", "content": "# Time: O(n)\n# Space: O(n)\n\nclass Solution(object):\n def nextGreaterElements(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n result, stk = [0] * len(nums), []\n for i in reversed(range(2*len(nums))):\n while stk and stk[-1] <= nums[i % len(nums)]:\n stk.pop()\n result[i % len(nums)] = stk[-1] if stk else -1\n stk.append(nums[i % len(nums)])\n return result\n\n"}}