笔记仓库

正常人的正常笔记集

LeetCode Weekly Contest 133

https://leetcode.com/contest/weekly-contest-133

Matrix Cells in Distance Order

We are given a matrix with R rows and C columns has cells with integer coordinates (r, c), where 0 <= r < R and 0 <= c < C.

Additionally, we are given a cell in that matrix with coordinates (r0, c0).

Return the coordinates of all cells in the matrix, sorted by their distance from (r0, c0) from smallest distance to largest distance. Here, the distance between two cells (r1, c1) and (r2, c2) is the Manhattan distance, |r1 - r2| + |c1 - c2|. (You may return the answer in any order that satisfies this condition.)

Example 1:

1
2
3
Input: R = 1, C = 2, r0 = 0, c0 = 0
Output: [[0,0],[0,1]]
Explanation: The distances from (r0, c0) to other cells are: [0,1]

Example 2:

1
2
3
4
Input: R = 2, C = 2, r0 = 0, c0 = 1
Output: [[0,1],[0,0],[1,1],[1,0]]
Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2]
The answer [[0,1],[1,1],[0,0],[1,0]] would also be accepted as correct.

Example 3:

1
2
3
4
Input: R = 2, C = 3, r0 = 1, c0 = 2
Output: [[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]
Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2,2,3]
There are other answers that would also be accepted as correct, such as [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]].

Note:

  1. 1 <= R <= 100
  2. 1 <= C <= 100
  3. 0 <= r0 < R
  4. 0 <= c0 < C

按照Manhattan距离从小到大去遍历矩阵上的点坐标,有点BFS的意思,这里可以early return,不过我懒得写了就这样写也可以通过

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
class Solution(object):
def allCellsDistOrder(self, R, C, r0, c0):
"""
:type R: int
:type C: int
:type r0: int
:type c0: int
:rtype: List[List[int]]
"""
res = []
for dist in range(R + C):
for r in range(dist + 1):
c = dist - r
if 0 <= r0 + r < R:
if 0 <= c0 + c < C:
res.append([r0 + r, c0 + c])
if c != 0 and 0 <= c0 - c < C:
res.append([r0 + r, c0 - c])
if r != 0 and 0 <= r0 - r < R:
if 0 <= c0 + c < C:
res.append([r0 - r, c0 + c])
if c != 0 and 0 <= c0 - c < C:
res.append([r0 - r, c0 - c])

return res

Two City Scheduling

原题地址 https://leetcode.com/contest/weekly-contest-133/problems/two-city-scheduling/

There are 2N people a company is planning to interview. The cost of flying the i-th person to city A is costs[i][0], and the cost of flying the i-th person to city B is costs[i][1].

Return the minimum cost to fly every person to a city such that exactly N people arrive in each city.

Example 1:

1
2
3
4
5
6
7
8
9
Input: [[10,20],[30,200],[400,50],[30,20]]
Output: 110
Explanation:
The first person goes to city A for a cost of 10.
The second person goes to city A for a cost of 30.
The third person goes to city B for a cost of 50.
The fourth person goes to city B for a cost of 20.

The total minimum cost is 10 + 30 + 50 + 20 = 110 to have half the people interviewing in each city.

Note:

  1. 1 <= costs.length <= 100
  2. It is guaranteed that costs.length is even.
  3. 1 <= costs[i][0], costs[i][1] <= 1000

先把costs按照costs[i][1]-costs[i][0]的大小排序,表示每个iA地比去B地更节省成本的程度,所以前N个人安排在B地,后N个人安排在A地。

1
2
3
4
5
6
7
8
9
10
class Solution(object):
def twoCitySchedCost(self, costs):
"""
:type costs: List[List[int]]
:rtype: int
"""
n = len(costs)
costs.sort(key=lambda (a, b): b - a)

return sum(b for [a, b] in costs[:n / 2]) + sum(a for [a, b] in costs[n / 2:])

Maximum Sum of Two Non-Overlapping Subarrays

原题地址 https://leetcode.com/contest/weekly-contest-133/problems/maximum-sum-of-two-non-overlapping-subarrays/

Given an array A of non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengths L and M. (For clarification, the L-length subarray could occur before or after the M-length subarray.)

Formally, return the largest V for which V = (A[i] + A[i+1] + ... + A[i+L-1]) + (A[j] + A[j+1] + ... + A[j+M-1]) and either:

  1. 0 <= i < i + L - 1 < j < j + M - 1 < A.length, or
  2. 0 <= j < j + M - 1 < i < i + L - 1 < A.length.

