Advertisement
Coriic

Untitled

Apr 27th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Solution {
  4.  
  5. public int solution(int[] A, int[] B){
  6. Map<Integer, Integer> mappedIntervals = prepareMap(A, B);
  7. Integer[] keyArray = mappedIntervals.keySet().toArray(new Integer[mappedIntervals.size()]);
  8. ArrayList<Integer> keyArrayList = new ArrayList<>(Arrays.asList(keyArray));
  9. int begginingFirst, endingFirst, begginingSecond, endingSecond;
  10. int iterator = 0;
  11. while(iterator < keyArrayList.size()-1){
  12. begginingFirst = keyArrayList.get(iterator);
  13. endingFirst = mappedIntervals.get(begginingFirst);
  14. begginingSecond = keyArrayList.get(iterator+1);
  15. endingSecond = mappedIntervals.get(begginingSecond);
  16. if((begginingSecond <= endingFirst) && (endingSecond >= endingFirst)){
  17. mappedIntervals.put(begginingFirst, endingSecond);
  18. mappedIntervals.remove(begginingSecond);
  19. keyArrayList.remove(iterator+1);
  20. }
  21. else{
  22. iterator++;
  23. }
  24. }
  25. return mappedIntervals.size();
  26. }
  27.  
  28. private Map<Integer, Integer> prepareMap(int[] A, int[] B){
  29. Map<Integer, Integer> mappedIntervals = new TreeMap<>();
  30. for(int i = 0; i<A.length; i++){
  31. mappedIntervals.put(A[i], B[i]);
  32. }
  33. return mappedIntervals;
  34. }
  35.  
  36. public static void main(String[] args){
  37. Solution solution = new Solution();
  38. int result = solution.solution(new int[]{1, 12, 42, 70, 36, -4, 43, 15}, new int[]{5, 15, 44, 72, 36, 2, 69, 24});
  39. System.out.println(result);
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement