Guest User

Untitled

a guest
Jul 18th, 2024
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. public class Solution {
  2. public int[][] Merge(int[][] intervals) {
  3. var store = new HashSet<int>[10001];
  4. var result = new List<int[]>();
  5. var currentStore = 1;
  6.  
  7. foreach(var interval in intervals)
  8. {
  9. var left = interval[0];
  10. var right = interval[1];
  11.  
  12. while (left <= right)
  13. {
  14. if (store[left] is null)
  15. store[left] = new HashSet<int>();
  16.  
  17. store[left++].Add(currentStore);
  18. }
  19.  
  20. currentStore++;
  21. }
  22.  
  23. for (var i = 0; i < store.Length; ++i)
  24. {
  25. if (store[i] is null)
  26. continue;
  27.  
  28. var currentSet = new HashSet<int>(store[i]);
  29. var innerResult = new int[2];
  30.  
  31. innerResult[0] = i;
  32.  
  33. while (i < store.Length && store[i] is not null && currentSet.Any(x => store[i].Contains(x)))
  34. {
  35. currentSet.UnionWith(store[i]);
  36. i++;
  37. }
  38.  
  39. innerResult[1] = --i;
  40.  
  41. result.Add(innerResult);
  42. }
  43.  
  44. return result.ToArray();
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment