veryinnocentboy

MaxCommonSubarrayInMatrix.java

Jun 12th, 2021 (edited)
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1. package playground;
  2.  
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.Map;
  6. import java.util.Set;
  7.  
  8. public class MaxCommonSubarrayInMatrix {
  9.     public static void main(String[] args) {
  10.         MaxCommonSubarrayInMatrix maxCommonSubarrayInMatrix = new MaxCommonSubarrayInMatrix();
  11. //        maxCommonSubarrayInMatrix.action(
  12. //                new int[][]{
  13. //                        {1,2,3}, {3,1,2},
  14. //                }
  15. //        );
  16. //        maxCommonSubarrayInMatrix.action(
  17. //                new int[][]{
  18. //                        {1,2,3,4}, {3,4,1,2},
  19. //                }
  20. //        );
  21. //        maxCommonSubarrayInMatrix.action(
  22. //                new int[][]{
  23. //                        {2,3,4,5,1}, {4,5,1,3,2},
  24. //                }
  25. //        );
  26.         maxCommonSubarrayInMatrix.action(
  27.                 new int[][]{
  28.                         {2,3,14,5,1,9}, {4,5,15,5,1,9},{1,2,31,5,1,9}
  29.                 }
  30.         );
  31.  
  32.     }
  33.     Map<String, Integer> mp;
  34.     public void action(int[][] arr){
  35.         mp = new HashMap<>();
  36.         System.out.println(
  37.                 getMaxCommonSubarrayInMatrix(arr)
  38.         );
  39.     }
  40.     public int getMaxCommonSubarrayInMatrix(int[][] arr){
  41.         Map<Integer, Integer> otherMap =new HashMap<>();
  42. //        int ans = 0;
  43.         // O(n^2) loop to get directional pairs
  44.         // if a directional pair exist in all of rows() then it might be part of final max subarray
  45.          for(int i=0;i<arr.length;++i){
  46.             for(int j=0;j<arr[0].length-1;++j){
  47.                 String key = String.format("%s|%s", arr[i][j], arr[i][j+1]);
  48.                 mp.put(key, mp.getOrDefault(key, 0)+1);
  49.                 if(mp.get(key) == arr.length){
  50. //                    System.out.println(mp.get(key));
  51. //                    ans = mp.get(key);
  52.                     otherMap.put(arr[i][j], arr[i][j+1]);
  53.                 }
  54.             }
  55.         }
  56.         System.out.println(otherMap);
  57.         // here we need to merge all intervals
  58.         // merging logic is just O(N) operation
  59.         // going as deep as can while following trail on hashmap
  60.         int maxcount =0;
  61.         for(Map.Entry<Integer, Integer> entry : otherMap.entrySet()){
  62.             int count = 0;
  63.             Integer startKey = entry.getKey();
  64.             while(
  65.                             otherMap.containsKey(startKey)){
  66.                 count++;
  67.                 startKey = otherMap.get(startKey);
  68.             }
  69.             maxcount = Math.max(maxcount, count);
  70.         }
  71.  
  72.         return maxcount;
  73.     }
  74. }
  75.  
Add Comment
Please, Sign In to add comment