楼主: zhuli19901106
跳转到指定楼层
上一主题 下一主题
收起左侧

分享我的Lintcode题解,目前进度244/248

 
🔗
 楼主| zhuli19901106 2015-7-21 20:34:45 | 只看该作者
全局:
Minimum Adjustment Cost
题意:给定一个数组,你可以随意改变每个元素,要求改好之后相邻两元素的差值不大于target。
解法:刚拿到这题真是毫无思路,后来才注意到下面给的提示,所有元素都不大于100,这其实就是在提示我们可以用比较暴力的做法,于是就有了下面的DP。如果不加这个限制条件,这种近乎穷举的做法必然是不可行的。
代码:
  1. #include <algorithm>
  2. #include <climits>
  3. using namespace std;

  4. class Solution {
  5. public:
  6.     /**
  7.      * @param A: An integer array.
  8.      * @param target: An integer.
  9.      */
  10.     int MinAdjustmentCost(vector<int> A, int target) {
  11.         const int MAXV = 100;
  12.         int n = A.size();
  13.         if (n <= 1) {
  14.             return 0;
  15.         }
  16.         vector<vector<int> > dp;
  17.         dp.resize(n, vector<int>(MAXV + 1, INT_MAX));
  18.         
  19.         int i, j, k;
  20.         for (i = 1; i <= MAXV; ++i) {
  21.             dp[0][i] = abs(i - A[0]);
  22.         }
  23.         int ll, rr;
  24.         for (i = 1; i < n; ++i) {
  25.             for (j = 1; j <= MAXV; ++j) {
  26.                 ll = max(1, j - target);
  27.                 rr = min(MAXV, j + target);
  28.                 for (k = ll; k <= rr; ++k) {
  29.                     dp[i][j] = min(dp[i][j], dp[i - 1][k]);
  30.                 }
  31.                 dp[i][j] += abs(j - A[i]);
  32.             }
  33.         }
  34.         int ans = INT_MAX;
  35.         for (i = 1; i <= MAXV; ++i) {
  36.             ans = min(ans, dp[n - 1][i]);
  37.         }
  38.         return ans;
  39.     }
  40. private:
  41.     int abs(int x) {
  42.         return x >= 0 ? x : -x;
  43.     }
  44. };
复制代码
复杂度:懒得算。不知为何,对这题有种厌恶感。就像当年讨厌考试一样,总觉得是为了考而考。做不出来不觉得可惜,AC了也毫无成就感。
回复

使用道具 举报

🔗
 楼主| zhuli19901106 2015-7-21 20:38:23 | 只看该作者
全局:
Backpack
题意:背包问题,给定N件物品和一个容量M,看最满能装多满。
解法:请看代码。
代码:
  1. class Solution {
  2. public:
  3.     /**
  4.      * @param m: An integer m denotes the size of a backpack
  5.      * @param A: Given n items with size A[i]
  6.      * @return: The maximum size
  7.      */
  8.     int backPack(int m, vector<int> A) {
  9.         vector<bool> dp;
  10.         dp.resize(m + 1, false);
  11.         dp[0] = true;
  12.         
  13.         int n = A.size();
  14.         int i, j;
  15.         for (i = 0; i < n; ++i) {
  16.             for (j = m; j >= A[i]; --j) {
  17.                 if (dp[j - A[i]]) {
  18.                     dp[j] = true;
  19.                 }
  20.             }
  21.         }
  22.         i = m;
  23.         while (!dp[i]) {
  24.             --i;
  25.         }
  26.         return i;
  27.     }
  28. };
复制代码
复杂度:时间O(N * M),空间O(M)。
回复

使用道具 举报

🔗
 楼主| zhuli19901106 2015-7-21 20:45:06 | 只看该作者
