Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 超车_Java - Fatal编程技术网

Java 超车

Java 超车,java,Java,给出了一个由N个整数组成的非空零索引数组。数组A的连续元素表示道路上的连续车辆 阵列A仅包含0和/或1: 0 represents a car traveling east, 1 represents a car traveling west. 目标是统计过往车辆。我们说一对汽车(P,Q),其中0≤ P

给出了一个由N个整数组成的非空零索引数组。数组A的连续元素表示道路上的连续车辆

阵列A仅包含0和/或1:

    0 represents a car traveling east,
    1 represents a car traveling west.

目标是统计过往车辆。我们说一对汽车(P,Q),其中0≤ P

例如,考虑数组A这样:

  A[0] = 0
  A[1] = 1
  A[2] = 0
  A[3] = 1
  A[4] = 1
我们有五对超车:(0,1),(0,3),(0,4),(2,3),(2,4)

编写一个函数:

class Solution { public int solution(int[] A); } 
给定一个由N个整数组成的非空零索引数组,返回经过的车辆数

函数应该返回−1如果过往车辆数量超过100000000辆

例如,假设:

  A[0] = 0
  A[1] = 1
  A[2] = 0
  A[3] = 1
  A[4] = 1
函数应该返回5,如上所述

假设:

    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.
复杂性:

    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).
可以修改输入数组的元素


我不明白为什么有5辆车经过,而不是6辆。为什么(2,1)不算作过往车辆?有人能解释一下如何解决这个问题吗?

