题解 | 预知

预知

https://www.nowcoder.com/practice/d9dd8fb2f4ca4385b3728b7bf9c9d0a4

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void (async function () {
    // Write your code here
	// 如果只有一种种类,那么无论如何都会输
    // 最好的情况下,所有种类只有一张,那么不需要预知
    // 较好的情况下,张数最多的拿到只剩一张,如果此时其他种类都是各一张那么也是随便拿
    // 最差的情况下,我把张数最多种类的预知完,那么剩下的拿哪张都是不一样的
    
    await readline();
    while (await readline()) {
        const line = await readline();
        let tokens = line.split(" ").map((item) => Number(item));
		// 只有一种种类,一定输
        if (tokens.length == 1) {
            console.log(-1);
            continue;
        }
		// 所有种类只有一张
        if (Math.max(...tokens) == 1) {
            console.log(0);
            continue;
        }
		// 张数最多的拿到只剩一张,如果此时其他种类都是各一张那么也是随便拿
        if(tokens.filter(item=>item==1).length==tokens.length-1){
            console.log(Math.max(...tokens)-1)
            continue;
        }
		// 最差的情况下,我把张数最多种类的预知完,那么剩下的拿哪张都是不一样的
        console.log(Math.max(...tokens))
    }
})();

全部评论

相关推荐

哇哇的菜鸡oc:他这不叫校招offer,而是实习offer
点赞 评论 收藏
分享
10-30 19:23
已编辑
山东大学(威海) C++
牛至超人:其实简历是不需要事无巨细的写的,让对方知道你有这段经历就行了,最重要的是面试的时候讲细讲明白
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务