全局:
Balanced Binary Tree
题意:给定一棵二叉树,判断是否为平衡二叉树。
解法:左子树和右子树的高度差不超过1,则平衡。当然,空树也算平衡。递归解决。
代码:
  1. #include <unordered_map>
  2. using namespace std;
  3. /**
  4. * Definition of TreeNode:
  5. * class TreeNode {
  6. * public:
  7. *     int val;
  8. *     TreeNode *left, *right;
  9. *     TreeNode(int val) {
  10. *         this->val = val;
  11. *         this->left = this->right = NULL;
  12. *     }
  13. * }
  14. */
  15. class Solution {
  16. public:
  17.     /**
  18.      * @param root: The root of binary tree.
  19.      * @return: True if this Binary tree is Balanced, or false.
  20.      */
  21.     bool isBalanced(TreeNode *root) {
  22.         um.clear();
  23.         if (root == NULL) {
  24.             return true;
  25.         }
  26.         um[NULL] = 0;
  27.         height(root);
  28.         return balance(root);
  29.     }
  30. private:
  31.     unordered_map<TreeNode *, int> um;
  32.    
  33.     int height(TreeNode *root) {
  34.         if (root == NULL) {
  35.             return 0;
  36.         }
  37.         return um[root] = max(height(root->left), height(root->right)) + 1;
  38.     }
  39.    
  40.     bool balance(TreeNode *root) {
  41.         if (root == NULL) {
  42.             return true;
  43.         }
  44.         return balance(root->left) && balance(root->right) &&
  45.                abs(um[root->left] - um[root->right]) <= 1;
  46.     }
  47. };
复制代码
复杂度:时间O(N),空间O(N)。
回复

使用道具 举报

🔗
 楼主| zhuli19901106 2015-7-21 20:55:56 | 只看该作者
全局:
Binary Tree Maximum Path Sum
题意:给定一棵二叉树,如果把这棵树看成一个无向图,起点和终点任选,求从一个节点到另一个节点的所有路径中,和最大的。求出最大和。
解法:这题代码比较简洁。其中singlePath表示从节点root出发,往下走能得到的最大和。doublePath表示经过root节点的路径的最大和。其余请参见代码。
代码:
  1. #include <algorithm>
  2. #include <climits>
  3. #include <unordered_map>
  4. using namespace std;
  5. /**
  6. * Definition of TreeNode:
  7. * class TreeNode {
  8. * public:
  9. *     int val;
  10. *     TreeNode *left, *right;
  11. *     TreeNode(int val) {
  12. *         this->val = val;
  13. *         this->left = this->right = NULL;
  14. *     }
  15. * }
  16. */
  17. class Solution {
  18. public:
  19.     /**
  20.      * @param root: The root of binary tree.
  21.      * @return: An integer
  22.      */
  23.     int maxPathSum(TreeNode *root) {
  24.         ans = INT_MIN;
  25.         um.clear();
  26.         if (root == NULL) {
  27.             return ans;
  28.         }
  29.         um[NULL] = 0;
  30.         singlePath(root);
  31.         doublePath(root);
  32.         return ans;
  33.     }
  34. private:
  35.     int ans;
  36.     unordered_map<TreeNode *, int> um;
  37.    
  38.     void singlePath(TreeNode *root) {
  39.         if (root == NULL) {
  40.             return;
  41.         }
  42.         singlePath(root->left);
  43.         singlePath(root->right);
  44.         um[root] = max(um[root->left], um[root->right]) + root->val;
  45.         um[root] = max(um[root], root->val);
  46.     }
  47.    
  48.     void doublePath(TreeNode *root) {
  49.         if (root == NULL) {
  50.             return;
  51.         }
  52.         ans = max(ans, um[root->left] + um[root->right] + root->val);
  53.         ans = max(ans, root->val);
  54.         doublePath(root->left);
  55.         doublePath(root->right);
  56.     }
  57. };
复制代码
复杂度:时间O(N),空间O(N)。
回复

使用道具 举报

🔗
 楼主| zhuli19901106 2015-7-21 21:00:39 | 只看该作者
全局:
Validate Binary Search Tree
题意:给定一棵二叉树,判断是不是BST。
解法:既可以中序遍历然后判断序列是否有序,也可以像下面这样直接判断求出中序的前驱和后继。道理都是一样的。
代码:
  1. /**
  2. * Definition of TreeNode:
  3. * class TreeNode {
  4. * public:
  5. *     int val;
  6. *     TreeNode *left, *right;
  7. *     TreeNode(int val) {
  8. *         this->val = val;
  9. *         this->left = this->right = NULL;
  10. *     }
  11. * }
  12. */
  13. class Solution {
  14. public:
  15.     /**
  16.      * @param root: The root of binary tree.
  17.      * @return: True if the binary tree is BST, or false
  18.      */
  19.     bool isValidBST(TreeNode *root) {
  20.         if (root == NULL) {
  21.             return true;
  22.         }
  23.         int lmin, rmax;
  24.         return validate(root, lmin, rmax);
  25.     }
  26. private:
  27.     bool validate(TreeNode *root, int &lmin, int &rmax) {
  28.         int ll, lr, rl, rr;
  29.         if (root->left != NULL) {
  30.             if (!validate(root->left, ll, lr) || lr >= root->val) {
  31.                 return false;
  32.             }
  33.         } else {
  34.             ll = lr = root->val;
  35.         }
  36.         lmin = ll;
  37.         if (root->right != NULL) {
  38.             if (!validate(root->right, rl, rr) || rl <= root->val) {
  39.                 return false;
  40.             }
  41.         } else {
  42.             rl = rr = root->val;
  43.         }
  44.         rmax = rr;
  45.         
  46.         return true;
  47.     }
  48. };
