Acy夕拾算法 Week2_day4


贪心无套路;局部最优 推 全局最优

LeetCode 122. 买卖股票的最佳时机 II

/*
局部最优-》全局最优
·判断升序降序,遇到降序的结尾,买入下一个,在升序的最后一个卖出;如果下一个不是最后一个数,且后续有升序,买入下一个。循环
!但从哪升从哪降不好判断,并且题目没有问从哪开始买卖,只要求返回最大利润

整理思路:
·不要被实例的解释迷惑
·所以可以直接贪心,求所有的今天买入、明天卖出,只要获取的利润是正的,就加到最大利润中
·连续的正利润相加,与从最低点买、随后的最高点卖,获得的最大利润相等
*/

贪心

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int resMax = 0;
        for(int i = 1; i < prices.size(); i++)
        {
            if(prices[i]-prices[i-1] > 0)
                resMax += prices[i]-prices[i-1];
        }
        return resMax;
    }
};

动态规划----待

在这里插入代码片

LeetCode 455. 分发饼干

/*
注意:
sort(g.begin(),g.end());
注意边界 for(int i = g.size()-1, j = s.size()-1; i >= 0 && j >= 0; i–)

我的思路:小饼干
·把孩子胃口和饼干从小到大排序,依次投喂>=
·排序后,先填饱胃口小的孩子,饼干>=胃口,
·饼干不符合就一直++,直到满足下一个小孩
·胃口[3,4] 饼干[1,2,3]

代码随想录:大饼干满足大胃口的孩子
·从大饼干、大胃口开始遍历
·按照饼干,找孩子
*/

按小饼干先满足小胃口

class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        sort(g.begin(),g.end());
        sort(s.begin(),s.end());
        int res = 0;
        for(int i = 0, j = 0; i < g.size() && j < s.size(); j++)
        {//j++一直找饼干
            if(s[j] >= g[i])
            {
                res++;
                i++;//直到找到满足第一个孩子胃口的饼干,才继续对孩子胃口进行遍历
            }
        }
        return res;
    }
};

按大饼干先满足大胃口

class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        sort(g.begin(),g.end());
        sort(s.begin(),s.end());
        int res = 0;
        for(int i = g.size()-1, j = s.size()-1; i >= 0 && j >= 0; i--)
        {//j++一直找饼干
            if(s[j] >= g[i])
            {
                res++;
                j--;//直到找到这个饼干能满足的第一个孩子,才继续对饼干进行遍历
            }
        }
        return res;
    }
};

LeetCode 55. 跳跃游戏

/*
最后一个位置能获得的最大跳跃长度是无效的
初始位置为 下标[0];目标位置是 下标[size()-1];

看覆盖范围
代码还可以更精简------

回溯-试一下
*/

