Skip to content
🤔prompts chat🧠
🔍
question:from typing import List, Union def find_shortest_path(grid: List[List[int]], src: int, dst: int) -> Union[List[int], int]: You are given a 2D grid representing a list of flight routes between several cities where `grid[i][j]` is `1` if there is a direct flight from city `i` to city `j`, and `0` otherwise. Each city is represented by an index ranging from `0` to `n-1` (`n` being the total number of cities). You are also given two cities: `src` and `dst` as indices. Return the shortest path from `src` to `dst` in terms of the number of flights (edges). If there is no valid path, return `-1`. The path should be returned as a list of city indices. If there are multiple shortest paths, return any of them. >>> grid = [ ... [0, 1, 0, 0], ... [0, 0, 1, 0], ... [0, 0, 0, 1], ... [0, 0, 0, 0] ... ] >>> find_shortest_path(grid, 0, 3) [0, 1, 2, 3] >>> grid = [ ... [0, 1, 0, 0], ... [0, 0, 0, 0], ... [0, 0, 0, 1], ... [0, 0, 0, 0] ... ] >>> find_shortest_path(grid, 0, 3) -1 >>> grid = [ ... [0, 1, 0, 0], ... [0, 0, 1, 0], ... [0, 0, 0, 1], ... [0, 0, 0, 0] ... ] >>> find_shortest_path(grid, 0, 0) [0] >>> grid = [ ... [0, 1, 1, 0], ... [0, 0, 0, 1], ... [0, 0, 0, 1], ... [0, 0, 0, 0] ... ] >>> result = find_shortest_path(grid, 0, 3) >>> result == [0, 1, 3] or result == [0, 2, 3] True

answer:from collections import deque def find_shortest_path(grid, src, dst): n = len(grid) visited = [False] * n parent = [-1] * n queue = deque([src]) visited[src] = True while queue: city = queue.popleft() if city == dst: path = [] while city != -1: path.append(city) city = parent[city] return path[::-1] for next_city in range(n): if grid[city][next_city] == 1 and not visited[next_city]: queue.append(next_city) visited[next_city] = True parent[next_city] = city return -1

question:def maxRenovationCost(nums): Calculate the maximum renovation cost without renovating two adjacent houses. :param nums: List[int] - List of integers representing the renovation cost for each house. :return: int - Maximum renovation cost. pass # Unit tests def test_maxRenovationCost_empty(): assert maxRenovationCost([]) == 0 def test_maxRenovationCost_single(): assert maxRenovationCost([5]) == 5 def test_maxRenovationCost_two(): assert maxRenovationCost([5, 10]) == 10 assert maxRenovationCost([10, 5]) == 10 def test_maxRenovationCost_example(): assert maxRenovationCost([2, 7, 9, 3, 1]) == 12 def test_maxRenovationCost_all_zeroes(): assert maxRenovationCost([0, 0, 0, 0]) == 0 def test_maxRenovationCost_large_numbers(): assert maxRenovationCost([100, 200, 300, 400, 500]) == 900 def test_maxRenovationCost_alternating_numbers(): assert maxRenovationCost([5, 1, 5, 1, 5, 1, 5]) == 20 def test_maxRenovationCost_small_numbers(): assert maxRenovationCost([1, 2, 3, 1]) == 4

answer:def maxRenovationCost(nums): Calculate the maximum renovation cost without renovating two adjacent houses. :param nums: List[int] - List of integers representing the renovation cost for each house. :return: int - Maximum renovation cost. if not nums: return 0 n = len(nums) if n == 1: return nums[0] dp = [0] * n # dp array to store the max cost up to each house dp[0] = nums[0] dp[1] = max(nums[0], nums[1]) for i in range(2, n): dp[i] = max(dp[i-1], nums[i] + dp[i-2]) return dp[-1]

question:def longest_substring_without_repeating_characters(s: str) -> str: Returns the longest substring without repeating characters. If there are multiple substrings with the same length, the substring that appears first is returned. >>> longest_substring_without_repeating_characters("abcabcbb") == "abc" >>> longest_substring_without_repeating_characters("bbbbb") == "b" >>> longest_substring_without_repeating_characters("pwwkew") == "wke" >>> longest_substring_without_repeating_characters("") == "" >>> longest_substring_without_repeating_characters("abcdef") == "abcdef" >>> longest_substring_without_repeating_characters("abcbde") == "cbde" >>> longest_substring_without_repeating_characters("aab") == "ab" >>> longest_substring_without_repeating_characters("dvdf") == "vdf" >>> longest_substring_without_repeating_characters("anviaj") == "nviaj"

answer:def longest_substring_without_repeating_characters(s): Returns the longest substring without repeating characters. If there are multiple substrings with the same length, the substring that appears first is returned. :param s: Input string consisting of lowercase English letters :type s: str :return: The longest substring without repeating characters :rtype: str char_index = {} start = 0 max_len = 0 max_substr = "" for end, char in enumerate(s): if char in char_index and char_index[char] >= start: start = char_index[char] + 1 char_index[char] = end current_len = end - start + 1 if current_len > max_len: max_len = current_len max_substr = s[start:end+1] return max_substr

question:def sumEvenRowEvenCol(mat: List[List[int]]) -> int: Returns the sum of all matrix elements located on even rows and even columns. >>> sumEvenRowEvenCol([ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16] ]) 20 >>> sumEvenRowEvenCol([ [5] ]) 5 >>> sumEvenRowEvenCol([ [1, 2], [3, 4] ]) 1 >>> sumEvenRowEvenCol([ [] ]) 0 >>> sumEvenRowEvenCol([]) 0 >>> sumEvenRowEvenCol([ [4, 7, 1, 6], [2, 5, 8, 3], [10, 13, 14, 11], [15, 18, 2, 20] ]) 29 >>> sumEvenRowEvenCol([ [1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15] ]) 45

answer:def sumEvenRowEvenCol(mat): Returns the sum of all matrix elements located on even rows and even columns. Parameters: mat (List[List[int]]): The 2D matrix of integers. Returns: int: The sum of elements on even rows and even columns. total_sum = 0 for i in range(0, len(mat), 2): for j in range(0, len(mat[i]), 2): total_sum += mat[i][j] return total_sum

Released under the chat License.

has loaded