gpt4 book ai didi

java - 曼哈顿天际线封面未能通过一些测试用例

转载 作者:搜寻专家 更新时间:2023-10-31 08:26:07 24 4
gpt4 key购买 nike

我正在做关于可塑性的练习。我在这个问题上花了两天时间,我的分数没有提高。我的正确性得分为 100%,但由于返回了错误的答案(不是因为时间或空间复杂性)而未能通过某些性能测试。我的错误结果总是低于预期的答案。谁能想到我需要添加我丢失的石头的情况?

这是提示:

You are going to build a stone wall. The wall should be straight and N meters long, and its thickness should be constant; however, it should have different heights in different places. The height of the wall is specified by a zero-indexed array H of N positive integers.

H[I] is the height of the wall from I to I+1 meters to the right of its left end. In particular, H[0] is the height of the wall's left end and H[N−1] is the height of the wall's right end.

The wall should be built of cuboid stone blocks (that is, all sides of such blocks are rectangular). Your task is to compute the minimum number of blocks needed to build the wall.

Write a function that, given a zero-indexed array H of N positive integers specifying the height of the wall, returns the minimum number of blocks needed to build it.

class Solution { public int solution(int[] H); }

For example, given array H containing N = 9 integers:

  H[0] = 8    H[1] = 8    H[2] = 5    
H[3] = 7 H[4] = 9 H[5] = 8
H[6] = 7 H[7] = 4 H[8] = 8

The function should return 7. The figure shows one possible arrangement of seven blocks.

Assume that:

  • N is an integer within the range [1..100,000];
  • Each element of array H is an integer within the range [1..1,000,000,000].

Complexity:

  • Expected worst-case time complexity is \$O(N)\$;
  • Expected worst-case space complexity is \$O(N)\$, beyond input storage (not counting the storage required for input arguments).
  • Elements of input arrays can be modified.

这是我的解决方案:

 import java.util.*;

class Solution {
public int solution(int[] H) {
// write your code in Java SE 8

LinkedList<Integer> stack = new LinkedList<Integer>();
int count = 1;
int lowest = H[0];
stack.push(H[0]);

if(H.length == 1){
return 1;
}
else{
for(int i = 1; i<H.length; i++){
if(H[i] > H[i-1]){
stack.push(H[i]);
count++;
}
if(H[i] < lowest){
while(stack.size() > 0){
stack.pop();
}
stack.push(H[i]);
lowest = H[i];
count++;
}
if(H[i] < H[i-1] && H[i] > lowest){
while(stack.size() > 0 && stack.peek() > H[i]){
stack.pop();
}
if(stack.size() > 0 && stack.peek() < H[i]){
stack.push(H[i]);
count++;
}
}
}
}

return count;
}
}

最佳答案

可以发现的一个可能问题是,当 H[i]==lowest 时,链表管理不当。 .当H[i]==lowest ,程序应该只用一个最低 block 重置链表。只需将第二个 if block 更正为:

if(H[i] <= lowest){
while(stack.size() > 0){
stack.pop();
}
stack.push(H[i]);
if (H[i]!=lowest)
{
lowest = H[i];
count++;
}
}

考虑案例 H = {1,4,3,4,1,4,3,4,1} .正确的输出是 7,而您的代码返回 6。

问题出现在i is 6时.第三个 if block 中的 while 循环重置 stack到 {3,1} 导致 stack.peek() < H[i]即将到来的 if block 失败 (stack.peek() = H[6] = 3)。

此外,三个 if block 可以重写为 if-else-if-else-if block ,因为 H[i] 的值只能满足任何 i 的三个条件之一。

关于java - 曼哈顿天际线封面未能通过一些测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26228432/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com