Skip to content

Commit

Permalink
July 9: reorder list & manhattan distance [H]
Browse files Browse the repository at this point in the history
list manipulation, like the get middle of linked list, reverse linked list
reorder linked list is a combination of middle and reverse problem

the manhattan distance is a math problem
  • Loading branch information
aucker committed Jul 9, 2024
1 parent fe2099c commit 6355f8d
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions daily/Jul9.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include <bits/stdc++.h>
using namespace std;

struct ListNode {
int val;
ListNode* next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode* next) : val(x), next(next) {}
};

class Solution {
ListNode* middleNode(ListNode* head) {
ListNode *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}

ListNode* reverseList(ListNode* head) {
ListNode *pre = nullptr, *cur = head;
while (cur) {
ListNode* nxt = cur->next;
cur->next = pre;
pre = cur;
cur = nxt;
}
return pre;
}

public:
/**
* @brief LC: 143: reorder list [M]
* Time: O(n), Space: O(1)
* Interview: bilibili
* Just use the middle node and reverse the list
* according the middle node, reverse the rest
*
* @param head
*/
void reorderList(ListNode* head) {
ListNode* mid = middleNode(head);
ListNode* head2 = reverseList(mid);
while (head2->next) {
ListNode* nxt = head->next;
ListNode* nxt2 = head2->next;
head->next = head2;
head2->next = nxt;
head = nxt;
head2 = nxt2;
}
}

public:
/**
* @brief LC: 3102: Minimize Manhattan Distances [H]
* WTF? Manhattan Distance to Chebyshev Distance
* Time: O(nlogn), Space: O(n), n is the length of points
*
* @param points
* @return int
*/
int minimumDistance(vector<vector<int>>& points) {
multiset<int> xs, ys;
for (auto& p : points) {
xs.insert(p[0] + p[1]);
ys.insert(p[1] - p[0]);
}

int ans = INT_MAX;
for (auto& p : points) {
int x = p[0] + p[1], y = p[1] - p[0];
xs.erase(xs.find(x)); // remove one x
ys.erase(ys.find(y)); // remove one y

int dx = *xs.rbegin() - *xs.begin();
int dy = *ys.rbegin() - *ys.begin();
ans = min(ans, max(dx, dy));

xs.insert(x);
ys.insert(y);
}
return ans;
}
};

1 comment on commit 6355f8d

@aucker
Copy link
Owner Author

@aucker aucker commented on 6355f8d Jul 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reorder linked list is the basic & important algorithm to prepare for the interview. >_

Please sign in to comment.