You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Codility栈与队列Fish问题:大规模随机测试用例结果不符求助

Hey there! Let's break down this tricky issue you're facing with the Codility Fish problem, especially the large random test case where your result (868) doesn't match the expected (840). First, let's align on the core problem: we have fish swimming either upstream (direction 0) or downstream (direction 1), and when opposing fish meet, the larger one eats the smaller. Our goal is to count how many fish survive in the end.

Common Pitfalls That Cause Large Test Case Failures

Since your code works for most small cases, the issue is likely an edge case or logic flaw that only surfaces with high-volume, random data. Here are the most likely culprits:

1. Incomplete Stack Comparison Logic

The biggest mistake here is not fully resolving conflicts between an upstream fish and all downstream fish in the stack. For example, if you only compare the upstream fish to the top of the stack once (instead of looping until the stack is empty or the upstream fish is eaten), you'll leave surviving downstream fish unaccounted for, leading to an overcount of survivors (which matches your 868 vs 840 discrepancy).

Wrong Example:

# Only compares once, doesn't resolve all conflicts
if direction == 0:
    if stack and stack[-1] > size:
        continue
    else:
        if stack:
            stack.pop()
        count += 1

Correct Approach:
You need to loop through the stack, letting the upstream fish fight every downstream fish until it's eaten or the stack is empty:

if direction == 0:
    # Keep fighting downstream fish in the stack
    while stack:
        top_size = stack[-1]
        if top_size > size:
            # Upstream fish gets eaten, stop
            break
        elif top_size < size:
            # Downstream fish gets eaten, pop and keep fighting
            stack.pop()
        else:
            # Both fish are eaten, pop and stop
            stack.pop()
            break
    else:
        # Stack is empty, upstream fish survives
        count += 1

2. Mishandling Equal-Sized Fish

If your code doesn't account for fish of the same size correctly, you'll end up overcounting. When two opposing fish are the same size, both should be eaten—neither survives. Forgetting to pop the stack and skip counting the upstream fish in this scenario will add extra survivors.

3. Direction Mix-Up

Accidentally swapping upstream (0) and downstream (1) directions will completely break the conflict logic. For example, if you treat direction 0 as downstream, you'll trigger conflicts when fish are actually swimming the same way, leading to incorrect eat events and wrong counts.

4. Incorrect Survivor Count Calculation

Make sure you're adding the remaining downstream fish in the stack to your final count. A common error is only counting surviving upstream fish and forgetting the downstream fish that never encountered opposition.

Full Correct Example Code

Here's a robust implementation that handles all edge cases, including large datasets:

def solution(A, B):
    downstream_stack = []
    surviving_fish = 0
    
    for size, direction in zip(A, B):
        if direction == 1:
            # Downstream fish: add to stack, no conflict yet
            downstream_stack.append(size)
        else:
            # Upstream fish: fight all downstream fish in stack
            while downstream_stack:
                top_downstream = downstream_stack[-1]
                if top_downstream > size:
                    # Upstream fish is eaten, stop fighting
                    break
                elif top_downstream < size:
                    # Downstream fish is eaten, continue fighting
                    downstream_stack.pop()
                else:
                    # Both fish are eaten, stop fighting
                    downstream_stack.pop()
                    break
            else:
                # No more downstream fish, upstream survives
                surviving_fish += 1
    
    # Add all remaining downstream fish (they never met opposition)
    surviving_fish += len(downstream_stack)
    return surviving_fish

Steps to Debug Your Code

  1. Test small edge cases: Try cases where equal-sized fish meet, or a single upstream fish eats multiple downstream fish. Compare your code's output to the expected result.
  2. Add print statements: For a small test case, print the stack state and survivor count after each fish is processed to see where your logic diverges from the correct flow.
  3. Check loop conditions: Ensure your upstream fish loop continues until the stack is empty or the fish is eaten—no early breaks that skip conflicts.

内容的提问来源于stack exchange,提问作者codinggosu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:01:06