找回密码
 注册

QQ登录

快捷登录

新浪微博登陆

搜索
CDD 法语助手

三道GOOGL的笔试题 请教这里的大牛们

19
回复
2839
查看
[ 复制链接 ]
头像被屏蔽

新浪微博达人勋

提示: 该帖被管理员或版主屏蔽
2010-2-17 14:32:45

新浪微博达人勋

不牛的帮顶
2010-2-17 15:50:37

使用道具 举报

新浪微博达人勋

回复 2# 小田


    谢谢啦
2010-2-17 18:24:28

使用道具 举报

新浪微博达人勋

数学和info的结合
第一题用dimention为n的cube解决
2010-2-17 22:21:19

使用道具 举报

新浪微博达人勋

本帖最后由 Dillon 于 2010-2-19 10:12 编辑
3 给stack加求最小值函数
对现在的Stack(栈)数据结构进行改进,加一个min()功能,使之能在常数,即O(1)时间内给出栈中的最小值。可对push()和pop()函数进行修改,但要求其时间复杂度都只能是O(1)
Elène85 发表于 2010-2-17 14:32



The idea is to store the current min value or its index for each push.
And the push could be at O(1) complexity because it only need to compare the new element with the previous min value.
Finally the pop and the min functions are quite straightforward.

Quick&dirty pseudo code only:

template
<class T> class RegularStack
{
public:
   
RegularStack
(){...}
   
push
(T NewElement){...}
   
T
pop(){...}
   
bool
bIsEmpty(){...}
protected
:
   
vector
<T> mao_StackData; // Stack data container.
};

template
<class T> class MinStack: public RegularStack<T>
{

public
:
   
MinStack():RegularStack()
{...}
   
push
(T NewElement)
   
{

        
if
( bIsEmpty() )
            
mao_MinStackData
.push_back(0);
        
else

            
mao_MinStackData
.push_back( T < peek( mao_MinStackData[ui_GetCount()-1] ) ? ui_GetCount() : ui_GetCount()-1 );
        
RegularStack
<T>::push(T);
    }
    T pop()
    {
        mao_MinStackData.pop_back();

        
return
RegularStack<T>::pop();
    }
    T min()
    {

        
assert
( !bIsEmpty() );
        
return
peek( maul_MinIndices[ui_GetCount()-1] );
    }

protected
:
    unsigned int ui_GetCount()
    {
        return mao_StackData.size();
    }
    T peek(unsigned int index)
    {
        assert(index < ui_GetCount() );
        return mao_StackData[index];
    }
    vector<unsigned int> maul_MinIndices; // Each element holds the index to the min element of the corresponding stack level.
};
2010-2-18 11:14:59

使用道具 举报

新浪微博达人勋

本帖最后由 Dillon 于 2010-2-19 10:16 编辑
2
有n个任务,每个任务运行时需要使用R的资源,运行完毕后需要占用O的资源(O<R), 假设现在我们总共有s的资源,请设计一个调度算法,能保证所有任务能顺利执行;举个例子,资源可以是内存;比如有两个任务1,2;R[1]=10,O[1]=2, R[2]=5,O[2]=3,总资源是10,如果先执行任务1,剩余资源10-2=8,可以执行任务2;反过来先执行任务2,剩余资源10-3=7 7<R[1]=10,无法执行任务1.Elène85 发表于 2010-2-17 14:32


Basic concept:
For this kind of scheduling problem, since there are many local minimal during the search, there's no way to use a global solution which converge. It means solutions like A* or genetic algorithm won't help to find the optimal best solution.
To really find the optimal best solution, we really have to test all possibilities, which is extemely time-consuming. It's the reaon why here they don't ask to find an optimal solution, instead, they only ask to find one working solution.
So here I would like to suggest an experimental solution as below.
And it's possible to use a simpler experimental solution in order to make it faster or more robust, so the solution here is only for illustrative purpose.

The observation is:
We could always try to run these tasks with minimal R( if several tasks have the same R, choose the one with the maximal O ), until some new tasks whese O exceed the current remaining time.
At this point, we must cancel the previous task, and choose one new task whese O exceeds.
Here the tricky part, we should NOT choose the task with biggest O, because it works for this task, but it could fail later on for another task with big O.
Therefore we are supposed to choose the task with the smallest R among these tasks with exceeding O.
But at the end, it could still fail, and the solution is to cancel some already scheduled taskes until it works.
If one already scheduled task has an even bigger O than the current task with problem, it makes no sense to cancel it, and it means the scheduling is bound to fail.

The solution is:
1) Try to run first the task with minimal R, if the new remaining time is still enough for the maximum O of all the remainning tasks.
2) However, if there's NO enough new remaining time for any remaining task:
    A) Find all remainning tasks whose maximum O is smaller than the previous remaining time, but bigger than the new remaining time. It's guarenteed to find at least one.
    B) Pick the one with the minimal R, and run it.
3) Enter into the next iteration, and if there's one remaining task whose O is bigger than the current remaining time, try to cancel previously scheduled taskes one by one.
4) If everything are cancelled, it fail.

class
Task
{

public
:
Run();
int O;
int R;
};

class
TaskSet
{

public
:
    TaskSet(){...};
    Task *GetTaskWithMaxO();
    Task *GetTaskWithMinR_ThenWithMaxO();
    Task *GetTaskWithMinR_WithOinRangeXY(x,y);
    bool bAnyRemainingTaskWithBiggerMaxO();
    void RemoveTask(Task *);
private
:
    vector<Task *> mapo_Tasks;
};

