Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.21 KB | None | 0 0
  1. public class Solution {
  2.     public IList<IList<int>> PacificAtlantic(int[][] matrix) {
  3.  
  4.         if(matrix == null || matrix.Length == 0)
  5.             return new List<IList<int>>();
  6.        
  7.         var tmp = Enumerable
  8.             .Range(0, matrix.Length)
  9.             .Select(r => new Coord { R = r, C = 0 });
  10.  
  11.         tmp = tmp.Concat(Enumerable
  12.             .Range(1, matrix[0].Length - 1)
  13.             .Select(c => new Coord { R = 0, C = c }));
  14.  
  15.         var pacificEdges = tmp.ToArray();
  16.  
  17.         var lrindex = matrix.Length - 1;
  18.         tmp = Enumerable
  19.             .Range(0, matrix.Length)
  20.             .Select(r => new Coord { R = lrindex - r, C = matrix[lrindex-r].Length - 1 });
  21.  
  22.         var lcindex = matrix[lrindex].Length - 1;
  23.         tmp = tmp.Concat(Enumerable
  24.             .Range(0, matrix[lrindex].Length)
  25.             .Select(c => new Coord { R = lrindex, C = lcindex - c }));
  26.  
  27.         var atlanticEdges = tmp.ToArray();
  28.  
  29.         //atlantic points
  30.         var atlanticContext = new Context(matrix);
  31.         foreach(var coord in atlanticEdges)
  32.         {
  33.             if(!atlanticContext.Traversed.Contains(coord))
  34.                 FindPeak(new[] { coord }, atlanticContext);
  35.         }
  36.  
  37.         var pacificContext = new Context(matrix);
  38.         foreach(var coord in pacificEdges)
  39.         {
  40.             if(!pacificContext.Traversed.Contains(coord))
  41.                 FindPeak(new[] { coord }, pacificContext);
  42.         }
  43.  
  44.         return atlanticContext.Traversed
  45.             .Intersect(pacificContext.Traversed)
  46.             .Select(p => p.ToList())
  47.             .ToList();
  48.     }
  49.  
  50.     public static void FindPeak(IEnumerable<Coord> bfs, Context context)
  51.     {
  52.         foreach (var coord in bfs)
  53.             context.Traversed.Add(coord);
  54.  
  55.         //gather next level
  56.         var set = new HashSet<Coord>();
  57.         foreach(var coord in bfs)
  58.         {
  59.             var nextcoords = NextCoords(coord, context);
  60.             foreach (var c in nextcoords)
  61.                 set.Add(c);
  62.         }
  63.  
  64.         if (set.Count() > 0)
  65.             FindPeak(set, context);
  66.     }
  67.  
  68.     public static Coord[] NextCoords(Coord coord, Context context)
  69.     {
  70.         var list = new List<Coord>();
  71.  
  72.         //left
  73.         var tmp = coord.C - 1;
  74.         if (tmp >= 0
  75.             && !context.IsTraversed(coord.R, tmp)
  76.             && context.ValueOf(coord.R, tmp) >= context.ValueOf(coord.R, coord.C))
  77.             list.Add(new Coord { C = tmp, R = coord.R });
  78.  
  79.         //right
  80.         tmp = coord.C + 1;
  81.         if (tmp < context.Matrix[coord.R].Length
  82.             && !context.IsTraversed(coord.R, tmp)
  83.             && context.ValueOf(coord.R, tmp) >= context.ValueOf(coord.R, coord.C))
  84.             list.Add(new Coord { C = tmp, R = coord.R });
  85.  
  86.         //top
  87.         tmp = coord.R - 1;
  88.         if (tmp >= 0
  89.             && !context.IsTraversed(tmp, coord.C)
  90.             && context.ValueOf(tmp, coord.C) >= context.ValueOf(coord.R, coord.C))
  91.             list.Add(new Coord { C = coord.C, R = tmp });
  92.  
  93.         //right
  94.         tmp = coord.R + 1;
  95.         if (tmp < context.Matrix.Length
  96.             && !context.IsTraversed(tmp, coord.C)
  97.             && context.ValueOf(tmp, coord.C) >= context.ValueOf(coord.R, coord.C))
  98.             list.Add(new Coord { C = coord.C, R = tmp });
  99.  
  100.         return list.ToArray();
  101.     }
  102. }
  103.  
  104. public class Context
  105. {
  106.     public HashSet<Coord> Traversed { get; } = new HashSet<Coord>();
  107.     //public HashSet<Coord> Peaks { get; } = new HashSet<Coord>();
  108.  
  109.     public int[][] Matrix { get; }
  110.  
  111.     public Context(int[][] matrix)
  112.     {
  113.         Matrix = matrix;
  114.     }
  115.  
  116.     public bool IsTraversed(int r, int c)
  117.         => Traversed.Contains(new Coord { R = r, C = c });
  118.  
  119.     public int ValueOf(int r, int c) => Matrix[r][c];
  120. }
  121.  
  122. public class Coord
  123. {
  124.     public int C { get; set; }
  125.     public int R { get; set; }
  126.  
  127.     public IList<int> ToList() => new List<int> { R, C };
  128.  
  129.     public override string ToString() => $"{R},{C}";
  130.  
  131.     public override bool Equals(object obj)
  132.     {
  133.         return obj is Coord other
  134.             && other.C == C
  135.             && other.R == R;
  136.     }
  137.  
  138.     public override int GetHashCode()
  139.     {
  140.         var hash = 3 * 5 + R;
  141.         return hash * 7 + C;
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement