xie 的个人资料追忆似水流年照片日志列表 工具 帮助

日志


5月31日

解江湖棋局,迎可人MM

此乃一转载文章。"MM不是那么容易得到的。本局名叫《丹凤朝阳》,是象棋江湖排局中“八大名局”之一。引用水木象棋版的解释: 这是象棋中的“江湖排局”,已有数百年历史。所谓“江湖排局”,封建社会时期就有人在路边摆棋摊赢钱维持生计。排局就是编排出来的局的意思,这类棋大多是一些热衷于欣赏象棋环环相扣的精彩杀法的一些人精心编排出来的,有些经典局例需要多次修改,最终成型。江湖排局一般最终结局是和棋,但是中间过程很复杂,双方都有一些陷阱。象棋初级爱好者者极其容易坠入陷阱。" "解此局的关键在于 - 这个mm摆局的意图就是告诉你: 别急着打炮 要挺帅 要送车 她也会送你一个孩子(卒)"
5月8日

MS interview question

Give a interger array of size n, how to find a contiguous subarray whose

sum is equal to k.if there is, return 1, else 0.

e.g.  array[n]={1, 3, -4, 8, -1}    k=10
because 3+8-1=10, then return 1;

What is the best time complexity??

=================================================================
 
I think the following is O(nlogn).

Given an array A in which we need to find the contiguous sub-array of sum k, create a new Array B such that

B[i] = Sum j = 1 to i A[i]

i.e B[m] = A[1] + A[2] + ...  A[m] is the prefix sum array of A.

Now create a third array C such that C[i] = (B[i], i) (ordered pairs)

Now sort array C using the values B only (i.e ignore the second co-ordinate of the ordered pair).

Now for each element C[p] =  (B[q],q) do a binary search for B[q] + K in C. Say you find the element(s) C[r] = (B[s], s)

if q < s, then we are done, else continue.

(Note, we might have to create a new array C' by merging sub-arrays with same value of B and also store the set of indices of B which take that value i.e. instead of just using the ordered pair (B,i) we use (B, {i1, i2, ..., ik}) and do a binary search on C')
+++++++++++++++++++++++++++++++++++++++++++++++++++
这个题目不同于找最大之和,最大之和的解能在O(1)空间下,在O(n)时间内完成
5月4日

finding the single number in a 2n+1 numbers array

----------------------------------------------------------
I have an array consisting of 2n+1 elements. n elements in it are
married, i.e they occur twice in the array, however there is one element
which only appears once in the array. I need to find that number in a
single pass using constant memory. {assume all are positive numbers}
Eg : 3 4 1 3 1 7 2 2 4
Ans: 7
===============================================
var array = [3, 4, 1, 3, 1, 7, 2, 2, 4]
var remaining = 0;

for(var i = 0; i < array.length; i++)
{
  remaining ^= array[i];
}

return remaining;

tech interview

sort the array...

given an array of size n.. sub arrays of a[0:k] and a[k+1:n] are sorted..
 sort the array in O(n) time and O(1) space
 
==========================================================
首先,应该用原地置换的思想。2,用两个指针,分别指向0和k+1位置。两个比较,
小的换到0的那个指针(表示全局的较小),后面那个指针得到的换过来的元素要和它的后续比较一下,
要确定这个指针所指是后半部分的最小。这样两指针一直从左到右移动,就排序完了。