lintcode 515. Paint House】的更多相关文章

Paint House 自己的写法: class Solution { public: /** * @param costs: n x 3 cost matrix * @return: An integer, the minimum cost to paint all houses */ int minCost(vector<vector<int> > &costs) { // write your code here int length = costs.size();…
House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能获得的最大值 Paint House:用3种颜色,相邻的房屋不能用同一种颜色,求花费最小 Paint House II:用k种颜色,相邻的房屋不能用同一种颜色,求花费最小Paint Fence:用k种颜色,相邻的可以用同一种颜色,但不能超过连续的2个,求有多少种可能性 198. House Robb…
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…
There is a fence with n posts, each post can be painted with one of the k colors.You have to paint all the posts such that no more than two adjacent fence posts have the same color.Return the total number of ways you can paint the fence. Notice n and…
There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the sam…
There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color. The cost of pai…
Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or white). Leo has a magical brush which can paint any row with black color, or any column with white color. Each time he uses the brush, the previous co…
一.setXfermode(Xfermode xfermode) Xfermode国外有大神称之为过渡模式,这种翻译比较贴切但恐怕不易理解,大家也可以直接称之为图像混合模式,因为所谓的“过渡”其实就是图像混合的一种,这个方法跟我们上面讲到的setColorFilter蛮相似的.查看API文档发现其果然有三个子类:AvoidXfermode, PixelXorXfermode和PorterDuffXfermode,这三个子类实现的功能要比setColorFilter的三个子类复杂得多. 二.Avo…
自定义view里面的onDraw方法,在这里我们可以绘制各种图形,onDraw里面有两个API我们需要了解清楚他们的用法:Canvas 和 Paint. Canvas翻译成中文就是画布的意思,Canvas负责进行绘制各种各样的图形,它有如下的一些绘制图形方法: drawArc 绘制弧 drawBitmap 绘制位图 drawCircle 绘制圆形 drawLine 绘制线 drawOval 绘制椭圆 drawPath 绘制路径 drawPoint 绘制一个点 drawPoints 绘制多个点 d…
There is a fence with n posts, each post can be painted with one of the k colors. You have to paint all the posts such that no more than two adjacent fence posts have the same color. Return the total number of ways you can paint the fence. Note:n and…
There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color. The cost of pai…
There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the sam…
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是简单的剖析思路以及不能bug-free的具体细节原因. ---------------------------------------------------------------- ------------------------------------------- 第九周:图和搜索. ---…
-------------------------------------------- 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.…
-------------------- 递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o) AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left…
----------------------------------- AC代码: /** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * } */ public class Solution { /** * @param node: the node…
----------------------------------------------- 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; * } * } */…
-------------------------- 水题 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; * } * } */ public class Solut…
------------------------ 只要设置两个指针,称为快慢指针,当链表没有环的时候快指针会走到null,当链表有环的时候快指针早晚会追上慢指针的. AC代码: /** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * } */ publi…
--------------------------------------- 按照给定的峰值定义,峰值的左半部分一定是递增的,所以只要找到不递增的即可. AC代码: class Solution { /** * @param A: An integers array. * @return: return any of peek positions. */ public int findPeak(int[] A) { for(int i=1;i<A.length;i++){ if(A[i]>=…
------------------------------------------------------------ 卧槽竟然连题意都没看懂,百度了才明白题目在说啥....我好方啊....o(╯□╰)o AC代码: class Solution { /** * @param prices: Given an integer array * @return: Maximum profit */ public int maxProfit(int[] prices) { int res=0; fo…
-------------------------------- 这道题好坑啊,自己说是2*n+1个数字,结果有组测试数据竟然传了个空数组进来... 经典位算法: n^n==0 n^0==n AC代码: public class Solution { /** *@param A : an integer array *return : a integer */ public int singleNumber(int[] A) { if(A.length==0) return 0; int n=A…
--------------------------------- 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; * } * } */ public class S…
------------------------ AC代码: class Solution { /** * param n: As description. * return: A list of strings. */ public ArrayList<String> fizzBuzz(int n) { ArrayList<String> results = new ArrayList<String>(); for (int i = 1; i <= n; i++…
木头加工 题目描述 有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目至少为 k.当然,我们希望得到的小段越长越好,你需要计算能够得到的小段木头的最大长度. 注意事项 木头长度的单位是厘米.原木的长度都是正整数,我们要求切割得到的小段木头的长度也要求是整数.无法切出要求至少 k 段的,则返回 0 即可. 样例 有3根木头[232, 124, 456], k=7, 最大长度为114,则返回114. 简单分析 暴力解法就是从1开始遍历所有的长度,对于长度L,计算所有木头可…
题目1 落单的数 给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字. 链接:http://www.lintcode.com/zh-cn/problem/single-number/ 样例 给出 [1,2,2,1,3,4,3],返回 4 挑战 一次遍历,常数级的额外空间复杂度 解决方案 方法1思路:将所有的数转换成二进制,因为是int类型,共32位.申请常数级(32位)的额外空间,然后每个数对应的位相加,最后对应位上的和模2.最后的结果就是单个数对应的二进制数.…
题目链接: http://www.lintcode.com/zh-cn/problem/binary-tree-zigzag-level-order-traversal/ 二叉树的锯齿形层次遍历 给出一棵二叉树,返回其节点值的锯齿形层次遍历(先从左往右,下一层再从右往左,层与层之间交替进行) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回其锯齿形的层次遍历为: [ [3], [20,9], [15,7] ] 思路: 我们用双端队列模拟一下…