- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的代码似乎为 codility 上的一个测试用例返回了错误的答案。(其余测试用例都是正确的)
对于测试用例:大随机
大型随机测试,N = ~100,000我得到了一个
得到 868 预期 840
问题链接: https://app.codility.com/programmers/lessons/7-stacks_and_queues/fish/
<p></p>
<p>def solution(A, B):
#declare stacks for fish traveling downstrea, upstream
dstrm = []
ustrm = []
#set counter to zero for fish traveling upstream UNHINDERED
#by fish traveling downstream, needed because iteration starts upstream
counter = 0
n = len(A)
#iterate over input, starting from upstream
for i in range(n):
if B[i] == 0:
ustrm.append(A[i])
elif B[i] == 1:
dstrm.append(A[i])</p>
# clear upstream stack of fish known to be UNHINDERED, increase counter
if len(ustrm) > 0 and len(dstrm) == 0:
counter += len(ustrm)
ustrm.clear()
#compare what's bigger and decrease stack of fish that is eaten
while len(dstrm) > 0 and len(ustrm) > 0:
if dstrm[-1] > ustrm[-1]:
ustrm.pop()
elif ustrm[-1] > dstrm[-1]:
dstrm.pop()
return len(dstrm) + len(ustrm) + counter
最佳答案
这是我的方法 - 也许有人能想到 - 我是如何解决它的Codility Python 中的可溶性 100 %
Code
def solution(A, B):
"""
Codility 100%
https://app.codility.com/demo/results/trainingF5HTCA-YFV/
Idea is to use stack concept
For each iteration if current fish is of type 1 add it to stack 1 fish
Create stack 1 fish - it always holds the downstream ie 1 kind of fish
and keep killing the smaller fish from the list if it is greater
else killed by current fish.
Now if stack 1 fish has no fish it means current fish can be counted as remaining fish.
0 represents a fish flowing upstream - 0 fish
1 represents a fish flowing downstream - 1 fish
A[0] = 4 B[0] = 0 alive fish
A[1] = 3 B[1] = 1 downstream
A[2] = 2 B[2] = 0 eaten by A[1]
A[3] = 1 B[3] = 0 eaten by A[1]
A[4] = 5 B[4] = 0 eat to A[1] and remain alive
"""
count = 0
# stores the 1 fish in stack
stack_1_fish = []
print(A)
print(B)
for index in range(len(A)):
if B[index] == 0:
# until stack has some 1 fish
while stack_1_fish:
# get last fish from stack and check if it can die or not
# the larger fish eats the smaller one.
# two fish P and Q meet each other when P < Q, B[P] = 1 and B[Q] = 0, and there are no living fish between them
if stack_1_fish[-1] > A[index]:
# stack 1 fish kill to current fish
# exit from while loop and else block also execute next top for loop
# check for other fish fight
print("Killed by " + str(stack_1_fish[-1]) + " Die " + str(A[index]))
break
else:
# stack 1 fish is killed by current fish
p = stack_1_fish.pop()
print("Killed by " + str(A[index]) + " Die " + str(p))
# this is the case when stack becomes empty ie. no fish of kind 1
# it would never meet previous fish but can fight with downstream fish
else:
print("Count fish as remaining......." + str(A[index]))
count += 1
else:
# fish 1 found add to stack
stack_1_fish.append(A[index])
print("1 fish found, add to stack, it can kill or killed by someone..." + str(A[index]))
print(stack_1_fish)
print("Count: " + str(count))
print("Stack 1 fish: " + str(len(stack_1_fish)))
return count + len(stack_1_fish)
Execution Output -
if __name__ == '__main__':
result = solution([4, 3, 2, 1, 5], [0, 1, 0, 0, 0])
# result = solution([4, 3, 2, 1, 5], [0, 0, 0, 0, 0])
# result = solution([4, 3, 2, 1, 5], [1, 1, 1, 1, 1])
print("")
print("Solution " + str(result))
[4, 3, 2, 1, 5]
[0, 1, 0, 0, 0]
Count fish as remaining.......4
1 fish found, add to stack, it can kill or killed by someone...3
[3]
Killed by 3 Die 2
Killed by 3 Die 1
Killed by 5 Die 3
Count fish as remaining.......5
Count: 2
Stack 1 fish: 0
Solution 2
[4, 3, 2, 1, 5]
[0, 0, 0, 0, 0]
Count fish as remaining.......4
Count fish as remaining.......3
Count fish as remaining.......2
Count fish as remaining.......1
Count fish as remaining.......5
Count: 5
Stack 1 fish: 0
Solution 5
[4, 3, 2, 1, 5]
[1, 1, 1, 1, 1]
1 fish found, add to stack, it can kill or killed by someone...4
[4]
1 fish found, add to stack, it can kill or killed by someone...3
[4, 3]
1 fish found, add to stack, it can kill or killed by someone...2
[4, 3, 2]
1 fish found, add to stack, it can kill or killed by someone...1
[4, 3, 2, 1]
1 fish found, add to stack, it can kill or killed by someone...5
[4, 3, 2, 1, 5]
Count: 0
Stack 1 fish: 5
Solution 5
关于Python 代码类(class) : stacks and queues fish bug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48627743/
为了对 fish 提示进行一些小的更改,我修改了 ~/.config/fish/目录中的 fish_prompt.fish/fish_right_prompt.fish 文件,但这并没有改变任何东西。
在 Fish 中是否有可能以我将命令定义为 echo 等命令的单个参数的方式执行命令替换?在 Bash 中,它等同于例如: $ echo "$(cat file.txt)" lorem ipsum F
所以,我真的很喜欢 Fish - 但我需要一些脚本方面的帮助。 尤其是找到正在运行的脚本的路径。 这是BASH的解决方案 Getting the source directory of a Bash
我正在从 ZSH 迁移到 Fish,我唯一没弄清楚的是我的 c功能: 函数/c: #!/bin/sh cd "$PROJECTS/$1" 函数/_c: #compdef c _files -W $PR
快速提问。是否可以通过在 fish 中编写脚本来定义新命令?我想按照以下方式编写一个新命令: newFunction --flag1 flag1Input --flag2 flag2Input the
我从 fish 开始,我在大量文档中找不到的其中一件事是自动完成提要。 提到了 Tab Completions在本教程中,但它解决了机制本身的存在,而不是它的配置。 我有一堆通过 连接的虚拟机 mac
在 bash shell 脚本,我通常会运行 :> file清空文件。 现在使用fish,情况略有不同,上面的命令不起作用。 什么是 fish 当量? 最佳答案 虽然没有:那么短, true是一个可以
我已经仔细阅读了fish shell 文档,我正在尝试弄清楚如何在fish 函数中有条件地设置局部变量。 换句话说,如果您有一个函数,并且希望使用 if-then-else 逻辑修改局部变量。 fun
如果我将一个名为 myfunc.fish 的文件放在名为 functions 的目录中,并且它包含一个名为 myfunc 的函数,那么如果我将 myfunc 作为命令键入,fish 将找到它。 如果我
我在不同的地方(家庭网络、云……)有无数台机器,我在每台机器上都使用了fish。问题是每次更改其中的内容时我都必须同步它们的配置。 有没有办法从远程存储库加载配置? (= 存储它的地方,不一定是 gi
我有一个包含多个项目的目录,我想对每个项目运行一些命令。 现在我正在做的是: cd /my/project/root set root (pwd) for p in * cd $p git r
有another question关于测试特定 字符串的命令替换。我想测试一个命令是否在单个语句中输出任何内容,即等同于: if [[ -n "$(foo)" ]] 在庆典中。 Fish 无法识别 [
我想在 fish 中添加一个带有空格的别名,如下所示: alias hello there 'print hello there' 这有可能吗? (我知道这个例子是无效的,我只是举个例子)。 提前致谢
我想将字符串重定向到命令的 STDIN 中。 我会使用类似 cmd <<< "my string" 的东西来做它在 Bash 中,甚至 cmd <
如何使 ~/.config/fish/config.fish 中定义的别名和~/.config/fish/functions 中定义的函数root 用户也可以使用吗? 符号链接(symbolic li
my-fish-script a b c d 假设你想从第二个参数开始获取所有参数,所以 b c d . 在 bash 中,您可以使用 shift转储第一个参数并使用 "$@" 访问其余参数. 你将如
Fish是否可以根据您所在的目录自动加载其他配置? 一个可能的例子,但是显然这是行不通的。您导航到项目文件夹: cd ~/my_proj 该文件夹中有一个名为.fish.config的文件,其中包含一
我查看了 Stack Overflow 上的所有类似问题,并在他们的文档中尝试了很多东西。我目前的尝试如下。 我想要完成的是基于计算机用户的不同行为。我有一台工作电脑和一台家用电脑,当我在命令行中输入
这是2.1.0版本 $ set a foo $ set b bar $ echo $a_$b $ echo {$a}_$b foo_bar 为什么第一个“echo”没有输出任何东西?我本以为会看到“酒
我是 Windows 用户。我正在通过 WSL-2 尝试 Ubuntu。我正在努力在 fish shell 中选择带有箭头键的文本。我尝试了很多地方来回答这个问题,但没有人给我合适的解决方案。在文档中
我是一名优秀的程序员,十分优秀!