KK's blog

每天积累多一些

0%

LeetCode

<div>

You are given an m x n integer matrix points (0-indexed). Starting with 0 points, you want to maximize the number of points you can get from the matrix.

To gain points, you must pick one cell in each row. Picking the cell at coordinates (r, c) will add points[r][c] to your score.

However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows r and r + 1 (where 0 <= r < m - 1), picking cells at coordinates (r, c<sub>1</sub>) and (r + 1, c<sub>2</sub>) will subtract abs(c<sub>1</sub> - c<sub>2</sub>) from your score.

Return the maximum number of points you can achieve.

abs(x) is defined as:

  • x for x >= 0.
  • -x for x < 0.

Example 1:

<pre>Input: points = [[1,2,3],[1,5,1],[3,1,1]] Output: 9 Explanation: The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0). You add 3 + 5 + 3 = 11 to your score. However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score. Your final score is 11 - 2 = 9. </pre>

Example 2:

<pre>Input: points = [[1,5],[2,3],[4,2]] Output: 11 Explanation: The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0). You add 5 + 3 + 4 = 12 to your score. However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score. Your final score is 12 - 1 = 11. </pre>

Constraints:

  • m == points.length
  • n == points[r].length
  • 1 <= m, n <= 10<sup>5</sup>
  • 1 <= m * n <= 10<sup>5</sup>
  • 0 <= points[r][c] <= 10<sup>5</sup>

</div>

题目大意:

矩阵中含点数,每行取一个cell上的点数,但若两行之间的cell的列不同,要扣去列下标差,求最大点数

优化DP解题思路(推荐):

求数值的最大值,容易想到用DP,dp[i][j]定义为每个cell的累计最大点数,递归式为

1
dp[i][j] = max(dp[i - 1][k] - abs(j - k)) + points[i][j], k = 0..len(dp[0])
复杂度为n立方。

如果没有扣除的规则,其实就是找上一行的最大值,但要考虑下标,考虑怎么移除这个限制,若将上一个某个cell搬到跟目前列,就是dp[i - 1][k] - (j - k), 所以可以提前计算, 而且有绝对值,所以类似于LeetCode 042 Trapping Rain Water拆分为向左向右最大值: left[i]是该行第i个cell,上一行在该列左边的cell的累计最大点数(已扣除),同理 right[i]是该行第i个cell,上一行在该列右边的cell的累计最大点数(已扣除)

最后,上一行的最大值只能在左边或右边

1
dp[i][j] = max(left[j], right[j]) + points[i][j], k = 0..len(dp[0])

解题步骤:

N/A

注意事项:

  1. left[j], right[j]的引入

Python代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def maxPoints(self, points: List[List[int]]) -> int:
m, n = len(points), len(points[0])
dp = [[0 for _ in range(n)] for _ in range(m)]
for j in range(n):
dp[0][j] = points[0][j]
for i in range(1, m):
left, right = [0] * n, [0] * n
left[0], right[-1] = dp[i - 1][0], dp[i - 1][-1]
for j in range(1, n):
left[j] = max(dp[i - 1][j], left[j - 1] - 1)
for j in range(n - 2, -1, -1):
right[j] = max(dp[i - 1][j], right[j + 1] - 1)
for j in range(n):
dp[i][j] = points[i][j] + max(left[j], right[j])
return max(dp[-1])

算法分析:

时间复杂度为<code>O(n<sup>2</sup>)</code>,空间复杂度<code>O(n<sup>2</sup>)</code>


暴力DP算法II解题思路(不推荐):

Python代码:

1
2
3
4
5
6
7
8
9
def maxPoints2(self, points: List[List[int]]) -> int:
dp = [[0 for _ in range(len(points[0]))] for _ in range(len(points))]
for j in range(len(dp[0])):
dp[0][j] = points[0][j]
for i in range(1, len(dp)):
for j in range(len(dp[0])):
for k in range(len(dp[0])):
dp[i][j] = max(dp[i][j], dp[i - 1][k] + points[i][j] - abs(j - k))
return max(dp[-1])

算法分析:

时间复杂度为<code>O(n<sup>3</sup>)</code>,空间复杂度<code>O(n<sup>2</sup>)</code>

LeetCode

<div>

You are given a stream of records about a particular stock. Each record contains a timestamp and the corresponding price of the stock at that timestamp.

Unfortunately due to the volatile nature of the stock market, the records do not come in order. Even worse, some records may be incorrect. Another record with the same timestamp may appear later in the stream correcting the price of the previous wrong record.

Design an algorithm that:

  • Updates the price of the stock at a particular timestamp, correcting the price from any previous records at the timestamp.
  • Finds the latest price of the stock based on the current records. The latest price is the price at the latest timestamp recorded.
  • Finds the maximum price the stock has been based on the current records.
  • Finds the minimum price the stock has been based on the current records.

