[CI] Bulb Switcher

hanging light bulb

 

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 nth round, you only toggle the last bulb. Find how many bulbs are on after n rounds.

It’s quite a interesting problem and more like a math problem than a coding one. The direct solution is do the swith round by round and we can get the result at the final round. The time complexity of the direct solution is O(n2). The example code is as follows,

public int bulbSwitch(int n) {
        int[] lights = new int[n];
        int cnt = 0;
        int i = 0;
        // initailize the array
        for (i = 0; i < n; i++) {
            lights[i] = 0;
        }
        // do the round toggle
        for (i = 0; i < n; i++) {
            for (int j = i; j < n; j += i + 1) {
                lights[j] = lights[j] == 0 ? 1 : 0;
            }
        }
        // get the result
        for (i = 0; i < n; i++) {
            cnt += lights[i];
        }
        return cnt;
}

Seems like a perfect answer which definitely is not. When I submitted the above code on LeetCode OJ, the platform threw out an error of RUNNING TIME OUT. So it’s time to get the code optimized.
As you can see, each one of the bulb is turned on or off for n times which is the number of divisors. But still this cannot solve our problem for the time complexity is O(n2) too! What about printing the result to see whether there is a pattern? Let do it.
when n=40, the result is 1001000010000001000000001000000000010000 . And when n=60, the result is 100100001000000100000000100000000001000000000000100000000000 .
What do you see, guys? Yes, the mumber of each group which has only one 1 is an Arithmetic sequence. So we can get the final result without any toggling rounds. A half but passed solution is as follows,

public int bulbSwitch(int n) {
        int cnt = 0;
        for (int i = 3; n > 0; i += 2) {
            n -= i;
            cnt++;
        }
        return cnt;
}

That’s it.
The time complexity of the above code is O(n) and the code can pass the OJ’s test. But is this the most optimized one? No, of course. If you know more maths, the above code can be optimized to the time complexity of O(1) which means there is some formula to get the final result. Can you figure it out, buddy?
By the way, the Ultimate answer is  SQRT(n).
source page: https://leetcode.com/problems/bulb-switcher/

健身计划一期(2015.8.28-2015.10.31)

person holding barbell

大家相互监督、相互鼓励,把身体练好!
目标
体重不做高要求,减10斤左右即可,关键是脂肪减下去,肌肉长起来
人员
jasper, bill, xinli
一期周期
2015.8.28-2015.10.31
时间
每周保证四天,每天中午至少一小时
流程
11:40出发,健身完毕后洗澡,吃饭
健身流程

  • 跑步30min以上
    • 慢跑,心率140-160 beat/min
  • 柔韧5min
    • 压腿
    • 扭腰
    • 胳膊
  •  力量15min,包括
    • 臂力:选择合适重量,每组15个,至少两组,做到不能做为止
    • 腹肌:仰卧起坐或够腿,每组15个,做到不能做为止
    • 卧推:选择合适重量,每组20个,至少两组,做到不能做为止
  • 放松活动10min

饮食

  • 健身时多饮水
  • 多吃蛋白质,如蛋清、瘦肉、鸡肉、豆制品、脱脂奶等
  • 少吃碳水化合物,如米饭、馒头、面条等主食,糖类等
  • 不吃油炸、肥肉、坚果等高脂肪含量的食物
  • 不喝饮料(0热量的可以)
  • 晚饭少吃
  • 保证睡眠,良好的睡眠是最轻松的减肥方式

面试程序题-二分查找(1)

black binocular on round device

一个数组,大小先减后增,请找到增减部分的分界点,要求算法时间复杂度O(logN)。
下面给出了一个递归实现的版本。
这个问题还可以让查找其中的某个元素,也是二分,思路一样。

#include <iostream>
#define MAX 13
using namespace std;
void findBreakpoint(int* a, int starti, int endi){
int i = (endi-starti)/2+starti;
if(starti==i){
cout<<i<<endl;
return;
}
if(a[i]<a[i+1]){
findBreakpoint(a, starti, i);
}else{
findBreakpoint(a, i+1, endi);
}
}
int main(){
int a[MAX] = {12, 11, 10, 8, 5, 6, 7, 9, 11, 12, 13, 20, 50};
findBreakpoint(a, 0, MAX-1);
return 0;
}

