Programming Assignment 7: Performance

Here is the original link

  • Inversions.java(逆序对常出现于排序,投票论,协同过滤,等级聚合及非参数统计)
public class Inversions {

    // Return the number of inversions in the permutation a[].
    public static long count(int[] a) {
        long count = 0;
        for (int i = 0; i < a.length; i++) {
            for (int j = i + 1; j < a.length; j++) {
                if (a[i] > a[j]) {
                    count++;
                }
            }
        }
        return count;
    }

    // Return a permutation of length n with exactly k inversions.
    public static int[] generate(int n, long k) {
        int i = 0, j = n - 1;
        int[] p = new int[n];
        for (int idx = 0; idx < n; idx++)
        {
            if (k >= (j - i))
            {
                k -= j;
                p[idx] = j--;
            }
            else
            {
                p[idx] = i++;
            }
        }
        return p;
    }

    // Takes an integer n and a long k as command-line arguments,
    // and prints a permutation of length n with exactly k inversions.
    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]);
        long k = Long.parseLong(args[1]);
        int[] p = generate(n, k);
        for (int i : p) {
            StdOut.print(i + " ");
        }
    }
}
  • Ramanujan.java(拉马努金,印度的天才数学家,发现了1729在两种方法写成两个立方和的数中,它最小)
public class Ramanujan {

    // Is n a Ramanujan number?
    public static boolean isRamanujan(long n) {
        int count = 0;
        long n_cbrt = (long)Math.cbrt(n);
        for (long i = 1; i <= n_cbrt; i++) {
            double j = Math.cbrt(n - i * i * i);
            if (Math.abs(j - Math.round(j)) < Double.MIN_VALUE) {
                count++;
            }
        }
        return count >= 4;
    }

    // Takes a long integer command-line arguments n and prints true if
    // n is a Ramanujan number, and false otherwise.
    public static void main(String[] args) {
        long n = Long.parseLong(args[0]);
        StdOut.println(isRamanujan(n));
    }
}
  • MaximumSquareSubmatrix.java(最大平方子矩阵,常见于数据库、图像处理、最大似然估计,经典面试题了)
public class MaximumSquareSubmatrix {

    // Returns the size of the largest contiguous square submatrix
    // of a[][] containing only 1s.
    public static int size(int[][] a) {
        int n = a.length;
        int[][] aux = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                aux[i][j] = a[i][j];
            }
        }
        for (int i = 1; i < n; i++) {
            for (int j = 1; j < n; j++) {
                // here is the key
                if (aux[i][j] == 1) {
                    int min = Math.min(aux[i-1][j], aux[i][j-1]);
                    min = Math.min(min, aux[i-1][j-1]) + 1;
                    aux[i][j] = Math.max(aux[i][j], min);
                }
           }
        }
        int ans = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                ans = Math.max(ans, aux[i][j]);
            }
        }
        return ans;
    }

    // Reads an n-by-n matrix of 0s and 1s from standard input
    // and prints the size of the largest contiguous square submatrix
    // containing only 1s.
    public static void main(String[] args) {
        int n = StdIn.readInt();
        int[][] a = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                a[i][j] = StdIn.readInt();
            }
        }
        int ans = size(a);
        StdOut.println(ans);
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值