📣 独立日限时特惠: VIP通行证立减$68
查看: 2166| 回复: 5
跳转到指定楼层
上一主题 下一主题
收起左侧

求lc Paint House II的O(nk)解法

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
求地里大神能给一下解答,目前小弟只想到用DP的解法但是复杂度是O(nk^2), 目前能想到的改进成nk的方法是用bucket sort,但是太渣,不会写桶排序,求大神指导

上一篇:【LintCode, heap】为什么Heapify这题的时间复杂度是O(n)?
下一篇:【LRU cache】value相同的时候怎么确定谁是least recently used?
🔗
jiebour 2015-8-21 07:38:48 | 只看该作者
全局:
一天了,没人回复。。。。
回复

使用道具 举报

🔗
flamen 2015-8-21 08:21:30 | 只看该作者
全局:
DP为啥是O(nk^2)? 难道不是O(nk)?
回复

使用道具 举报

全局:
public int minCostII(int[][] costs){
                if(costs == null || costs.length == 0 || costs[0].length <= 1){
                        return 0;
                }
                int k = costs.length;
                int n = costs[0].length;
                int firstIndex = -1;
                int first = 0;
                int second = 0;
                for(int i=1;i<n;i++){
                        int newFirst = Integer.MAX_VALUE;
                        int newSecond = Integer.MAX_VALUE;
                        int newFirstIndex = -1;
                        for(int j=0;j<k;j++){
                                int cur = costs[j][i];
                                costs[j][i] = cur + (firstIndex == j? second:first);
                                if(costs[j][i] < newFirst){
                                        newSecond = newFirst;
                                        newFirst = costs[j][i];
                                        newFirstIndex = j;
                                }
                                else if(costs[j][i] < newSecond){
                                        newSecond = costs[j][i];
                                }
                        }
                        first = newFirst;
                        firstIndex = newFirstIndex;
                        second = newSecond;
                }
                return first;
        }
写了一个,因为没有买premium,所以不知道能不能AC,只是提供一下自己的思路,也不知道对不对,求讨论。
用DP,如果不能改变costs[][]的值,就新create一个matrix,cost[j][i]表示第i个房子刷第k种颜色,前i个房子最少的cost,记录firstIndex是为了满足相邻房子不能刷同种颜色的条件。
可能会有bug,
回复

使用道具 举报

🔗
jiebour 2015-8-21 08:39:35 | 只看该作者
全局:
宝贝忆彼岸 发表于 2015-8-21 08:23
public int minCostII(int[][] costs){
                if(costs == null || costs.length == 0 || costs[0].length

复杂度是多少?nk?
回复

使用道具 举报

全局:
jiebour 发表于 2015-8-21 08:39
复杂度是多少?nk?

是的 字。。。。。。。数
回复

使用道具 举报

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

本版积分规则

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