Codility----PassingCars
A non-empty zero-indexed array A consisting of N integers is given. The consecutive elements of array A represent consecutive cars on a road.
Array A contains only 0s and/or 1s:
- 0 represents a car traveling east,
- 1 represents a car traveling west.
The goal is to count passing cars. We say that a pair of cars (P, Q), where 0 ≤ P < Q < N, is passing when P is traveling to the east and Q is traveling to the west.
For example, consider array A such that:
A[0] = 0 A[1] = 1 A[2] = 0 A[3] = 1 A[4] = 1
We have five pairs of passing cars: (0, 1), (0, 3), (0, 4), (2, 3), (2, 4).
Write a function:
class Solution { public int solution(int[] A); }
that, given a non-empty zero-indexed array A of N integers, returns the number of pairs of passing cars.
The function should return −1 if the number of pairs of passing cars exceeds 1,000,000,000.
For example, given:
A[0] = 0 A[1] = 1 A[2] = 0 A[3] = 1 A[4] = 1
the function should return 5, as explained above.
Assume that:
- N is an integer within the range [1..100,000];
- each element of array A is an integer that can have one of the following values: 0, 1.
Complexity:
- expected worst-case time complexity is O(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.
// 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");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int zeroCnt=0, oneCnt =0;
for(int i=0; i<A.length; i++) {
if(A[i] == 0) {
zeroCnt += 1;
} else if(A[i] == 1) {
oneCnt += zeroCnt;
}
if(oneCnt > 1000000000)
return -1;
}
return oneCnt;
}
}
https://codility.com/demo/results/training8U5YGT-RJR/
Codility----PassingCars的更多相关文章
- Codility NumberSolitaire Solution
1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...
- codility flags solution
How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
- *[codility]Peaks
https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...
- *[codility]Country network
https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...
- *[codility]AscendingPaths
https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...
- *[codility]MaxDoubleSliceSum
https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...
- *[codility]Fish
https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...
- *[codility]CartesianSequence
https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...
- [codility]CountDiv
https://codility.com/demo/take-sample-test/count_div 此题比较简单,是在O(1)时间里求区间[A,B]里面能被K整除的数字,那么就计算一下就能得到. ...
随机推荐
- 【PHP Manager for IIS】让IIS支持PHP
本文安装环境: 操作系统:Win7 64位 PHP版本:PHP 5.5.15(VC11 x64 Thread Safe) 下载地址:http://windows.php.net/download ...
- JavaScript window.location物
演示样例 注意 方法 常常使用window.location.它的结构总是记不住.简单梳理下.方便以后查询. 演示样例 URL:http://b.a.com:88/index.php? name=ka ...
- PL/SQL Developer跑在Oracle 64位数据库上初始化错误
安装完Oracle(64位).PL/SQL Developer后执行PL/SQL出现例如以下的错误: 网上查资料说,我的PL/SQL Developer与ORACLE不兼容,即PL/SQL不支持64位 ...
- 线性方程组的求解(C++)
1. 最佳求解方案 Most efficient way to solve a system of linear equations 求解形如 Ax=b 的最佳方式 将 A 分解为三角矩阵,A=M1⋅ ...
- jQuery迭代器
http://www.imooc.com/code/3417 迭代器 迭代器是一个框架的重要设计.我们经常需要提供一种方法顺序用来处理聚合对象中各个元素,而又不暴露该对象的内部,这也是设计模式中的迭代 ...
- javascript 次序li
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- wpf控件设计时支持(2)
原文:wpf控件设计时支持(2) 这篇介绍在wpf设计时集合项属性添加项的定义和自定义控件右键菜单的方法 集合项属性设计时支持 1.为集合属性设计器识别具体项类型 wpf设计器允许定义集合项的类型,如 ...
- .net core实现前后端彻底分离
问题的关键在跨域 1.我们在services里面 添加跨域内容如下: public void ConfigureServices(IServiceCollection services) { //这个 ...
- cocos2dx-3.1加入cocosStudio参考库 libCocosStudio
一个,创建好项目 两,在"解(项目名称)".选择现有项目 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2dzbHVja3k=/fon ...
- 关于VS2015中的code snippet无法使用的问题
什么是code snippet? Code snippets are small blocks of reusable code that can be inserted in a code file ...