【LeetCode】贪心

[452] Minimum Number of Arrows to Burst Balloons[Medium]

给一堆线段,使用最少的arrow,穿过所有的线段。陈题,第一条线段的终点。

Input:
[[10,16], [2,8], [1,6], [7,12]]

Output:
2

Explanation:
One way is to shoot one arrow for example at x = 6 (bursting the balloons [2,8] and [1,6]) and another arrow at x = 11 (bursting the other two balloons).

 1 // 陈题。 第一条线段的终点。
 2 //wyzhang
 3 class Solution {
 4 public:
 5     static bool cmp(pair<int, int>& a, pair<int, int>& b) {
 6         return a.second < b.second;
 7     }
 8
 9     int findMinArrowShots(vector<pair<int, int>>& points) {
10         sort(points.begin(), points.end(), cmp);
11         vector<bool> valid_segments(points.size(), true);
12
13         int ans = 0;
14         for (size_t i = 0; i < points.size(); ++i) {
15             if(!valid_segments[i]) { // balloon has been shot
16                 continue;
17             }
18             const int end = points[i].second;
19             for (size_t j = i + 1; j < points.size(); ++j) {
20                 if (!valid_segments[j]) {
21                     continue;
22                 }
23                 if (end >= points[j].first) {
24                     valid_segments[j] = false;
25                 }
26             }
27             ans++;
28         }
29         return ans;
30     }
31 };

时间: 2024-10-20 08:53:46

【LeetCode】贪心的相关文章

Leetcode 贪心 Best Time to Buy and Sell StockII

l and dished out an assist in the Blackhawks' 5-3 win over the Nashville Predators.Shaw said just playing with the Blackhawks was enough motivation for him."Positive, I'm playing in the NHL," Shaw said after Sunday's win. "What can't you be

Leetcode 贪心 Best Time to Buy and Sell Stock

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Best Time to Buy and Sell Stock Total Accepted: 13234 Total Submissions: 43145 Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to

Leetcode 贪心 Longest Consecutive Sequence

Longest Consecutive Sequence Total Accepted: 19169 Total Submissions: 68303My Submissions Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consec

Leetcode 贪心 Jump Game II

Jump Game II Total Accepted: 16242 Total Submissions: 65802My Submissions Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that pos

Leetcode 贪心 container with most water

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Container With Most Water Total Accepted: 15241 Total Submissions: 48936My Submissions Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertica

Leetcode 贪心 Jump Game

Jump Game Total Accepted: 18745 Total Submissions: 68916My Submissions Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that positi

[LeetCode] Jump Game II 贪心

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps

[C++]LeetCode: 77 Best Time to Buy and Sell Stock II (贪心算法)

题目: Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times)

[LeetCode]621. 任务调度器(贪心)

题目 给定一个用字符数组表示的 CPU 需要执行的任务列表.其中包含使用大写的 A - Z 字母表示的26 种不同种类的任务.任务可以以任意顺序执行,并且每个任务都可以在 1 个单位时间内执行完.CPU 在任何一个单位时间内都可以执行一个任务,或者在待命状态. 然而,两个相同种类的任务之间必须有长度为?n 的冷却时间,因此至少有连续 n 个单位时间内 CPU 在执行不同的任务,或者在待命状态. 你需要计算完成所有任务所需要的最短时间. 示例 1: 输入: tasks = ["A",&q

[LeetCode]Wildcard Matching 通配符匹配(贪心)

一開始採用递归写.TLE. class Solution { public: bool flag; int n,m; void dfs(int id0,const char *s,int id1,const char *p){ if(flag)return; if(id0>=n){ if(id1>=m)flag=1; else{ int j=0; while(j<m&&p[j]=='*')j++; if(j>=m)flag=1; } return; } else i