Appearance
question:You are given a list of strings `words` where each string consists of lowercase letters. A **word ladder** is a sequence of words `w1, w2, ..., wk` such that each word in the sequence has exactly one letter different from the previous word. The first word `w1` is provided, and the final word `wk` is the target word to find. Return the length of the shortest transformation sequence from the start word to the target word. If no such sequence exists, return 0.
answer:from collections import deque def word_ladder_length(start: str, target: str, words: list) -> int: Returns the length of the shortest transformation sequence from start to target word. If no such sequence exists, return 0. if target not in words: return 0 word_set = set(words) queue = deque([(start, 1)]) # Each element in the queue is a tuple (current_word, current_length) while queue: current_word, current_length = queue.popleft() if current_word == target: return current_length # Try changing each character in the current word to every possible lowercase letter for i in range(len(current_word)): for c in 'abcdefghijklmnopqrstuvwxyz': next_word = current_word[:i] + c + current_word[i+1:] if next_word in word_set: word_set.remove(next_word) queue.append((next_word, current_length + 1)) return 0
question:A company's finance department needs a program to manage and monitor transactions. You are given an integer array `transactions` where `transactions[i]` represents an amount deposited or withdrawn. Implement a system where you can perform the following operations: - `TransactionSystem(int[] transactions)`: Initializes the system with the given list of transactions. - `void addTransaction(int amount)`: Adds a new transaction to the end of the transactions list. - `int currentBalance()`: Returns the current balance which is the sum of all transactions in the list. - `int maxTransaction()`: Returns the maximum transaction amount recorded. Design and implement the `TransactionSystem` class accordingly.
answer:class TransactionSystem: def __init__(self, transactions): Initializes the system with the given list of transactions. self.transactions = transactions def addTransaction(self, amount): Adds a new transaction to the end of the transactions list. self.transactions.append(amount) def currentBalance(self): Returns the current balance which is the sum of all transactions in the list. return sum(self.transactions) def maxTransaction(self): Returns the maximum transaction amount recorded. return max(self.transactions)
question:You are given an array of integers representing the number of tokens you can collect in a game. Each token grants you a certain number of points. You can perform one of two actions on a token: 1. You can spend a token at position `i` to gain `points[i]` points. 2. You can discard a token at position `i` to move to the next token at position `i + 1`. However, there is a constraint: once you discard a token, you must discard at least one of the following tokens before you can spend another token. For example, if you discard the token at position `i`, you must also discard the token at position `i + 1` before you can spend the token at position `i + 2`. Write a function that calculates the maximum number of points you can accumulate by optimally choosing to spend or discard tokens according to the given rules. **Input:** - An array of integers `points` where `points[i]` represents the points gained from spending the token at position `i`. **Output:** - An integer representing the maximum points that can be accumulated.
answer:def max_points(points): n = len(points) if n == 0: return 0 elif n == 1: return points[0] dp = [0] * n dp[0] = points[0] if n > 1: dp[1] = max(points[0], points[1]) for i in range(2, n): dp[i] = max(dp[i - 1], points[i] + dp[i - 2]) return dp[-1]
question:Given a matrix `mat` where each row is sorted in non-decreasing order, return the **smallest common element** in all rows. If there is no common element, return `-1`. For example, if `mat` is: ``` [ [1, 2, 3, 4, 5], [2, 4, 5, 8, 10], [3, 5, 7, 9, 11], [1, 3, 5, 7, 9] ] ``` The smallest common element in all rows is `5`. If no such element exists, return `-1`.
answer:def smallest_common_element(mat): Finds the smallest common element in all rows of the matrix. If no such element exists, returns -1. from collections import Counter # Flatten the matrix and count the occurrences of each element count = Counter() for row in mat: count.update(set(row)) # Use set to avoid counting duplicate elements in a single row # Find the smallest element that appears in all rows num_rows = len(mat) common_elements = [elem for elem, freq in count.items() if freq == num_rows] return min(common_elements) if common_elements else -1