复制代码
复杂度:时间O(N),空间O(N)。
回复

使用道具 举报

🔗
 楼主| zhuli19901106 2015-7-21 22:14:47 | 只看该作者
全局:
Partition List
题意:给定一个单链表和一个值target。把所有小于target的节点放前面,大于等于的放后面。要求两部分内部的相对顺序不能变。
解法:其实相对顺序不变不说也一样,因为最直观的解法就是逐个节点访问,按照大小分成两条链,然后再首尾合并即可。
代码:
  1. /**
  2. * Definition of ListNode
  3. * class ListNode {
  4. * public:
  5. *     int val;
  6. *     ListNode *next;
  7. *     ListNode(int val) {
  8. *         this->val = val;
  9. *         this->next = NULL;
  10. *     }
  11. * }
  12. */
  13. class Solution {
  14. public:
  15.     /**
  16.      * @param head: The first node of linked list.
  17.      * @param x: an integer
  18.      * @return: a ListNode
  19.      */
  20.     ListNode *partition(ListNode *head, int x) {
  21.         if (head == NULL) {
  22.             return head;
  23.         }
  24.         ListNode *h1, *t1, *h2, *t2;
  25.         ListNode *p;
  26.         
  27.         h1 = t1 = NULL;
  28.         h2 = t2 = NULL;
  29.         while (head != NULL) {
  30.             p = head;
  31.             head = head->next;
  32.             if (p->val < x) {
  33.                 if (h1 == NULL) {
  34.                     h1 = t1 = p;
  35.                     t1->next = NULL;
  36.                 } else {
  37.                     t1->next = p;
  38.                     t1 = t1->next;
  39.                     t1->next = NULL;
  40.                 }
  41.             } else {
  42.                 if (h2 == NULL) {
  43.                     h2 = t2 = p;
  44.                     t2->next = NULL;
  45.                 } else {
  46.                     t2->next = p;
  47.                     t2 = t2->next;
  48.                     t2->next = NULL;
  49.                 }
  50.             }
  51.         }
  52.         if (t1 != NULL) {
  53.             t1->next = h2;
  54.             return h1;
  55.         } else {
  56.             return h2;
  57.         }
  58.     }
  59. };
复制代码
复杂度:时间O(N),空间O(1)。
回复

使用道具 举报

🔗
 楼主| zhuli19901106 2015-7-21 22:17:45 | 只看该作者
全局:
Maximum Depth of Binary Tree
题意:求二叉树的深度。
解法:递归解决。
代码:
  1. #include <algorithm>
  2. using namespace std;
  3. /**
  4. * Definition of TreeNode:
  5. * class TreeNode {
  6. * public:
  7. *     int val;
  8. *     TreeNode *left, *right;
  9. *     TreeNode(int val) {
  10. *         this->val = val;
  11. *         this->left = this->right = NULL;
  12. *     }
  13. * }
  14. */
  15. class Solution {
  16. public:
  17.     /**
  18.      * @param root: The root of binary tree.
  19.      * @return: An integer
  20.      */
  21.     int maxDepth(TreeNode *root) {
  22.         if (root == NULL) {
  23.             return 0;
  24.         }
  25.         return max(maxDepth(root->left), maxDepth(root->right)) + 1;
  26.     }
  27. };
复制代码
复杂度:时间O(N),空间O(N)。

回复

使用道具 举报

🔗
 楼主| zhuli19901106 2015-7-21 22:27:39 | 只看该作者
