• 一道算法题:待解


    You are given N round clocks.

    Every clock has M hands, and these hands can point to positions 1, 2, 3, ..., P (yes, these represent numbers around each face). The clocks are represented by the matrix A consisting of N rows and M columns of integers. The first row represents the hands of the first clock, and so on.

    For example, you are given matrix A consisting of five rows and two columns, and P = 4:

      A[0][0] = 1    A[0][1] = 2
      A[1][0] = 2    A[1][1] = 4
      A[2][0] = 4    A[2][1] = 3
      A[3][0] = 2    A[3][1] = 3
      A[4][0] = 1    A[4][1] = 3

    You can rotate the clocks to obtain several clocks that look identical. For example, if you rotate the third, fourth and fifth clocks you can obtain the following clocks:

    After rotation, you have four pairs of clocks that look the same: (1, 3), (1, 4), (2, 5) and (3, 4).

    Write a function:

    int solution(int **A, int N, int M, int P);

    that, given a zero-indexed matrix A consisting of N rows and M columns of integers and integer P, returns the maximal number of pairs of clocks that can look the same after rotation.

    For example, given the following array A and P = 4:

        A[0][0] = 1     A[0][1] = 2
        A[1][0] = 2     A[1][1] = 4
        A[2][0] = 4     A[2][1] = 3
        A[3][0] = 2     A[3][1] = 3
        A[4][0] = 1     A[4][1] = 3

    the function should return 4, as explained above.

    Assume that:

    • N is an integer within the range [1..500];
    • M is an integer within the range [1..500];
    • P is an integer within the range [1..1,000,000,000];
    • each element of matrix A is an integer within the range [1..P];
    • the elements of each row of matrix A are all distinct.

    Complexity:

    • expected worst-case time complexity is O(N*M*log(M)+N*log(N));
    • expected worst-case space complexity is O(N*M).
    Copyright 2009–2013 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
     
     
    思路:钟是否看以来一样取决于指针之间的距离,注意是一个圈,所以终点与起点之间的距离要用到P即总距离
    对A=[[7, 16, 20, 24], [5, 14, 18, 22], [6, 7, 10, 15], [6, 7, 10, 15], [3, 7, 11, 18], [4, 8, 12, 19]]  P=24
    answer是7
    可以求得距离为
    从A到D的时间复杂度为O(N*M)
      D=[[9=16-7, 4=20-16, 4=24-20, 7=P(24)-9-4-4], [9, 4, 4, 7], [1, 3, 5, 15], [1, 3, 5, 15], [4, 4, 7, 9], [4, 4, 7, 9]]
    D[i]之和肯定为P
      D[0]显然和D[1]是相同的,但D[0]和D[5]也是相同的,原因是可以循环左移(右移)
     
    D[0]循环左移之后与D[4]是一样的,问题转换成了一个list能否经过循环左移(右移)之后与另一个list完全相同,暴力的话O(M^2)
    再加对D的双重遍历暴力解,
    用O(N^2*M^2)可以解决
     
    题目中要求的复杂度是N*logN+N*M*logM,该怎么解呢
     
    如果D中的list有序的关系的话,能用O(NlogN)排序相同的相邻,然后遍历O(N)统计就可以在O(NlogN)计算出结果
     
    问题是D中的[9,4,4,7] ,[1,3,5,15],[4,4,7,9]如何能有序的关系且能在min(NMlogM,nlogN)内实现
     
    ??????????????????????????????????????????????????????????????????????????????????????????????????
    有没有一种类似哈希的算法,使相同的(比如[9,4,4,7]和[4,4,7,9])最后能产生一个相同的值,且在mlogm时间内能实现?
    ??????????????????????????????????????????????????????????????????????????????????????????????????
     
    根据找最小值的思路,能在最坏情况O(M^2)定位起点,即9447定位到第二个4,4479定位到第一个4,最坏情况在M个值都一样的情况下出现
     
    相当于可以在O(M^2)的时间里将一个列表变换为最终形式,即D=[[4, 4, 7, 9],[4, 4, 7, 9], [1, 3, 5, 15], [1, 3, 5, 15], [4, 4, 7, 9], [4, 4, 7, 9]] 最坏复杂度N*M^2
     
    可以用O(M)的时间比较,最后复杂度为NlogN*M
     
    时间复杂度为(NlogN +NM^2+NlogN*M)
     
    时间复杂度太难搞定了.
     
     
     
           
  • 相关阅读:
    在SQL Server中使用NewID()随机取得某行
    委托和事件:第 3 页 事件的由来
    case when then else 详解
    spring-boot-starter-security Spring Boot中集成Spring Security
    spring-boot-actuator健康监控
    汉字转拼音开源工具包Jpinyin介绍
    JAVA实现汉字转换为拼音 pinyin4j/JPinyin
    Spring MVC 后端接口支持跨域CORS调用
    web开发-CORS支持
    maven POM.xml 标签详解
  • 原文地址:https://www.cnblogs.com/zsc347/p/3278583.html
Copyright © 2020-2023  润新知