资讯专栏INFORMATION COLUMN

Check some

Zhuxy / 2459人阅读

public static boolean checkDuplicateWithinK(int[][] mat, int k){
    class Cell{
        int row;
        int col;
        public Cell(int r, int c){
            this.row = r;
            this.col = c;
        }
    }
    int n = mat.length;
    int m = mat[0].length;
    k = Math.min(k, n*m);
    //map from distance to cell postions of the matrix
    Map> map = new HashMap>();
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(map.containsKey(mat[i][j])){
                for(Cell c : map.get(mat[i][j])){
                    int Dist = Math.abs(i - c.row)+Math.abs(j - c.col);
                    if(Dist <= k){
                        return true;
                    }
                    if(i - c.row > k){
                        map.remove(c);
                    }
                }
                map.get(mat[i][j]).add(new Cell(i, j));
            }
            else{
                map.put(mat[i][j], new HashSet());
                map.get(mat[i][j]).add(new Cell(i, j));
            }
        }
    }
    return false;
}
public boolean checkDuplicatesWithinK(int[][] matrix, int k) {
        //convert matrix to an array
        int[] arr = Arrays.stream(matrix)
                .flatMapToInt(Arrays::stream)
                .toArray();


        // Creates an empty hashset
        HashSet set = new HashSet<>();

        // Traverse the input array
        for (int i = 0; i < arr.length; i++) {
            // If already present n hash, then we found
            // a duplicate within k distance
            if (set.contains(arr[i]))
                return true;

            // Add this item to hashset
            set.add(arr[i]);

            // Remove the k+1 distant item
            if (i >= k)
                set.remove(arr[i - k]);
        }

    
        return false;
    }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/64971.html

相关文章

  • Some useful JS techniques that you may not know

    I complete reading JavaScript Enlightenment recently. The book is more about basics in JavaScript and suitable for beginners. Here are a list of my benefits and extensions from the book. Math JavaScri...

    stackvoid 评论0 收藏0
  • Promise到底解决了什么问题?

    摘要:我的博客大家都知道解决了回调地狱的问题。这就是异步的嵌套带来的可读性的问题,它是由异步的运行机制引起的。在与第三方团队沟通之后问题得到了解决。这不但使代码变得臃肿不堪,还进一步加剧了可读性的问题。的特征保证了可以解决信任问题。 我的github博客 https://github.com/zhuanyongxigua/blog 大家都知道Promise解决了回调地狱的问题。说到回调地狱,...

    yibinnn 评论0 收藏0
  • Enzyme

    Since the enzyme can make us get easier to update to new version of React and make our test code more lean and maintainableYoud better not use props() to get props from component, because Enzyme docum...

    Amos 评论0 收藏0
  • Dubbo压测插件的实现——基于Gatling

    摘要:为了控制压测时的,则需要实现逻辑。则是获取属性并初始化客户端客户端配置则提供了设置泛化调用入参的以及接下来要介绍的部分的全链路压测中,我们都使用校验请求结果,压测插件中,我们也实现了基于的校验。 Dubbo 压测插件已开源,本文涉及代码详见gatling-dubbo Gatling 是一个开源的基于 Scala、Akka、Netty 实现的高性能压测框架,较之其他基于线程实现的压测框架...

    BigTomato 评论0 收藏0
  • Dubbo压测插件的实现——基于Gatling

    摘要:为了控制压测时的,则需要实现逻辑。则是获取属性并初始化客户端客户端配置则提供了设置泛化调用入参的以及接下来要介绍的部分的全链路压测中,我们都使用校验请求结果,压测插件中,我们也实现了基于的校验。 Dubbo 压测插件已开源,本文涉及代码详见gatling-dubbo Gatling 是一个开源的基于 Scala、Akka、Netty 实现的高性能压测框架,较之其他基于线程实现的压测框架...

    CoreDump 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<