你需要计算车辆通过的次数。根据输入信息,汽车被定位在道路上,并开始向任一方向行驶。当汽车行驶时,我们可以很容易地看到,它将由朝相反方向行驶的汽车驾驶,但前提是汽车在它前面。基本上可以表述为:

  • 想象数组0..N

  • 获取元素X(从0迭代到第n个元素)

  • 如果元素X的值为0,则计算其右侧有多少个1元素

  • 若元素X的值为1,则计算其左侧有多少个0元素

  • 重复下一个X

  • 求和并除以2(因为需要两辆车才能通过),这就是答案

  • 如果
    01101
    我们有3+1+2+2+2=10。除以2等于5次通过

    我们不计算2-1对,因为第二辆车向东行驶,从未经过第一辆车向西行驶。

    时间复杂性-O(n) 空间复杂性-O(1) 我提出的逻辑是这样的

    • 有两个变量。计数和递增。将两者初始化为零
    • 遍历数组。每次找到0时,递增增量
    • 每次找到1时,通过将递增值添加到count来修改count
    • 数组遍历完成后,返回计数
    注意:下面提供的示例代码假设静态数组和预定义的数组大小。可以使用向量使其成为动态的

    #include <iostream>
    using namespace std;
    
    int getPass(int* A, int N)
    {
        unsigned long count = 0;
        int incrementVal = 0;
        for(int i = 0; i < N; i++)
        {
            if(A[i]==0)
            {
                incrementVal++;
            }
            else if (A[i]==1)
            {
                count = count + incrementVal;
            }
            if(count > 1000000000) return -1;
        }
        return count;
    }
    
    int main()
    {
       int A[]={0,1,0,1,1};
       int size = 5;
       int numPasses = getPass(A,size);
       cout << "Number of Passes: " << numPasses << endl;
    }
    
    #包括
    使用名称空间std;
    int getPass(int*A,int N)
    {
    无符号长计数=0;
    int incrementVal=0;
    对于(int i=0;i100000000)返回-1;
    }
    返回计数;
    }
    int main()
    {
    int A[]={0,1,0,1,1};
    int size=5;
    int numpasss=getPass(A,size);
    coutdef前缀和(A):
    n=len(A)
    p=[0]*(n+1)
    对于x范围内的k(1,n+1):
    p[k]=p[k-1]+A[k-1]
    返回p

    我认为时间复杂性是O(N^2) def溶液(A): lenA=len(A) x=0 numPassingCars=0 对于我来说,在一个: x+=1 对于A[x:]中的j: 如果i==0和j==1: numPassingCars+=1 如果numPassingCars>1000000000: 返回-1 还车

    我相信时间复杂性是O(N^2) def解决方案2(A):

    我相信时间复杂性是O(N) def解决方案3(A):

    B=[0,1,0,1,1]

    打印解决方案(B) 打印解决方案2(B) 打印解决方案3(B)

    输出:

    5
    5
    5
    

    我们只需要遍历零,就可以找到总的交叉数

    不需要为1运行额外的代码,因为我们检查的每个零的交叉数等于我们得到的交叉数,当我们检查所有1的特定零时

    public class passingCars {
    
    
    public static void main(String[] args) {
        int k;
        int cnt = 0;
         // Test array
        int[] A = { 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1 };
    
        for (int i = 0; i < A.length; i++) {
    
            if (A[i] == 0) {
                k = i + 1;
                while (k < A.length) {
                    if (A[k] == 1) {
                        cnt++;
                    }
                    k++;
                }
    
            }
    
        }
    
        System.out.println(cnt);
    
    }
    
    }
    
    公共级通行车{
    公共静态void main(字符串[]args){
    int k;
    int-cnt=0;
    //测试阵列
    int[]A={1,1,0,1,0,0,1,1,0,0,1,1};
    for(int i=0;i

    结果:17

    这是我在C语言中获得100%的代码#


    这是Java中的一个简单示例。我认为应该100%传递

    public int solution(int[] A) {
            int countOfZeros = 0, count = 0;
    
            for (int i = 0; i < A.length; i++){
                if (A[i] == 0) countOfZeros++;                    
                if (A[i] == 1) count += countOfZeros;    
                if (count > 1000000000) return -1;
            }
            return count;
    }
    
    public int解决方案(int[]A){
    int countOfZeros=0,count=0;
    for(int i=0;i100000000)返回-1;
    }
    返回计数;
    }
    
    PHP中的超车解决方案(100%基于可编程性)

    函数解决方案($A){
    $count=0;
    $helper=0;
    对于($i=0;$i 100000000)回报-1;
    }
    返回$count;
    }
    
    这是我用C语言编写的解决方案,它的分数为100%,而且很容易理解。诀窍是向后遍历数组

    int solution(int A[], int N) 
    {
    
    int goingWest = 0; 
    int passes = 0;
    
    for(int i = N-1; i >= 0; i--)
    {
        if(A[i] == 1)
        {
            goingWest++;   
        }
        else
        {
            passes += goingWest;   
    
            if(passes > 1000000000)
            return -1;
        }
    }
    return passes;
    
    }

    在目标c中,100%

    int passingCars(NSMutableArray*a)
    {
    如果(a.计算100000000)
    {
    返回-1;
    }
    }
    }
    回程车辆;
    }
    
    Java解决方案,100%可编程性:

    public int solution(int[] A) {
    
        int countZro = 0;
        int countCars = 0;
    
        for(int i=0; i<A.length;i++)
        {
            if(A[i] == 0)
            {
                countZro++;
            }
    
            if(countZro > 0 && A[i]==1 )
            {
                countCars += countZro;
    
            }
    
             if(countCars>1000000000)
             {
                 return -1;
             }
        }
    
        return countCars;
    
    }
    
    public int解决方案(int[]A){
    int countZro=0;
    int countCars=0;
    对于(int i=0;i 0&&A[i]=1)
    {
    countCars+=countZro;
    }
    如果(countCars>100000000)
    {
    返回-1;
    }
    }
    返回车辆;
    }
    
    在VB.NET中以100精度和性能编写代码

    私有函数解决方案(A作为整数())作为整数
    
    public int solution(int[] A) {
            int countOfZeros = 0, count = 0;
    
            for (int i = 0; i < A.length; i++){
                if (A[i] == 0) countOfZeros++;                    
                if (A[i] == 1) count += countOfZeros;    
                if (count > 1000000000) return -1;
            }
            return count;
    }
    
    function solution($A) {
    $count = 0;
    $helper = 0;
    for($i=0;$i<count($A);$i++){
        if($A[$i]==0){ 
            $helper++;                
        }
        else if($A[$i]==1){
            $count = $count + $helper;
        }
        if($count > 1000000000) return -1;
    }
    return $count;
    }
    
    int solution(int A[], int N) 
    {
    
    int goingWest = 0; 
    int passes = 0;
    
    for(int i = N-1; i >= 0; i--)
    {
        if(A[i] == 1)
        {
            goingWest++;   
        }
        else
        {
            passes += goingWest;   
    
            if(passes > 1000000000)
            return -1;
        }
    }
    return passes;
    
    int passingCars(NSMutableArray* a)
    {
        if (a.count <= 1)
        {
            return 0;
        }
        int passingCars = 0;
        int goingEast = 0;
    
        for (int i = 0; i < a.count; i ++)
        {
            if ([a[i] intValue] == 0)
            {
                goingEast++;
            }
            else
            {
                passingCars += goingEast;
                if (passingCars > 1000000000)
                {
                    return -1;
                }
            }
        }
        return passingCars;
    }
    
    public int solution(int[] A) {
    
        int countZro = 0;
        int countCars = 0;
    
        for(int i=0; i<A.length;i++)
        {
            if(A[i] == 0)
            {
                countZro++;
            }
    
            if(countZro > 0 && A[i]==1 )
            {
                countCars += countZro;
    
            }
    
             if(countCars>1000000000)
             {
                 return -1;
             }
        }
    
        return countCars;
    
    }
    
    public int solution(int[] A) {
        // write your code in C# 6.0 with .NET 4.5 (Mono)
    
            int west = 0; //  1
            int east = 0; //  0
            int pairsCounter = 0;
            if (A.Length < 0 || A.Length > 100000)
                return 0;
    
            if (A.Length == 2 && A[0] == 0 && A[1] == 1)
                return 1;
            // finds all west moving cars
            for (int i = 0; i < A.Length; i++)
            {
                if (A[i] == 1)
                    west++;
            }
    
            east = A.Length - west;
    
            if (east == 0 || west == 0)
                return 0;
    
            //if (east >= west) // a possible error in test case on codility. It let it be the situation when P >= Q situation, while P < Q < N
            //    return 0;
    
    
            for (int i = 0; (i < A.Length && west > 0); i++)
            {
                if (A[i] == 0 && west > 0)
                {   // calculates the combinations
                    pairsCounter = pairsCounter + west;
                }
                else
                {
                    west--;
                }
    
                if (pairsCounter > 1000000000)
                    return -1;
    
            }
    
    
            return pairsCounter;
    
    }
    
    // https://codility.com/demo/results/demoHECS6Y-NF5/ 100
    
    public class PassingCars {
      // [0,0,0,0,0] = 0
      // [0,0,1,1,1,1] = 8
      // [0,0,1,1,1,1,0] = 8
      // [1,0] = 0
      // [0,1,1,1,1,1,1,0] = 6
      // [1,1,1,1,0,1,0] = 1
      // [1,1,1,1,0,1,1,0,1] = 4
        public static final int FAIL = -1;
    
        public int solution(int[] A) {
            int N = A.length;
    
            if ( N < 2) { return 0; }
    
            int onesCount = 0;
            boolean zeroHappenedBefore = false;
            for (int i =0; i < N; ++i ) {
                if(zeroHappenedBefore && A[i] == 1 ) {
                    ++onesCount;
                } else if (A[i] == 0) {
                    zeroHappenedBefore = true;
                }
            }
    
            if (onesCount ==0) { return 0; }
    
            long combinations = 0;
            int conditionReturnFail = 1000000000;
            zeroHappenedBefore = false;
    
            for (int i=0; i < N; ++i) {
                if (A[i] == 0) {
                    combinations += onesCount;
                    if(conditionReturnFail < combinations) {
                        return FAIL;
                    }
                    zeroHappenedBefore = true;
                } else {
                    if (zeroHappenedBefore) {
                        --onesCount;
                    }
                }
            }
    
            return (int) combinations;
        }
    
    }
    
    void calculateOnes(int[] A, int[] sum) {
        int N = A.length;
        sum[N - 1] = A[N - 1];
        for (int i = N - 2; i >= 0; i--) {
            sum[i] = A[i] + sum[i + 1];
        }
    }
    
    public int solution(int[] A) {
        int N = A.length;
        int[] sum = new int[N];
        calculateOnes(A, sum);
        int counter = 0;
        for (int i = 0; i < N; i++) {
            if (A[i] == 0) {
                counter += sum[i];
            }
        }
        return counter;
    }
    
    class Solution {
        public int solution(int[] A) {
            final int MAX_RESULT = 1000000000;
            int result = 0, one_counter=0;
            for(int i=A.length-1; i>=0; i--) {
                if(A[i]==1) {
                    one_counter++;
                }
                else {
                    if(result>MAX_RESULT) {
                        return -1;
                    }
                    result+=one_counter;                
                }
            }
            return result;
        }
    }
    
    int solution(int A[], int N) {
        // write your code in C99 (gcc 4.8.2)
        int X = 0, count = 0, i;
    
        for(i = 0; i < N; i++)
        {
            if(A[i] == 0)
              X++;
            else {
                count += X;
            }        
    
            if(count > 1000000000) {
                return -1;
            }
        }
    
        return count;
    }
    
    function solution(A) {
        var zeroesCount = 0; //keeps track of zeroes
        var pairs = 0; //aka the result
    
        for (var i=0; i<A.length; i++) {
            A[i]===0 ? zeroesCount++ : pairs += zeroesCount; //count 0s or add to answer when we encounter 1s
    
            if (pairs > 1000000000 ) { //required by the question
                return -1;   
            }
        }
    
        return pairs;
    }
    
    class Solution {
    public int solution(int[] A) {
        int countZeros=0;
        int sumOfValidPairs=0;
        int i=0;
    
        while(i<A.length){
            if(A[i] == 0){
                countZeros++;
            }
            else{
                sumOfValidPairs += countZeros;
            }
            i++;
        }
        //handle overflow with ">=0"
        if(sumOfValidPairs <= 1000000000 && sumOfValidPairs >= 0){
        return sumOfValidPairs;
        }
        return -1;
      }
    
    }
    
    0 ≤ P < Q < N
    
    public int solution(int[] A) {
        int ones =0, count=0;
         for (int j = A.length-1; j >=0; j--) {
             if (A[j] ==1) {
                 ones++;
             } else if (A[j] ==0) {
                 count+=ones;
             }             
             if ( count > 1_000_000_000) {
               return     -1;
             }
         }
         return count;
     }
    
    public func SuffixCount(_ A : inout [Int]) -> [Int] {
    
        var P = [Int](repeating: 0, count: A.count+1)
    
        P[A.count] = 0
    
        for i in stride(from: A.count-1, to: 0, by: -1) {
            P[i] = P[i+1] + A[i]
        }
    
        return P
    }
    
    A = [0, 1, 0, 1, 1]
    print("SufficeCount: ", SuffixCount(&A))
    
    public func PassingCars(_ A : inout [Int]) -> Int {
    
        let P = SuffixCount(&A)
    
        var result = 0
    
        for i in  0..<A.count {
    
            if A[i] == 0 {
                result += P[i+1]
            }
        }
    
        return result
    }
    
    print("PassingCars: ", PassingCars(&A))
    
    public int solution(int[] A) {
            int A1 = 0;
            int pair = 0;
            for(int i = A.length-1 ; i>=0; i--){
                if(A[i]==1){
                    A1++;
                }else{
                    pair += A1;
                }
                if(pair > 1000000000){
                    return -1;
                }
            }
            return pair;
    }
    
    class Solution 
    {
        public int solution(int[] A) {
    
            int N = A.length;
            int[] P = new int[N+1];
            int sum = 0;
    
            for(int i=1; i < N+1; i++){
                P[i] = P[i-1] + A[i-1];
            }
    
            for(int i=0; i < N; i++){
                if(A[i]==0)
                {
                  sum += P[N] - P[i];
                }
    
            }
    
            if( sum > 1000000000 || sum < 0 )
            {
              return -1;
            }
    
            return sum;
    
    
        }
    }
    
    function solution (A) {
      let count0 = 0;
      let total = 0;
    
      A.forEach((elem) => {
        if (elem === 0) count0++;
        if (elem === 1) {
          total = count0 * 1 + total;
        }
      })
    
      return total > 1000000000
        ? -1
        : total;
    }
    
    def solution(array):
        zero_count = 0
        combinations = 0
        for item in array:
            if item == 0:
                zero_count += 1
            else:
                combinations += zero_count
    
            if combinations > 1000000000:
                return -1
    
        return combinations
    
    function solution(A) {
      let pairsCount = 0;
      const eastBound = 0;
      let incCounter = 0;
      for (let i = 0; i < A.length; i++) {
        // If current car is east bound, increment the counter by 1
        // next time onwards, when westbound car comes, par count would be increased by increased counter
        if (eastBound === A[i]) {
          incCounter++;
        } else {
          pairsCount += incCounter;
        }
      }
      return pairsCount <= 1000000000 ? pairsCount : -1;
    }
    
        // 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 sum = 0;
            int occurence = 0;
    
            for ( int i =0 ; i < A.length; i ++ )
            {
    
                if ( A[i] == 0 )
                    occurence++;
                else
                {
                    sum+=occurence;
                    if ( sum > 1000000000 )
                        return -1;
                }
            }
    
            return sum;
        }
    }
    
    public int solution(int[] A) {
            int countOne = 0, result = 0, n = A.length;
            //bactracking and adding # of 1s to result when met with 0
            while (n >= 1) {
                if (A[n - 1] == 1)
                    countOne++;
                else
                    result += countOne;
                n--;
    
                if (result > 1000000000)
                    return -1;
            }
            return result;
    
        }
    
    function solution(A) {
    // let consider car going east be the one we choose. when we go east and find any cars going west increment the count and add the increment to the pairs 
    const len = A.length;
    let count = 0; 
    let pairs = 0; 
    for( let i = 0; i < len; i++) {
        if (A[i] === 0) count++;
        if (A[i] === 1) pairs = pairs + count;
        if (pairs > 1000000000) {
            pairs = -1;
            break;
        }
    }
    return pairs;