Implement the StockPrice class:

  • StockPrice() Initializes the object with no price records.
  • void update(int timestamp, int price) Updates the price of the stock at the given timestamp.
  • int current() Returns the latest price of the stock.
  • int maximum() Returns the maximum price of the stock.
  • int minimum() Returns the minimum price of the stock.

Example 1:

<pre>Input ["StockPrice", "update", "update", "current", "maximum", "update", "maximum", "update", "minimum"] [[], [1, 10], [2, 5], [], [], [1, 3], [], [4, 2], []] Output [null, null, null, 5, 10, null, 5, null, 2]

Explanation StockPrice stockPrice = new StockPrice(); stockPrice.update(1, 10); // Timestamps are [1] with corresponding prices [10]. stockPrice.update(2, 5); // Timestamps are [1,2] with corresponding prices [10,5]. stockPrice.current(); // return 5, the latest timestamp is 2 with the price being 5. stockPrice.maximum(); // return 10, the maximum price is 10 at timestamp 1. stockPrice.update(1, 3); // The previous timestamp 1 had the wrong price, so it is updated to 3. // Timestamps are [1,2] with corresponding prices [3,5]. stockPrice.maximum(); // return 5, the maximum price is 5 after the correction. stockPrice.update(4, 2); // Timestamps are [1,2,4] with corresponding prices [3,5,2]. stockPrice.minimum(); // return 2, the minimum price is 2 at timestamp 4. </pre>

Constraints:

  • 1 <= timestamp, price <= 10<sup>9</sup>
  • At most 10<sup>5</sup> calls will be made in total to update, current, maximum, and minimum.
  • current, maximum, and minimum will be called only after update has been called at least once.

</div>

题目大意:

实现一个关于股票的数据结构,可以更新时间点对应的股价,最大最小值,最新价格

解题思路:

求最大最小值容易想到用heap,但heap不支持更新,难点是怎么支持更新股价。 仍然(price, timestamp)加入到heap中,在出堆时验证

解题步骤:

N/A

注意事项:

  1. 验证堆顶: 若股价和时间不匹配(用time_to_price验证),表示这是stale股价,不断去掉,直到验证成功为止,最后加入到堆中

Python代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class StockPrice(TestCases):

def __init__(self):
self.time_to_price = {}
self.cur_time = 0
self.min_heap = []
self.max_heap = []

def update(self, timestamp: int, price: int) -> None:
self.time_to_price[timestamp] = price
self.cur_time = max(self.cur_time, timestamp)
heapq.heappush(self.min_heap, (price, timestamp))
heapq.heappush(self.max_heap, (-price, timestamp))

def current(self) -> int:
return self.time_to_price[self.cur_time]

def maximum(self) -> int:
price, timestamp = heapq.heappop(self.max_heap)
while -price != self.time_to_price[timestamp]:
price, timestamp = heapq.heappop(self.max_heap)
heapq.heappush(self.max_heap, (price, timestamp))
return -price

def minimum(self) -> int:
price, timestamp = heapq.heappop(self.min_heap)
while price != self.time_to_price[timestamp]:
price, timestamp = heapq.heappop(self.min_heap)
heapq.heappush(self.min_heap, (price, timestamp))
return price

算法分析:

update时间复杂度为O(logn),空间复杂度O(1)

LeetCode

<div>

An integer array original is transformed into a doubled array changed by appending twice the value of every element in original, and then randomly shuffling the resulting array.

Given an array changed, return original if changed is a doubled array. If changed is not a doubled array, return an empty array. The elements in original may be returned in any order.

Example 1:

<pre>Input: changed = [1,3,4,2,6,8] Output: [1,3,4] Explanation: One possible original array could be [1,3,4]:

  • Twice the value of 1 is 1 * 2 = 2.
  • Twice the value of 3 is 3 * 2 = 6.
  • Twice the value of 4 is 4 * 2 = 8. Other original arrays could be [4,3,1] or [3,1,4]. </pre>

Example 2:

<pre>Input: changed = [6,3,0,1] Output: [] Explanation: changed is not a doubled array. </pre>

Example 3:

<pre>Input: changed = [1] Output: [] Explanation: changed is not a doubled array. </pre>

Constraints:

  • 1 <= changed.length <= 10<sup>5</sup>
  • 0 <= changed[i] <= 10<sup>5</sup>

</div>

题目大意:

给定一个数组,求这个数组是否可以分成两部分,后一部分的每个元素是否前一部分某元素的两倍

解题思路:

由最大值容易确定它的一半是否在数组中。所以排序后由大到小遍历。注意数组元素可能相等,所以不能用visited set来记录已用过的数,val_to_index也不支持重复,只有val_to_count支持

解题步骤:

N/A

注意事项:

  1. 用val_to_count,注意遍历时候就要减去,不要进入if才减去

