Weekly Contest 129
1020. Partition Array Into Three Parts With Equal Sum
Given an array
A
of integers, returntrue
if and only if we can partition the array into three non-empty parts with equal sums.Formally, we can partition the array if we can find indexes
i+1 < j
with(A[0] + A[1] + ... + A[i] == A[i+1] + A[i+2] + ... + A[j-1] == A[j] + A[j-1] + ... + A[A.length - 1])
Example 1:
Input: [0,2,1,-6,6,-7,9,1,2,0,1]
Output: true
Explanation: 0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1Example 2:
Input: [0,2,1,-6,6,7,9,-1,2,0,1]
Output: falseExample 3:
Input: [3,3,6,5,-2,2,5,1,-9,4]
Output: true
Explanation: 3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4
Note:
3 <= A.length <= 50000
-10000 <= A[i] <= 10000
Approach #1:
class Solution {
public:
bool canThreePartsEqualSum(vector<int>& A) {
int len = A.size();
vector<int> sums(len, 0);
unordered_map<int, int> m_;
sums[0] = A[0];
m_[sums[0]] = 0;
for (int i = 1; i < len; ++i) {
sums[i] = sums[i-1] + A[i];
m_[sums[i]] = i;
}
for (int i = 0; i < len-2; ++i) {
int s, m, e; if (m_.find(2*sums[i]) != m_.end() && m_.find(3*sums[i]) != m_.end()) {
s = i, m = m_[2*sums[i]], e = m_[3*sums[i]];
if (s < m && m < e && e == len-1) return true;
} } return false;
}
};
1022. Smallest Integer Divisible by K
Given a positive integer
K
, you need find the smallest positive integerN
such thatN
is divisible byK
, andN
only contains the digit 1.Return the length of
N
. If there is no suchN
, return -1.
Example 1:
Input: 1
Output: 1
Explanation: The smallest answer is N = 1, which has length 1.Example 2:
Input: 2
Output: -1
Explanation: There is no such positive integer N divisible by 2.Example 3:
Input: 3
Output: 3
Explanation: The smallest answer is N = 111, which has length 3.
Note:
1 <= K <= 10^5
Approach #1:
class Solution {
public:
int smallestRepunitDivByK(int K) {
if (K % 2 == 0) return -1;
int last = 0;
int temp = 0;
for (int i = 1; i <= K; ++i) {
temp = last;
temp = temp * 10 + 1;
temp = temp % K;
if (temp % K == 0) return i;
last = temp;
}
return -1;
}
};
1021. Best Sightseeing Pair
Given an array
A
of positive integers,A[i]
represents the value of thei
-th sightseeing spot, and two sightseeing spotsi
andj
have distancej - i
between them.The score of a pair (
i < j
) of sightseeing spots is (A[i] + A[j] + i - j)
: the sum of the values of the sightseeing spots, minus the distance between them.Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2,A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
Approach #1:
class Solution {
public:
int maxScoreSightseeingPair(vector<int>& A) {
int res = 0, cur = 0; for (int a : A) {
res = max(res, cur + a);
cur = max(cur, a) - 1;
} return res;
}
};
1023. Binary String With Substrings Representing 1 To N
Given a binary string
S
(a string consisting only of '0' and '1's) and a positive integerN
, return true if and only if for every integer X from 1 to N, the binary representation of X is a substring of S.
Example 1:
Input: S = "0110", N = 3
Output: trueExample 2:
Input: S = "0110", N = 4
Output: false
Note:
1 <= S.length <= 1000
1 <= N <= 10^9
Approach #1:
class Solution {
public:
bool queryString(string S, int N) {
vector<bool> seen(N, false);
for (int i = 0; i < S.length(); ++i) {
for (auto j = i, num = 0; num <= N && j < S.length(); ++j) {
num = (num << 1) + S[j] - '0';
if (num > 0 && num <= N) seen[num-1] = true;
}
} return all_of(seen.begin(), seen.end(), [](bool s) { return s; });
}
};
Weekly Contest 129的更多相关文章
- 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 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...
随机推荐
- async与await
在方法上可以加 async,方法体内需要有 await,没有await的话,会出现warn警告.async单独出现是没有用的. await只能出现在Task前面.await Task的后面的代码会被封 ...
- java 错误: 找不到或无法加载主类
这个问题应该很常见的,笔者经常手工编译一些测试代码或者小工具,经常用到 javac和java来编译并运行一些简单的小工具. 以Hello World来测试. HelloWorld.java publi ...
- 768A Oath of the Night's Watch
A. Oath of the Night's Watch time limit per test 2 seconds memory limit per test 256 megabytes input ...
- src/lxml/includes/etree_defs.h:14:31: 致命错误:libxml/xmlversion.h:没有那个文件或目录
fedora21平台下解决办法:yum install libxml-devel ubuntu下可以使用 apt-get intalll xxxx 如果仍然出现,可以尝试安装这两个包libxslt-d ...
- OSGi 系列(十二)之 Http Service
OSGi 系列(十二)之 Http Service 1. 原始的 HttpService (1) 新建 web-osgi 工程,目录结构如下: (2) HomeServlet package com. ...
- echarts 使用配置模式(含事件)
<!-- 引入echarts UMD 环境--> <script src="js/echarts/build/dist/echarts.js"></s ...
- 关于IBatisNet的配置文件中数据库连接字符串加密处理
我们通常在IBatisNet配置文件 properties.config 加入数据库连接字符串.数据库连接字符串直接放在里面,没有被加密,很不安全.如果我们把 properties.config 文件 ...
- python之零碎知识
一 join方法 主要是做字符串的拼接:join后面跟的类型必须是要可迭代得到对象 for循环的对象是可迭代对象 # result = "".join(li) # print(re ...
- 2018.10.16 NOIP模拟 华莱士(并查集)
传送门 按照题意模拟维护最小的环套树森林就行了. 然而考试的时候naivenaivenaive瞎写了一个错误的贪心. 代码
- 关于linux下/srv、/var和/tmp的职责区分
转载自:https://blog.csdn.net/u012107143/article/details/54972544?utm_source=itdadao&utm_medium=refe ...