man and woman near table

一面在准备一些基础的算法,一面在准备语言相关,泛技术渣太苦逼了,在准备两种语言PHP和Java,而我最近一直在做是却是C++,是不是相当奇葩。。。

关于Java类的hashCode和equals方法

前面两篇文章中提到了利用哈希表的数据结构,如HashMap,HashSet等,这些数据结构利用对象的hashCode和equals函数来识别对象是否相同,原则如下:
* hashCode不同的对象一定不同
* hashCode相同的对象不一定相同
比方说HashMap,查找元素时,会先以key的hashCode来查找,如果找到对象,然后再以equals方法来判定是否相等,如果相等,才返回相应的value。
自定义数据结构做key时,需要成对的实现两个函数才可以正确的使用利用哈希表实现的数据结构。

Java中HashSet, TreeSet, LinkedHashSet区别

三个结构都实现了Set接口,不允许含有重复元素。
* HashSet是用哈希表实现的,增删查的复杂度都是O(1),不能保证元素的顺序
* TreeSet是用一个树形结构实现(红黑树),增删查的复杂度为O(log(n)),维护一个排序的Set,可以用 first(), last(), headSet(), tailSet()等获取最大最小的元素。
* LinkedHashSet是用链表+哈希表实现,因为是Linked,所以可以保持插入的顺序,增删查复杂度都是O(1)

Java中ArrayList,Vector,LinkedList区别

三种结构都实现了Collection接口和List接口。而LinkedList还实现了Queue接口。
内部实现区别:
* ArrayList可以理解为一个可变长度数组,特点是内存连续,随机访问快,插入、删除慢,扩展容量时,每次增加原容量的50%;
* Vector可以理解为线程安全的ArrayList,扩展容量时,每次容量翻倍;
* LinkedList实现上为一个双向链表,随机访问慢,插入、删除快。
refrence: http://www.programcreek.com/2013/03/arraylist-vs-linkedlist-vs-vector/

内存数据更新策略总结

因为后台线程更新数据,而前台继续服务,所以涉及数据共享的问题。

解决方案大致有以下几种:

  1. 使用读写锁
  2. 开辟两份内存,reload时来回切换
  3. 在reload的时候开辟一块内存,切换之后,等待一段时间,前台线程全部切换为新内存后,把老内存释放

方法1效率相对低下,不考虑。

方法2完全可以满足需求,并且不用担心前台线程引用到不可用的数据,如果是配置文件之类,完全没有问题,但是如果占用内存比较可观时,成本较高,可行性不高。

方法3效率高且不需要长期占用双份内存,是较好的方法,但是在实践中,还是需要注意一些问题:

  • 读取数据的线程,需要将一个临时指针指向使用的数据(不要使用成员指针),防止在查询过程中(可能有遍历等比较费时的操作)内存被更新的问题,可能导致程序core掉。
  • sleep的时间尽可能长些,以等待后台线程更新内存(要考虑到如果更新线程每次是新起的话,更新间隔不能太小,如果是单一线程,就不用考虑这问题)

wdlinux面板wdcp失效的问题

前些日子帮一个朋友恢复wdcp的数据,恢复完成之后发现站点操作全部执行失败,仔细找了一天无果。后来将wdcp的wdapache、wdphp打开错误日志,看到了一个sudo相关的错误,本来就知道应该是权限导致的问题,现在sudo的错误提示更说明了这一点。
于是乎打开sudo的配置文件/etc/sudoers,发现home是有特殊权限的,于是将home分区重新加载到别的分区,然后在wdcp系统面板中更改了新分区,问题解决。
以前一直没想明白面板用户是怎样管理别的用户的文件的,我太傻了,可以sudo嘛,当然为了安全起见,最好在/etc/sudoer里指定用户可以执行的命令。