Thursday, February 27, 2014

第二周主题:链表 之 Linked List Cycle in LeetCode (in java)



要求:
I Given a linked list, determine if it has a cycle in it.
II Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
Follow up: Can you solve it without using extra space?
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
(给定一个ListNode结构的链表,(I)看是否有cycle;(II)看循环在哪个节点,如果没有cycle,返回null)
解答:
(I) 设定两个index node: slow and fast,沿着链表的顺序跑下去:slow跑得慢,每次跑一步;fast跑得快,每次跑两步。如果有cycle的话,迟早两个index是会相遇的。
(0) ListNode slow = head; ListNode fast = head;
(1) while (true) 循环内slow = slow.next; fast = fast.next.next; (注意先判断fast.next是否为null)
  • fast.next == null -> return false;
  • slow == null -> return false;
  • fast == null -> return false;
  • slow == fast -> return true;
(II) 假设找到了cycle (slow == fast),怎么找是哪个节点开始呢?我们引入一个ListNode pre = head; pre每次跑一步,slow循环一圈找是否slow == pre。那怎么判断slow是否循环一圈呢?别忘了,我们的fast还在那呢,保持fast不动,只要看是否slow == fast就知道了。
(0) slow == fast is true
(1) while (pre != slow) 循环内 slow = slow.next;
  • slow跑圈 
while ((slow != pre) && (slow != fast)) { slow = slow.next; }
  •  如果slow跑完了,或者遇到pre了
if (slow == pre)
break;
else
pre = pre.next;
(2) return slow;


References:

Tuesday, February 18, 2014

LeetCode Notes001

Albert Chen's Week001

  • MergeSortedArray (Solution088)
http://oj.leetcode.com/problems/merge-sorted-array/
Merge from the end


http://oj.leetcode.com/problems/single-number-ii/

(0)初始设定ones=0; twos=0;
(1)用twos记录到当前为止,二进制”1“出现3k+2次的位数;twos = twos | (ones & A[i]);
(2)用ones记录到当前为止,二进制“1”出现3k+1次的位数;ones = ones ^ A[i];
(3)当ones和twos的某一位同时为”1“时,即该位出现二进制”1“ 3k+3次(也就是3k次),清零该位。xthrees = ~(ones & twos); // xthrees 是二进制“1”没有出现3k次的位数ones = ones & xthrees;twos = twos & xthrees;
(4)遍历完数组中所有的数之后,ones记录的就是最终结果。

  • SortColors (Solution075)

http://oj.leetcode.com/problems/sort-colors/
int i = 0; // for end of zeros
int j = n; // for start of twos;
int k = 0; // iterator

  • Permutation(Solution045), PermuationII(Solution046), NextPermutation(Solution030)

http://oj.leetcode.com/problems/permutations/
http://oj.leetcode.com/problems/permutations-ii/
http://oj.leetcode.com/problems/next-permutation/

(0)  input: int[] num
(1) sort(num)
(1.5) do {add}while (nextPermutation is true)
(2-7) boolean nextPermutaion()
  • (2-6) 循环体
    • (2) 从后往前找到最大的倒序subarray of num (i, num.length-1)
      • for i = num.length-1; i >0; i--
      • if num[i-1]<num[i]
    • (3) 从后往前在subarray里找到第一个num[j]大于num[i-1]
      • for j = num.length-1; j>=i; j--
      • if num[j]>num[i-1]
    • (4) swap(num[j],num[i-1])
    • (5) reverse subarray (i, num.length-1)
    • (6) return true
  • (7) 终止条件,并且维持next permutation的语义
      • reverse num(0, num.length-1) and return false;






Monday, February 10, 2014

MATLAB Distributed Computing

1. Installing and Configuring Parallel Computing Toolbox and MATLAB Distributed Computing Server

1.1 System Requirements

Required:
SSE2

1.2 Parallel Computing with MATLAB on a Cluster
Interaction between the Distributed Computing productsParallel computing with MATLAB on a cluster requires two products:
  • Parallel Computing Toolbox (formerly Distributed Computing Toolbox) should be installed on the computer where you write your applications.
  • MATLAB Distributed Computing Server (formerly MATLAB Distributed Computing Engine) should be installed on each computer of the cluster that performs the computation.
1.2.1 Install Products and Configure Your Cluster (latest release)
1.2.1.1 install Licence Manager in Server

使用MDCE引擎建立集群,需要获得Mathworks公司的授权。Licence Manager可以通过Network方式下安装,在standalone方式下安装是没有的。在一个集群中只要一个Node安装License Manager,其它节点就可以获得授权。
  • 安装参考
