Appearance
question:def buildings_with_sunset_view(heights): Returns the indices of the buildings that have a view of the sunset. A building has a view of the sunset if all the buildings to its right are shorter. >>> buildings_with_sunset_view([4, 2, 3, 1]) [0, 2, 3] >>> buildings_with_sunset_view([4, 3, 2, 1]) [0, 1, 2, 3] >>> buildings_with_sunset_view([1, 2, 3, 4]) [3] >>> buildings_with_sunset_view([1]) [0] >>> buildings_with_sunset_view([1, 3, 2, 4, 5]) [4] >>> buildings_with_sunset_view([]) []
answer:def buildings_with_sunset_view(heights): Returns the indices of the buildings that have a view of the sunset. A building has a view of the sunset if all buildings to its right are shorter. n = len(heights) result = [] if n == 0: return result max_height = heights[-1] result.append(n - 1) for i in range(n - 2, -1, -1): if heights[i] > max_height: result.append(i) max_height = heights[i] return result[::-1]
question:class TreeNode: A TreeNode class for a binary tree node with left and right children. def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def is_balanced(root): Returns True if the binary tree is balanced, otherwise False. def check_height(node): if not node: return 0 return check_height(root) != -1
answer:class TreeNode: A TreeNode class for a binary tree node with left and right children. def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def is_balanced(root): Returns True if the binary tree is balanced, otherwise False. def check_height(node): if not node: return 0 left_height = check_height(node.left) right_height = check_height(node.right) if left_height == -1 or right_height == -1 or abs(left_height - right_height) > 1: return -1 return max(left_height, right_height) + 1 return check_height(root) != -1
question:def construct_lexicographically_smallest_string(s: str, t: str) -> str: Construct the lexicographically smallest string by transforming s using characters from t. Args: s (str): The original string. t (str): The transformation string. Returns: str: The lexicographically smallest string that can be obtained. Examples: >>> construct_lexicographically_smallest_string("abc", "xyz") 'abc' >>> construct_lexicographically_smallest_string("acb", "cab") 'aab' >>> construct_lexicographically_smallest_string("zzzz", "aaaa") 'aaaa' >>> construct_lexicographically_smallest_string("abcd", "abdd") 'abcd' >>> construct_lexicographically_smallest_string("dcb", "aba") 'aba'
answer:def construct_lexicographically_smallest_string(s, t): Construct the lexicographically smallest string by transforming s using characters from t. Args: s (str): The original string. t (str): The transformation string. Returns: str: The lexicographically smallest string that can be obtained. result = [] for i in range(len(s)): if s[i] < t[i]: result.append(s[i]) else: result.append(t[i]) return ''.join(result)
question:def count_increasing_pairs(buildings): Given a list of `n` integers, where each integer represents the height of a building, find the number of pairs of buildings `(i, j)` such that `i < j` and the height of building `i` is less than the height of building `j`. Return the total number of such pairs. >>> count_increasing_pairs([1, 3, 2]) # (1, 3), (1, 2) 2 >>> count_increasing_pairs([5, 4, 3, 2, 1]) 0 >>> count_increasing_pairs([1, 1, 1, 1]) 0 >>> count_increasing_pairs([1, 2, 3, 4]) # (1,2), (1,3), (1,4), (2,3), (2,4), (3,4) 6 >>> count_increasing_pairs([1, 2, 1, 2, 3]) # (1,2), (1,2), (1,3), (2,3), (1,2), (1,3), (2,3) 7 import unittest class TestCountIncreasingPairs(unittest.TestCase): def test_count_increasing_pairs_with_example_case(self): buildings = [1, 3, 2] self.assertEqual(count_increasing_pairs(buildings), 2) def test_count_increasing_pairs_with_no_pairs(self): buildings = [5, 4, 3, 2, 1] self.assertEqual(count_increasing_pairs(buildings), 0) def test_count_increasing_pairs_with_all_equal_heights(self): buildings = [1, 1, 1, 1] self.assertEqual(count_increasing_pairs(buildings), 0) def test_count_increasing_pairs_with_increasing_heights(self): buildings = [1, 2, 3, 4] self.assertEqual(count_increasing_pairs(buildings), 6) def test_count_increasing_pairs_with_decreasing_and_increasing_heights(self): buildings = [1, 2, 1, 2, 3] self.assertEqual(count_increasing_pairs(buildings), 7) if __name__ == "__main__": unittest.main()
answer:def count_increasing_pairs(buildings): This function returns the number of pairs (i, j) where i < j and buildings[i] < buildings[j]. :param buildings: List of integers where each integer represents the height of a building :return: The total number of pairs (i, j) where i < j and buildings[i] < buildings[j] n = len(buildings) count = 0 for i in range(n): for j in range(i + 1, n): if buildings[i] < buildings[j]: count += 1 return count