Weekly Contest 78-------->808. Soup Servings
There are two types of soup: type A and type B. Initially we have N ml of each type of soup. There are four kinds of operations:
- Serve 100 ml of soup A and 0 ml of soup B
- Serve 75 ml of soup A and 25 ml of soup B
- Serve 50 ml of soup A and 50 ml of soup B
- Serve 25 ml of soup A and 75 ml of soup B
When we serve some soup, we give it to someone and we no longer have it. Each turn, we will choose from the four operations with equal probability 0.25. If the remaining volume of soup is not enough to complete the operation, we will serve as much as we can. We stop once we no longer have some quantity of both types of soup.
Note that we do not have the operation where all 100 ml's of soup B are used first.
Return the probability that soup A will be empty first, plus half the probability that A and B become empty at the same time.
Example:
Input: N = 50
Output: 0.625
Explanation:
If we choose the first two operations, A will become empty first. For the third operation, A and B will become empty at the same time. For the fourth operation, B will become empty first. So the total probability of A becoming empty first plus half the probability that A and B become empty at the same time, is 0.25 * (1 + 1 + 0.5 + 0) = 0.625.
Notes:
0 <= N <= 10^9.- Answers within
10^-6of the true value will be accepted as correct.
Approach #1: C++. Using DP.
class Solution {
private:
double memo[200][200];
public:
double soupServings(int N) {
return N > 4800 ? 1.0 : solve((N+24) / 25, (N + 24) / 25);
}
private:
double solve(int a, int b) {
if (a <= 0 && b <= 0) return 0.5;
if (a <= 0) return 1.0;
if (b <= 0) return 0.0;
if (memo[a][b] > 0) return memo[a][b];
memo[a][b] = 0.25 * (solve(a-4, b) + solve(a-3, b-1) + solve(a-2, b-2) + solve(a-1, b-3));
return memo[a][b];
}
};
Approach #2: Java.
class Solution {
private double[][] memo = new double[200][200];
public double soupServings(int N) {
return N > 4800 ? 1.0 : solve((N + 24) / 25, (N + 24) / 25);
}
private double solve(int a, int b) {
if (a <= 0 && b <= 0) return 0.5;
if (a <= 0) return 1.0;
if (b <= 0) return 0.0;
if (memo[a][b] > 0) return memo[a][b];
memo[a][b] = 0.25 * (solve(a-4, b) + solve(a-3, b-1) + solve(a-2, b-2) + solve(a-1, b-3));
return memo[a][b];
}
}
Approach #3: Python.
class Solution(object):
memo = {}
def soupServings(self, N):
if N > 4800: return 1
def f(a, b):
if (a, b) in self.memo: return self.memo[a, b]
if a <= 0 and b <= 0: return 0.5
if a <= 0: return 1
if b <= 0: return 0
self.memo[(a, b)] = 0.25 * (f(a - 4, b) + f(a - 3, b - 1) + f(a - 2, b - 2) + f(a - 1, b - 3))
return self.memo[(a, b)]
N = math.ceil(N / 25.0)
return f(N, N)
Weekly Contest 78-------->808. Soup Servings的更多相关文章
- 【LeetCode】808. Soup Servings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/soup-serv ...
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- LeetCode之Weekly Contest 91
第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- LeetCode Weekly Contest 47
闲着无聊参加了这个比赛,我刚加入战场的时候时间已经过了三分多钟,这个时候已经有20多个大佬做出了4分题,我一脸懵逼地打开第一道题 665. Non-decreasing Array My Submis ...
- [Swift]LeetCode808. 分汤 | Soup Servings
There are two types of soup: type A and type B. Initially we have N ml of each type of soup. There a ...
随机推荐
- 高性能MySQL(三)
服务器性能剖析 性能优化概述 性能优化是降低CPU使用率?错误,资源就是用来消耗的,新版本MySQL的InnoDB引擎对资源的利用率还增高了,所以这不是一个好的衡量标准. 提升每秒查询量?其实就是吞吐 ...
- toad for oracle中文显示乱码
toad for oracle中文显示乱码 数据入库的时候中文显示正常,在toad for oracleclient和页面显示都是乱码!!! 原因:在数据入库时候出现的问题. 解决方式: 在系统变量中 ...
- Storage,Memcache,KVDB都是存储服务,如何区分何时用何种服务
Storage :是SAE为开发者提供的分布式文件存储服务,用来存放用户的持久化存储的文件.用户需要先在在线管理平台创建Domain(相当于一级子目录). Storage为开发者提供分布式文件存 ...
- EasyDarwin EasyCamera支持海康摄像机接入了
本文转自EasyDarwin开源团队成员Alex的博客:http://blog.csdn.net/cai6811376/article/details/52709816 EasyCamera默认使用海 ...
- CentOS 64位 6.4 命令行安装 oracle 11g 客户端
1.官网下载oracle 客户端(需登录) http://download.oracle.com/otn/linux/instantclient/11204/oracle-instantclient1 ...
- SAP bseg 使用注意点:1.不要使用;2.有主键再用,
粗表-簇表 cluster-table BSEGRFBLG 池表 pool-table 我記的沒錯的話,在21天學會ABAP中有一節是專門在講Cluster table,另外在 www.sapfans ...
- matlab面向对象设计---类的概念和使用
代码: classdef MadgwickAHRS < handle %MADGWICKAHRS Implementation of Madgwick's IMU and AHRS algori ...
- 使用官方Android-support-v7在低版本上使用ActionBarActivity
昨天晚上更新了下Android SDK Manager,发现Extras下的Android Support Library已经更新到19.1了,上网一查原来是sdk\extras\android\su ...
- wav音频文件头解析
wav概述 WAV为微软公司(Microsoft)开发的一种声音文件格式,它符合RIFF(ResourceInterchange File Format)文件规范,用于保存Windows平台的音频信息 ...
- 步入C编程的第一天
我想学ruby以后开发网站,但ruby是高级语言,隐藏了许多底层的东西,因此先熟悉c语言 首先c程序的文件名是以.c结尾的 c程序的格式: 第一行#include<stdio.h> #是一 ...