799. 香槟塔

我们把玻璃杯摆成金字塔的形状,其中第一层有1个玻璃杯,第二层有2个,依次类推到第100层,每个玻璃杯(250ml)将盛有香槟。

从顶层的第一个玻璃杯开始倾倒一些香槟,当顶层的杯子满了,任何溢出的香槟都会立刻等流量的流向左右两侧的玻璃杯。当左右两边的杯子也满了,就会等流量的流向它们左右两边的杯子,依次类推。(当最底层的玻璃杯满了,香槟会流到地板上)

例如,在倾倒一杯香槟后,最顶层的玻璃杯满了。倾倒了两杯香槟后,第二层的两个玻璃杯各自盛放一半的香槟。在倒三杯香槟后,第二层的香槟满了 - 此时总共有三个满的玻璃杯。在倒第四杯后,第三层中间的玻璃杯盛放了一半的香槟,他两边的玻璃杯各自盛放了四分之一的香槟,如下图所示。

现在当倾倒了非负整数杯香槟后,返回第 i 行 j 个玻璃杯所盛放的香槟占玻璃杯容积的比例(i 和 j都从0开始)。

示例 1:

输入: poured(倾倒香槟总杯数) = 1, query_glass(杯子的位置数) = 1, query_row(行数) = 1

输出: 0.0

解释: 我们在顶层(下标是(0,0))倒了一杯香槟后,没有溢出,因此所有在顶层以下的玻璃杯都是空的。

示例 2:

输入: poured(倾倒香槟总杯数) = 2, query_glass(杯子的位置数) = 1, query_row(行数) = 1

输出: 0.5

解释: 我们在顶层(下标是(0,0)倒了两杯香槟后,有一杯量的香槟将从顶层溢出,位于(1,0)的玻璃杯和(1,1)的玻璃杯平分了这一杯香槟,所以每个玻璃杯有一半的香槟。

注意:

poured 的范围[0, 10 ^ 9]。

query_glass 和query_row 的范围 [0, 99]。

class Solution {
public double champagneTower(int poured, int query_row, int query_glass) {
double[][] arr = new double[query_row + 2][query_row + 2];
arr[0][0] = poured;
for (int i = 0; i <= query_row; i++) {
for (int j = 0; j <= i; j++) {
if (arr[i][j] > 1) {
arr[i + 1][j] += (arr[i][j] - 1) / 2.0;
arr[i + 1][j + 1] += (arr[i][j] - 1) / 2.0;
arr[i][j] = 1;
}
}
}
return arr[query_row][query_glass];
}
}

Java实现 LeetCode 799 香槟塔 (暴力模拟)的更多相关文章

  1. [LeetCode] Champagne Tower 香槟塔

    We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so ...

  2. Java for LeetCode 060 Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  3. Java for LeetCode 044 Wildcard Matching

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  4. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  5. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  6. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  7. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  8. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  9. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

随机推荐

  1. VS Code的安装与配置

    VS Code的安装与配置 Visual Studio Code(VS Code)是微软旗下的一个开源文本编辑器,支持Windows.macOS.Linux操作系统.数量众多.种类多样的插件极大提高了 ...

  2. Linux 物理卷(PV)、逻辑卷(LV)、卷组(VG)管理

    (一)相关概念 逻辑卷是使用逻辑卷组管理(Logic Volume Manager)创建出来的设备,如果要了解逻辑卷,那么首先需要了解逻辑卷管理中的一些概念. 物理卷(Physical Volume, ...

  3. 基于ELK搭建MySQL日志平台的要点和常见错误

    第一部分 概括 ELK是集分布式数据存储.可视化查询和日志解析于一体的日志分析平台.ELK=elasticsearch+Logstash+kibana,三者各司其职,相互配合,共同完成日志的数据处理工 ...

  4. mybatis中 #{} 和 ${}

    在mybatis中#{}表示一个占位符: 1.#将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号 2.#在很大程度上可以防止sql注入 3.例如#{id}:#{}中的id表示输入的参数名称 ...

  5. 使用plupload实现多文件上传,自定义参数

    下载地址:点击打开链接 1.在开发中可能需要用户附件上传的功能,实现批量上传功能其实就将多个上传任务放到一个集合中,分别上传. 2,使用plupload js插件可以很轻松的实现带参数的多文件上传 3 ...

  6. mac下charles使用

    设置charles  电脑上一次性的工作 1 下载下面两个文件(这里版本自己定) a charles-proxy-4.1.4.dmg b charles4.1.4的副本.jar 2 进行charles ...

  7. Redux:Reducers

    action只是描述了“发生了什么事情(导致state需要更新)”,但并不直接参与更新state的操作.state的更新由reducer函数执行. 其基本模式是:(state,action)=> ...

  8. JS的函数和对象三

    复习 判断是否含有某个属性 对象.属性名 === undefined 对象.hasOwnProperty('属性名') '属性名' in 对象 方法  { say:function(){  this ...

  9. mysql小白系列_13 Online DDL

    Online DDL: 一.FIC之前的添加删除二级索引: 1.首先创建临时表,定义目标新表的选项和索引 2.逐行拷贝数据到临时表 3.插入行时更新索引信息 4.数据全部被拷贝到新表后,删除旧表,re ...

  10. 王艳 201771010127《面向对象程序设计(java)》第十三周学习总结

    一:理论部分. 1.事件处理基础. 1)事件源:能够产生事件的对象都可以成为事件源,如文本框.按钮等.一个事件源是一个能够注册监听器并向监听器发送事件对象的对象. 2)事件监听器:事件监听器对象接收事 ...