Weekly Contest 119
第一题:
973. K Closest Points to Origin
points on the plane. Find the K closest points to the origin (0, 0).(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000-10000 < points[i][0] < 10000-10000 < points[i][1] < 10000
题目大意:给你一些点,让你找离远点最近的K个点。主要考的是二维数组排序。
class Solution {
public:
vector<vector<int> > kClosest(vector<vector<int> >& points, int K) {
vector<vector<int> > ans;
int len = points.size();
for(int i=; i<len; i++) {
int x = points[i][];
int y = points[i][];
points[i].push_back(x*x+y*y);
}
sort(points.begin(), points.end(), [](const vector<int> &a, const vector<int> &b) { return a[] < b[]; });
for(int i=; i<K; i++) {
vector<int> res;
res.push_back(points[i][]);
res.push_back(points[i][]);
ans.push_back(res);
}
return ans;
}
};
第二题:
976. Largest Perimeter Triangle
A of positive lengths, return the largest perimeter of a triangle with non-zero area, formed from 3 of these lengths.If it is impossible to form any triangle of non-zero area, return 0.
Example 1:
Input: [2,1,2]
Output: 5
Example 2:
Input: [1,2,1]
Output: 0
Example 3:
Input: [3,2,3,4]
Output: 10
Example 4:
Input: [3,6,2,3]
Output: 8
Note:
3 <= A.length <= 100001 <= A[i] <= 10^6
题目大意:从数组中,找出三个点组成一个周长最大的三角形,然后输出周长。
数据比较小,直接暴力的。
class Solution {
public:
bool ok(int a, int b, int c) {
return a-b<c;
}
int largestPerimeter(vector<int>& A) {
int len = A.size();
sort(A.begin(), A.end());
for(int i=len-; i>=; i--) {
for(int j=i-; j>=; j--) {
if( ok(A[i], A[j], A[j-])) {
return A[i]+A[j]+A[j-];
}
}
}
return ;
}
};
第三题:
974. Subarray Sums Divisible by K
A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000-10000 <= A[i] <= 100002 <= K <= 10000
题目大意:统计有多少条满足条件的子序列。条件1:连续,必须是连续子序列,条件2:序列和可以整除K。
思路:求出前缀和,想要找到连续子序列之和是可以整除K的话,那么前缀和只差模K也就是为0,也就是说统计模K相等的前缀和之差的个数,求一个等差数列求和。
做题的时候开始考虑错了,后面想到了解决方法但是没时间了,对照rank写出了一个我自己都认为是错的程序,提交AC。想不清楚,等过几天想清楚了,在仔细写下吧。
class Solution {
public:
int subarraysDivByK(vector<int>& A, int K) {
int a[K];
memset(a, , sizeof(a));
int len = A.size(); a[] = ;
int res = ;
for(int i=; i<len; i++) {
A[i] += A[i-];
int cnt = (A[i]%K+K)%K;
a[cnt] ++;
}
for(int i=; i<K; i++) {
res += (a[i]-)*a[i]/;
}
return res;
}
};
Weekly Contest 119的更多相关文章
- 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 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...
随机推荐
- less 基础+ flex
1.less 中的变量 @ 符号 引入 /*普通变量*/ @color:pinker; .styles{ color:@color; } /*选择器变量*/ @I:img; @{I}{ width: ...
- python语法_字符编码
二进制: ascll:只能存英文和拉听字符,一个字符占一个字节,8位 gb2312:只能存6700多个中文,1980年 gbk1.0:能存2万多字符,1995年 gbk18030:2000 27000 ...
- bitmap 合并图片
把两张bitmap覆盖合成为一张图 /** * 把两个位图覆盖合成为一个位图,以底层位图的长宽为基准 * @param backBitmap 在底部的位图 * @param frontBitmap 盖 ...
- Vs Code 中文包设置
首先打开Vs Code 然后点击扩展 下载中文包 安装中文包 在没打开任何文件的时候我们可以看到一些提示 这个时候使用快捷键 Ctrl + Shift + P (显示所有命令),然后选择" ...
- Spring Boot2.1.3全局跨域
/** * 配置跨域访问 * * @author Terwer */ @Bean public WebMvcConfigurer corsConfigurer() { return new WebMv ...
- nginx的proxy_redirect
proxy_redirect 语法:proxy_redirect [ default|off|redirect replacement ]; 默认:proxy_redirect default; 配置 ...
- RF - selenium - 常用关键字 - 示例
1. 打开浏览器 Open Browser http://www.baidu.com chrome 2. 关闭浏览器 Close Browsers Close All Browser 3. ...
- ASA failover
Active-Standby 1.作用:提供设备冗余 2.物理概念:primary 和 secondary ,需要命令敲得,角色不会切换, 3.虚拟概念:active和standby ,需要选举,角色 ...
- python学习笔记3-列表
# 1.列表长度可变,内容可修改 a = [0,1,2,3] a[0] = 'a0' a # ['a0', 1, 2, 3] # 2.添加元素 # 2.1列表末尾添加元素 a.append(4) a ...
- B. Lynyrd Skynyrd
传送门: 题意:给出 n,m,q 然后给出模板串,从1-n数字只出现一次,然后给出长度为m的要询问的串. q组询问:每组询问输出 ‘1’或者‘0’ 每组询问 一对x,y 问在x到y中有没有模板串 ...