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总的的边数减去重叠的 ...
随机推荐
- ABAP ALV显示前排序合并及布局显示
有时候会有用户要求显示出来的ALV立即就是升序或者降序,或者是上下同一个字段值一样的情况显示一次,如 变为 这个时候内表用SORT有时候会不好用,可以使用函数 REUSE_ALV_GRID_DISPL ...
- python爬取 “得到” App 电子书信息
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 静觅 崔庆才 PS:如有需要Python学习资料的小伙伴可以加点击下 ...
- Python爬取前程无忧网站上python的招聘信息
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 我姓刘却留不住你的心 PS:如有需要Python学习资料的小伙伴可以 ...
- 用Python进行数据清洗,这7种方法你一定要掌握
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者 | 常国珍.赵仁乾.张秋剑 来源 |<Python数据科学:技术 ...
- ZooKeeper安装、配置和使用
Zookeeper的概述: ZooKeeper是一个开源的分布式的,为分布式应用提供协调服务的Apache项目. ZooKeeper从设计模式角度来理解:是一个基于观察者模式设计的分布式服务管理框架, ...
- Java基础--注解、反射
一.注解(Annotation) 1.什么是注解? 从JDK5开始,Java增加了Annotation(注解),Annotation是代码里的特殊标记,这些标记可以在编译.类加载.运行时被读取,并执行 ...
- 1-4-JS基础-条件判断
第一种 1.if(条件成立){ 执行某件事} 2.if(条件成立){执行某件事}else{执行另外一件事 } 3.if(条件1成立){执行某件事}else if(条件2成立){执行某件事}else i ...
- 「SAP技术」SAP不够严谨?
SAP不够严谨? 大家知道采购业务里,有一种特殊的采购形式,就是按单采购,意思是所采购的物料只用于指定的销售订单的销售出库.这种业务场景在SAP项目实践中,比较常见. 强大无比的SAP系统当然有解决方 ...
- [N久以前发布的文章]php 获取yahoo股票csv数据并封闭成为接口输出json数据
思路 先从yahoo接口获取数据,再定义接口,转化成为json输出.只供卡通网(kt5.cn)使用 stock.php 接口处理代码 <?php header("Content-Typ ...
- 透过systemctl管理mysqld服务
1. 背景 CentOS 7.x 之前的版本,系统启动时,第一支呼叫的程序是 init ,然后 init 去唤起所有的系统所需要的服务,无论是本地服务还是网络服务.所有的服务启动脚本都放置于 /etc ...