diff --git a/solution/0800-0899/0825.Friends Of Appropriate Ages/README.md b/solution/0800-0899/0825.Friends Of Appropriate Ages/README.md index 456c7e99695a7..4e00b92accac7 100644 --- a/solution/0800-0899/0825.Friends Of Appropriate Ages/README.md +++ b/solution/0800-0899/0825.Friends Of Appropriate Ages/README.md @@ -77,7 +77,15 @@ tags: -### 方法一 +### 方法一:计数 + 枚举 + +我们可以用一个长度为 $121$ 的数组 $\textit{cnt}$ 记录每个年龄的人数。 + +接下来,枚举所有可能的年龄对 $(\textit{ax}, \textit{ay})$,如果 $\textit{ax}$ 和 $\textit{ay}$ 不满足题目中的任意一个条件,这些年龄对 $(\textit{ax}, \textit{ay})$ 就可以互发好友请求。 + +此时,如果 $\textit{ax} = \textit{ay}$,年龄相同,那么 $\textit{ax}$ 和 $\textit{ay}$ 之间的好友请求数为 $\textit{cnt}[\textit{ax}] \times (\textit{cnt}[\textit{ax}] - 1)$;否则,年龄不同,那么 $\textit{ax}$ 和 $\textit{ay}$ 之间的好友请求数为 $\textit{cnt}[\textit{ax}] \times \textit{cnt}[\textit{ay}]$。我们将这些好友请求数累加到答案中即可。 + +时间复杂度 $O(n + m^2)$,其中 $n$ 是数组 $\textit{ages}$ 的长度,而 $m$ 是年龄的最大值,本题中 $m = 121$。 @@ -86,16 +94,14 @@ tags: ```python class Solution: def numFriendRequests(self, ages: List[int]) -> int: - counter = Counter(ages) + cnt = [0] * 121 + for x in ages: + cnt[x] += 1 ans = 0 - for i in range(1, 121): - n1 = counter[i] - for j in range(1, 121): - n2 = counter[j] - if not (j <= 0.5 * i + 7 or j > i or (j > 100 and i < 100)): - ans += n1 * n2 - if i == j: - ans -= n2 + for ax, x in enumerate(cnt): + for ay, y in enumerate(cnt): + if not (ay <= 0.5 * ax + 7 or ay > ax or (ay > 100 and ax < 100)): + ans += x * (y - int(ax == ay)) return ans ``` @@ -104,20 +110,16 @@ class Solution: ```java class Solution { public int numFriendRequests(int[] ages) { - int[] counter = new int[121]; - for (int age : ages) { - ++counter[age]; + final int m = 121; + int[] cnt = new int[m]; + for (int x : ages) { + ++cnt[x]; } int ans = 0; - for (int i = 1; i < 121; ++i) { - int n1 = counter[i]; - for (int j = 1; j < 121; ++j) { - int n2 = counter[j]; - if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) { - ans += n1 * n2; - if (i == j) { - ans -= n2; - } + for (int ax = 1; ax < m; ++ax) { + for (int ay = 1; ay < m; ++ay) { + if (!(ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100))) { + ans += cnt[ax] * (cnt[ay] - (ax == ay ? 1 : 0)); } } } @@ -132,16 +134,16 @@ class Solution { class Solution { public: int numFriendRequests(vector& ages) { - vector counter(121); - for (int age : ages) ++counter[age]; + const int m = 121; + vector cnt(m); + for (int x : ages) { + ++cnt[x]; + } int ans = 0; - for (int i = 1; i < 121; ++i) { - int n1 = counter[i]; - for (int j = 1; j < 121; ++j) { - int n2 = counter[j]; - if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) { - ans += n1 * n2; - if (i == j) ans -= n2; + for (int ax = 1; ax < m; ++ax) { + for (int ay = 1; ay < m; ++ay) { + if (!(ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100))) { + ans += cnt[ax] * (cnt[ay] - (ax == ay ? 1 : 0)); } } } @@ -153,25 +155,49 @@ public: #### Go ```go -func numFriendRequests(ages []int) int { - counter := make([]int, 121) - for _, age := range ages { - counter[age]++ +func numFriendRequests(ages []int) (ans int) { + cnt := [121]int{} + for _, x := range ages { + cnt[x]++ } - ans := 0 - for i := 1; i < 121; i++ { - n1 := counter[i] - for j := 1; j < 121; j++ { - n2 := counter[j] - if !(j <= i/2+7 || j > i || (j > 100 && i < 100)) { - ans += n1 * n2 - if i == j { - ans -= n2 - } + for ax, x := range cnt { + for ay, y := range cnt { + if ay <= ax/2+7 || ay > ax || (ay > 100 && ax < 100) { + continue + } + if ax == ay { + ans += x * (x - 1) + } else { + ans += x * y } } } - return ans + + return +} +``` + +#### TypeScript + +```ts +function numFriendRequests(ages: number[]): number { + const m = 121; + const cnt = Array(m).fill(0); + for (const x of ages) { + cnt[x]++; + } + + let ans = 0; + for (let ax = 0; ax < m; ax++) { + for (let ay = 0; ay < m; ay++) { + if (ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100)) { + continue; + } + ans += cnt[ax] * (cnt[ay] - (ax === ay ? 1 : 0)); + } + } + + return ans; } ``` diff --git a/solution/0800-0899/0825.Friends Of Appropriate Ages/README_EN.md b/solution/0800-0899/0825.Friends Of Appropriate Ages/README_EN.md index 246df0457f069..c9bdff14438a4 100644 --- a/solution/0800-0899/0825.Friends Of Appropriate Ages/README_EN.md +++ b/solution/0800-0899/0825.Friends Of Appropriate Ages/README_EN.md @@ -75,7 +75,15 @@ tags: -### Solution 1 +### Solution 1: Counting + Enumeration + +We can use an array $\textit{cnt}$ of length $121$ to record the number of people of each age. + +Next, we enumerate all possible age pairs $(\textit{ax}, \textit{ay})$. If $\textit{ax}$ and $\textit{ay}$ satisfy the conditions given in the problem, these age pairs $(\textit{ax}, \textit{ay})$ can send friend requests to each other. + +If $\textit{ax} = \textit{ay}$, meaning the ages are the same, then the number of friend requests between $\textit{ax}$ and $\textit{ay}$ is $\textit{cnt}[\textit{ax}] \times (\textit{cnt}[\textit{ax}] - 1)$. Otherwise, if the ages are different, the number of friend requests between $\textit{ax}$ and $\textit{ay}$ is $\textit{cnt}[\textit{ax}] \times \textit{cnt}[\textit{ay}]$. We accumulate these friend request counts into the answer. + +The time complexity is $O(n + m^2)$, where $n$ is the length of the array $\textit{ages}$, and $m$ is the maximum age, which is $121$ in this problem. @@ -84,16 +92,14 @@ tags: ```python class Solution: def numFriendRequests(self, ages: List[int]) -> int: - counter = Counter(ages) + cnt = [0] * 121 + for x in ages: + cnt[x] += 1 ans = 0 - for i in range(1, 121): - n1 = counter[i] - for j in range(1, 121): - n2 = counter[j] - if not (j <= 0.5 * i + 7 or j > i or (j > 100 and i < 100)): - ans += n1 * n2 - if i == j: - ans -= n2 + for ax, x in enumerate(cnt): + for ay, y in enumerate(cnt): + if not (ay <= 0.5 * ax + 7 or ay > ax or (ay > 100 and ax < 100)): + ans += x * (y - int(ax == ay)) return ans ``` @@ -102,20 +108,16 @@ class Solution: ```java class Solution { public int numFriendRequests(int[] ages) { - int[] counter = new int[121]; - for (int age : ages) { - ++counter[age]; + final int m = 121; + int[] cnt = new int[m]; + for (int x : ages) { + ++cnt[x]; } int ans = 0; - for (int i = 1; i < 121; ++i) { - int n1 = counter[i]; - for (int j = 1; j < 121; ++j) { - int n2 = counter[j]; - if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) { - ans += n1 * n2; - if (i == j) { - ans -= n2; - } + for (int ax = 1; ax < m; ++ax) { + for (int ay = 1; ay < m; ++ay) { + if (!(ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100))) { + ans += cnt[ax] * (cnt[ay] - (ax == ay ? 1 : 0)); } } } @@ -130,16 +132,16 @@ class Solution { class Solution { public: int numFriendRequests(vector& ages) { - vector counter(121); - for (int age : ages) ++counter[age]; + const int m = 121; + vector cnt(m); + for (int x : ages) { + ++cnt[x]; + } int ans = 0; - for (int i = 1; i < 121; ++i) { - int n1 = counter[i]; - for (int j = 1; j < 121; ++j) { - int n2 = counter[j]; - if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) { - ans += n1 * n2; - if (i == j) ans -= n2; + for (int ax = 1; ax < m; ++ax) { + for (int ay = 1; ay < m; ++ay) { + if (!(ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100))) { + ans += cnt[ax] * (cnt[ay] - (ax == ay ? 1 : 0)); } } } @@ -151,25 +153,49 @@ public: #### Go ```go -func numFriendRequests(ages []int) int { - counter := make([]int, 121) - for _, age := range ages { - counter[age]++ +func numFriendRequests(ages []int) (ans int) { + cnt := [121]int{} + for _, x := range ages { + cnt[x]++ } - ans := 0 - for i := 1; i < 121; i++ { - n1 := counter[i] - for j := 1; j < 121; j++ { - n2 := counter[j] - if !(j <= i/2+7 || j > i || (j > 100 && i < 100)) { - ans += n1 * n2 - if i == j { - ans -= n2 - } + for ax, x := range cnt { + for ay, y := range cnt { + if ay <= ax/2+7 || ay > ax || (ay > 100 && ax < 100) { + continue + } + if ax == ay { + ans += x * (x - 1) + } else { + ans += x * y } } } - return ans + + return +} +``` + +#### TypeScript + +```ts +function numFriendRequests(ages: number[]): number { + const m = 121; + const cnt = Array(m).fill(0); + for (const x of ages) { + cnt[x]++; + } + + let ans = 0; + for (let ax = 0; ax < m; ax++) { + for (let ay = 0; ay < m; ay++) { + if (ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100)) { + continue; + } + ans += cnt[ax] * (cnt[ay] - (ax === ay ? 1 : 0)); + } + } + + return ans; } ``` diff --git a/solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.cpp b/solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.cpp index 2391a481bb7ce..eb65048457aab 100644 --- a/solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.cpp +++ b/solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.cpp @@ -1,19 +1,19 @@ class Solution { public: int numFriendRequests(vector& ages) { - vector counter(121); - for (int age : ages) ++counter[age]; + const int m = 121; + vector cnt(m); + for (int x : ages) { + ++cnt[x]; + } int ans = 0; - for (int i = 1; i < 121; ++i) { - int n1 = counter[i]; - for (int j = 1; j < 121; ++j) { - int n2 = counter[j]; - if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) { - ans += n1 * n2; - if (i == j) ans -= n2; + for (int ax = 1; ax < m; ++ax) { + for (int ay = 1; ay < m; ++ay) { + if (!(ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100))) { + ans += cnt[ax] * (cnt[ay] - (ax == ay ? 1 : 0)); } } } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.go b/solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.go index 63c581630b7be..7185f8d6acbb2 100644 --- a/solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.go +++ b/solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.go @@ -1,20 +1,20 @@ -func numFriendRequests(ages []int) int { - counter := make([]int, 121) - for _, age := range ages { - counter[age]++ +func numFriendRequests(ages []int) (ans int) { + cnt := [121]int{} + for _, x := range ages { + cnt[x]++ } - ans := 0 - for i := 1; i < 121; i++ { - n1 := counter[i] - for j := 1; j < 121; j++ { - n2 := counter[j] - if !(j <= i/2+7 || j > i || (j > 100 && i < 100)) { - ans += n1 * n2 - if i == j { - ans -= n2 - } + for ax, x := range cnt { + for ay, y := range cnt { + if ay <= ax/2+7 || ay > ax || (ay > 100 && ax < 100) { + continue + } + if ax == ay { + ans += x * (x - 1) + } else { + ans += x * y } } } - return ans -} \ No newline at end of file + + return +} diff --git a/solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.java b/solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.java index 6aa39d3d928b0..54ee798628887 100644 --- a/solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.java +++ b/solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.java @@ -1,22 +1,18 @@ class Solution { public int numFriendRequests(int[] ages) { - int[] counter = new int[121]; - for (int age : ages) { - ++counter[age]; + final int m = 121; + int[] cnt = new int[m]; + for (int x : ages) { + ++cnt[x]; } int ans = 0; - for (int i = 1; i < 121; ++i) { - int n1 = counter[i]; - for (int j = 1; j < 121; ++j) { - int n2 = counter[j]; - if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) { - ans += n1 * n2; - if (i == j) { - ans -= n2; - } + for (int ax = 1; ax < m; ++ax) { + for (int ay = 1; ay < m; ++ay) { + if (!(ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100))) { + ans += cnt[ax] * (cnt[ay] - (ax == ay ? 1 : 0)); } } } return ans; } -} \ No newline at end of file +} diff --git a/solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.py b/solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.py index 651bc1134b06e..80dbfa08eacdd 100644 --- a/solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.py +++ b/solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.py @@ -1,13 +1,11 @@ class Solution: def numFriendRequests(self, ages: List[int]) -> int: - counter = Counter(ages) + cnt = [0] * 121 + for x in ages: + cnt[x] += 1 ans = 0 - for i in range(1, 121): - n1 = counter[i] - for j in range(1, 121): - n2 = counter[j] - if not (j <= 0.5 * i + 7 or j > i or (j > 100 and i < 100)): - ans += n1 * n2 - if i == j: - ans -= n2 + for ax, x in enumerate(cnt): + for ay, y in enumerate(cnt): + if not (ay <= 0.5 * ax + 7 or ay > ax or (ay > 100 and ax < 100)): + ans += x * (y - int(ax == ay)) return ans diff --git a/solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.ts b/solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.ts new file mode 100644 index 0000000000000..1ff369f26a76f --- /dev/null +++ b/solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.ts @@ -0,0 +1,19 @@ +function numFriendRequests(ages: number[]): number { + const m = 121; + const cnt = Array(m).fill(0); + for (const x of ages) { + cnt[x]++; + } + + let ans = 0; + for (let ax = 0; ax < m; ax++) { + for (let ay = 0; ay < m; ay++) { + if (ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100)) { + continue; + } + ans += cnt[ax] * (cnt[ay] - (ax === ay ? 1 : 0)); + } + } + + return ans; +}