Weekly Contest 133
1030. Matrix Cells in Distance Order
We are given a matrix with
Rrows andCcolumns has cells with integer coordinates(r, c), where0 <= r < Rand0 <= 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]].
Note:
1 <= R <= 1001 <= C <= 1000 <= r0 < R0 <= c0 < C
Approach #1:
class Solution {
public:
vector<vector<int>> allCellsDistOrder(int R, int C, int r0, int c0) {
map<int, vector<pair<int, int>>> m;
for (int i = 0; i < R; ++i) {
for (int j = 0; j < C; ++j) {
int dis = abs(i - r0) + abs(j - c0);
m[dis].push_back({i, j});
}
}
int index = 0;
vector<vector<int>> ret = vector(R*C, vector<int>(2));
map<int, vector<pair<int, int>>>::iterator it;
for (it = m.begin(); it != m.end(); ++it) {
vector<pair<int, int>> temp = it->second;
for (int i = 0; i < temp.size(); ++i) {
ret[index][0] = temp[i].first;
ret[index][1] = temp[i].second;
index++;
}
}
return ret;
}
};
1029. Two City Scheduling
There are
2Npeople a company is planning to interview. The cost of flying thei-th person to cityAiscosts[i][0], and the cost of flying thei-th person to cityBiscosts[i][1].Return the minimum cost to fly every person to a city such that exactly
Npeople arrive in each city.
Example 1:
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 <= costs.length <= 100- It is guaranteed that
costs.lengthis even.1 <= costs[i][0], costs[i][1] <= 1000
Approach #1:
class Solution {
public int twoCitySchedCost(int[][] costs) {
int N = costs.length / 2;
int[][] dp = new int[N+1][N+1];
for (int i = 1; i <= N; ++i) {
dp[i][0] = dp[i-1][0] + costs[i-1][0];
}
for (int i = 1; i <= N; ++i) {
dp[0][i] = dp[0][i-1] + costs[i-1][1];
}
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= N; ++j) {
dp[i][j] = Math.min(dp[i-1][j] + costs[i+j-1][0], dp[i][j-1] + costs[i+j-1][1]);
}
}
return dp[N][N];
}
}
1031. Maximum Sum of Two Non-Overlapping Subarrays
Given an array
Aof non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengthsLandM. (For clarification, theL-length subarray could occur before or after theM-length subarray.)Formally, return the largest
Vfor whichV = (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, or0 <= 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.
Note:
L >= 1M >= 1L + M <= A.length <= 10000 <= A[i] <= 1000
Approach #1:
class Solution {
public int maxSumTwoNoOverlap(int[] A, int L, int M) {
for (int i = 1; i < A.length; ++i) {
A[i] += A[i-1];
}
int res = A[L+M-1], Lmax = A[L-1], Mmax = A[M-1];
for (int i = L + M; i < A.length; ++i) {
Lmax = Math.max(Lmax, A[i-M] - A[i-M-L]);
Mmax = Math.max(Mmax, A[i-L] - A[i-M-L]);
res = Math.max(res, Math.max(Lmax + A[i] - A[i-M], Mmax + A[i] - A[i-L]));
}
return res;
}
}
1032. Stream of Characters
Implement the
StreamCheckerclass as follows:
StreamChecker(words): Constructor, init the data structure with the given words.query(letter): returns true if and only if for somek >= 1, the lastkcharacters queried (in order from oldest to newest, including this letter just queried) spell one of the words in the given list.
Example:
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 <= words.length <= 20001 <= words[i].length <= 2000- Words will only consist of lowercase English letters.
- Queries will only consist of lowercase English letters.
- The number of queries is at most 40000.
Approach #1:
class StreamChecker {
public class TriNode {
boolean isEnd = false;
TriNode[] next = new TriNode[26];
}
TriNode root = new TriNode();
StringBuilder buf = new StringBuilder();
void insert(String word) {
TriNode temp = root;
for (int i = 0; i < word.length(); ++i) {
char ch = word.charAt(word.length()-i-1);
if (temp.next[ch-'a'] == null) temp.next[ch-'a'] = new TriNode();
temp = temp.next[ch-'a'];
}
temp.isEnd = true;
}
public StreamChecker(String[] words) {
for (String word : words) {
insert(word);
}
}
public boolean query(char letter) {
buf.append(letter);
// System.out.println(buf.toString());
TriNode p = root;
for (int i = buf.length() - 1; i >= 0; --i) {
char ch = buf.charAt(i);
p = p.next[ch-'a'];
if (p == null) return false;
if (p.isEnd) return true;
}
return false;
}
}
/**
* Your StreamChecker object will be instantiated and called as such:
* StreamChecker obj = new StreamChecker(words);
* boolean param_1 = obj.query(letter);
*/
Weekly Contest 133的更多相关文章
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- LeetCode之Weekly Contest 91
第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- LeetCode Weekly Contest 47
闲着无聊参加了这个比赛,我刚加入战场的时候时间已经过了三分多钟,这个时候已经有20多个大佬做出了4分题,我一脸懵逼地打开第一道题 665. Non-decreasing Array My Submis ...
- 75th LeetCode Weekly Contest Champagne Tower
We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so ...
- LeetCode之Weekly Contest 102
第一题:905. 按奇偶校验排序数组 问题: 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...
随机推荐
- kbmmw 5.0 中的REST 服务
目前关于REST 服务的话题越来越热,kbmmw 在5.0 里面开始支持rest.今天我就试一下kbmmw 的 rest 服务.闲话少说,开始. 老规矩,放上两个kbmMWServer1和 kbmMW ...
- 【转】ssh-copy-id帮你建立信任
本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc. == 对于做运维的同学来说,给两台UNIX/Linux机器建立ssh信任关系是 ...
- 【转】如何用Redis做LRU-Cache
LRU(Least Recently Used)最近最少使用算法是众多置换算法中的一种. Redis中有一个maxmemory概念,主要是为了将使用的内存限定在一个固定的大小.Redis用到的LRU ...
- Python图表绘制:matplotlib绘图库入门(转)
matplotlib 是Python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地行制图.而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中. 它的文档相当完备,并 ...
- Java核心技术之基础知识
一.类型转换 数值类型之间的转换 强制类型转换 a) 将一个数值强制转换成另一种类型时,如果超出目标类型的便是范围,结果就会截断成一个完全不同的值.(如:(byte)300的实际值为44) ...
- 类型转化&WCF不同binding的区别
需要使用队列时并且涉及多线程时使用ConcurrentQueue 这个性内比自己使用Queue并且配合lock要好很多 calcFactory = new ChannelFactory<ICal ...
- C语言三种方法调用数组
#include <stdio.h> /********************************* * 方法1: 第一维的长度可以不指定 * * 但必须指定第二维的长度 * *** ...
- Object.create() 创建实例对象
var person1 = { name: '张三', age: 38, greeting: function() { console.log('Hi! I\'m ' + this.name + '. ...
- ArcGIS 关于Web_Mercator
#小知识#EPSG,即 European Petroleum Standards Group 欧洲石油标准组织 在ArcGIS 10中Web Mercator有三种EPSG编号.他们分别是EPSG38 ...
- Ng第三课:线性代数回顾(Linear Algebra Review)
3.1 矩阵和向量 3.2 加法和标量乘法 3.3 矩阵向量乘法 3.4 矩阵乘法 3.5 矩阵乘法的性质 3.6 逆.转置 3.1 矩阵和向量 如图:这个是 4×2 矩阵,即 4 行 ...