全局:
Sort List
题意:利用归并排序的思想,把一个单链表排序。要求常数空间复杂度哦。
解法:这题有意思,尤其是空间复杂度O(1)的要求。让我想起数组归并排序的非递归实现。这题思想也类似,不过实现很是复杂,我调都调了半天。现场面试碰见这种题,还要求O(1)空间的话,那就悬了。
代码:
  1. // Let's make it strictly O(1) in space
  2. /**
  3. * Definition of ListNode
  4. * class ListNode {
  5. * public:
  6. *     int val;
  7. *     ListNode *next;
  8. *     ListNode(int val) {
  9. *         this->val = val;
  10. *         this->next = NULL;
  11. *     }
  12. * }
  13. */
  14. class Solution {
  15. public:
  16.     /**
  17.      * @param head: The first node of linked list.
  18.      * @return: You should return the head of the sorted linked list,
  19.                     using constant space complexity.
  20.      */
  21.     ListNode *sortList(ListNode *head) {
  22.         if (head == NULL) {
  23.             return head;
  24.         }
  25.         
  26.         int tlen = 0;
  27.         ListNode *p1, *p2;
  28.         
  29.         p1 = head;
  30.         while (p1 != NULL) {
  31.             p1 = p1->next;
  32.             ++tlen;
  33.         }
  34.         
  35.         ListNode *h1, *t1;
  36.         ListNode *h2, *t2;
  37.         ListNode *h3, *t3;
  38.         ListNode *h, *t;
  39.         int len = 1;
  40.         int i;
  41.         while (len < tlen) {
  42.             // Set all pointers to NULL
  43.             h = t = NULL;
  44.             while (true) {
  45.                 h1 = h2 = t1 = t2 = NULL;
  46.                 i = 0;
  47.                 // Get two sublists
  48.                 while (head != NULL && i < len) {
  49.                     if (h1 == NULL) {
  50.                         h1 = t1 = head;
  51.                         head = head->next;
  52.                     } else {
  53.                         t1->next = head;
  54.                         head = head->next;
  55.                         t1 = t1->next;
  56.                     }
  57.                     t1->next = NULL;
  58.                     ++i;
  59.                 }
  60.                 i = 0;
  61.                 while (head != NULL && i < len) {
  62.                     if (h2 == NULL) {
  63.                         h2 = t2 = head;
  64.                         head = head->next;
  65.                     } else {
  66.                         t2->next = head;
  67.                         head = head->next;
  68.                         t2 = t2->next;
  69.                     }
  70.                     t2->next = NULL;
  71.                     ++i;
  72.                 }
  73.                 if (h1 == NULL && h2 == NULL) {
  74.                     break;
  75.                 }
  76.                
  77.                 // Merge them into one sorted list
  78.                 p1 = h1;
  79.                 p2 = h2;
  80.                 h3 = t3 = NULL;
  81.                 while (p1 != NULL && p2 != NULL) {
  82.                     if (p1->val < p2->val) {
  83.                         if (h3 == NULL) {
  84.                             h3 = t3 = p1;
  85.                             p1 = p1->next;
  86.                         } else {
  87.                             t3->next = p1;
  88.                             p1 = p1->next;
  89.                             t3 = t3->next;
  90.                         }
  91.                     } else {
  92.                         if (h3 == NULL) {
  93.                             h3 = t3 = p2;
  94.                             p2 = p2->next;
  95.                         } else {
  96.                             t3->next = p2;
  97.                             p2 = p2->next;
  98.                             t3 = t3->next;
  99.                         }
  100.                     }
  101.                     t3->next = NULL;
  102.                 }
  103.                 while (p1 != NULL) {
  104.                     if (h3 == NULL) {
  105.                         h3 = t3 = p1;
  106.                         p1 = p1->next;
  107.                     } else {
  108.                         t3->next = p1;
  109.                         p1 = p1->next;
  110.                         t3 = t3->next;
  111.                     }
  112.                     t3->next = NULL;
  113.                 }
  114.                 while (p2 != NULL) {
  115.                     if (h3 == NULL) {
  116.                         h3 = t3 = p2;
  117.                         p2 = p2->next;
  118.                     } else {
  119.                         t3->next = p2;
  120.                         p2 = p2->next;
  121.                         t3 = t3->next;
  122.                     }
  123.                     t3->next = NULL;
  124.                 }
  125.                
  126.                 // Add the list to tail
  127.                 if (h == NULL) {
  128.                     h = h3;
  129.                     t = t3;
  130.                 } else {
  131.                     t->next = h3;
  132.                     t = t3;
  133.                 }
  134.             }
  135.             head = h;
  136.             len <<= 1;
  137.         }
  138.         return head;
  139.     }
  140. };
