日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

825. Friends Of Appropriate Ages**

發(fā)布時(shí)間:2023/12/20 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 825. Friends Of Appropriate Ages** 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

825. Friends Of Appropriate Ages**

https://leetcode.com/problems/friends-of-appropriate-ages/

題目描述

Some people will make friend requests. The list of their ages is given and ages[i] is the age of the ith person.

Person A will NOT friend request person B (B != A) if any of the following conditions are true:

  • age[B] <= 0.5 * age[A] + 7
  • age[B] > age[A]
  • age[B] > 100 && age[A] < 100
    Otherwise, A will friend request B.

Note that if A requests B, B does not necessarily request A. Also, people will not friend request themselves.

How many total friend requests are made?

Example 1:

Input: [16,16] Output: 2 Explanation: 2 people friend request each other.

Example 2:

Input: [16,17,18] Output: 2 Explanation: Friend requests are made 17 -> 16, 18 -> 17.

Example 3:

Input: [20,30,100,110,120] Output: Explanation: Friend requests are made 110 -> 100, 120 -> 110, 120 -> 100.

Notes:

  • 1 <= ages.length <= 20000.
  • 1 <= ages[i] <= 120.

前言

有個(gè)笑話, 我必須笑一下:

因?yàn)槲以趯懘a的過程中也發(fā)現(xiàn)了這個(gè)問題, 然后停下來開始懷疑了人生 … 🤣🤣🤣

C++ 實(shí)現(xiàn) 1

我的思路稍微復(fù)雜一些, 要簡潔的方法可以看 C++ 實(shí)現(xiàn) 2.

從第二個(gè)條件可以發(fā)現(xiàn), 年輕的不會和年老的交友, 因此對年齡從小到大排序, 從后向前開始考慮. 對于 ages[i], 它只能和 [0 ... i - 1] 范圍內(nèi)的人交友. 但這里有個(gè)例外, 即 ages[i] == ages[i - 1] 時(shí), 此時(shí) ages[i - 1] 也能和 ages[i] 交友, 這種特殊情形我們之后考慮.

由于 ages[i] 不會和 age[B] <= 0.5 * age[i] + 7 的人交朋友, 其實(shí)就是在數(shù)組找 0.5 * age[i] + 7 的 upper_bound, 即第一個(gè)大于 0.5 * age[i] + 7 的數(shù) (更具體是 int(std::floor(ages[i] / 2.)) + 7, 代入一個(gè)具體的數(shù)值進(jìn)去就會發(fā)現(xiàn)是 floor 而不是 ceil.). 第三個(gè)條件 age[B] > 100 && age[A] < 100 是冗余的, 包含于條件二, 因此不用考慮. 那么 ages[i] 能交友的范圍為 [j, i - 1], 個(gè)數(shù)就是 (i - 1) + 1 - j = i - j 個(gè). 然而這里有兩個(gè)問題:

  • 如果 j 不存在, 即 upper_bound 不存在, 那么 j = ages.size(), i - j 此時(shí)小于 0, 因此最后用 std::max(i - j, 0) 表示 ages[i] 能交友的個(gè)數(shù).
  • 另外還需要考慮 ages[i] == ages[i + 1] 的情況, 此時(shí) ages[i] 的交友個(gè)數(shù)其實(shí)和 ages[i + 1] 的交友個(gè)數(shù)一樣, 因此用 prev 表示 ages[i + 1] 的交友個(gè)數(shù); 當(dāng)訪問到 ages[i] 時(shí), 如果 ages[i] == ages[i+ 1], 那么交友個(gè)數(shù)就是 prev.
class Solution { public:int numFriendRequests(vector<int>& ages) {std::sort(ages.begin(), ages.end());int res = 0, prev = 0;for (int i = ages.size() - 1; i >= 0; -- i) {int target = int(std::floor(ages[i] / 2.)) + 7;int j = std::upper_bound(ages.begin(), ages.end(), target) - ages.begin();if (i < ages.size() - 1 && ages[i] == ages[i + 1]) res += prev;else {res += std::max(i - j, 0);prev = std::max(i - j, 0);}}return res;} };

C++ 實(shí)現(xiàn) 2

來自 LeetCode Submission. 由于年齡的大小是有限制的, 在 0 ~ 120 之間, 因此可以用數(shù)組保存各個(gè)年齡的人數(shù), 然后過濾不滿足條件的交友關(guān)系, 另外注意不能和自己交友.

class Solution { public:int numFriendRequests(vector<int>& ages) {vector<int> count(121, 0);int ret = 0;for (int age : ages) count[age]++;for (int i = 0; i < 121; i++) {for (int j = 0; j < 121; j++) {if (j <= 0.5 * i + 7) continue;if (j > i) continue;if (j > 100 && i < 100) continue;ret += count[i] * count[j];if (i == j) ret -= count[i]; // 不能和自己交友} }return ret;} };

總結(jié)

以上是生活随笔為你收集整理的825. Friends Of Appropriate Ages**的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。