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 ...
随机推荐
- 使用Fluent配置表关系
转载MS官方文档:https://msdn.microsoft.com/zh-cn/data/jj591620 Configuring Relationships with the Fluent AP ...
- tomcat7/8 启用调试模式,可进行远程调试
tomcat7,和 tomcat6 的jpda 不一样,tomcat7已经把jpda配置的属性在catalina.sh/catalina.bat里面已经写好了,我们不需要向tomcat6那样去设置参数 ...
- golang 前置补0
package main import ( "fmt" ) func main() { a := 1 fmt.Println(a) //前置补0 fmt.Printf(" ...
- hashMap的get()方法,错用并发造成cpu和负载高
一次线上问题的解决 线上发现服务cpu使用达到98%,负载高达200多,64核心cpu,下面介绍解决过程: 1.top命令查出占用cpu高的进程pid 2.使用jstack -l pid >du ...
- 树莓派(rasperberry pi 2)上装mysql远程无法访问
于是组合关键字(树莓派 mysql)求百度问谷歌.未果.操刀自己来吧.检查mysql的配置文件 /etc/mysql/my.cnf 发现如下配置 # Instead of skip-networkin ...
- SQL Server 显示执行一条语句的执行时间
set statistics time on执行语句set statistics time off
- iOS Safari 上加载的最大的图片尺寸
做WAP端项目时发现, 写css代码显示图片, 却显示不出.或用canvas 来加载图片的某一部分的时候显示不出, 代码如下: background: url() no-repeat -1000px ...
- PHP面向对象.__set(),__get(),__isset(),__unset()四个方法的
一般来说,总是把类的属性定义为private,这更符合现实的逻辑.但是, 对属性的读取和赋值操作是非常频繁的,因此在PHP5中,预定义了两个函数”__get()”和”__set()”来获取和赋值其属性 ...
- 线程小demo
下午就手写了两个demo,整理了一下. #!/sur/bin/env python # -*- coding:utf-8 -*- __author__ = 'ganzl' import threadi ...
- 优化 从Draw Calls到GC
原文出处: 慕容小匹夫的博客(@慕容小匹夫) 欢迎分享原创到伯乐头条 前言: 刚开始写这篇文章的时候选了一个很土的题目...<Unity3D优化全解析>.因为这是一篇临时起意才写的文章 ...