Appearance
question:You are given an array `tasks` where `tasks[i]` represents the time required to complete the `i-th` task. You have a `deadline` integer representing the maximum time allowed to complete all the tasks, and an integer `rest` representing the minimum time of break required between two consecutive tasks. Determine the maximum number of tasks you can complete in the given time frame including rest periods, without exceeding the `deadline`. Return the _maximum number of tasks_ you can complete.
answer:def max_tasks(tasks, deadline, rest): tasks.sort() time_spent = 0 completed_tasks = 0 for task in tasks: if time_spent + task <= deadline: time_spent += task completed_tasks += 1 if time_spent + rest <= deadline: time_spent += rest else: break else: break return completed_tasks
question:You are given a 2D grid of size `m x n` with non-negative integers. The grid represents a map where `grid[i][j]` denotes the height at the cell `(i, j)`. You want to find the length of the longest increasing path in the grid. From each cell, you can move in four directions: left, right, up, or down. You may not move diagonally or move outside the boundary. Return _the length of the longest increasing path in the grid_.
answer:def longest_increasing_path(grid): if not grid or not grid[0]: return 0 m, n = len(grid), len(grid[0]) dp = [[-1] * n for _ in range(m)] def dfs(x, y): if dp[x][y] != -1: return dp[x][y] directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] max_length = 1 for dx, dy in directions: nx, ny = x + dx, y + dy if 0 <= nx < m and 0 <= ny < n and grid[nx][ny] > grid[x][y]: max_length = max(max_length, 1 + dfs(nx, ny)) dp[x][y] = max_length return max_length res = 0 for i in range(m): for j in range(n): res = max(res, dfs(i, j)) return res
question:You are given a list of non-negative integers `arr` representing the heights of pillars in a fence. The width of each pillar is 1. The goal is to select two pillars and build a roof over them such that the water trapped between the pillars and the roof is maximized. The water trapped is the shorter height of the two pillars multiplied by the distance between them. Write a function that returns _the maximum amount of water trapped between two pillars_ in the fence.
answer:def max_water_trapped(arr): Returns the maximum amount of water trapped between two pillars in the fence. max_water = 0 left = 0 right = len(arr) - 1 while left < right: height = min(arr[left], arr[right]) width = right - left max_water = max(max_water, height * width) if arr[left] <= arr[right]: left += 1 else: right -= 1 return max_water
question:You are given a string `s` and an integer `k`. You can choose any characters from the string and remove them to form a new string. Return the length of the longest new string you can achieve that contains no more than `k` distinct characters.
answer:def longest_substring_with_k_distinct(s, k): Returns the length of the longest substring with at most k distinct characters. if k == 0 or not s: return 0 char_count = {} max_length = 0 left = 0 for right in range(len(s)): char_count[s[right]] = char_count.get(s[right], 0) + 1 while len(char_count) > k: char_count[s[left]] -= 1 if char_count[s[left]] == 0: del char_count[s[left]] left += 1 max_length = max(max_length, right - left + 1) return max_length