选择network安装
1) choose "install manually without using the internet" 
2) enter the "file installation key"   xxxxx-xxxxx-xxxxx-xxxxx-xxxxx
3) check out "license manager" option
4) use "license_server.dat" when asked for license file
  • 启动参考
Windows Platform
1) 以管理员模式运行$MATLABROOT$/etc/win64/lmtools.exe
2) 选择Start/Stop Reread标签,单击Start Server
Linux&Unix Platform
1) 在$MATLAB$/etc/license.dat中有SERVER一栏,后面填入your hostname
2) 将license server的端口设置成27000
3) 设置DAEMON MLM为DAEMON MLM $MATLAB$/etc/lm_matlab 
4) 设置OPTIONS=$MATLAB$/etc/lmopts.sh
5) 用普通用户运行./lmstart,开启License Manager

1.2.1.1 install Matlab Distrubuted Computing Engine (分布式计算引擎MDCE的安装)

  • Windows Platform
1) 用管理员启动cmd.exe, cd %MATLABROOT%/toolbox/distcomp/bin
2) > mdce install
3) > mdce start
4) WIN+R,输入services.msc,查看MDCS是否启动
5) 运行addMatlabToWindowsFirewall.bat, 配置防火墙,开放MDCE服务

说明windows服务默认Local System帐号启动,用Local System启动的服务是没有权限访问UNC(Universal Naming Convention)命名约定的网络资源,UNC格式为\\servername[server IP]\sharename\directory\filename。MDCE中传输和共享文件都是使用UNC命名约定,因此要使MDCE支持文件共享,启动它的用户需要有权限访问UNC网络资源。为了配置方便,用登入帐号启动MDCE服务,就可以访问共享资源。
  • Linux&Unix Platform
1) $cd $MATLABROOT$/toolbox/distcomp/bin
2) #./mdce install
3) #./mdce start
4) config iptables, make input chain ports tcp(135,139,445) and udp(137,138) open
说明在UNIX LIKE的系统中mdce是以ROOT身份启动的,可以轻松访问UNC格式的网络资源。
  • DNS设置
MDCE引擎使用机器名(hostname)在各个节点之间通信。为了能够正确解析集群中所有的node,需要在各个node的hosts文件中添加集群中所有机器的ip和hostname的映射条目。

1.2.1.2 调度程序(Job Manager)和work node启动配置

  • 启动Job Manager
在集群中所有的提交的Job都是通过Job Manager分配和管理的,所有的work node都是注册在Job Manager这台机器上,需要固定这台Job Manager主机。client端编写完任务就可以向这台Job Manager这台主机提交。MDCE支持当前所有主流的作业调度程序如LSF, CCS, PBS Pro等。作为实验性的环境,第三方调度程序没有配置,而是采用了随MDCE一起发布的Job Manager来管理集群中的作业调度。 Jm.jpg

Notes:

if you can't run matlabpool, try this in command line:>> distcomp.feature( 'LocalUseMpiexec', false )
or
>> matlabpool close

Reference:

第一周主题:数组 之 Single Number II in LeetCode (in java)


要求:
Given an array of integers, every element appears three times except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
(给定一个包含n个整数的数组,除了一个数出现一次外所有的整数均出现三次,找出这个只出现一次的整数。)
解答:
这是一道很有趣的位运算题。百思不得其解,最后看了[1]介绍的思路。

如果用二进制来表达数组中每一个数字的话,如果我们遍历一遍数组中的所有的数,即将数组中所有数的二进制表达的每位上的“1”都数一遍,那么除了需要找的那个只出现一次的整数外,其他所有的数字二进制表示中每一位“1”都出现了3k次(k为非负整数)。
如果能有办法将所有的二进制表达中这些3k个“1”清零,那么剩下的二进制表达就表示了需要找到的数。

(0)初始设定ones=0; twos=0;

(1)用twos记录到当前为止,二进制”1“出现3k+2次的位数;
twos = twos | (ones & A[i]);

(2)用ones记录到当前为止,二进制“1”出现3k+1次的位数;
ones = ones ^ A[i];

(3)当ones和twos的某一位同时为”1“时,即该位出现二进制”1“ 3k+3次(也就是3k次),清零该位。
xthrees = ~(ones & twos); // xthrees 是二进制“1”没有出现3k次的位数
ones = ones & xthrees;
twos = twos & xthrees;

(4)遍历完数组中所有的数之后,ones记录的就是最终结果。

扩展:

同样的道理,可以解决{给定一个包含n个整数的数组,除了一个数出现两次外所有的整数均出现三次,找出这个只出现两次的整数。},return twos就行了。

更强的扩展: {给定一个包含n个整数的数组,有一个整数x出现b次,一个整数y出现c次,其他所有的数均出现a次,其中b和c均不是a的倍数,找出x和y。}可以参见[1],思路是一样的。

Reference: