Task description

A non-empty zero-indexed array A consisting of N integers is given. Theproduct of triplet (P, Q, R) equates to A[P] * A[Q] * A[R] (0 ≤ P < Q < R < N).

For example, array A such that:

A[0] = -3 A[1] = 1 A[2] = 2 A[3] = -2 A[4] = 5 A[5] = 6

contains the following example triplets:

  • (0, 1, 2), product is −3 * 1 * 2 = −6
  • (1, 2, 4), product is 1 * 2 * 5 = 10
  • (2, 4, 5), product is 2 * 5 * 6 = 60

Your goal is to find the maximal product of any triplet.

Write a function:

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

that, given a non-empty zero-indexed array A, returns the value of the maximal product of any triplet.

For example, given array A such that:

A[0] = -3 A[1] = 1 A[2] = 2 A[3] = -2 A[4] = 5 A[5] = 6

the function should return 60, as the product of triplet (2, 4, 5) is maximal.

Assume that:

  • N is an integer within the range [3..100,000];
  • each element of array A is an integer within the range [−1,000..1,000].

Complexity:

  • expected worst-case time complexity is O(N*log(N));
  • expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).

Elements of input arrays can be modified.

Solution

 
Programming language used: Java
Total time used: 14 minutes
Code: 00:27:07 UTC, java, final, score:  100
// you can also use imports, for example:
// import java.util.*; // you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.Arrays;
import java.lang.Math;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
Arrays.sort(A);
int res = A[0] * A[1] * A[2];
res = Math.max(res, A[0] * A[1] *A[A.length-1]);
res = Math.max(res, A[0] * A[A.length-1] *A[A.length-2]);
res = Math.max(res, A[A.length-3] * A[A.length-1] *A[A.length-2]);
return res;
}
}
https://codility.com/demo/results/trainingHWDPP7-ZXQ/

Codility---MaxProductOfThree的更多相关文章

  1. Codility NumberSolitaire Solution

    1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...

  2. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  3. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  4. *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...

  5. *[codility]Country network

    https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...

  6. *[codility]AscendingPaths

    https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...

  7. *[codility]MaxDoubleSliceSum

    https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...

  8. *[codility]Fish

    https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...

  9. *[codility]CartesianSequence

    https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...

  10. [codility]CountDiv

    https://codility.com/demo/take-sample-test/count_div 此题比较简单,是在O(1)时间里求区间[A,B]里面能被K整除的数字,那么就计算一下就能得到. ...

随机推荐

  1. 关于babel和babel-polyfill

    使用babel-cli命令babel xx -d xx把一个js文件转成了ES5的,并在package.json里加了"babel-polyfill": "^6.23.0 ...

  2. 设置Oracle tnslsnr监听器口令

    绿盟扫描提示引用程序脆弱账号  Oracle tnslsnr 监听器,加密主要为了防止监听被恶意远程关闭.关于这个安全问题的详细说明参见文字结尾转载的说明<Oracle的监听口令及监听器安全&g ...

  3. 用js获取实时的获取当前的年月日,时分秒,以及星期

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...

  4. 通过控制台查看电脑中.net framework的版本

    cmd打开控制台之后输入以下命令reg query "HKLM\Software\Microsoft\NET Framework Setup\NDP" /s /v version ...

  5. UVa 11400 Lighting System Design(DP 照明设计)

    意甲冠军  地方照明系统设计  总共需要n不同类型的灯泡  然后进入 每个灯电压v  相应电压电源的价格k  每一个灯泡的价格c   须要这样的灯泡的数量l   电压低的灯泡能够用电压高的灯泡替换   ...

  6. PAMIE- Python实现IE自动化的模块

    PAMIE- Python实现IE自动化的模块(附 网易注册代码) 收藏 安装: 今晚弄了一下.已经成功导入PAMIE.具体步骤如下 1.假如你要用PAM30那就就得去下个Python 3.0安装.因 ...

  7. 简化网站开发:SiteMesh小工具

    在一个站点的制备,几乎所有的页面将具有相同的部分.导航栏例如,顶,每一页都是一样的,在底部的版权声明,每一页还都是一样的. 因此,在顶部导航栏的准备.第一种方法是直接复制的所有导航栏的代码,这种方法是 ...

  8. STM32 模拟I2C (STM32F051)

    /** ****************************************************************************** * @file i2c simu. ...

  9. Converter

    public class ImgPathConvert : IValueConverter { public object Convert(object value, Type targetType, ...

  10. BackgroundWorker使用

    using System.ComponentModel; private BackgroundWorker worker;  worker = new BackgroundWorker(); work ...