bool
bSchedular( int S, int N, vector<Task> oTaskSet )
{
    int RemainingS = S;
    While( !oTaskSet.bEmpty() )
    {
        Task *pTask1 = oTaskSet.GetTaskWithMaxO();
        if( pTask->O > RemainingS )
        {
            While( ScheduledTaskListIsNotEmpty() && ( pTask->O > RemainingS ) )
            {
                   Task *pTaskAddback = ScheduledTaskListI.RemoveTaskWithMinimalO_AndOisSmallerThanReference( pTask->O );
                   if( pTaskAddback == NULL)
                         return false;
                   TaskSet.Add(pTaskAddback );
                   RemainingS = TaskSet.RecalculateRemaingTime();
            }
            if( pTask->O > RemainingS )
                   return false;
        }
        Task *pTask2 = oTaskSet.GetTaskWithMinR_ThenWithMaxO();
        int RemainingSTemp = RemainingS - pTask2->S;
        if( !bAnyRemainingTaskWithBiggerMaxO(RemainingSTemp))
        {
            pTask2->Run();
            oTaskSet.RemoveTask(pTask2);
            ScheduledTaskList.push_back(pTask2);
            RemainingS -= pTask2->S;
        }
        else
        {
            Task *pTask3 = oTaskSet.GetTaskWithMinR_WithOinRangeXY( RemainingSTemp, RemainingS );
            pTask3->Run();
            oTaskSet.RemoveTask(pTask3);
            ScheduledTaskList.push_back(pTask2);
            RemainingS -= pTask3->S;
        }
    }
    return true;
}
2010-2-18 12:46:30

使用道具 举报

新浪微博达人勋

楼上牛人。。。很久没写代码的路过。。。
2010-2-18 14:11:38

使用道具 举报

新浪微博达人勋

计算机白痴也帮顶!
2010-2-18 23:03:46

使用道具 举报

新浪微博达人勋

其实我只想说,我连题目也没看懂{:12_497:}
2010-2-18 23:32:08

使用道具 举报

新浪微博达人勋

回复 6# Dillon


     谢谢DD政委  您老人家太牛了
2010-2-19 02:39:15

使用道具 举报

新浪微博达人勋

楼上牛人, non je suis pas d'accord, homme tigre!!
2010-2-19 12:06:07

使用道具 举报

新浪微博达人勋

1 如果我现在有一个n位的海明码序列,是(X1,X2,......Xm)这样一个序列,其中m=2^n;
那么n+1位的海明码序列因该是X1,X2,.....Xm 这个序列每个元素最高位前加一个0,以及Xm,Xm-1.....X2,X1每个元素最高位前加一个1,这样就形成了一个新的海明码序列(0X1,0X2,....0Xm,1Xm,1Xm-1....1X1)。
递归的主要的时间开销主要是讲n位海明码翻转,时间代价是n,这样就可以递归求解,递推式是T(n)=T(n-1)+n/2;
所以时间开销是T(n)=O(2^n), 2^n也就是序列长度

至于空间开销,就是O(2^n)
2010-2-19 14:01:56

使用道具 举报

新浪微博达人勋

本帖最后由 Dillon 于 2010-2-19 21:43 编辑
1 如果我现在有一个n位的海明码序列,是(X1,X2,......Xm)这样一个序列,其中m=2^n;
那么n+1位的海明码序列因该是X1,X2,.....Xm 这个序列每个元素最高位前加一个0,以及Xm,Xm-1.....X2,X1每个元素最高位前加一个1,这样就形成了一个新的海明码序列(0X1,0X2,....0Xm,1Xm,1Xm-1....1X1)。
递归的主要的时间开销主要是讲n位海明码翻转,时间代价是n,这样就可以递归求解,递推式是T(n)=T(n-1)+n/2;
所以时间开销是T(n)=O(2^n), 2^n也就是序列长度
至于空间开销,就是O(2^n)Elène85 发表于 2010-2-19 14:01


这个方法其实是最优解,但是前提是必须把递归改成迭代(当然也可以使用深度优先递归来保证如用迭代一样的顺序,这样虽然也能保证存储空间最优,但是效率低下很多)。

迭代Pseudo code如下:
void GetSerial( int n, unsigned int *OutputSerial )
{
      int CurrentElement = 0;
      OutputSerial[CurrentElement++ ] = 0;
      int PreviousLevelTotal = 1;
      for( int i = 0; i < n; i++ )
     {
           unsigned int CurrentLevelTotal = PreviousLevelTotal * 2;
           unsigned int OrMask = PreviousLevelTotal;
           for( int j = 0; j < PreviousLevelTotal; j++,CurrentElement++  )
                  OutputSerial[ CurrentElement ] = OutputSerial[ CurrentLevelTotal - 1 - CurrentElement ] | OrMask;
           PreviousLevelTotal = CurrentLevelTotal;
     }
}

如果n为0, 直接返回 OutputSerial[0] = 0
如果n为1,外循环一次,内循环一次,OutputSerial[1] = 1 ( =OutputSerial[1-1] | 1 )
如果n为2,外循环两次,内循环加两次,OutputSerial[2] = 11( =OutputSerial[3-2] | 1 ), OutputSerial[3] = 10( =OutputSerial[3-3] | 1 )
如果n为3,外循环三次,内循环加四次,。。。。。。。。。。。。。。。。。。。。

循环往复,以至无穷,时间和空间复杂度都是最低。
2010-2-19 18:53:12

使用道具 举报

新浪微博达人勋

INFO白痴进来瞻仰的....
2010-2-19 19:34:10

使用道具 举报

新浪微博达人勋

这个方法其实是最优解,但是前提是必须把递归改成迭代(当然也可以使用深度优先递归来保证如用迭代一样的 ...
Dillon 发表于 2010-2-19 18:53



      多谢政委指教 无限崇拜中 ............................
2010-2-19 19:37:54

使用道具 举报

12下一页
您需要登录后才可以回帖 登录 | 注册 新浪微博登陆

本版积分规则

返回顶部