Skip to content
🤔prompts chat🧠
🔍
question:def min_makespan(t: int, test_cases: List[Tuple[int, List[int]]]) -> List[int]: Find the minimum possible makespan for scheduling tasks on two machines. >>> min_makespan(2, [(4, [3, 1, 4, 2]), (3, [2, 3, 5])]) [5, 5] >>> min_makespan(1, [(5, [5, 5, 5, 5, 5])]) [15]

answer:def min_makespan(t, test_cases): def find_min_makespan(tasks): total_sum = sum(tasks) n = len(tasks) # DP array where dp[i][j] means if it is possible to get j time # using the first i tasks dp = [[False] * (total_sum + 1) for _ in range(n + 1)] dp[0][0] = True for i in range(1, n + 1): for j in range(total_sum + 1): dp[i][j] = dp[i - 1][j] if j >= tasks[i - 1]: dp[i][j] = dp[i][j] or dp[i - 1][j - tasks[i - 1]] min_makespan = float('inf') for j in range(total_sum // 2 + 1): if dp[n][j]: first_machine = j second_machine = total_sum - j min_makespan = min(min_makespan, max(first_machine, second_machine)) return min_makespan results = [] for case in test_cases: n, tasks = case results.append(find_min_makespan(tasks)) return results # Sample usage t = 2 test_cases = [ (4, [3, 1, 4, 2]), (3, [2, 3, 5]) ] print(min_makespan(t, test_cases)) # Output: [5, 5]

question:class SequenceProcessor: def __init__(self, sequence): self.sequence = sequence def update(self, i, v): Update the ith element of the sequence to be v. pass def sum_range(self, l, r): Calculate the sum of the elements in the range [l, r] (both inclusive). pass def process_queries(n, sequence, q, queries): Process a series of queries on a sequence of integers. Args: n (int): the number of elements in the sequence. sequence (List[int]): the elements of the sequence. q (int): the number of queries. queries (List[str]): the queries to be processed. Returns: List[int]: the results of the queries of type "S l r". Example: >>> n = 5 >>> sequence = [1, 2, 3, 4, 5] >>> q = 5 >>> queries = ["S 1 3", "U 2 10", "S 1 3", "U 5 6", "S 1 5"] >>> process_queries(n, sequence, q, queries) [6, 14, 24] pass

answer:class SequenceProcessor: def __init__(self, sequence): self.sequence = sequence def update(self, i, v): self.sequence[i - 1] = v def sum_range(self, l, r): return sum(self.sequence[l - 1:r]) def process_queries(n, sequence, q, queries): processor = SequenceProcessor(sequence) results = [] for query in queries: parts = query.split() if parts[0] == 'U': processor.update(int(parts[1]), int(parts[2])) elif parts[0] == 'S': result = processor.sum_range(int(parts[1]), int(parts[2])) results.append(result) return results

question:def maxAreaOfIsland(grid): Return the maximum area of an island in the given grid. Args: grid: List[List[int]] : 2-dimensional list representing the grid Returns: int : maximum area of an island in the grid Example: >>> maxAreaOfIsland([[0,0,1,0,0,0,0,1,0,0,0,0,0], [0,0,0,0,0,0,0,1,1,1,0,0,0], [0,1,1,0,1,0,0,0,0,0,0,0,0], [0,1,0,0,1,1,0,0,1,0,1,0,0], [0,1,0,0,1,1,0,0,1,1,1,0,0], [0,0,0,0,0,0,0,0,0,0,1,0,0], [0,0,0,0,0,0,0,1,1,1,0,0,0], [0,0,0,0,0,0,0,1,1,0,0,0,0]]) 6 >>> maxAreaOfIsland([[0,0,0,0,0,0,0,0]]) 0

answer:def maxAreaOfIsland(grid): Returns the maximum area of an island in the given grid. if not grid: return 0 rows = len(grid) cols = len(grid[0]) def dfs(r, c): if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0: return 0 # Mark the cell as visited by setting it to 0 grid[r][c] = 0 area = 1 # Check all four directions area += dfs(r + 1, c) area += dfs(r - 1, c) area += dfs(r, c + 1) area += dfs(r, c - 1) return area max_area = 0 for r in range(rows): for c in range(cols): if grid[r][c] == 1: max_area = max(max_area, dfs(r, c)) return max_area

question:def maxCoins(piles: List[int], k: int) -> int: Given a list of non-negative integers representing the amount of coins in each stack, and an integer k representing the number of coins allowed to pick from the stacks, return the maximum number of coins you can obtain after k operations by picking the largest coins. >>> maxCoins([2, 4, 1, 2, 7, 8], 3) == 19 >>> maxCoins([2, 4, 5], 2) == 9 >>> maxCoins([5, 6, 7, 8, 9], 3) == 24 from typing import List def test_example_1(): piles = [2, 4, 1, 2, 7, 8] k = 3 assert maxCoins(piles, k) == 19 def test_example_2(): piles = [2, 4, 5] k = 2 assert maxCoins(piles, k) == 9 def test_example_3(): piles = [5, 6, 7, 8, 9] k = 3 assert maxCoins(piles, k) == 24 def test_small_piles(): piles = [1, 2, 3] k = 2 assert maxCoins(piles, k) == 5 def test_large_k(): piles = [10, 20, 30, 40, 50] k = 5 assert maxCoins(piles, k) == 150 def test_large_piles(): piles = [i for i in range(1, 10001)] k = 100 expected = sum(piles[-100:]) # sum of the last 100 elements which are the largest in this list assert maxCoins(piles, k) == expected

answer:def maxCoins(piles, k): Returns the maximum number of coins that can be obtained after k operations by picking the largest coins. # Sort the piles in descending order sorted_piles = sorted(piles, reverse=True) # Sum the first k elements in the sorted list return sum(sorted_piles[:k])

Released under the chat License.

has loaded