当前位置:   article > 正文

C语言 | Leetcode C语言题解之第218题天际线问题

C语言 | Leetcode C语言题解之第218题天际线问题

题目:

题解:

  1. struct pair {
  2. int first, second;
  3. };
  4. struct Heap {
  5. struct pair* heap;
  6. int heapSize;
  7. bool (*cmp)(struct pair*, struct pair*);
  8. };
  9. void init(struct Heap* obj, int n, bool (*cmp)(struct pair*, struct pair*)) {
  10. obj->heap = malloc(sizeof(struct pair) * (n + 1));
  11. obj->heapSize = 0;
  12. obj->cmp = cmp;
  13. }
  14. bool cmp1(struct pair* a, struct pair* b) {
  15. return a->second < b->second;
  16. }
  17. void swap(struct pair* a, struct pair* b) {
  18. struct pair tmp = *a;
  19. *a = *b, *b = tmp;
  20. }
  21. void push(struct Heap* obj, int x, int y) {
  22. int p = ++(obj->heapSize), q = p >> 1;
  23. obj->heap[p] = (struct pair){x, y};
  24. while (q) {
  25. if (!obj->cmp(&(obj->heap[q]), &(obj->heap[p]))) {
  26. break;
  27. }
  28. swap(&(obj->heap[q]), &(obj->heap[p]));
  29. p = q, q = p >> 1;
  30. }
  31. }
  32. void pop(struct Heap* obj) {
  33. swap(&(obj->heap[1]), &(obj->heap[(obj->heapSize)--]));
  34. int p = 1, q = p << 1;
  35. while (q <= obj->heapSize) {
  36. if (q + 1 <= obj->heapSize) {
  37. if (obj->cmp(&(obj->heap[q]), &(obj->heap[q + 1]))) {
  38. q++;
  39. }
  40. }
  41. if (!obj->cmp(&(obj->heap[p]), &(obj->heap[q]))) {
  42. break;
  43. }
  44. swap(&(obj->heap[q]), &(obj->heap[p]));
  45. p = q, q = p << 1;
  46. }
  47. }
  48. struct pair top(struct Heap* obj) {
  49. return obj->heap[1];
  50. }
  51. bool empty(struct Heap* obj) {
  52. return obj->heapSize == 0;
  53. }
  54. int cmp(int* a, int* b) {
  55. return *a - *b;
  56. }
  57. int** getSkyline(int** buildings, int buildingsSize, int* buildingsColSize, int* returnSize, int** returnColumnSizes) {
  58. int n = buildingsSize;
  59. struct Heap* heap = malloc(sizeof(struct Heap));
  60. init(heap, n << 1, cmp1);
  61. int boundaries[n << 1];
  62. for (int i = 0; i < n; i++) {
  63. boundaries[i << 1] = buildings[i][0];
  64. boundaries[i << 1 | 1] = buildings[i][1];
  65. }
  66. qsort(boundaries, n << 1, sizeof(int), cmp);
  67. int** ret = malloc(sizeof(int*) * (n << 1));
  68. *returnColumnSizes = malloc(sizeof(int) * (n << 1));
  69. *returnSize = 0;
  70. int idx = 0;
  71. for (int i = 0; i < (n << 1); i++) {
  72. int boundary = boundaries[i];
  73. while (idx < n && buildings[idx][0] <= boundary) {
  74. push(heap, buildings[idx][1], buildings[idx][2]);
  75. idx++;
  76. }
  77. while (!empty(heap) && top(heap).first <= boundary) {
  78. pop(heap);
  79. }
  80. int maxn = empty(heap) ? 0 : top(heap).second;
  81. if ((*returnSize) == 0 || maxn != ret[(*returnSize) - 1][1]) {
  82. int* tmp = malloc(sizeof(int) * 2);
  83. tmp[0] = boundary, tmp[1] = maxn;
  84. (*returnColumnSizes)[*returnSize] = 2;
  85. ret[(*returnSize)++] = tmp;
  86. }
  87. }
  88. return ret;
  89. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/798231
推荐阅读
相关标签
  

闽ICP备14008679号