class Solution {
public:
    bool canJump(vector<int>& nums) {
        if(nums.size() == 1)    return true;//就在目标下标,获得多少步都可到达

        int rangeEnd = 0+nums[0];//存储覆盖范围,第一个:当前下标+获得步数
        for(int i = 0; i < rangeEnd;)// && rangeEnd <= nums.size()-1; )
        {//只遍历到覆盖范围的结束;覆盖范围可以大于最大下标,再此之前就返回了
            if(i+nums[i] >= nums.size()-1)  return true;//如果能覆盖
            else
            {
                rangeEnd = max(i+nums[i] + 1, rangeEnd);//取覆盖范围更大的
                i++;
            }
        }
        return false;
    }
};
### Java实现Delaunay三角剖分算法 Delaunay三角剖分是一种在二维空间中对点集进行三角化的方法,其核心特性是:任意两个点之间的边不会与其他边相交,并且每个三角形的外接圆内不包含其他点。这种特性使得Delaunay三角剖分广泛应用于计算机图形学、地理信息系统(GIS)等领域。 在Java中实现Delaunay三角剖分,通常可以采用**增量插入法**(Bowyer-Watson算法),该算法通过逐步插入点并删除与新点冲突的三角形来维护Delaunay性质。以下是一个基于该算法的Java实现示例。 --- ### Java代码实现 ```java import java.util.*; class Point { double x, y; Point(double x, double y) { this.x = x; this.y = y; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof Point)) return false; Point other = (Point) obj; return Double.compare(other.x, x) == 0 && Double.compare(other.y, y) == 0; } @Override public int hashCode() { return Objects.hash(x, y); } } class Triangle { Point a, b, c; Set<Edge> edges = new HashSet<>(); Triangle(Point a, Point b, Point c) { this.a = a; this.b = b; this.c = c; edges.add(new Edge(a, b)); edges.add(new Edge(b, c)); edges.add(new Edge(c, a)); } boolean circumcircleContains(Point p) { double abx = a.x - b.x; double aby = a.y - b.y; double acx = a.x - c.x; double acy = a.y - c.y; double d = 2 * (abx * (a.y - c.y) - acx * aby); double cx_ = ( (a.x * a.x - b.x * b.x) * (a.y - c.y) - (a.x * a.x - c.x * c.x) * aby ) / d; double cy_ = ( (a.y * a.y - b.y * b.y) * (abx) - (a.y * a.y - c.y * c.y) * acx ) / d; double r = Math.sqrt((a.x - cx_) * (a.x - cx_) + (a.y - cy_) * (a.y - cy_)); double px = p.x - cx_; double py = p.y - cy_; return (px * px + py * py) <= r * r + 1e-8; } } class Edge { Point p1, p2; Edge(Point p1, Point p2) { if (p1.x < p2.x || (p1.x == p2.x && p1.y < p2.y)) { this.p1 = p1; this.p2 = p2; } else { this.p1 = p2; this.p2 = p1; } } @Override public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof Edge)) return false; Edge other = (Edge) obj; return p1.equals(other.p1) && p2.equals(other.p2); } @Override public int hashCode() { return Objects.hash(p1, p2); } } public class DelaunayTriangulation { private static List<Triangle> triangulate(List<Point> points) { List<Triangle> triangles = new ArrayList<>(); Point p1 = new Point(-1000, -1000); Point p2 = new Point(1000, -1000); Point p3 = new Point(0, 1000); Triangle superTriangle = new Triangle(p1, p2, p3); triangles.add(superTriangle); for (Point p : points) { Set<Edge> polygon = new HashSet<>(); List<Triangle> badTriangles = new ArrayList<>(); for (Triangle t : triangles) { if (t.circumcircleContains(p)) { badTriangles.add(t); for (Edge e : t.edges) { boolean shared = false; for (Triangle other : triangles) { if (other != t && other.edges.contains(e)) { shared = true; break; } } if (!shared) { polygon.add(e); } } } } for (Edge e : polygon) { triangles.add(new Triangle(e.p1, e.p2, p)); } triangles.removeAll(badTriangles); } // 移除包含超级三角形顶点的三角形 triangles.removeIf(t -> t.a == p1 || t.a == p2 || t.a == p3 || t.b == p1 || t.b == p2 || t.b == p3 || t.c == p1 || t.c == p2 || t.c == p3); return triangles; } public static void main(String[] args) { List<Point> points = new ArrayList<>(); points.add(new Point(0.0, 0.0)); points.add(new Point(1.0, 0.0)); points.add(new Point(0.5, 1.0)); points.add(new Point(0.5, 0.5)); List<Triangle> result = triangulate(points); for (Triangle t : result) { System.out.println("Triangle: (" + t.a.x + "," + t.a.y + ") - (" + t.b.x + "," + t.b.y + ") - (" + t.c.x + "," + t.c.y + ")"); } } } ``` --- ### 代码说明 - **Point类**:表示二维空间中的点。 - **Edge类**:表示三角形的边,用于边去重。 - **Triangle类**:表示一个三角形,并包含判断点是否在三角形外接圆内的方法。 - **triangulate方法**:使用Bowyer-Watson算法进行增量插入,构建Delaunay三角剖分。 - **main方法**:提供测试点集并输出三角剖分结果。 该实现能够处理任意二维点集,并输出符合Delaunay条件的三角形集合。算法的时间复杂度为O(n&sup2;),适用于中等规模的数据集。 --- ### 性能优化与扩展 对于大规模点集,可以引入**分治法**或**平面扫描法**来提升性能。此外,该算法也可以扩展到三维空间,形成**三维Delaunay三角剖分**(Tetrahedralization),但实现复杂度显著增加[^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值