iT邦幫忙

2024 iThome 鐵人賽

DAY 17
0
自我挑戰組

Leetcode 解題之旅:逐日攻克系列 第 17

每日一LeetCode(17)

  • 分享至 

  • xImage
  •  

3194. Minimum Average of Smallest and Largest Elements

題目敘述:

You have an array of floating point numbers averages which is initially empty. You are given an array nums of n integers where n is even.

You repeat the following procedure n / 2 times:

Remove the smallest element, minElement, and the largest element maxElement, from nums.
Add (minElement + maxElement) / 2 to averages.
Return the minimum element in averages.

class Solution {
public:
    double minimumAverage(vector<int>& v) {
        int n=v.size();
        sort(v.begin(),v.end());
        int i=0,j=n-1;
        double ans=1000;
        while(i<j)
        {
            double k=(v[i]+v[j])/(2*1.0);
            ans=min(ans,k);
            i++;
            j--;
        }
        return ans;
    }
};

上一篇
每日一LeetCode(16)
系列文
Leetcode 解題之旅:逐日攻克17
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言