doushuangdui5419 2018-02-08 08:42
浏览 36
已采纳

切片结构使用太多内存

I tried to solve this problem with BFS, but for input "99 100" my program uses more than 260 Mb and online-judge system throws MEMORY_LIMIT_EXCEEDED. I guess the problem is the way I use QUEUE. So what do you think is the problem? And how should I solve it?

Here is my code. And thanks in advance!:

package main

import (
    "fmt"
)

type pair struct {
    nn int
    dd int
}

func main() {
    var n, m int
    fmt.Scanf("%d%d", &n, &m)

    if n >= m {
        fmt.Println(n - m)
    } else {
        device := make([]pair, 1)
        device[0] = pair{n, 0}

        ans := 0
        for {
            // pop front element
            tmp := device[0]
            device = device[1:]

            if tmp.nn == m { // reached destination
                ans = tmp.dd
                break
            }

            // add neighbors to the queue
            device = append(device, pair{tmp.nn - 1, tmp.dd + 1})
            device = append(device, pair{tmp.nn * 2, tmp.dd + 1})
        }

        fmt.Println(ans)
    }
}

EDIT: More readable and working(ACCEPTED) code:

package main

import (
    "fmt"
)

type pair struct {
    location int
    dist     int
}

func main() {
    var n, m int
    fmt.Scanf("%d%d", &n, &m)

    if n >= m {
        fmt.Println(n - m)
    } else {
        visited := make([]bool, m+2)

        queue := make([]pair, 1)
        queue[0] = pair{n, 0}

        ans := 0
        visited[n] = true
        for {
            // pop front element
            tmp := queue[0]
            queue = queue[1:]

            // reached destination
            if tmp.location == m {
                ans = tmp.dist
                break
            }

            // add neighbors to the queue
            if tmp.location*2 <= m+1 && visited[tmp.location*2] == false {
                queue = append(queue, pair{tmp.location * 2, tmp.dist + 1})
                visited[tmp.location*2] = true
            }
            if tmp.location-1 >= 0 && visited[tmp.location-1] == false {
                queue = append(queue, pair{tmp.location - 1, tmp.dist + 1})
                visited[tmp.location-1] = true
            }
        }

        fmt.Println(ans)
    }
}
  • 写回答

2条回答 默认 最新

  • dtfo55908 2018-02-08 08:58
    关注

    Your algorithm is not BFS, because, you can visit the same state more than one.

    For example, 4 -> 3 -> 6 and 4 -> 8 -> 7 -> 6, which 6 will end up to be processed twice.

    Secondly, for number x that is greater than the target, the minimum number of step is always

    x - target + step to reach x
    

    so you should not add it into the queue.

    By doing those two modifications, the space complexity will be limited to O(m) which should help you to solve the problem.

    Sample code

    ans := -1
    dist := make([]int, m + 1)
    q := make([]int,1)
    q[0] = n
    
    for i := 0; i < len(q); i++ {
        node := q[i]
        if node == m {
           if ans == -1 || ans > dist[m]{
              ans = dist[m]
           }
           break;
        }
        a := node*2
        b := node - 1
        if a >= m {
           if ans == -1 || ans > (1 + dist[node] + a - m) {
              ans = 1 + dist[node] + a - m
           }
        }else if dist[a] == 0 && a != n {
           q = append(q, a)
           dist[a] = 1 + dist[node]
        }
        if dist[b] == 0 && b != n {
           q = append(q, b)
           dist[b] = 1 + dist[node]
        } 
    }
    return ans
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