英语流利说技术类笔试题

编程题

在这里插入图片描述
在这里插入图片描述

//通过率只有0.8。不知道哪里判断出问题了
const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});


var inputs=[]; //用于存储每行的输入

rl.on('line',function(data){
    var can=[1,5,10,20,50]
    var t=parseInt(data)
    if(t===0){
        console.log(0)
    }else {
        console.log(combinationSum(can,t))
    }



});
var combinationSum = function(candidates, target) {
    candidates.sort((a,b) => a - b);
    const results = [];
    const innerFunction = (currArr, target, index) => {
        if (target === 0) {
            results.push(currArr.slice());
            return;
        }
        for (let i = index; i < candidates.length; i += 1) {
            if (target >= candidates[i]) {
                const newTarget = target - candidates[i];
                currArr.push(candidates[i]);
                innerFunction(currArr, newTarget, i);
                currArr.pop();
            } else {
                return;
            }
        }
    }
    innerFunction([], target, 0);
    return results.length;
};




简答题

You all know the simple 2 egg puzzle
Statement: You are given 2 identical eggs. You have access to a 100-storey building. Eggs can be very hard or very fragile means it may break if dropped from the first floor or may not even break if dropped from 100th floor. But if an egg breaks from certain floor it will certainly break from higher floors too. You are given task to figure out the highest floor of a 100-storey building an egg can be dropped without breaking. Now the question is how many drops you need to get certain about the floor from which eggs start breaking. You are allowed to break only 2 eggs in the process.
Unlike my previous posts where most people know the solution part for simple problem, for this problem mostly people only know the answer part and completely unaware of how it has been derived.

参考答案

Suppose N is the maximum number of drops to determine the breaking floor then first egg should be dropped from N if it breaks we can identify the floor with second egg in linear time. If the 1st egg doesnt break then we left with N-1 more attempts so instead of 2N floor we have to drop egg from 2N-1 floor (so that if it break we can identify the floor linearly in N-2 attempts with 2nd egg)
Dropping of 1st egg so follow this pattern(let say S): N…(N+N-1)…(N+N-1+N-2)…(N+N-1+N-2+N-3)……upto…(N+N-1+N-2……+1)
And as mentioned before if egg 1 breaks then follow with egg 2 linearly.
Summing the equation S we get
S=N*(N+1)/2 ….i hope no need to explain this simple AP.
Now we have 100 floors so S should just be greater than or equal to 100 to make it work
Therefore, N*(N+1)/2 >= 100
Hence we get N=14…the smallest satisfying this.

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值