Example 1:

1
2
3
Input: A = [0,6,5,2,2,5,1,9,4], L = 1, M = 2
Output: 20
Explanation: One choice of subarrays is [9] with length 1, and [6,5] with length 2.

Example 2:

1
2
3
Input: A = [3,8,1,3,2,1,8,9,0], L = 3, M = 2
Output: 29
Explanation: One choice of subarrays is [3,8,1] with length 3, and [8,9] with length 2.

Example 3:

1
2
3
Input: A = [2,1,5,6,0,9,5,0,3,8], L = 4, M = 3
Output: 31
Explanation: One choice of subarrays is [5,6,0,9] with length 4, and [3,8] with length 3.

Note:

  1. L >= 1
  2. M >= 1
  3. L + M <= A.length <= 1000
  4. 0 <= A[i] <= 1000

用滑动窗口找最大和,不多说了,就是情况分的比较多容易遗漏。

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Solution(object):
def maxSumTwoNoOverlap(self, A, L, M):
"""
:type A: List[int]
:type L: int
:type M: int
:rtype: int
"""
n = len(A)
start = sum(A[:L + M]) # |--L--|--M--|--|
res = start

temp = start
for i in range(L + M, n): # |--|--L--|--M--|--|
temp -= A[i - L - M]
temp += A[i]
res = max(res, temp)

temp = start
for dist in range(1, n - L - M + 1): # |--L--|--dist--|--M--|--|
temp -= A[L + dist - 1]
temp += A[L + dist + M - 1]
res = max(res, temp)
slide = temp
for i in range(n - L - M - dist): # |--|--L--|--dist--|--M--|--|
slide -= A[i]
slide += A[i + L]
slide -= A[i + L + dist]
slide += A[i + L + M + dist]
res = max(res, slide)

if L == M:
return res

temp = start
for dist in range(1, n - L - M + 1): # |--M--|--dist--|--L--|--|
temp -= A[M + dist - 1]
temp += A[M + dist + L - 1]
res = max(res, temp)
slide = temp
for i in range(n - L - M - dist): # |--|--M--|--dist--|--L--|--|
slide -= A[i]
slide += A[i + M]
slide -= A[i + M + dist]
slide += A[i + M + L + dist]
res = max(res, slide)

return res

Stream of Characters

原题地址 https://leetcode.com/contest/weekly-contest-133/problems/stream-of-characters/

Implement the StreamChecker class as follows:

  • StreamChecker(words): Constructor, init the data structure with the given words.
  • query(letter): returns true if and only if for some k >= 1, the last k characters queried (in order from oldest to newest, including this letter just queried) spell one of the words in the given list.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
StreamChecker streamChecker = new StreamChecker(["cd","f","kl"]); // init the dictionary.
streamChecker.query('a'); // return false
streamChecker.query('b'); // return false
streamChecker.query('c'); // return false
streamChecker.query('d'); // return true, because 'cd' is in the wordlist
streamChecker.query('e'); // return false
streamChecker.query('f'); // return true, because 'f' is in the wordlist
streamChecker.query('g'); // return false
streamChecker.query('h'); // return false
streamChecker.query('i'); // return false
streamChecker.query('j'); // return false
streamChecker.query('k'); // return false
streamChecker.query('l'); // return true, because 'kl' is in the wordlist

Note:

  1. 1 <= words.length <= 2000
  2. 1 <= words[i].length <= 2000
  3. Words will only consist of lowercase English letters.
  4. Queries will only consist of lowercase English letters.
  5. The number of queries is at most 40000.

本来应该用trie tree做的,发现暴力穷举也可以AC就懒了,用最直接粗暴的方法

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 StreamChecker(object):

def __init__(self, words):
"""
:type words: List[str]
"""
self.d = collections.defaultdict(set)
self.maxlen = dict()
self.q = ""
for w in words:
self.d[w[-1]].add(w)
self.maxlen[w[-1]] = max(self.maxlen.get(w[-1], 0), len(w))

def query(self, letter):
"""
:type letter: str
:rtype: bool
"""
self.q = self.q + letter
if letter not in self.maxlen:
return False
for i in range(1, self.maxlen[letter] + 1):
if self.q[-i:] in self.d[letter]:
return True
return False


# Your StreamChecker object will be instantiated and called as such:
# obj = StreamChecker(words)
# param_1 = obj.query(letter)