LintCode "Post Office Problem" !!!】的更多相关文章

* Non-intuitive state design class Solution { public: /** * @param A an integer array * @param k an integer * @return an integer */ int postOffice(vector<int>& A, int k) { int n = A.size(); sort(A.begin(), A.end()); // Cost btw. 2 houses i-j wit…
原题链接在这里:http://www.lintcode.com/en/problem/a-b-problem/ 不让用 数学运算符,就用位运算符. a的对应位 ^ b的对应位 ^ carry 就是res中的对应位. carry 更新为0还是1要分别讨论. Time Complexity: O(1), 一共32位. Space: O(1). AC Java: class Solution { /* * param a: The first integer * param b: The second…
Description There are n houses on a line. Given an array A and A[i] represents the position of i-th house. Now you need to pick k position to build k post offices. What is the minimum sum distance from these n houses to the nearest post office? All p…
Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping between nuts and bolts. Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and b…
Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping between nuts and bolts. Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and b…
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (2016-02-10) For more problems and solutions, you can see my LintCode repository. I'll keep updating for full summary and better solutions. See cnblogs t…
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ pub…
----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩下一个元素的时候(事实上只要满足一个元素出现过半就一定会剩下一个元素的)这个元素就是我们要找的数了. AC代码: public class Solution { /** * @param nums: a list of integers * @return: find a majority numb…
----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * } */ pu…
------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还是只做出了O(n^2)... 勉强AC代码: public class Solution { /** * @param str: a string * @return: a boolean */ public boolean isUnique(String s) { for(int i=0;i<s.…