Weekly Contest 119

第一题:

973. K Closest Points to Origin

We have a list of 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. 1 <= K <= points.length <= 10000
  2. -10000 < points[i][0] < 10000
  3. -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=0; i<len; i++) {
            int x = points[i][0];
            int y = points[i][1];
            points[i].push_back(x*x+y*y);
        }
        sort(points.begin(), points.end(), [](const vector<int> &a, const vector<int> &b) { return a[2] < b[2]; });
        for(int i=0; i<K; i++) {
            vector<int> res;
            res.push_back(points[i][0]);
            res.push_back(points[i][1]);
            ans.push_back(res);
        }
        return ans;
    }
};

第二题:

976. Largest Perimeter Triangle

Given an array 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:

  1. 3 <= A.length <= 10000
  2. 1 <= 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-1; i>=0; i--) {
            for(int j=i-1; j>=0; j--) {
                if( ok(A[i], A[j], A[j-1])) {
                    return A[i]+A[j]+A[j-1];
                }
            }
        }
        return 0;
    }
};

第三题:

974. Subarray Sums Divisible by K

Given an array 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. 1 <= A.length <= 30000
  2. -10000 <= A[i] <= 10000
  3. 2 <= 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, 0, sizeof(a));
        int len = A.size(); a[0] = 1;
        int res = 0;
        for(int i=0; i<len; i++) {
            A[i] += A[i-1];
            int cnt = (A[i]%K+K)%K;
            a[cnt] ++;
        }
        for(int i=0; i<K; i++) {
            res += (a[i]-1)*a[i]/2;
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/Asimple/p/10262296.html

时间: 2024-07-31 05:35:03

Weekly Contest 119的相关文章

[Swift Weekly Contest 119]LeetCode975. 奇偶跳 | Odd Even Jump

You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps.

Leetcode Weekly Contest 86

Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个由整数组成的 N × N 矩阵,其中有多少个 3 × 3 的 "幻方" 子矩阵?(每个子矩阵都是连续的). 直接模拟即可,本来是签到题,由于粗心,浪费了时间. 1 class Solution { 2 public: 3 int numMagicSquaresInside(vector&l

LeetCode之Weekly Contest 93

第一题:二进制间距 问题: 给定一个正整数 N,找到并返回 N 的二进制表示中两个连续的 1 之间的最长距离. 如果没有两个连续的 1,返回 0 . 示例 1: 输入:22 输出:2 解释: 22 的二进制是 0b10110 . 在 22 的二进制表示中,有三个 1,组成两对连续的 1 . 第一对连续的 1 中,两个 1 之间的距离为 2 . 第二对连续的 1 中,两个 1 之间的距离为 1 . 答案取两个距离之中最大的,也就是 2 . 示例 2: 输入:5 输出:2 解释: 5 的二进制是 0

LeetCode之Weekly Contest 101

前一段时间比较忙,而且做这个对于我来说挺耗时间的,已经间隔了几期的没做总结了,后面有机会补齐.而且本来做这个的目的就是为了防止长时间不做把编程拉下,不在追求独立作出所有题了.以后完赛后稍微尝试下,做不出来的直接放弃. 第一题:问题 问题:900. RLE 迭代器 编写一个遍历游程编码序列的迭代器. 迭代器由 RLEIterator(int[] A) 初始化,其中 A 是某个序列的游程编码.更具体地,对于所有偶数i,A[i] 告诉我们在序列中重复非负整数值 A[i + 1] 的次数. 迭代器支持一

[Swift Weekly Contest 113]LeetCode952. 按公因数计算最大组件大小 | Largest Component Size by Common Factor

Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.

LeetCode(Weekly Contest 183)题解

0. 前言 中文版地址:https://leetcode-cn.com/contest/weekly-contest-183/ 英文版地址:https://leetcode.com/contest/weekly-contest-183/ 1. 题解 1.1 5376. 非递增顺序的最小子序列(1403. Minimum Subsequence in Non-Increasing Order) 中文版题目描述:https://leetcode-cn.com/problems/minimum-sub

834. Sum of Distances in Tree —— weekly contest 84

Sum of Distances in Tree An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i

835. Image Overlap —— weekly contest 84

Image Overlap Two images A and B are given, represented as binary, square matrices of the same size.  (A binary matrix has only 0s and 1s as values.) We translate one image however we choose (sliding it left, right, up, or down any number of units),

838. Push Dominoes —— weekly contest 85

Push Dominoes There are N dominoes in a line, and we place each domino vertically upright. In the beginning, we simultaneously push some of the dominoes either to the left or to the right. After each second, each domino that is falling to the left pu