三色球

普通解法

可以发现,如果可以兑换出张票,那么[1,x]张票都可以兑换出。
如果不可以兑换出x张票,那么[x,inf]张票都不可能兑换出。
所以这个答案是满足二分性质的。我们二分这个答案,考虑检查x张***是否可以兑换出来。
如果要换x张票,如果气球数量大于x,它就可以用于兑换其他气球。否则它需要被其他气球补全。所以按照这个逻辑去检查是否可行即可。

int solve(int a, int b, int c){
    int l = min(min(a, b), c);
    int r = max(max(a, b), c);
    int ans = l;
    while(l <= r){
        int mid = (r+l)/2;
        int ta = a-mid, tb = b-mid, tc = c-mid;
        int ok = 0;
        if(ta >= 0 && tb >= 0 && tc >= 0){
            ok = 1;
        }
        if(ta < 0){
            if(tb+2*ta >= 0 && tc+ta >= 0)ok = 1;
        }
        else if(tb < 0){
            if(tc+2*tb >= 0 && ta+tb >= 0) ok = 1;
        }else if(tc < 0){
            if(ta+2*tc >= 0 && tb+tc >= 0) ok = 1;
        }
        if(ok) ans = mid, l = mid+1;
        else r = mid-1;
    }return ans;
}

最优解法

贪心。设最少的气球是ret个,则一定可以兑换ret个彩票,然后用剩余的气球去填补用完的气球,可以填补出多少彩票可以直接算出来:比如现在0个红,b个黄,c个蓝,设兑换x个票,则 b-2x >= x, c-x >=x,得到x=min(b/3,c/2)。

int solve(int a, int b, int c) {
        // write code here
        int ret = min(min(a,b),c);
        a-=ret;
        b-=ret;
        c-=ret;
        if (a==0) {
            ret += min(b/3, c/2);
        } 
        else if (b==0) {
            ret += min(c/3, a/2);
        } 
        else {
            ret += min(a/3, b/2);
        }
        return ret;
}
全部评论

相关推荐

1.自我介绍2.介绍一下mcp,&nbsp;skills3.了解react哪些状态管理库4.对话是sse还是什么?是用fetch还是EventSource?5.ts中的any&nbsp;和&nbsp;unknown讲一讲6.是直接用组件库的组件还是自己封装了一些别的7.代码输出题1function&nbsp;main()&nbsp;{{var&nbsp;a&nbsp;=&nbsp;1let&nbsp;b&nbsp;=&nbsp;2}console.log(a);console.log(b);}main()console.log(a);8.什么是块级作用域&nbsp;全局作用域&nbsp;函数作用域9.代码输出题2for&nbsp;(var&nbsp;i&nbsp;=&nbsp;0;i&nbsp;&amp;lt;&nbsp;5;i++)&nbsp;{setTimeout(()&nbsp;=&amp;gt;&nbsp;{console.log(i);},&nbsp;100);}10.代码输出题3for&nbsp;(var&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&amp;lt;&nbsp;5;&nbsp;i++){function&nbsp;printText(temp)&nbsp;{setTimeout(()&nbsp;=&amp;gt;&nbsp;{console.log(temp);},&nbsp;100);}printText(i)}11.代码输出题4for(var&nbsp;i&nbsp;=&nbsp;0;i&nbsp;&amp;lt;&nbsp;5;i++){function&nbsp;printText(temp)&nbsp;{var&nbsp;temp&nbsp;=&nbsp;isetTimeout(()&nbsp;=&amp;gt;&nbsp;{console.log(temp);},&nbsp;100);}printText(i)}12.代码输出题5for(var&nbsp;i&nbsp;=&nbsp;0;i&nbsp;&amp;lt;&nbsp;5;i++){function&nbsp;printText(temp)&nbsp;{setTimeout(()&nbsp;=&amp;gt;&nbsp;{var&nbsp;temp&nbsp;=&nbsp;iconsole.log(temp);},&nbsp;100);}printText(i)}13.点击控制台输出题export&nbsp;default&nbsp;function&nbsp;App()&nbsp;{const&nbsp;[count,&nbsp;setCount]&nbsp;=&nbsp;useState(0)console.log('render',count)return&nbsp;(&lt;div&gt;&lt;h1&gt;{count}&lt;/h1&gt;{setCount(count&nbsp;+&nbsp;1)setTimeout(()&nbsp;=&amp;gt;&nbsp;console.log('setTimeout',&nbsp;count),&nbsp;1000)}}&amp;gt;+1&lt;/div&gt;)}//这个组件点击按钮后,控制台的输出顺序和值如下://&nbsp;1.&nbsp;render&nbsp;1&nbsp;(组件重新渲染,&nbsp;count&nbsp;更新为&nbsp;1)//&nbsp;2.&nbsp;setTimeout&nbsp;0&nbsp;(1秒后输出,注意这里是&nbsp;0&nbsp;而不是&nbsp;1)14.算法:给有序数组arr&nbsp;=&nbsp;[-4,&nbsp;-1,&nbsp;0,&nbsp;3,&nbsp;5],返回平方后的排序//&nbsp;有序数组平方后排序const&nbsp;arr&nbsp;=&nbsp;[-4,&nbsp;-1,&nbsp;0,&nbsp;3,&nbsp;5]function&nbsp;solution(arr)&nbsp;{const&nbsp;len&nbsp;=&nbsp;arr.lengthconst&nbsp;result&nbsp;=&nbsp;new&nbsp;Array(len)let&nbsp;left&nbsp;=&nbsp;0let&nbsp;right&nbsp;=&nbsp;len&nbsp;-&nbsp;1let&nbsp;index&nbsp;=&nbsp;len&nbsp;-&nbsp;1while&nbsp;(left&nbsp;&amp;lt;=&nbsp;right)&nbsp;{if&nbsp;(arr[left]&nbsp;*&nbsp;arr[left]&nbsp;&amp;gt;&nbsp;arr[right]&nbsp;*&nbsp;arr[right])&nbsp;{result[index]&nbsp;=&nbsp;arr[left]&nbsp;*&nbsp;arr[left]left++}&nbsp;else&nbsp;{result[index]&nbsp;=&nbsp;arr[right]&nbsp;*&nbsp;arr[right]right--}index--}return&nbsp;result}console.log(solution(arr));15.反问
查看14道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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