weekly contest 115
958. Check Completeness of a Binary Tree
Given a binary tree, determine if it is a complete binary tree.
Definition of a complete binary tree from Wikipedia:
In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.
Example 1:

Input: [1,2,3,4,5,6]
Output: true
Explanation: Every level before the last is full (ie. levels with node-values {1} and {2, 3}), and all nodes in the last level ({4, 5, 6}) are as far left as possible.
Example 2:

Input: [1,2,3,4,5,null,7]
Output: false
Explanation: The node with value 7 isn't as far left as possible.
Note:
- The tree will have between 1 and 100 nodes.
Approach #1: C++.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isCompleteTree(TreeNode* root) {
queue<TreeNode*> nodes;
nodes.push(root);
while (true) {
TreeNode* cur = nodes.front();
if (cur == NULL) break;
nodes.push(cur->left);
nodes.push(cur->right);
nodes.pop();
}
while (!nodes.empty()) {
TreeNode* cur = nodes.front();
if (cur != NULL) return false;
nodes.pop();
}
return true;
}
};
957. Prison Cells After N Days
There are 8 prison cells in a row, and each cell is either occupied or vacant.
Each day, whether the cell is occupied or vacant changes according to the following rules:
- If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
- Otherwise, it becomes vacant.
(Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.)
We describe the current state of the prison in the following way: cells[i] == 1 if the i-th cell is occupied, else cells[i] == 0.
Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.)
Example 1:
Input: cells = [0,1,0,1,1,0,0,1], N = 7
Output: [0,0,1,1,0,0,0,0]
Explanation:
The following table summarizes the state of the prison on each day:
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
Example 2:
Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000
Output: [0,0,1,1,1,1,1,0]
Note:
cells.length == 8cells[i]is in{0, 1}1 <= N <= 10^9
Appraoch #1: C++.
class Solution {
public:
vector<int> prisonAfterNDays(vector<int>& cells, int N) {
unordered_map<string, int> seen;
while (N > 0) {
string temp(cells.begin(), cells.end());
seen[temp] = N--;
vector<int> dummy(8, 0);
for (int j = 1; j < 7; ++j)
dummy[j] = cells[j-1] == cells[j+1] ? 1 : 0;
cells = dummy;
string ant(cells.begin(), cells.end());
if (seen.count(ant))
N %= seen[ant] - N;
}
return cells;
}
};
Analysis:
There are only six binary numbers, so all of the difference possibilities is 2^6.
so we can use a map to store the case, if N is very large we can reduce the number of calculation by
N %= seen[ant] - N;
959. Regions Cut By Slashes
In a N x N grid composed of 1 x 1 squares, each 1 x 1 square consists of a /, \, or blank space. These characters divide the square into contiguous regions.
(Note that backslash characters are escaped, so a \ is represented as "\\".)
Return the number of regions.
Example 1:
Input:
[
" /",
"/ "
]
Output: 2
Explanation: The 2x2 grid is as follows:
![]()
Example 2:
Input:
[
" /",
" "
]
Output: 1
Explanation: The 2x2 grid is as follows:
![]()
Example 3:
Input:
[
"\\/",
"/\\"
]
Output: 4
Explanation: (Recall that because \ characters are escaped, "\\/" refers to \/, and "/\\" refers to /\.)
The 2x2 grid is as follows:
![]()
Example 4:
Input:
[
"/\\",
"\\/"
]
Output: 5
Explanation: (Recall that because \ characters are escaped, "/\\" refers to /\, and "\\/" refers to \/.)
The 2x2 grid is as follows:
![]()
Example 5:
Input:
[
"//",
"/ "
]
Output: 3
Explanation: The 2x2 grid is as follows:
![]()
Note:
1 <= grid.length == grid[0].length <= 30grid[i][j]is either'/','\', or' '.
Approach #1: C++. [Union Find]
class Solution {
public:
int regionsBySlashes(vector<string>& grid) {
n = grid.size();
count = n*n*4;
f.resize(n*n*4);
for (int i = 0; i < n*n*4; ++i) f[i] = i;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i > 0) _union(g(i-1, j, 2), g(i, j, 0));
if (j > 0) _union(g(i, j-1, 1), g(i, j, 3));
if (grid[i][j] != '/') {
_union(g(i, j, 0), g(i, j, 1));
_union(g(i, j, 2), g(i, j, 3));
}
if (grid[i][j] != '\\') {
_union(g(i, j, 0), g(i, j, 3));
_union(g(i, j, 1), g(i, j, 2));
}
}
}
return count;
}
private:
int count, n;
vector<int> f;
int find(int x) {
if (x != f[x]) {
f[x] = find(f[x]);
}
return f[x];
}
void _union(int x, int y) {
x = find(x);
y = find(y);
if (x != y) {
f[x] = y;
count--;
}
}
int g(int i, int j, int k) {
return (i*n + j) * 4 + k;
}
};
Analysis:
In this problem we split the ceil with 4 subceils (like tihs). if the subceils are connected then we draw with the same color. the subceils use the number of (i*n + j) * 4 + k represented and using union-find to find the connected ceils. If two ceils are connected we should
--cout.
960. Delete Columns to Make Sorted III
We are given an array A of N lowercase letter strings, all of the same length.
Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices.
For example, if we have an array A = ["babca","bbazb"] and deletion indices {0, 1, 4}, then the final array after deletions is ["bc","az"].
Suppose we chose a set of deletion indices D such that after deletions, the final array has every element (row) in lexicographic order.
For clarity, A[0] is in lexicographic order (ie. A[0][0] <= A[0][1] <= ... <= A[0][A[0].length - 1]), A[1] is in lexicographic order (ie. A[1][0] <= A[1][1] <= ... <= A[1][A[1].length - 1]), and so on.
Return the minimum possible value of D.length.
Example 1:
Input: ["babca","bbazb"]
Output: 3
Explanation: After deleting columns 0, 1, and 4, the final array is A = ["bc", "az"].
Both these rows are individually in lexicographic order (ie. A[0][0] <= A[0][1] and A[1][0] <= A[1][1]).
Note that A[0] > A[1] - the array A isn't necessarily in lexicographic order.
Example 2:
Input: ["edcba"]
Output: 4
Explanation: If we delete less than 4 columns, the only row won't be lexicographically sorted.
Example 3:
Input: ["ghi","def","abc"]
Output: 0
Explanation: All rows are already lexicographically sorted.
Note:
1 <= A.length <= 1001 <= A[i].length <= 100
Approach #1: C++. [DP]
class Solution {
public:
int minDeletionSize(vector<string>& A) {
int m = A.size(), n = A[0].size();
int res = INT_MAX;
vector<int> dp(n, 1);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
int good = 1;
for (int k = 0; k < m; ++k) {
if (A[k][j] > A[k][i]) {
good = 0;
break;
}
}
if (good == 1)
dp[i] = max(dp[i], dp[j]+1);
}
res = min(res, n-dp[i]);
}
return res;
}
};
Analysis:
We use dp[i] represent the maximum increasing subsequence's length.
dynamic transfer equation:
dp[i] = max(dp[i], dp[j]+1);
weekly contest 115的更多相关文章
- 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 93
第一题:二进制间距 问题: 给定一个正整数 N,找到并返回 N 的二进制表示中两个连续的 1 之间的最长距离. 如果没有两个连续的 1,返回 0 . 示例 1: 输入:22 输出:2 解释: 22 的 ...
- 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 ...
随机推荐
- Python numpy函数:transpose()
transpose用于对高维数组进行转置,转置时候需要一个由轴编号组成的元组. 比如说三维的数组,那就对维度进行编号,也就是0,1,2:这样说可能比较抽象.这里的0,1,2可以理解为对shape返回元 ...
- ICanPay介绍
ICanPay介绍 ICanPay是一个支持多商户多种支付方式的跨平台网关处理类库,使用ICanPay可以简化订单的创建.查询.退款和接收网关返回的支付通知等操作. 目前支持的支付网关有:支付宝(Al ...
- 二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历
二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历 二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则 ...
- SQL 用;with 由所有的子节点查询到树结构中所有父节点
1.所有的子节点查询到树结构中所有父节点 RETURNS @Tree Table(PID )) as begin --DECLARE @ID VARCHAR() --SET @ID = ' ;with ...
- 版本管理 word 文档比较
1.因为公司还在用SVN, 2.而且 还在用word 写文档, 3.而且 commit log 基本不写, 所以导致,想了解word文档 改动, 很浪费时间!!!! 所以想 快速了解word 改动, ...
- sql---left join;right join;inner join---区别
sql---left join;right join;inner join---区别 分为以下几类: 1.内联接(典型的联接运算,使用像 = 或 <> 之类的比较运算符).包括相等联接和自 ...
- HeapCreate深入研究
本机:win7(x86),4G内存 #include"stdafx.h"#include<windows.h>#include<stdio.h>#inclu ...
- 11-03SQLserver基础--子查询语句
一.子查询--查询的嵌套(重点记忆) select MAX(age)from haha where bumen='销售部' --汇总-- select MAX(age)from haha where ...
- nested exception is java.net.UnknownHostException: mybatis.org异常处理
最近自己写了个小项目(丛林商城V1.0),一个简单的网上商铺:主界面是商品的展示和登录,面对三种角色的人群:一般客户,VIP客户,管理员,与之对应的三种商品价格,登陆后根据具体角色来显示商品的价格:还 ...
- 配gzip的过滤器进行压缩解决表单加载慢问题
一个客户的表单上字段超过五百,经浏览器的调试器发现主要问题是从服务器取数据花费了大量时间,下载内容大小约1.2M,下载时间在10s左右,导致样式加载完大约在17s左右(不清除浏览器缓存).最终考虑利用 ...