Appearance
question:def trap(height: List[int]) -> int: Returns the total amount of rainwater trapped. Args: height: List[int] - a list of integers representing the height of buildings. Returns: int - the total amount of rainwater trapped. >>> trap([]) == 0 >>> trap([4]) == 0 >>> trap([5, 4, 3, 2, 1]) == 0 >>> trap([1, 2, 3, 4, 5]) == 0 >>> trap([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]) == 6 >>> trap([2, 0, 2]) == 2 >>> trap([4, 2, 0, 3, 2, 5]) == 9
answer:def trap(height): Returns the total amount of rainwater trapped. Args: height: List[int] - a list of integers representing the height of buildings. Returns: int - the total amount of rainwater trapped. if not height: return 0 left, right = 0, len(height) - 1 left_max, right_max = height[left], height[right] water_trapped = 0 while left < right: if left_max < right_max: left += 1 left_max = max(left_max, height[left]) water_trapped += max(0, left_max - height[left]) else: right -= 1 right_max = max(right_max, height[right]) water_trapped += max(0, right_max - height[right]) return water_trapped
question:def count_pairs(nums, target): Returns the number of unique pairs (i, j) in the list such that nums[i] + nums[j] equals the target. The result is given modulo 10^9 + 7. Examples: >>> count_pairs([1, 2, 3, 4], 5) 2 >>> count_pairs([1, 2, 3, 4], 8) 0 >>> count_pairs([4, 4, 4, 4], 8) 1 >>> count_pairs([-1, -2, 3, 5, 2, -3], 1) 2 >>> count_pairs([1000000000000, 500000000000, 1000000000000, -1000000000000, 0], 0) 1
answer:def count_pairs(nums, target): Returns the number of unique pairs (i, j) in the list such that nums[i] + nums[j] equals the target. The result is given modulo 10^9 + 7. MOD = 10**9 + 7 nums.sort() pairs = set() seen = set() for num in nums: if target - num in seen: pairs.add((min(num, target - num), max(num, target - num))) seen.add(num) return len(pairs) % MOD
question:def can_form_by_concatenation(s: str, words: List[str]) -> bool: Determines if the string `s` can be formed by concatenating all words from the list `words` exactly once and in any order. >>> can_form_by_concatenation("wordgoodgoodword", ["word", "good", "good", "word"]) == True >>> can_form_by_concatenation("wordgoodgoodwordextra", ["word", "good", "good", "word"]) == False >>> can_form_by_concatenation("wordgoodgooword", ["word", "good", "good", "word"]) == False >>> can_form_by_concatenation("goodwordgoodword", ["word", "good", "good", "word"]) == True >>> can_form_by_concatenation("", []) == True >>> can_form_by_concatenation("word", ["word"]) == True >>> can_form_by_concatenation("abc", ["a", "b", "c"]) == True >>> can_form_by_concatenation("abc", ["a", "c", "b"]) == True >>> can_form_by_concatenation("aabbcc", ["aa", "bb", "cc"]) == True >>> can_form_by_concatenation("aabbbcc", ["aa", "bb", "cc"]) == False
answer:def can_form_by_concatenation(s, words): Determines if the string `s` can be formed by concatenating all words from the list `words` exactly once and in any order. Parameters: s (str): The string to be checked. words (list): List of words to concatenate. Returns: bool: True if `s` can be formed, else False. from collections import Counter # Combine the words to see if they add up to the given string `s` combined_words = ''.join(words) # If the combined length does not match `s`, return False early if len(combined_words) != len(s): return False # Use Counter to count characters in `s` and combined words return Counter(s) == Counter(combined_words)
question:def knapsack_max_weight(weights: List[int], capacity: int) -> int: Returns the maximum weight that can be accommodated in the knapsack without exceeding the capacity. >>> knapsack_max_weight([1, 3, 4, 5], 7) 7 >>> knapsack_max_weight([1, 3, 4, 5], 6) 6 >>> knapsack_max_weight([10, 20, 30], 5) 0 >>> knapsack_max_weight([1, 2, 3], 0) 0 >>> knapsack_max_weight([1, 2, 3, 8, 7, 4], 11) 11 >>> knapsack_max_weight([15, 10, 20, 5], 25) 25 >>> knapsack_max_weight([5, 3, 4, 2], 10) 10 >>> knapsack_max_weight([5, 3, 4, 2], 9) 9 >>> knapsack_max_weight([5, 3, 4, 2], 8) 8 >>> knapsack_max_weight([5, 3, 4, 2], 7) 7
answer:def knapsack_max_weight(weights, capacity): Returns the maximum weight that can be accommodated in the knapsack without exceeding the capacity. n = len(weights) # Initialize a DP array with zeros. dp[j] represents the maximum weight for capacity j. dp = [0] * (capacity + 1) for weight in weights: # Traverse the dp array backwards to avoid recomputing results with the same weight for j in range(capacity, weight - 1, -1): dp[j] = max(dp[j], dp[j - weight] + weight) return dp[capacity]