[日常刷题]leetcode D35

版权声明:希望各位多提意见多多互相交流哦~ https://blog.csdn.net/wait_for_taht_day5/article/details/83050810

447. Number of Boomerangs

Given n points in the plane that are all pairwise distinct, a “boomerang” is a tuple of points (i, j, k) such that the distance between i and j equals the distance between iand k (the order of the tuple matters).

Find the number of boomerangs. You may assume that n will be at most 500 and coordinates of points are all in the range [-10000, 10000] (inclusive).

Example:

Input:
[[0,0],[1,0],[2,0]]

Output:
2

Explanation:
The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]]

Solution in C++:

关键点:

  • 排列

思路:

  • 计算每两个点之间的距离,然后使用map存储距离相同的点,然后通过排列的公式 P 2 n P_2^n = n ! ( n 2 ) ! \frac{n!}{(n-2)!} = n ( n 1 ) n * (n - 1) 计算个数即可。
double distance(pair<int, int>&  a, pair<int, int>&  b){
        double distance = sqrt( pow((a.first - b.first), 2) + pow((a.second - b.second), 2));
        return distance;
    }
    
    int numberOfBoomerangs(vector<pair<int, int>>& points) {
        
        size_t size = points.size();
        int count = 0;
        if (size != 0){
            // 计算每两个点之间的距离
            for(int i = 0; i < size; ++i){
                map<double, int> distances;
                for(int j = 0; j < size; ++j){
                    if (i != j){
                        double tmp = distance(points[i], points[j]);
                        ++distances[tmp];
                    }
                }
                
                for(auto distance : distances){
                    if (distance.second > 1)
                        count += distance.second * (distance.second - 1);
                }
            }
        }
        
        return count;
    }

453. Minimum Moves to Equal Array Elements

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.

Example:

Input:
[1,2,3]

Output:
3

Explanation:
Only three moves are needed (remember each move increments two elements):

[1,2,3]  =>  [2,3,3]  =>  [3,4,3]  =>  [4,4,4]

Solution in C++:

扫描二维码关注公众号,回复: 3843400 查看本文章

关键点:

  • 最小值与最后结果的关系

思路:

  • 开始的思维还蛮混乱的,但是后来分析了几个之后就发现还是有规律可循的,比如最小值增加的次数就是最终要求的次数,然后通过这个关系式就可以得出一个方程。sum + (n - 1) * x = n * ( min + x) 其中x为加的次数,min为数列中的最小值,sum为数列的和。然后就可以求出x = sum - n * min。
int minMoves(vector<int>& nums) {
        int min = INT_MAX;
        size_t size = nums.size();
        
        if (size == 0)
            return 0;
        
        // sum + (n - 1) * x = n * ( min + x)  其中x为加的次数,min为数列中的最小值,sum为数列的和
        long long sum = 0;
        for(auto num : nums){
            sum += num;
            if (min > num)
                min = num;
        }
        return sum - size * min;
    }

455. Assign Cookies

Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor g i g_i , which is the minimum size of a cookie that the child will be content with; and each cookie j has a size s j s_j . If s j s_j >= g i g_i , we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.

Note:
You may assume the greed factor is always positive.
You cannot assign more than one cookie to one child.

Example 1:

Input: [1,2,3], [1,1]

Output: 1

Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. 
And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.
You need to output 1.

Example 2:

Input: [1,2], [1,2,3]

Output: 2

Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2. 
You have 3 cookies and their sizes are big enough to gratify all of the children, 
You need to output 2.

Solution in C++:

关键点:

思路:

  • 两个都排序一下,然后s大于等于g的最后结果就+1,不然就继续往后遍历s,遍历完s即可得到最后的答案。
int findContentChildren(vector<int>& g, vector<int>& s) {
        int count = 0;
        
        sort(g.begin(), g.end());
        sort(s.begin(), s.end());
        for(int i = 0, j = 0; i < g.size() && j < s.size(); ++i, ++j){
            if(g[i] <= s[j])
                ++count;
            else{
                while(g[i] > s[j] && j < s.size())
                    ++j;
                if (j != s.size())
                	++count;
            }
        }
        
        return count;
    }

小结

哈哈,今天比较有进步的点有几点:

  • 1.对刷题的态度开始经过两天的挫败,开始有所改变,因为到时候笔试的时候我也不一定都能很顺利的解决,所以现在要开始慢慢调整做题的状态,在觉得不是很好处理或者调试有一定阻碍的时候应该转换思维或者先刷另外的题。
  • 2.对于数学思维的培养,对于不太懂要怎么处理的题目,先列出几个例子看看,找找其中的规律。

知识点

  • pair
  • 数学思维

猜你喜欢

转载自blog.csdn.net/wait_for_taht_day5/article/details/83050810
今日推荐