当前位置: 首页 > news >正文

LeetCode -- Bulb Switcher

题目描述:
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the ith round, you toggle every i bulb. For the nth round, you only toggle the last bulb. Find how many bulbs are on after n rounds.


Example:


Given n = 3. 


At first, the three bulbs are [off, off, off].
After first round, the three bulbs are [on, on, on].
After second round, the three bulbs are [on, off, on].
After third round, the three bulbs are [on, off, off]. 


So you should return 1, because there is only one bulb is on.


N个灯泡初始化为关闭。
有N个灯泡,遍历N轮,在第i轮中对每i个灯泡进行toggle操作。toggle:开->关;关->开。
分析:
N个灯泡中被操作偶数次的一定是关闭,奇数次的为开。
因此,对于数字N,遍历从[1,N]的每个数字,找出能够被整除奇数次的进行求和即可。


方案一:


public int BulbSwitch(int n)
{
	var sum = 0;
	for(var i = 1;i <= n; i++){
		var count = 0;
		for(var j = 1; j<= i ;j++){
			if(i % j == 0){
				count ++;
			}
		}
	
		if(count % 2 != 0){
			sum++;
		}
	}
	
	return sum;
}



复杂度为N^2,当N=99999时直接超时。


方案二:
N=1,result=1 (1)
N=2,result=1 (1)
N=3,result=1 (1)
N=4,result=2 (1,4)
N=5,result=2 (1,4)
N=6,result=2 (1,4)
N=7,result=2 (1,4)
N=8,result=2 (1,4)
...
规律:
当N=K,解的个数为K中包含完全平方数的个数,分析:
对于数字X,假设X不为完全平方数,意味不存在K1*K2=X 且 K1=K2。则无论K为几,到最后总是关闭状态,因为K被操作的次数总为偶数次。
因此题目进一步转化为,求小于N的完全平方数的个数。


private List<int> cache = new List<int>();
    public int BulbSwitch(int n) {
        if(n == 0){
            return 0;
        }
       int start = 0;
    	if(cache.Count > 0){
    		var len = cache.Count- 1;
    		for(var i = len;i>=0;i--){
    			if(cache[i]<n){
    				start = i;
    			}
    		}
    	}else{
    		cache.Add(1);
    	}
    	
    	for(var i = start+1; 2*i + 1 + cache[i-1] <=n ;i++)
    	{
    		cache.Add(2*i + 1 + cache[i-1]);
    	}
    	
    	return cache.Count();
    }



解法三:直接返回N的平方根。
public int BulbSwitch(int n) {
        return (int)Math.Sqrt(n);
}



相关文章:

  • 我在业务架构平台研讨会上的演讲视频
  • leet code -- Swap Nodes in Pairs
  • LeetCode -- Range Sum Query - Immutable
  • 嵌入式产品认证相关的小知识
  • leet code - Third Maximum Number
  • 关于中国软件发展的讨论沙龙
  • SQL server replication的三种方式
  • leetcode -- Count Numbers with Unique Digits
  • javascript小数四舍五入
  • 业务场景和业务用例场景的区别(作者:Arthur网友)
  • Android -- 打开时隐藏软键盘
  • 邀请大象一书的读者和广大网友写关于分析设计、建模方面的自愿者文章
  • Android -- 读取NFC卡号
  • Windows 7安装以及VS2008和Office2007冲突的问题
  • C# Windows form application 播放小视频
  • 03Go 类型总结
  • CoolViewPager:即刻刷新,自定义边缘效果颜色,双向自动循环,内置垂直切换效果,想要的都在这里...
  • Docker下部署自己的LNMP工作环境
  • Git初体验
  • Java新版本的开发已正式进入轨道,版本号18.3
  • js算法-归并排序(merge_sort)
  • JS正则表达式精简教程(JavaScript RegExp 对象)
  • Promise初体验
  • Protobuf3语言指南
  • React 快速上手 - 07 前端路由 react-router
  • UMLCHINA 首席专家潘加宇鼎力推荐
  • 从零到一:用Phaser.js写意地开发小游戏(Chapter 3 - 加载游戏资源)
  • 多线程 start 和 run 方法到底有什么区别?
  • 今年的LC3大会没了?
  • 开发了一款写作软件(OSX,Windows),附带Electron开发指南
  • 前端技术周刊 2018-12-10:前端自动化测试
  • 软件开发学习的5大技巧,你知道吗?
  • 设计模式走一遍---观察者模式
  • 使用阿里云发布分布式网站,开发时候应该注意什么?
  • 手写双向链表LinkedList的几个常用功能
  • 腾讯大梁:DevOps最后一棒,有效构建海量运营的持续反馈能力
  • 学习笔记TF060:图像语音结合,看图说话
  • 正则表达式小结
  • - 转 Ext2.0 form使用实例
  • 看到一个关于网页设计的文章分享过来!大家看看!
  • # 数据结构
  • # 再次尝试 连接失败_无线WiFi无法连接到网络怎么办【解决方法】
  • $GOPATH/go.mod exists but should not goland
  • (06)金属布线——为半导体注入生命的连接
  • (C语言)输入自定义个数的整数,打印出最大值和最小值
  • (二)hibernate配置管理
  • (附源码)ssm旅游企业财务管理系统 毕业设计 102100
  • (转)关于pipe()的详细解析
  • (转载)在C#用WM_COPYDATA消息来实现两个进程之间传递数据
  • .naturalWidth 和naturalHeight属性,
  • .net core 微服务_.NET Core 3.0中用 Code-First 方式创建 gRPC 服务与客户端
  • .net core使用RPC方式进行高效的HTTP服务访问
  • .net程序集学习心得
  • .net利用SQLBulkCopy进行数据库之间的大批量数据传递
  • @31省区市高考时间表来了,祝考试成功