LeetCode - Weekly Contest 133

1029. Two City Scheduling - Easy
1030. Matrix Cells in Distance Order - Easy
1031. Maximum Sum of Two Non-Overlapping Subarrays - Medium
1032. Stream of Characters - Hard

1029. 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:
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:
  It is guaranteed that costs.length is even.

Solution:
  一开始没想明白怎么算,一直想dp,但是没想明白,就直接二叉树dfs了,但是TLE了。其实就是用两个cost相减,对这个相减后的数组排序,前一半去一个城市,后一半去另一个。

int twoCitySchedCost(vector<vector<int>>& costs) {
	int min_cost = 0;
	sort(costs.begin(), costs.end(), [](const vector<int>& a, const vector<int>& b) { return a[0] - a[1] < b[0] - b[1]; });
	// 前一半去A,后一半去B
	for (int i = 0; i < costs.size() / 2; ++i)            min_cost += costs[i][0];
	for (int i = costs.size() / 2; i < costs.size(); ++i) min_cost += costs[i][1];
	return min_cost;
}

1030. 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:
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:
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:
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]].

Solution:
  其实就是bfs就行。

vector<vector<int>> allCellsDistOrder(int R, int C, int r0, int c0) {
	vector<vector<int>> res;
	queue<pair<int, int>> q;
	vector<vector<bool>> visited(R, vector<bool>(C, false));
	visited[r0][c0] = true;
	q.emplace(r0, c0);
	while(!q.empty()) {
		auto t = q.front();
		q.pop();
		res.push_back(vector<int>{ t.first, t.second });
		if (t.first + 1 < R && !visited[t.first + 1][t.second])	{
			visited[t.first + 1][t.second] = true;
			q.emplace(t.first + 1, t.second);
		}
		if (t.first - 1 >= 0 && !visited[t.first - 1][t.second]) {
			visited[t.first - 1][t.second] = true;
			q.emplace(t.first - 1, t.second);
		}
		if (t.second + 1 < C && !visited[t.first][t.second + 1]) {
			visited[t.first][t.second + 1] = true;
			q.emplace(t.first, t.second + 1);
		}
		if (t.second - 1 >= 0 && !visited[t.first][t.second - 1]) {
			visited[t.first][t.second - 1] = true;
			q.emplace(t.first, t.second - 1);
		}
	}
	return res;
}

1031. 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:
  0 <= i < i + L - 1 < j < j + M - 1 < A.length, or 0 <= j < j + M - 1 < i < i + L - 1 < A.length.
Example 1:
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:
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:
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.

Solution:
  用prefixSum的技巧,计算L长度的数组,每次再计算M个数在左边,M个数在右边所有情况中M个数最大和,和这一次的L个数的和求和,循环求出max_sum。

int maxSumTwoNoOverlap(const vector<int>& A, int L, int M) {
	vector<int> prefixSum{ 0 };	// 前边 i 个数的和
	int sum = 0, max_sum = 0;
	for (const auto& a : A) {
		sum += a;
		prefixSum.push_back(sum);
	}
	for(int i = L; i <= A.size(); ++i) {
		int L_sum = prefixSum[i] - prefixSum[i - L], M_sum = 0;

		// 如果 M 个数在这 L 个数的左边
		for (int j = M; j < i - L + 1; ++j)
			M_sum = max(M_sum, prefixSum[j] - prefixSum[j - M]);

		// 如果 M 个数在 L 个数的右边
		for (int j = i + M; j <= A.size(); ++j)
			M_sum = max(M_sum, prefixSum[j] - prefixSum[j - M]);

		max_sum = max(max_sum, M_sum + L_sum);
	}
	return max_sum;
}

1032. Stream of Characters

  看懂了再写吧,k是什么没懂,最近查询的k个,但是数据结构里或者函数参数里不是都没有k么。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值