Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LC138][C++][Linked List][v1] #168

Merged
merged 5 commits into from Oct 30, 2019
Merged

[LC138][C++][Linked List][v1] #168

merged 5 commits into from Oct 30, 2019

Conversation

xxks-kkk
Copy link
Owner

@xxks-kkk xxks-kkk commented Oct 28, 2019

Summary

Resolves: Leetcode 138

Citadel Research Platform team onsite (10/25/19)

Main Techniques:

  • Brute force

We first create a copy of the list without assigning value to random field. Next, we use a hash table to store the mapping from nodes in the original list to nodes in the copied list. Finally, traverse the original list and the new list in tandem, using the mapping to assign each random field.

  • Optimized

The key to improve the space complexity is to use the next field for each node in the original list to record the mapping from the original node to its copy. To avoid losing the structure of the original list, we use the next field in each copied node to point to the successor of its original node.

Ref: EPI p.446

Runtime Complexity Analysis

  • Brute force: O(n), where n is the number of nodes in the original list

  • Optimized: O(n)

Space Complexity Analysis

  • Brute force: O(n), where n is the number of nodes in the original list

  • Optimized: O(1)

Testing

Performance

Performance Metrics from OJ Platform

  • Brute force
Runtime: 36 ms, faster than 37.86% of C++ online submissions for Copy List with Random Pointer.
Memory Usage: 22.2 MB, less than 73.81% of C++ online submissions for Copy List with Random Pointer.
  • Optimized
Runtime: 24 ms, faster than 99.02% of C++ online submissions for Copy List with Random Pointer.
Memory Usage: 21.9 MB, less than 100.00% of C++ online submissions for Copy List with Random Pointer.

Performance Improved Since Last Change

Implementation Notice

In stage 3 of the optimized approach implementation, we have

    iter = head;
    auto new_list_head = iter->next;
    while(iter->next) {
      auto temp = iter->next;
      iter->next = temp->next;
      iter = temp;
    }

Here temp points to the original list and copied list node in alternative fashion so that we can fix the original list next field and the copied list next field at the same time in one while loop. Very smart!

Implementation Tricks

To Do

EPI p.446 has a robust C++ implementation with usage of shared_ptr. However, this implementation clashes with the Leetcode definition. For example, we need to implement new definition of Node with shared_ptr<Node_EPI> as the type for random and next field. Along the way, we may need to implement the corresponding test infrastructure. You can check branch https://github.com/xxks-kkk/shuati/tree/138-shared for part of implementation attempt.

To Learn

Questions

Language

  • R"({"$id":"1","next":{"$id":"2","next":null,"random":{"$ref":"2"},"val":2},"random":{"$ref":"2"},"val":1})" uses C++11 string literal to easily write string with ".

Failure Attempts

@xxks-kkk xxks-kkk added the C++ good C++ usage and practice; C++ usage note label Oct 29, 2019
@xxks-kkk xxks-kkk added this to the 1.0 milestone Oct 29, 2019
@xxks-kkk xxks-kkk added the todo Some leftover need to be done label Oct 30, 2019
@xxks-kkk xxks-kkk changed the title [LC138][C++][Hash Map][v1] [LC138][C++][Linked List][v1] Oct 30, 2019
@xxks-kkk xxks-kkk mentioned this pull request Oct 30, 2019
@xxks-kkk xxks-kkk merged commit dc853a0 into master Oct 30, 2019
@xxks-kkk xxks-kkk deleted the 138 branch October 30, 2019 04:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C++ good C++ usage and practice; C++ usage note todo Some leftover need to be done
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant