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 ...
随机推荐
- C#读写者线程(用AutoResetEvent实现同步)(转载)
C#读写者线程(用AutoResetEvent实现同步) 1. AutoResetEvent简介 通知正在等待的线程已发生事件.无法继承此类. 常用方法简介: AutoResetEvent(bool ...
- TCP、UDP、IP 协议分析
http://rabbit.xttc.edu.cn/rabbit/htm/artical/201091145609.shtml http://bhsc881114.github.io/2015/06 ...
- shell中的语法(1)
反引号 命令替换.将命令的输出放在命令行的任意位置. eg. [root@gam ~]# echo The Data is `date` The Data is Fri Nov 18 10:13:56 ...
- 使用VideoToolbox硬编码H.264<转>
文/落影loyinglin(简书作者)原文链接:http://www.jianshu.com/p/37784e363b8a著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. ======= ...
- Python自动化 【第十四篇】:HTML介绍
本节内容: Html 概述 HTML文档 常用标签 2. CSS 概述 CSS选择器 CSS常用属性 1.HTML 1.1概述 HTML是英文Hyper Text Mark-up Language(超 ...
- android 多线程下载
看代码: public class MainActivity extends AppCompatActivity { private final String TAG = MainActivity.c ...
- Xcode 7.2.1 下载地址
Xcode 7 下载地址(需要登录苹果开发者帐号) 7.2.1:https://developer.apple.com/services-account/download?path=/Develope ...
- webForm中的验证控件
1.非空验证控件:RequireFieldValidator :2.数据比较验证:CompareValidator :3.数据范围验证:RangeValidator :4.正则表达式验证:Regul ...
- React Test相关资料
karma 前端测试驱动器,生产测试报告,多个浏览器 mocha js的测试框架,相当于junit chai,单元测试的断言库,提供expect shudl assert enzyme sinon.j ...
- easyui datagrid 没数据时显示滚动条的解决方法
今天解决了一个bug,因为datagrid有多列,可是当没有数据的时候,后面的列无法通过滚动条拉动来显示,比较麻烦,而需求要求没有数据也要拉动滚动条查看后面有什么列,一开始在网上找了一些资料,发现都不 ...