1020. Partition Array Into Three Parts With Equal Sum

Given an array A of integers, return true 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 + 1

Example 2:

Input: [0,2,1,-6,6,7,9,-1,2,0,1]
Output: false

Example 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:

  1. 3 <= A.length <= 50000
  2. -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 integer N such that N is divisible by K, and N only contains the digit 1.

Return the length of N.  If there is no such N, 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 the i-th sightseeing spot, and two sightseeing spots i and j have distance j - 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:

    1. 2 <= A.length <= 50000
    2. 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 integer N, 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: true

Example 2:

Input: S = "0110", N = 4
Output: false

Note:

  1. 1 <= S.length <= 1000
  2. 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的更多相关文章

  1. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  2. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  3. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  4. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  5. LeetCode之Weekly Contest 91

    第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10  ...

  6. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

  7. LeetCode Weekly Contest 47

    闲着无聊参加了这个比赛,我刚加入战场的时候时间已经过了三分多钟,这个时候已经有20多个大佬做出了4分题,我一脸懵逼地打开第一道题 665. Non-decreasing Array My Submis ...

  8. 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 ...

  9. LeetCode之Weekly Contest 102

    第一题:905. 按奇偶校验排序数组 问题: 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...

随机推荐

  1. Thread(线程)和ThreadPool(线程池) Thread回调与返回值

    Thread(线程) Thread开启线程:接收一个参数 TestClass tc = new TestClass(); //没有返回值,有一个object类型的参数的委托:两种写法. Paramet ...

  2. metasploit渗透测试指南概要整理

    一.名词解释 exploit 测试者利用它来攻击一个系统,程序,或服务,以获得开发者意料之外的结果.常见的 有内存溢出,网站程序漏洞利用,配置错误exploit. payload 我们想让被攻击系统执 ...

  3. sphinx文档

    Navigation index modules | Sphinx主页 | 文档 » 下载 目前版本: 1.2 获得 Sphinx 从 Python Package Index, 或者使用如下命令安装 ...

  4. PowerDesigner :

    P:是否为主键: F:是否为外键: M:表示强制非空: D:是否在模型中显示 修改样式.字体.颜色.等:Tools->Display Preferences->Format-> Po ...

  5. 并发编程(五)LockSupport

    并发编程(五)LockSupport LockSupport 提供 park() 和 unpark() 方法实现阻塞线程和解除线程阻塞,实现的阻塞和解除阻塞是基于"许可(permit)&qu ...

  6. 深入应用c++11 随书代码

    代码并未在作者github上提供 将书中代码敲至vc 并调试运行 依赖BOOST库 编译环境vs2015 boost1.59 // Client.cpp : 定义控制台应用程序的入口点. // #in ...

  7. htmlparser学习(原创)

    --thumbelina.jar  这是一个演示图片搜索和显示的小程序JFrame Preferences.userNodeForPackage(getClass());  根据传入的class所在包 ...

  8. 编写JavaScript 代码的5个小技巧

    1.Array.includes 与条件判断 一般我们判断或用 || // condition function test(fruit) { if (fruit == "apple" ...

  9. 2018.09.27 codeforces1045D. Interstellar battle(期望dp)

    传送门 一道有意思的期望dp. 题意是给出一棵树,每个点最开始都有一个gg的概率,有m次修改,每次修改会把某个点gg的概率更换掉,让你求出每次修改之后整个树被分成的连通块的数量的期望(gg掉的点不算) ...

  10. 2018.09.26洛谷P1084 疫情控制(二分+倍增)

    传送门 好题啊. 题目要求的最大值最小,看到这里自然想到要二分答案. 关键在于怎么检验. 显然对于每个点向根走比向叶节点更优. 因此我们二分答案之后,用倍增将每个点都向上跳到跳不动为止. 这时我们ch ...