Guest User

Balloon Problem

a guest
Jun 9th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. import static java.lang.Integer.parseInt;
  8.  
  9. /**
  10.  * Created by bugkiller on 09/06/19.
  11.  */
  12. class Baloni {
  13.  
  14.     static int a[] = new int[1000000];
  15.     public static void main(String[] args) throws IOException {
  16.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  17.         int n = parseInt(br.readLine());
  18.         String s[] = br.readLine().split("\\s");
  19.         for (int i = 0; i < n; i++) {
  20.             a[i] = Integer.parseInt(s[i]);
  21.         }
  22.         System.out.println(minimumArrows(a, n));
  23.     }
  24.  
  25.     private static int minimumArrows(int[] a, int n) {
  26.         Map<Integer, Integer> frequency = new HashMap<>();
  27.         int arrows = 0;
  28.         for (int i = 0; i < n; i++) {
  29.             if (frequency.containsKey(a[i])) {
  30.                 frequency.compute(a[i], (key, value) -> value - 1);
  31.                 frequency.remove(a[i], 0);
  32.                 frequency.merge(a[i] - 1, 1, (ov, nv) -> ov + 1);
  33.             } else {
  34.                 frequency.merge(a[i] - 1, 1, (ov, nv) -> ov + 1);
  35.                 arrows++;
  36.             }
  37.         }
  38.         return arrows;
  39.     }
  40. }
Add Comment
Please, Sign In to add comment