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. 云主机启动Node服务后,关闭控制台,无法访问的问题

    之前一直用node app.js操作,开启服务后,关闭控制台,仍然可以正常访问我的网站.但昨晚新买腾讯云的服务器后,发现关闭控制台后,就无法访问网站了.然后给腾讯云发了个工单.腾讯云的工程师给了一篇技 ...

  2. Android开发入门——Andoird Studio的安装与配置

    Android的开发离不开Java,仍然需要对Java进行安装与配置,所以我写了上一篇文章,Java的安装与配置. 开始进行Android Studio的安装与配置. 一.进行配置Java,如果电脑里 ...

  3. Android 平台下Cordova 调用Activity插件开发

    首先建立一个包名为package com.JiajiaCy.CallActivity; package com.JajaCy.CallActivity; import org.apache.cordo ...

  4. 判断软件的闲置时间(使用Application.OnMessage过滤所有键盘鼠标消息)

    GetLastInputInfo是检测系统输入的,应用到某个程序中不合适! 此问题有二种解法来监控输入消息: 1.用线程级HOOK,钩上MOUSEHOOK与KEYBOARDHOOK 2.在Applic ...

  5. auxiliary variable(辅助变量)的引入

    辅助变量的引入是推导数学公式的一个重要手段. 1. 条件概率 ⇒ 积分 P(x=1|D)=∫10P(x=1|μ)P(μ|D)dμ=∫10μP(μ|D)dμ=E(μ|D) 2. 条件概率 ⇔ 边缘概率 ...

  6. Redis实战:如何构建类微博的亿级社交平台

    微博及 Twitter 这两大社交平台都重度依赖 Redis 来承载海量用户访问.本文介绍如何使用 Redis 来设计一个社交系统,以及如何扩展 Redis 让其能够承载上亿用户的访问规模. 虽然单台 ...

  7. MyEclipse迅速

    MyEclipse迅速 1.详细例如以下图 2.提示原因 3.解决方案 版权声明:本文博主原创文章.博客,未经同意不得转载.

  8. QComboBox实现复选功能(三种方法:嵌套QListWidget, 设置QStandardItemModel, 设置Delegate)

    今天介绍一下一个小东西 — 如何让QComboBox实现复选功能?   需求: 下拉列表有复选功能 不可编辑 显示所有选中项   关于QComboBox的复选功能有几种方案: QStandardIte ...

  9. oracle授权grant

    alter any cluster 修改任意簇的权限 alter any index 修改任意索引的权限 alter any role 修改任意角色的权限 alter any sequence 修改任 ...

  10. 1 DDD理论学习1 通用语言

    通用语言就是将事情描述清楚的语言 达到DDD的目标代码即设计,设计即代码.通俗的讲,也就是开发人员写的代码领域专家也能看懂. ddd模式跟传统模式的一个区别在于 传统先创建数据库表 再根据表创建类.而 ...