{"type": "session", "harness": "opencode", "id": "cp-10030", "name": "LeetCode 0523: Continuous Subarray Sum"} {"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: Continuous Subarray Sum (Difficulty: Medium)\nTags: Array, Hash Table, Math, Prefix Sum\n\nGiven an integer array nums and an integer k, return true if nums has a good subarray or false otherwise.\nA good subarray is a subarray where:\nNote that:\n\nExample 1:\nInput: nums = [23,2,4,6,7], k = 6\nOutput: true\nExplanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.\n\nExample 2:\nInput: nums = [23,2,6,4,7], k = 6\nOutput: true\nExplanation: [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.\n42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.\n\nExample 3:\nInput: nums = [23,2,6,4,7], k = 13\nOutput: false\n\nConstraints:\n- 1 <= nums.length <= 10^5\n- 0 <= nums[i] <= 10^9\n- 0 <= sum(nums[i]) <= 2^31 - 1\n- 1 <= k <= 2^31 - 1"}} {"type": "message", "message": {"role": "assistant", "content": "# Time: O(n)\n# Space: O(k)\n\nclass Solution(object):\n def checkSubarraySum(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: bool\n \"\"\"\n count = 0\n lookup = {0: -1}\n for i, num in enumerate(nums):\n count += num\n if k:\n count %= k\n if count in lookup:\n if i - lookup[count] > 1:\n return True\n else:\n lookup[count] = i\n\n return False\n\n"}}