Leetcode进阶之路——Balloons

37 篇文章 0 订阅

最近做了两道扎气球的题,都很有意思,特别是第二道,用到了动态规划,第一次看见还是有些难度的,现将其记录如下。
452.Minimum Number of Arrows to Burst Balloons

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it’s horizontal, y-coordinates don’t matter and hence the x-coordinates of start and end of the diameter suffice. Start is always smaller than end. There will be at most 104 balloons.
An arrow can be shot up exactly vertically from different points along the x-axis. A balloon with xstart and xend bursts by an arrow shot at x if xstart ≤ x ≤ xend. There is no limit to the number of arrows that can be shot. An arrow once shot keeps travelling up infinitely. The problem is to find the minimum number of arrows that must be shot to burst all balloons.
Example:
Input:
[[10,16], [2,8], [1,6], [7,12]]
Output:
2

题意大致如下:输入是一系列区间,表示在该区间内存在一只气球,用最少的箭将所有的气球都扎破
对于示例输入,可绘制如下图片,红、橙、绿、蓝表示四只气球,则可以在两个紫色的区间内放两支箭,即可将所有气球都扎破
balloon
于是就想到先对所有气球的起始坐标进行排序,由于个人习惯以及水平原因,这里就先构造了一个气球的结构体,包含:起始坐标、终止坐标以及是否被扎破,如下:

struct balloons
{
	int start, end;
	bool shot = false;
};

之后将输入向量转化为balloons结构,重新存储并排序(cmp为自定义的排序规则,按起始坐标从小到大排序)
遍历排序后的向量,记录当前气球的终止坐标,继续往后遍历,一边找第一个起始坐标大于终止坐标的气球,一边更新更小的终止坐标
例如第一个气球是[1, 8],下一个是[2, 6],那么判断第二个气球能跟第一个一起被扎破,同时将原来的终止坐标8更新为6
遍历结束后,返回计数即可

static bool ArrowShotscmp(balloons a, balloons b)
{
	return (a.start < b.start ? true : false);
}
int findMinArrowShots(vector<pair<int, int>>& points)
 {
	vector<balloons> vb;
	balloons bl;
	// 将输入balloons放入自建的向量
	for (int i = 0; i < points.size(); ++i)
	{
		bl.start = points[i].first;
		bl.end = points[i].second;
		vb.emplace_back(bl);
	}
	// 根据自定义的cmp规则,按起始坐标从小到大排序
	sort(vb.begin(), vb.end(), ArrowShotscmp);
	int res = 0;
	for (int i = 0; i < vb.size();)
	{
		// 更新计数
		res++;
		// 记录当前的终止坐标
		int tend = vb[i].end;
		vb[i].shot = true;
		for (int j = i + 1; j < vb.size(); ++j)
		{
			// 如果当前起始坐标在终止之内,则可以同时被扎破
			if (vb[j].start <= tend)
			{
				vb[j].shot = true;
				// 判断是否更新更小的终止坐标
				if (vb[j].end < tend)
					tend = vb[j].end;
			}
			// 若超出,则直接退出循环,j为下一轮遍历的起点
			else
			{
				i = j;
				break;
			}
		}
	}
	// res为计数值
	return res;
}

第二题 312.Burst Balloons

Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.
Find the maximum coins you can collect by bursting the balloons wisely.
Note:
You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100
Example:
Input: [3,1,5,8]
Output: 167

两道虽然都是balloons"系列",但解题却是完全不同
这道题的题意也很清晰,输入数组表示每个气球的价值,若选择扎一个气球i,则会获得nums[i - 1] * nums[i] * nums[i + 1]的价值,选择扎球策略,使得最后获得的总价值之和最大
最先想到的肯定是暴力搜索所有可能的情况,选择最大值,然后毫无疑问的TLE了
当然刷多了leetcode之后,看到这个题面还是会往动态规划上想的,只是更新策略上需要思考一下
首先在输入的开头和结尾添加价值1,那么此时数组边界为[0, len+1],原始数据的范围为[1, len]
用二维数组dp表示总价值,dp[i][j]表示从气球i到j能够获得的最大价值
假设气球m是最后一个被扎破的气球,那么扎破m后所能获得的最大价值为:
扎破1到m-1个气球获得的最大价值 + 扎破m+1到len个气球获得的最大价值 + 气球m与其"相邻"气球的乘积
于是问题转换到,扎破1~m-1 和 m + 1~len个气球各能获得多少价值
同样继续拆分:
若气球n为1~m-1之间,扎破的最后一个气球,则所获得的价值之和为:
扎破1到n-1个气球获得的最大价值 + 扎破n+1到m - 1个气球获得的最大价值 + 气球n与其"相邻"气球的乘积
由此可以继续拆分下去,直到区间长度为1
于是就能够得到下面的代码:

// 312.	Burst Balloons
int maxCoins(vector<int>& nums) {
	// 首尾各增加价值为1的气球,表示临界值
	nums.emplace_back(1);
	nums.insert(nums.begin(), 1);
	int len = nums.size();
	int** dp = new int*[len];
	for (int i = 0; i < len; ++i)
	{
		dp[i] = new int[len];
		for (int j = 0; j < len; ++j)
			dp[i][j] = 0;
	}
	len -= 2;
	for (int i = len; i >= 1; i--) {
		for (int j = i; j <= len; j++)
		{
			for (int k = i; k <= j; ++k)
			{
				dp[i][j] = max(dp[i][j], dp[i][k - 1] + dp[k + 1][j] + nums[i - 1] * nums[j + 1] * nums[k]);
			}
		}
	}
	return dp[1][len];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值