classSolution: defcountNegatives(self, grid: List[List[int]]) -> int: ifnot grid: return0 n_length = len(grid[0]) result = 0 for item in grid: for i inrange(n_length): if item[i] < 0: result += n_length - i break return result
1352. Product of the Last K Numbers
题面:
1 2 3 4 5 6 7 8 9 10
Implement the class ProductOfNumbers that supports two methods:
1. add(int num)
Adds the number num to the back of the current list of numbers. 2. getProduct(int k)
Returns the product of the last k numbers in the current list. You can assume that always the current list has at least k numbers. At any time, the product of any contiguous sequence of numbers will fit into a single 32-bit integer without overflowing.
Explanation ProductOfNumbers productOfNumbers = new ProductOfNumbers(); productOfNumbers.add(3); // [3] productOfNumbers.add(0); // [3,0] productOfNumbers.add(2); // [3,0,2] productOfNumbers.add(5); // [3,0,2,5] productOfNumbers.add(4); // [3,0,2,5,4] productOfNumbers.getProduct(2); // return 20. The product of the last 2 numbers is 5 * 4 = 20 productOfNumbers.getProduct(3); // return 40. The product of the last 3 numbers is 2 * 5 * 4 = 40 productOfNumbers.getProduct(4); // return 0. The product of the last 4 numbers is 0 * 2 * 5 * 4 = 0 productOfNumbers.add(8); // [3,0,2,5,4,8] productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers is 4 * 8 = 32
defgetProduct(self, k: int) -> int: if k in self._cache_result: return self._cache_result[k] cache_index = bisect.bisect(self._cache_index, k) - 1 if cache_index >= 0: last_k = self._cache_index[cache_index] result = self._cache_result[last_k] for i inrange(1, cache_index + 1): temp_last_k = last_k + i if temp_last_k >= len(self._value): break result *= self._value[-last_k] else: temp_value = ( self._value[-1 : -k - 1 : -1] if k <= len(self._value) else self._value ) result = reduce(mul, temp_value, 1) bisect.bisect_left(self._cache_index, k) self._cache_result[k] = result return result
1353. Maximum Number of Events That Can Be Attended
题面:
Given an array of events where events[i] = [startDayi, endDayi]. Every event i starts at startDayi and ends at endDayi. You can attend an event i at any day d where startTimei <= d <= endTimei. Notice that you can only attend one event at any time d. Return the maximum number of events you can attend.
示例
1 2 3 4 5 6 7
Input: events = [[1,2],[2,3],[3,4]] Output: 3 Explanation: You can attend all the three events. One way to attend them all is as shown. Attend the first event on day 1. Attend the second event on day 2. Attend the third event on day 3.
给定一个数组,数组中每个元素 x 代表一个活动,x[0], x[i] 代表该活动的起始与结束时间,一个用户一天只能参加一个活动,求用户最多能参加多少个活动。经典的一个贪心题目,首先对活动列表以结束时间进行排序,然后依次遍历每个时间,确认具体哪一天可以参加,整体时间复杂度为 O(max(nlogn,n*m))
classSolution: defisPossible(self, target: List[int]) -> bool: ifnot target: returnFalse total = sum(target) target = sorted([-x for x in target]) heapq.heapify(target) whileTrue: a = -heapq.heappop(target) total -= a if a == 1or total == 1: returnTrue if a < total or total == 0or a % total == 0: returnFalse a %= total total += a heapq.heappush(target, -a)