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. java8中的localdate和localtime用法举例

    java8中的localdate和localtime用法举例如下:这两个方法使我们可以方便的实现将旧的日期类转换为新的日期类,具体思路都是通过Instant当中介,然后通过Instant来创建Loca ...

  2. C# 反射调用私有事件

    原文:C# 反射调用私有事件 在 C# 反射调用私有事件经常会不知道如何写,本文告诉大家如何调用 假设有 A 类的代码定义了一个私有的事件 class A { private event EventH ...

  3. sql server通过脚本添加链接服务器

    exec sp_addlinkedserver  'ZZSJK','','SQLOLEDB','192.168.10.22'  --链接服务器名称 ‘’ ip地址exec sp_addlinkedsr ...

  4. win7(64位)彻底卸载mysql,重装不再烦恼

    [此文出身]鄙人mysql呆鸟,一时手残卸载了mysql,之后重装,始终在配置的时候始终无法通过(如图),纠结一天之久! 查看大图 在某人的鄙视鞭策下,通过度娘的多种指导及自身的多次尝试,终于在下班前 ...

  5. matlab 小波处理工具箱

    1. wavedec:多尺度(multilevel)一维小波分解 [C,L] = wavedec(X,N,'wname') [C,L] = wavedec(X,N,Lo_D,Hi_D) 返回值 L(b ...

  6. 使用JScript脚本批量修改VC工程设置

    作者:朱金灿 来源:http://blog.csdn.net/clever101 很多时候升级了第三方库,需要对很多工程修改设置.在VS中按住Ctrl键确实可以多选工程,但通过这样做也有麻烦的地方:一 ...

  7. Efficient store queue architecture

    One embodiment of the present invention provides a store queue that applies the stores to a memory s ...

  8. C++:怎样把一个int转成4个字节?

    大家都知道,一个int 或 unsigned int是由4个字节组成的,(<C/C++学习指南>,第3章,第3.2.3节:变量的内存视图) 比如, int   n  =  sizeof( ...

  9. OpenGL(九) 三维混色和深度缓存设置

    颜色的混合在现实世界中非常常见,例如隔着有色玻璃观看物体,此时在观察者严重呈现出来物体的颜色就是玻璃的颜色和物体的颜色的混合. OpenGL在RGBA颜色模式下使用函数glenable(GL_BLEN ...

  10. Matlab Tricks(二十三)—— 保存图像到 pdf

    printme = @(txt) print('-dpdf', sprintf('figures/Example_%s',txt)); % 该匿名函数的接受的参数为字符串类型,也即欲保存的文件名: % ...