Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Solution {
- public int solution(int[] A, int[] B){
- Map<Integer, Integer> mappedIntervals = prepareMap(A, B);
- Integer[] keyArray = mappedIntervals.keySet().toArray(new Integer[mappedIntervals.size()]);
- ArrayList<Integer> keyArrayList = new ArrayList<>(Arrays.asList(keyArray));
- int begginingFirst, endingFirst, begginingSecond, endingSecond;
- int iterator = 0;
- while(iterator < keyArrayList.size()-1){
- begginingFirst = keyArrayList.get(iterator);
- endingFirst = mappedIntervals.get(begginingFirst);
- begginingSecond = keyArrayList.get(iterator+1);
- endingSecond = mappedIntervals.get(begginingSecond);
- if((begginingSecond <= endingFirst) && (endingSecond >= endingFirst)){
- mappedIntervals.put(begginingFirst, endingSecond);
- mappedIntervals.remove(begginingSecond);
- keyArrayList.remove(iterator+1);
- }
- else{
- iterator++;
- }
- }
- return mappedIntervals.size();
- }
- private Map<Integer, Integer> prepareMap(int[] A, int[] B){
- Map<Integer, Integer> mappedIntervals = new TreeMap<>();
- for(int i = 0; i<A.length; i++){
- mappedIntervals.put(A[i], B[i]);
- }
- return mappedIntervals;
- }
- public static void main(String[] args){
- Solution solution = new Solution();
- int result = solution.solution(new int[]{1, 12, 42, 70, 36, -4, 43, 15}, new int[]{5, 15, 44, 72, 36, 2, 69, 24});
- System.out.println(result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement