thelili018's blog

By thelili018, history, 4 years ago, In English,

import java.util.Scanner;

public class Taxi2 {

public static void main (String[] args) {

    int n, x, count;
    int[] a;

    Scanner sc = new Scanner(System.in);
    n = sc.nextInt();
    a = new int[4];

    for(int i = 0; i < n; i++) {
       x = sc.nextInt();
       switch(x) {
       case 1: a[0]++; break;
       case 2: a[1]++; break;
       case 3: a[2]++; break;
       case 4: a[3]++; break;
       }
    }

    count = a[3];

    if(a[2] != 0 && a[0] != 0) {
       x = (a[2] >= a[0]) ? a[0] : a[2];
       for(int i = 0; i < x; i++) {
         a[2]--;
         a[0]--;
         count++;
       }
    }

    count += a[2];
    count += a[1] / 2;
    a[1] = a[1] % 2;

    if(a[1] != 0 && a[0] >= 2) {
       a[1]--;
       a[0] = a[0] - 2;
       count++;
    }
    else
       count += a[1];

    count += a[0] / 4;
    if(a[0] % 4 != 0)
       count++;

    System.out.println(count);
}

}

 
 
 
 
  • Vote: I like it
  • -6
  • Vote: I do not like it

»
4 years ago, # |
Rev. 3   Vote: I like it +8 Vote: I do not like it

deleted

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Is that why it fails on test 5? It's a time-complexity issue?

    • »
      »
      »
      4 years ago, # ^ |
      Rev. 2   Vote: I like it +5 Vote: I do not like it

      For test 5 answer is 1 but your code is giving output as 2.

      Test case 5 is

      2

      2 1

      You can put both groups in one taxi so answer is one.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    You cannot solve it in O(1) just because you have to input N numbers...

»
2 months ago, # |
  Vote: I like it 0 Vote: I do not like it

import java.util.Scanner; import java.util.Arrays; public class Solution{ //what is wrong in this code ,this code is not working on testcase 37 where 3 values are given 3 3 2 // this code will produce 2 but the actual answer should be 3.(but how it can produce 3 beacuse there //are total 8 children so we can divide it into 4 and 4 ,so we need 2 taxies only and also we have to //return minimum no of taxies not maximum.) //please anyone can explain this

public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    int n = s.nextInt();
    int a[] = new int[n];
    int sum = 0;
    for(int i = 0;i<n;i++){
        a[i] = s.nextInt();
        sum += a[i];
    }
    int ans = 0;
    if(sum%4 != 0){
        ans = ans+sum/4+1;
        System.out.println(ans);

    }
    if(sum%4 == 0){
        ans  = ans+sum/4;
        System.out.println(ans);
    }



}

}