Giter VIP home page Giter VIP logo

leecode_cplusplus's People

Contributors

hujianglang avatar

Stargazers

 avatar

Watchers

 avatar

leecode_cplusplus's Issues

C++ Leecode

这里是学习笔记,参考LeetCode101:和你一起轻松刷题(C++)
作者:高畅(Chang Gao)
再次表示感谢

贪心算法例子-孩子分饼干问题

  1. 算法解释:
    贪心算法的策略,保证每次操作都是局部最优的,从而使最后得到的结果是全局最优的。
int findContentChildren(vector<int>& children, vector<int>& cookies)
{
	sort(children.begin(), children.end());
	sort(cookies.begin(), cookies.end());
	int child = 0, cookie = 0;
	while (child < children.size() && cookie < cookies.size())
	{
		if (children[child] <= cookies[cookie]) ++child;
		cookie++;
	}
	return child;
}

2,分糖果问题
//分糖果问题
int cany(vector<int>& ratings)
{
    int size = ratings.size();
    if(size < 2){
        return size;
    }
    vector<int> num(size,1);
    for(int i = 1; i < size; ++i){
        if(ratings[i] > ratings[i-1]){
            num[i] = num[i-1] + 1;
        }
    }
    
    for(int i = size-1; i>0;--i)
    {
        if(ratings[i]<ratings[i-1]){
            num[i-1] = max(num[i-1],num[i]+1);
        }
    }
    return accumulate(num.begin(),num.end(),0);//std::accumulate方便求和
}

3,区间问题:Non-overlapping Intervals(Medium)
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
	if (intervals.empty())
		return 0;
	int n = intervals.size();
	sort(intervals.begin(), intervals.end(), [](vector<int> a, vector<int> b)
		{
			return a[1] < b[1];
		});
	int total = 0, prev = intervals[0][1];
	for (int i = 1; i < n; i++) {
		if (intervals[i][0] < prev)
			++total;
		else
			prev = intervals[i][1];
	}
	return total;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.