- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
Problem Statement
There are N plants in a garden. Each of these plants has been added with some amount of pesticide. After each day, if any plant has more pesticide than the plant at its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each plant. Print the number of days after which no plant dies, i.e. the time after which there are no plants with more pesticide content than the plant to their left.
Input Format
The input consists of an integer N. The next line consists of N integers describing the array P where P[i] denotes the amount of pesticide in plant i.
Constraints
1 ≤ N ≤ 100000
0 ≤ P[i] ≤ 109Output Format
Output a single value equal to the number of days after which no plants die.
Sample Input
7 6 5 8 4 7 10 9
Sample Output
2
Explanation
Initially all plants are alive.
Plants = {(6,1), (5,2), (8,3), (4,4), (7,5), (10,6), (9,7)}.
Plants[k] = (i,j) => jth plant has pesticide amount = i.
After the 1st day, 4 plants remain as plants 3, 5, and 6 die.
Plants = {(6,1), (5,2), (4,4), (9,7)}.
After the 2nd day, 3 plants survive as plant 7 dies.
Plants = {(6,1), (5,2), (4,4)}.
After the 3rd day, 3 plants survive and no more plants die.
Plants = {(6,1), (5,2), (4,4)}.
After the 2nd day the plants stop dying.
挑战链接: HackerRank : Poisonous Plant
我的提交: HackerRank Submission Link
到目前为止我的代码:
total = int(raw_input())
plants = map(int,raw_input().split())
num_deaths = 1
day = 0
while num_deaths > 0:
num_deaths = 0
temp_plants = []
day += 1
for i in range(1, len(plants)):
if plants[i] > plants[i - 1]:
num_deaths += 1
continue
else:
temp_plants.append(plants[i])
plants = temp_plants
print(day - 1)
它仍然无法通过一些测试用例。有什么建议/建议吗?
最佳答案
查看您的 Temp_plants
数组,您使用 []
对其进行了初始化。但是,由于您是从索引 1 开始迭代,因此始终排除索引 0 中的植物,因此您必须使用 [plant[0]]
进行初始化,因为最左边的植物始终包含在内。
示例:1 5 4 3 2
实际过程
回答=4
您的代码
回答=1
这是一个可以产生正确输出但速度相当慢的工作代码。它是正确的,但是 O(n^2) 使某些测试用例超时。这与您的算法基本相同。
n= input()
plant=map(int,raw_input().split())
count=0
# At each iteration the following loop updates the plant
# list by removing weak plants and count counts no. of steps
# temp temporarily stores surviving plants
while(True):
temp=[plant[0]]
for i in range(1,len(plant)):
if plant[i]<=plant[i-1]:
temp.append(plant[i])
# If temp and plants have same len, then no plant has
# been removed, so, the process ended.
if(len(temp)==len(plant)): break
plant=temp
count+=1
print count
我正在考虑一种应该可行的更快的动态方法,但尚未实现。
关于python - HackerRank 挑战 : Find total number of days Plants die,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31778691/
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我有这段代码,我想在另一张工作表中查找一些单元格,如果它们符合条件,则将相邻单元格添加到返回到调用单元格函数的总数中。 Function collectUtfall(A1 As String, Ax
我知道默认的“CTRL+B”Windows 命令可用于显示所有嵌套文件。 是否有显示所有嵌套文件夹的快捷方式? 最佳答案 我怀疑 Total Commander 中是否存在此功能。内置tree实用性对
关于我的问题SQL是, 我有一个表,用户可以在其中输入名为 time_report 的时间报告数据。每个 time_report 都输入一个服务代码,每个代码都有不同的含义,包括开始、停止、时间和总计
在 Total Commander 中搜索时如何忽略 .svn 文件夹? 最佳答案 要从搜索中排除某些文件或文件夹,请在“搜索字段:”中使用以下语法: 从搜索中排除 *.bak 和 *.old 文件
我将如何编写 css 来访问以下类“total total_plus hidden_elem”或“total total_plus”? 我有 class="total total_plus"和 c
我正在查看 powershellpro.com 上的一些代码示例,但不明白他为什么编写循环数组的示例代码: ...增量从零开始然后加一,直到它小于或等于数组的长度减一... for ($i=0; $i
做了一个虚拟数据: data dummy; input q1 q3 q4 q2 q6$ bu$ q5; cards; 1 2 3 5 sa an 3 2 4 3 6 sm sa 4 6 5 3 8 c
我想知道如何在 magento 1.7 的订单网格中添加以下两个额外的列 客户总订单数 客户在订单上花费的总金额 我已经设法添加了列,但我无法让它显示任何数据。我相信问题的关键在于函数 *_prepa
我正在使用 dataImportHandler 将数据从 Oracle 数据库导入 solr。尽管导入和索引编制成功,但由于未创建文档,我无法搜索。日志中也没有错误。这是我的配置文件片段。请帮忙。
项目 item_id title active = 1/0 items_categories_map item_id category_id 我需要得到结果 category_id items_tot
当我运行命令时: find / 2>/dev/null -user root -type f -mmin -1 -exec du -cb {} + | grep total | head -1 我得到
我不明白为什么有时,计划的总成本可能是一个非常小的数字,而查看计划的内部,我们会发现巨大的成本。 (确实查询很慢)。 有人可以解释我吗? 这是一个例子。 显然,成本高的部分来自主选择中的一个字段,该字
因此,脚本以初始值 $8.90 开始,其想法是根据所选选项添加额外费用,HTML 分为 3 个部分 1.check-boxes、2 .选择并3.输入(文本)。 每个部分都是独立工作的,我正在尝试找到一
我有一个小问题。我一直在网上寻找答案,但主要是针对未生成/添加的输入字段。 $(document).ready(function(){ /* --- ADD FIELD --- */ $('
js框架。我正在尝试将数据从发布请求保存到数据库 模型(文章.js): NEWSCHEMA('Article').make(function(schema) { schema.define('titl
我想要所有间隔的总和,但我写这段代码时出现错误:使用未分配的局部变量 total ? enter TimeSpan total; foreach (var grp in query) { Time
我得到了以下数据,我需要返回递归总数(在本例中为 60)。下面的代码返回TypeError: Cannot read property 'Symbol(Symbol.iterator)' of und
我的项目要插入几个小时,你花了多少时间。它适用于整数值,但现在我需要添加字符串值。像Sunday ="NotWorknig"这样的东西;星期一=“8”;星期二=“生病了”; function Tota
尝试让停止按钮在用户单击开始时提供数字的运行总数。停止按钮不会给出总数,也不会保存您单击它时开始给出的数字。 我在这里做错了什么? package com.egroegnosbig.dicerolle
我是一名优秀的程序员,十分优秀!