Leetcode Elemination Game
题目网址:https://leetcode.com/contest/2/problems/elimination-game/
题意: 给定一个从1到n的数列,第一次从最左边开始,每隔一个淘汰一个数字。然后从剩下的数字中,最右边的数字开始,每隔一个淘汰一个数字。重复上述步骤,求最后剩下的那个数字。
解析:是一个模拟题。只需要确定每趟淘汰赛开始时的数字即可(最后一个数字就是最后一趟淘汰赛的开始的数字)。
假设第i趟开始的数字为start,此时等差数列的差(distance)为2^i,数字个数为n/(2^i) (t)
则最后一个删除的数字与第一个删除的数字的差为distance*(t-1),且最后一个删除的数字与下一趟第一个删除的数字的差为distance/2.
另外,需要注意的一点是,当n=2*k + 1时与 n-1 = 2*k,最后剩下的数字相同。代码如下:
int lastRemaining(int n) {
int ans = 1;
int cnt = 0;
int distance = 1;
while(n > 1){
n /= 2;
cnt ++;
distance *= 2;
if(cnt&1){
ans += distance*(n-1) + distance/2;
}
else{
ans -= distance*(n-1) + distance/2;
}
}
return ans;
}
Leetcode Elemination Game的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- Mybtis框架总结(一)
一:Mybaits下载并搭建核心框架 1:下载mybatis的jar包: 2:创建mybatis框架链接数据库的配置文件Configuration.xml,格式如下 <!DOCTYPE conf ...
- vsftp 根据用户设置
#vsftpd.conf ###############pam_service_name=vsftpduserlist_enable=YEStcp_wrappers=YESlocal_root=/da ...
- JAVA-系统-【2】-创建自增长的用户表
[2]创建数据库表 用户表 自增 1.用户表结构 数据excel 表1 2.创建表 Create table A_USER( id number primary key, username ) n ...
- SqlLocalDB使用笔记
一.介绍 SqlLocalDB是VS安装时附带的数据库软件,相当于精简版的SQL Express. 二.使用 VS版本为2015,默认安装位置为:C:\Program Files\Microsoft ...
- python---常见的数据队列
一.FIFO队列(先进先出) #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = 'zhoufeng' #FIFO队列 import q ...
- 寻找 nani (1)
test.pl文件room(kitchen). room(office). room(hall). room('dining room'). room(cellar). location(desk,o ...
- MD5在java中的使用
MD5是什么? MD5是message-digest algorithm 5(信息-摘要算法)的缩写,被广泛用于加密和解密技术上,它可以说是文件的"数字指纹".任何一个文件,无论是 ...
- angularJs基础
AngularJs是为了克服Html在构建应用上的不足而设计的.Html是一门很好的为静态文件展示设计的声明式语言,但是要构建web应用的话就显得乏力了.所以我做了一些工作来让浏览器做我瞎向要的事. ...
- CALayer
刚刚无聊,画了一个月亮. - (void)viewDidLoad { [super viewDidLoad]; self.view.layer.backgroundColor = [UIColor b ...
- DSP下的#program
2014年7月22日 最近调试使用TMS320C6713的片子调试SDRAM,中间经过很多波折,这里就不吐槽了. 想将数据或者代码放到SDRAM上一定要用到#pragma .查阅资料后,感觉百度文库的 ...