复制代码
复杂度:时间O(N * log(N)),空间O(1)。空间O(N)的解法更容易实现,此处就不写了。
回复

使用道具 举报

🔗
 楼主| zhuli19901106 2015-7-21 22:35:06 | 只看该作者
全局:
Reorder List
题意:给定一个单链表,把它变成1->n->2->n-1->...的形式。
解法:把链表分成前后两段A和B,然后把B段反转,然后交替归并,即ABABABAB...。得到的就是期望结果。
代码:
  1. /**
  2. * Definition of ListNode
  3. * class ListNode {
  4. * public:
  5. *     int val;
  6. *     ListNode *next;
  7. *     ListNode(int val) {
  8. *         this->val = val;
  9. *         this->next = NULL;
  10. *     }
  11. * }
  12. */
  13. class Solution {
  14. public:
  15.     /**
  16.      * @param head: The first node of linked list.
  17.      * @return: void
  18.      */
  19.     void reorderList(ListNode *head) {
  20.         if (head == NULL || head->next == NULL) {
  21.             return;
  22.         }
  23.         
  24.         ListNode *h1, *h2;
  25.         ListNode *p1, *p2;
  26.         
  27.         p1 = p2 = head;
  28.         h1 = head;
  29.         while (true) {
  30.             p2 = p2->next;
  31.             if (p2 == NULL) {
  32.                 break;
  33.             }
  34.             p2 = p2->next;
  35.             if (p2 == NULL) {
  36.                 break;
  37.             }
  38.             p1 = p1->next;
  39.         }
  40.         h2 = p1->next;
  41.         p1->next = NULL;
  42.         h2 = reverseList(h2);
  43.         
  44.         ListNode *h, *t;
  45.         h = t = h1;
  46.         h1 = h1->next;
  47.         t->next = NULL;
  48.         while (h1 != NULL && h2 != NULL) {
  49.             t->next = h2;
  50.             h2 = h2->next;
  51.             t = t->next;
  52.             t->next = NULL;
  53.             
  54.             t->next = h1;
  55.             h1 = h1->next;
  56.             t = t->next;
  57.             t->next = NULL;
  58.         }
  59.         while (h1 != NULL) {
  60.             t->next = h1;
  61.             h1 = h1->next;
  62.             t = t->next;
  63.             t->next = NULL;
  64.         }
  65.         while (h2 != NULL) {
  66.             t->next = h2;
  67.             h2 = h2->next;
  68.             t = t->next;
  69.             t->next = NULL;
  70.         }
  71.     }
  72. private:
  73.     ListNode *reverseList(ListNode *head) {
  74.         if (head == NULL) {
  75.             return head;
  76.         }
  77.         ListNode *h, *p;
  78.         
  79.         h = head;
  80.         head = head->next;
  81.         h->next = NULL;
  82.         while (head != NULL) {
  83.             p = head->next;
  84.             head->next = h;
  85.             h = head;
  86.             head = p;
  87.         }
  88.         return h;
  89.     }
  90. };
复制代码
复杂度:时间O(N),空间O(1)。
回复

使用道具 举报

🔗
 楼主| zhuli19901106 2015-7-22 00:21:02 | 只看该作者
全局:
Remove Duplicates from Sorted Array
题意:给定一个单链表,去掉重复的值。
解法:略。
代码:
  1. /**
  2. * Definition of ListNode
  3. * class ListNode {
  4. * public:
  5. *     int val;
  6. *     ListNode *next;
  7. *     ListNode(int val) {
  8. *         this->val = val;
  9. *         this->next = NULL;
  10. *     }
  11. * }
  12. */
  13. class Solution {
  14. public:
  15.     /**
  16.      * @param head: The first node of linked list.
  17.      * @return: head node
  18.      */
  19.     ListNode *deleteDuplicates(ListNode *head) {
  20.         if (head == NULL) {
  21.             return head;
  22.         }
  23.         ListNode *p = head;
  24.         ListNode *q;
  25.         while (p->next != NULL) {
  26.             if (p->val == p->next->val) {
  27.                 q = p->next;
  28.                 p->next = q->next;
  29.                 delete q;
  30.                 continue;
  31.             }
  32.             p = p->next;
  33.         }
  34.         return head;
  35.     }
  36. };
复制代码
复杂度:时间O(N),空间O(1)。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表