Weekly Contest 129
1020. Partition Array Into Three Parts With Equal Sum
Given an array
Aof integers, returntrueif 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 < jwith(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 integerNsuch thatNis divisible byK, andNonly 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
Aof positive integers,A[i]represents the value of thei-th sightseeing spot, and two sightseeing spotsiandjhave distancej - ibetween 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 <= 500001 <= 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 <= 10001 <= 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 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...
随机推荐
- git(常用命令)思维导图...
Git 是一个很强大的分布式版本控制系统.它不但适用于管理大型开源软件的源代码,管理私人的文档和源代码也有很多优势. 来自文章:http://www.cnblogs.com/1-2-3/archive ...
- .net体系与java体系
对于.NET Framework体系结构,参考了"你必须知道的.NET"并”借用“别人的经典体系结构图从宏观上说明一下我的理解. 图1 简单的说下几个名词: CLR: 通用语言运行 ...
- linux下常用文件操作命令
1.find命令 按内容查找文件 find /home/vpopmail/domains/best-21ixi.jp/bounce/Maildir/new/ -name "*" | ...
- 团队项目:二次开发--v.2.1--软件工程
原先代码,对于基本对象的Get,Set方法构造函数等方法与实现基本功能的方法统一放到了一起,容易造成代码不清晰,别人比较难阅读的情况.而且其中代码冗余比较多. 改进代码,进行了层次的分析,将基本对象与 ...
- PAT 1010 一元多项式求导 (25)(STL-map+思路)
1010 一元多项式求导 (25)(25 分)提问 设计函数求一元多项式的导数.(注:x^n^(n为整数)的一阶导数为n*x^n-1^.) 输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均 ...
- cocos2d-js IOS接facebook插件
当前测试版本:cocos2d-x 3.8.1 3.7也试用,之下的版本没测过,一般是路径改变,文件名称一般不会变 注:当前工程是通过控制台new的工程,不是cocosStudio创建的工程 ...
- js技巧汇总
1.window.open()打开一个子页面,在子页面关闭时刷新父页面 子页面关闭事件代码:window.opener.location.href=window.opener.location.hre ...
- Oracle sql的基本优化写法和思路。
首先简单介绍下常规的sql优化的方式: 1.肯定有人说建索引啊. 2.数据量实在太大,建分区啊. 3.其实基于目前公司的业务还有一种办法那就是向上聚集表.根据查询业务,专门抽取上来一张表,直接做到se ...
- Metro Revealed: Building Windows 8 apps with XAML and C# 阅读笔记
第一章1.1.3中提到 Jesse Liberty 的<Pro Windows 8 Development with XAML and C#>,这是一本关于win8更全面的书,以后看.
- arduino 中通过寄存器地址访问寄存器内容
void call_func( void (*func)(void)){ (*func)(); } void setup() { // put your setup code here, to run ...