Python代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def findOriginalArray(self, changed: List[int]) -> List[int]:
if len(changed) % 2 == 1:
return []
changed.sort()
res = []
val_to_count = collections.Counter(changed)
for i in reversed(range(len(changed))):
if val_to_count[changed[i]] == 0:
continue
val_to_count[changed[i]] -= 1 # not in if statement
if changed[i] / 2 in val_to_count and val_to_count[changed[i] / 2] > 0:
val_to_count[changed[i] / 2] -= 1
res.append(int(changed[i] / 2))
return [] if len(res) * 2 != len(changed) else res

算法分析:

时间复杂度为O(nlogn),空间复杂度O(n)

LeetCode

<div>

You are given an m x n binary matrix grid.

In one operation, you can choose any row or column and flip each value in that row or column (i.e., changing all 0's to 1's, and all 1's to 0's).

Return true if it is possible to remove all 1's from grid using any number of operations or false otherwise.

Example 1:

<pre>Input: grid = [[0,1,0],[1,0,1],[0,1,0]] Output: true Explanation: One possible way to remove all 1's from grid is to:

  • Flip the middle row
  • Flip the middle column </pre>

Example 2:

<pre>Input: grid = [[1,1,0],[0,0,0],[0,0,0]] Output: false Explanation: It is impossible to remove all 1's from grid. </pre>

Example 3:

<pre>Input: grid = [[0]] Output: true Explanation: There are no 1's in grid. </pre>

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 300
  • grid[i][j] is either 0 or 1.

</div>

题目大意:

给定一个矩阵,每次可以flip一行或一列,求是否可以令矩阵变成全0

解题思路:

从例子找规律, 010和010属于一种类型 010和101也是同一种,每一行必须符合任何一种类型才是解

解题步骤:

N/A

注意事项:

Python代码:

1
2
3
4
5
6
def removeOnes(self, grid: List[List[int]]) -> bool:
row_patten, row_pattern_invert = grid[0], [1 - n for n in grid[0]]
for i in range(1, len(grid)):
if grid[i] != row_patten and grid[i] != row_pattern_invert:
return False
return True

算法分析:

时间复杂度为<code>O(n<sup>2</sup>)</code>,空间复杂度O(n)

LeetCode

<div>

You are given a 0-indexed string s that you must perform k replacement operations on. The replacement operations are given as three 0-indexed parallel arrays, indices, sources, and targets, all of length k.

To complete the i<sup>th</sup> replacement operation:

  1. Check if the substring sources[i] occurs at index indices[i] in the original string s.
  2. If it does not occur, do nothing.
  3. Otherwise if it does occur, replace that substring with targets[i].

For example, if s = "<u>ab</u>cd", indices[i] = 0, sources[i] = "ab", and targets[i] = "eee", then the result of this replacement will be "<u>eee</u>cd".

All replacement operations must occur simultaneously, meaning the replacement operations should not affect the indexing of each other. The testcases will be generated such that the replacements will not overlap.

  • For example, a testcase with s = "abc", indices = [0, 1], and sources = ["ab","bc"] will not be generated because the "ab" and "bc" replacements overlap.

Return the resulting string after performing all replacement operations on s.

A substring is a contiguous sequence of characters in a string.

Example 1:

<pre>Input: s = "abcd", indices = [0, 2], sources = ["a", "cd"], targets = ["eee", "ffff"] Output: "eeebffff" Explanation: "a" occurs at index 0 in s, so we replace it with "eee". "cd" occurs at index 2 in s, so we replace it with "ffff". </pre>

Example 2:

<pre>Input: s = "abcd", indices = [0, 2], sources = ["ab","ec"], targets = ["eee","ffff"] Output: "eeecd" Explanation: "ab" occurs at index 0 in s, so we replace it with "eee". "ec" does not occur at index 2 in s, so we do nothing. </pre>

Constraints:

  • 1 <= s.length <= 1000
  • k == indices.length == sources.length == targets.length
  • 1 <= k <= 100
  • 0 <= indexes[i] < s.length
  • 1 <= sources[i].length, targets[i].length <= 50
  • s consists of only lowercase English letters.
  • sources[i] and targets[i] consist of only lowercase English letters.

</div>

题目大意:

整洁题。找到位置,然后验证,最后替换

解题思路:

N/A

解题步骤:

N/A

注意事项:

  1. i是循环外的变量,所以poplate index_dict注意不能重名

Python代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def findReplaceString(self, s: str, indices: List[int], sources: List[str], targets: List[str]) -> str:
res = ''
index_dict = {}
for _i, _n in enumerate(indices):
index_dict[_n] = _i # 0 -> 0, 2 -> 1
i = 0
while i < len(s):
if i in index_dict and s[i:i + len(sources[index_dict[i]])] == sources[index_dict[i]]:
res += targets[index_dict[i]]
i += len(sources[index_dict[i]])
else:
res += s[i]
i += 1
return res

算法分析:

时间复杂度为O(n),空间复杂度O(n)

Free mock interview