LeetCode1691. Maximum Height by Stacking Cuboids的两种解法

首先必须注意到一点,两个立方体cub1和cub2, cub2可以放在cub1上的充要条件是min1<=min2且mid1<=mid2且max1<=max2。所以要首先对所有立方体中的3条边做个排序。
另外一点很重要的是,此题中的cub之间可理解为一种偏序关系
在此基础上我发现了两种高效解法,其时间复杂度都为O(n^2)

解法1:

后半部分用到了dp,思路类似于求最长递增序列。

class Solution {
    public int maxHeight(int[][] cuboids) {
    	for(int[] cub : cuboids) {
    		Arrays.sort(cub);
    	}
        Arrays.sort(cuboids, (cub1, cub2) -> cub1[2]-cub2[2] != 0 ? cub1[2]-cub2[2] : cub1[1]-cub2[1] != 0 ? cub1[1]-cub2[1] : cub1[0]-cub2[0]  );
        //因为偏序的关系,这样排序后,对任意排在cub2前面的cub1, cub1也许能被cub2容纳,也许不能。但所有能被cub2容纳的cub,是一定排在cub2前面的
        
        int[] maxsumFrom = new int[cuboids.length]; 
        maxsumFrom[cuboids.length-1] = cuboids[cuboids.length-1][2];
        for(int i=cuboids.length-2;i>=0;--i) {
        	maxsumFrom[i] = cuboids[i][2];
        	for(int j=i+1;j<cuboids.length;++j) {
        		if( cuboids[i][0] <= cuboids[j][0] && cuboids[i][1] <= cuboids[j][1]) {
        			maxsumFrom[i] = Math.max(maxsumFrom[i], cuboids[i][2]+maxsumFrom[j]);
        		}
        	}
        }
        int maxsum = 0;
        for(int sum : maxsumFrom) {
        	maxsum = Math.max(maxsum, sum);
        }
        return maxsum;
    }
}

解法2:

因为是偏序,所以可利用有向无环图,递归+记忆化求解:

class Solution {
	int N;
	int[] mem;
    public int maxHeight(int[][] cuboids) {  
    	for(int[] cub : cuboids) {
    		Arrays.sort(cub);
    	}
    	N = cuboids.length;
    	List<Integer>[] graph = new List[N];//注意点从0到N-1
    	Set<Integer> in0 = new HashSet<>();  //入度为0的点集合
    	for(int i=0;i<N;++i) {
        	graph[i] = new ArrayList<Integer>();
        	in0.add(i);
        }
        for(int i=0;i<N-1;++i) {  //有向无环中的偏序
        	for(int j=i+1;j<N;++j) {
        		if( cuboids[i][0] <= cuboids[j][0] && cuboids[i][1] <= cuboids[j][1] && cuboids[i][2] <= cuboids[j][2]) {
        			graph[j].add(i); 
        			in0.remove(i);
        		}else if(cuboids[i][0] >= cuboids[j][0] && cuboids[i][1] >= cuboids[j][1] && cuboids[i][2] >= cuboids[j][2]) {
        			graph[i].add(j); 
        			in0.remove(j);
        		}
            }
        }
    	
        int maxsum = 0;
        mem = new int[N];
        Arrays.fill(mem, -1);
        for(int top : in0) {
        	maxsum = Math.max(maxsum, downMax(top,graph,cuboids));
        }
        
        return maxsum;
    }
	private int downMax(int top, List<Integer>[] graph,int[][] cuboids) {
		if(mem[top]!=-1)
			return mem[top];
		int ans = cuboids[top][2];
		for(int adjacent : graph[top]) {
			ans = Math.max(ans, cuboids[top][2]+downMax(adjacent, graph,cuboids));
		}
		mem[top] = ans;
		return ans;
	}
}

此外因为参考1798. Maximum Number of Consecutive Values You Can Make的把硬币排序后借用01背包思路的解法(一般的背包问题中,硬币的顺序是无所谓的,但该题不同),我想到了类似的思路。但是后面发现效率不行。因为每次迭代,都得回去考虑所有立方体(硬币)的情况,而不是只考虑上一个立方体(硬币)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_23204557

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值