Weekly Contest 112
945. Minimum Increment to Make Array Unique
Given an array of integers A, a move consists of choosing any A[i]
, and incrementing it by 1
.
Return the least number of moves to make every value in A
unique.
Example 1:
Input: [1,2,2]
Output: 1
Explanation: After 1 move, the array could be [1, 2, 3].
Example 2:
Input: [3,2,1,2,1,7]
Output: 6
Explanation: After 6 moves, the array could be [3, 4, 1, 2, 5, 7].
It can be shown with 5 or less moves that it is impossible for the array to have all unique values.
Note:
0 <= A.length <= 40000
0 <= A[i] < 40000
Approach #1:
class Solution {
public:
int minIncrementForUnique(vector<int>& A) {
sort(A.begin(), A.end());
int move = 0;
for (int i = 1; i < A.size(); ++i) {
if (A[i] <= A[i-1]) {
int step= A[i] == A[i-1] ? 1 : A[i-1]+1-A[i];
A[i] += step;
move += step;
}
}
return move;
}
};
946. Validate Stack Sequences
Given two sequences pushed
and popped
with distinct values, return true
if and only if this could have been the result of a sequence of push and pop operations on an initially empty stack.
Example 1:
Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
Output: true
Explanation: We might do the following sequence:
push(1), push(2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
Example 2:
Input: pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
Output: false
Explanation: 1 cannot be popped before 2.
Note:
0 <= pushed.length == popped.length <= 1000
0 <= pushed[i], popped[i] < 1000
pushed
is a permutation ofpopped
.pushed
andpopped
have distinct values.
Approach #1:
class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
stack<int> ipush;
queue<int> ipop;
for (int i = 0; i < popped.size(); ++i)
ipop.push(popped[i]);
for (int i = 0; i < pushed.size(); ++i) {
ipush.push(pushed[i]);
while (!ipush.empty() && ipush.top() == ipop.front()) {
ipush.pop();
ipop.pop();
}
}
return ipush.empty();
}
};
948. Bag of Tokens
You have an initial power P
, an initial score of 0
points, and a bag of tokens.
Each token can be used at most once, has a value token[i]
, and has potentially two ways to use it.
- If we have at least
token[i]
power, we may play the token face up, losingtoken[i]
power, and gaining1
point. - If we have at least
1
point, we may play the token face down, gainingtoken[i]
power, and losing1
point.
Return the largest number of points we can have after playing any number of tokens.
Example 1:
Input: tokens = [100], P = 50
Output: 0
Example 2:
Input: tokens = [100,200], P = 150
Output: 1
Example 3:
Input: tokens = [100,200,300,400], P = 200
Output: 2
Note:
tokens.length <= 1000
0 <= tokens[i] < 10000
0 <= P < 10000
Approach #1:
class Solution {
public:
int bagOfTokensScore(vector<int>& tokens, int P) {
if (tokens.size() == 0) return 0;
sort(tokens.begin(), tokens.end());
if (P < tokens[0]) return 0;
int temp = 0, ans = 0;
int start = 0, end = tokens.size()-1;
while (start <= end && (temp > 0 || P >= tokens[ans])) {
if (P >= tokens[start]) {
P -= tokens[start];
temp++;
start++;
ans = max(ans, temp);
} else {
temp--;
P += tokens[end];
end--;
}
}
return ans;
}
};
947. Most Stones Removed with Same Row or Column
On a 2D plane, we place stones at some integer coordinate points. Each coordinate point may have at most one stone.
Now, a move consists of removing a stone that shares a column or row with another stone on the grid.
What is the largest possible number of moves we can make?
Example 1:
Input: stones = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]
Output: 5
Example 2:
Input: stones = [[0,0],[0,2],[1,1],[2,0],[2,2]]
Output: 3
Example 3:
Input: stones = [[0,0]]
Output: 0
Note:
1 <= stones.length <= 1000
0 <= stones[i][j] < 10000
class Solution {
void color(vector<vector<int>> &G, vector<int> &C, int i, int c) {
C[i] = c;
for (int j : G[i]) {
if (C[j] == -1) color(G, C, j, c);
}
}
public:
int removeStones(vector<vector<int>> & stones) {
int N = stones.size();
vector<vector<int>> G(N);
for (int i = 0; i < N-1; i++) {
int x = stones[i][0];
int y = stones[i][1];
for (int j = i + 1; j < N; j++) {
if ((stones[j][0] == stones[i][0])||(stones[j][1] == stones[i][1])) {
G[i].push_back(j);
G[j].push_back(i);
}
}
}
vector<int> C(N, -1);
int c = 0;
for (int i = 0; i < N; i++) {
if (C[i] == -1) color(G, C, i, c++);
}
return N - c;
}
};
Approach #2: C++ [UnionFind]
class Solution {
public:
int removeStones(vector<vector<int>>& stones) {
for (int i = 0; i < stones.size(); ++i)
uni(stones[i][0], ~stones[i][1]);
return stones.size() - islands;
} unordered_map<int, int> f;
int islands = 0; int find(int x) {
if (!f.count(x)) f[x] = x, islands++;
if (x != f[x]) f[x] = find(f[x]);
return f[x];
} void uni(int x, int y) {
x = find(x), y = find(y);
if (x != y) f[x] = y, islands--;
} };
Approach #3: Python [DFS]
class Solution(object):
def removeStones(self, stones):
"""
:type stones: List[List[int]]
:rtype: int
"""
index = collections.defaultdict(set)
for i, j in stones:
index[i].add(j + 10000)
index[j+10000].add(i) def dfs(i):
seen.add(i)
for j in index[i]:
if j not in seen:
dfs(j) seen = set()
islands = 0 for i, j in stones:
if i not in seen:
islands += 1
dfs(i)
dfs(j + 10000) return len(stones) - islands
come from:
https://www.jianshu.com/p/30d2058db7f7
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/197659/C%2B%2B-solution-using-graph-coloring
Weekly Contest 112的更多相关文章
- 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 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...
随机推荐
- 【题解】UVA10140 [Prime Distance]
[题解]UVA10140 Prime Distance 哈哈哈哈\(miller-rabbin\)水过去了哈哈哈 还能怎么办呢?\(miller-rabbin\)直接搞.枚举即可,还跑得飞快. 当然此 ...
- API的理解和使用——键管理
核心知识点: 1.键重命名:rename和renamenx,使用renamenx时newkey必须不存在,重命名后会使用del删除原来的键,如果值比较大也会会造成阻塞. 2.随机返回一个值:rando ...
- STemWin显示汉字 — SD卡外挂XBF字库
转载注明出处 方法来自安福莱教程 1: 使用emWin自带小工具生成字库 (1)启动软件 选择4位抗锯齿 (2)根据需求选择字体类型和字体大小 (3)另存为XBF格式 2: 创建XBF字体 #inc ...
- mac 在 finder 当前 路径下 打开 terminal 的办法
1. 在:系统偏好设置 -> 键盘 -> 服务 或者 finder -> 服务偏好设置, 如下: 建议配合快捷键使用,本人使用的快捷键: 在 terminal 新建标签 contro ...
- Too many open files解决方案及原理
以下是我解决Too many open files异常时学习的知识的理解和总结,如有不正确指出,敬请指出! 此问题中文搜索雷同,你可以尝试以下关键字:"file descriptor lea ...
- Win7、Win8、Win10始终以管理员身份运行程序。
在Win7.Win8.Win10系统中,以管理员身份运行程序很麻烦,一般有以下几种方式: 1.在可执行程序或快捷方式上右键,以管理员身份运行: 2.在可执行程序或快捷方式上右键->属性-> ...
- MyBatis映射文件中用#和$传递参数的特点
在MyBatis映射文件中用#和$传递参数的特点, #是以占位符的形式来传递对应变量的参数值的,框架会对传入的参数做预编译的动作, 用$时会将传入的变量的参数值原样的传递过去,并且用$传递传递参数的时 ...
- openfire插件(1)
插件核心类,这里对PacketInterceptor.Plugin进行继承.如果开发插件就一定要继承Plugin,而继承PacketInterceptor是拦截用户发送的消息包.对消息包进行过滤.拦截 ...
- 在线接口管理工具-eoapi
为了方便和前端沟通,临时在局域网搭建了一个接口管理工具,查了一些资料都说eoapi不错,那就试了一下: 1.安装 要在服务器或者自己的电脑,准备web环境,Linux可以是Apache/nginx , ...
- 1131 Subway Map(30 分)
In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...