LeetCode Contest 166
第一次知道LeetCode 也有比赛。
很久没有打过这种线上的比赛,很激动。
直接写题解吧
很弱智
class Solution {
public:
int subtractProductAndSum(int n) {
int num1=0;
int num2=1;
while(n)
{
int x = n%10;
num1+=x;
num2*=x;
n/=10;
}
return num2-num1;
}
};
第二题
也很简单的,我先排个序。
但是在用c++的快排的时候,被坑了,我一直的习惯是写自定义比较函数,没有写运算符重载,不知道为什么一直RE,浪费了挺多时间
struct Node
{
int value;
int index;
Node(){}
Node(int value,int index)
{
this->value = value;
this->index = index;
}
bool operator <(const Node &x)const
{
return value<x.value;
}
};
class Solution {
public:
int vis[1005];
Node a[1005];
vector<vector<int>> groupThePeople(vector<int>& groupSizes) {
int len = groupSizes.size();
for(int i=0;i<len;i++)
{
a[i] = Node(groupSizes[i],i);
}
sort(a,a+len);
vector<vector<int>> ans;
vector<int> res;
res.push_back(a[0].index);
int num=0;
for(int i=1;i<len;i++)
{
if(a[i].value==a[i-1].value)
{
if(res.size()==a[i].value)
{
ans.push_back(res);
res.clear();
res.push_back(a[i].index);
continue;
}
res.push_back(a[i].index);
}
else
{
ans.push_back(res);
res.clear();
res.push_back(a[i].index);
}
}
if(res.size()!=0)
{
ans.push_back(res);
}
return ans;
}
};
二分,一次过,没有怎么浪费时间
class Solution {
public:
int smallestDivisor(vector<int>& nums, int threshold) {
int num=0;
for(int i=0;i<nums.size();i++)
{
num=max(num,nums[i]);
}
int start = 1;
int end = num;
while(start<=end)
{
int mid = (start+end)/2;
int sum=0;
for(int i=0;i<nums.size();i++)
{
int x;
if(nums[i]%mid==0)
x = nums[i]/mid;
else
x = nums[i]/mid +1;
sum+=x;
}
if(sum > threshold)
{
start = mid + 1;
continue;
}
if(sum <= threshold)
{
end = mid - 1;
continue;
}
}
return start;
}
};
看着挺唬人的,我一开始纠结是不是DP,但是一看数据,最多只有3啊,暴力搜索,用位运算保存矩阵的状态,然后注意剪枝就过了。但是因为在第二题上耽误了一些时间,导致比赛结束后,才过了第四题,哎。。
class Solution {
public:
int vis[10005];
int ans[10005];
int map[10][10];
int n,m;
int result;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int minFlips(vector<vector<int>>& mat) {
int len = mat.size();
n=len;
m=mat[0].size();
for(int i=0;i<mat.size();i++)
{
for(int j=mat[i].size()-1;j>=0;j--)
{
map[i][j] = mat[i][j];
}
}
result = 9999999;
int x = compute();
if(x==0)
return 0;
vis[x]=1;
ans[x]=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
fun(i,j,1);
map[i][j] ^= 1;
for(int k=0;k<4;k++)
{
int xx = i+dir[k][0];
int yy = j+dir[k][1];
if(xx>=0&&xx<n&&yy>=0&&yy<m)
map[xx][yy] ^=1;
}
}
}
if(result == 9999999 )
result =-1;
return result;
}
void fun(int x,int y,int res)
{
map[x][y] ^= 1;
for(int i=0;i<4;i++)
{
int xx = x+dir[i][0];
int yy = y+dir[i][1];
if(xx>=0&&xx<n&&yy>=0&&yy<m)
map[xx][yy] ^=1;
}
int v = compute();
if(vis[v]==1)
{
if(res>=ans[v])
{
return;
}
else
{
ans[v]=res;
}
}
if(v==0)
{
result=min(result,res);
return;
}
vis[v]=1;
ans[v]=res;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
fun(i,j,res+1);
map[i][j] ^= 1;
for(int k=0;k<4;k++)
{
int xx = i+dir[k][0];
int yy = j+dir[k][1];
if(xx>=0&&xx<n&&yy>=0&&yy<m)
map[xx][yy] ^=1;
}
}
}
}
int compute()
{
int x=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
x <<= 1;
x |= map[i][j];
}
}
return x;
}
};
LeetCode Contest 166的更多相关文章
- 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)
[LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...
- leetcode contest 20
Q1: 520. Detect Capital Given a word, you need to judge whether the usage of capitals in it is right ...
- LeetCode No.166,167,168
No.166 FractionToDecimal 分数到小数 题目 给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数. 如果小数部分为循环小数 ...
- 【LeetCode】166. Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- LeetCode(166) Fraction to Recurring Decimal
题目 Given two integers representing the numerator and denominator of a fraction, return the fraction ...
- Atcoder Beginner Contest 166
VP赛况如下: 前言:感觉这一场题目难度还是比较贴近新生的,我一个codeforces小蓝名一小时居然AK了,F题数据有点水(?)暴力dfs居然都能过... A:A?C 题意:给你字符串,如果字符串是 ...
- 230th Weekly Leetcode Contest
题目二 题目描述 5690. 最接近目标价格的甜点成本 难度:中等-中等 题解 解法一:三进制状态压缩 考虑到baseCosts.toppingCosts的长度最多都为10,每一种辅料都有加0.1. ...
- 【刷题-LeetCode】166 Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
随机推荐
- Java生鲜电商平台-深刻理解电商的库存架构与解决方案
Java生鲜电商平台-深刻理解电商的库存架构与解决方案 说明:一般电商的库存都是跟SKU相关联的,那么怎么样才能进行SKU的库存管理呢?有以下几种方式与方法: 一.七大库存分类 首先得学习什么是库存, ...
- python中time、datetime模块的使用
目录 python中time.datetime模块的使用 1.前言 2.time模块 1.时间格式转换图 2.常用方法 3.datetime模块 python中time.datetime模块的使用 1 ...
- crm-2
1.分页 web必备的功能 1)批量制造测试数据 定义一个空列表用于存储 orm对象 ,models.表名(字段=...)创建orm对象append到列表 ,使用bulk_create(对象列表)一次 ...
- CSS @规则
最近在看极客时间winter大神的重学前端系列,遇到了许多不常用但是很重要的知识点.感觉视野得到了极大的开阔(打个广告,哈哈). 其中css @规则令人印象深刻.简单的做下笔记: @namespace ...
- 重新安装和更新所有的 nuget包
重新安装指定项目中所有的 nuget 包 Update-Package -ProjectName MyProject –reinstall 更新指定项目中所有的 nuget 包 Update-Pack ...
- arcgis api for javascript 学习(二) 发布并调用地图切片
文章将从发布切片地图到调用切片地图整个过程都展示出来. (一).切片地图的发布 1.还是前面的arcgis展示的地图 2.与发布动态地图前面的步骤是一样的 打开分享后,如图 3.一切就绪后,到达缓存的 ...
- SparkStreaming和storm的区别
这是2种不同的架构. 他们的区别是SparkStreaming的吞吐量非常高,秒级准实时处理,Storm是容错性非常高,毫秒级实时处理 解释:sparkStreaming是一次处理某个间隔的数据,比如 ...
- [20190522]DISABLE TABLE LOCK.txt
[20190522]DISABLE TABLE LOCK.txt --//如果禁止table lock时,一些ddl操作会被禁止.但是我有点吃惊的是增加字段不受限制.--//通过测试说明问题. 1.环 ...
- Mac遇到挖矿程序的应急方法
Mac遇到挖矿程序应急的方法 工作笔记: 1.起因:监控发现jsonrpc挖矿报警,询问当事人描述当时情况是安装了sketch软件. 网上可以定位到该IOC 运行后该IOC流量依然可以观测 ...
- Linxu:磁盘分区
了解磁盘分区 磁盘的物理组成: 圆形的磁盘盘(主要记录数据的部分): 机械手臂,与在机械手臂上的磁盘读取头(可擦写磁盘盘上的数据): 主轴马达,可以转动磁盘盘,让机械手臂的读取头在磁盘盘上读写数据. ...