Advertisement
dorucriv

Device crossover

Apr 1st, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.76 KB | None | 0 0
  1. public class Solution {
  2.    
  3.     enum VisitType { NONE, WEB, APP }
  4.    
  5.     public int solve(int[] A, int[] B) {
  6.         int count = 0;
  7.         int i = 0, j = 0;
  8.         VisitType prev = VisitType.NONE;
  9.  
  10.         while (i < A.length && j < B.length) {
  11.             if (A[i] < B[j]) {
  12.                 if (prev == VisitType.APP)
  13.                     count++;
  14.                 prev = VisitType.WEB;
  15.                 i++;
  16.             } else {
  17.                 if (prev == VisitType.WEB)
  18.                     count++;
  19.                 prev = VisitType.APP;
  20.                 j++;
  21.             }
  22.         }
  23.  
  24.         if (i < A.length && prev == VisitType.APP ||
  25.             j < B.length && prev == VisitType.WEB)
  26.             count++;
  27.  
  28.         return count;
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement