news 2026/6/10 16:38:00

leetcode 825. Friends Of Appropriate Ages 适龄的朋友

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
leetcode 825. Friends Of Appropriate Ages 适龄的朋友

Problem: 825. Friends Of Appropriate Ages 适龄的朋友

解题过程

双指针,相同年龄可以相互发送,所以二分查找拿到>ages[y]的索引,相减*2;然后就是满足条件的,ages[x] < (( ages[y] - 7 ) << 1);,二分查找拿到>=的索引ind,ind-x就是可用区间,不满足条件的直接跳过

Code

class Solution { public: int numFriendRequests(vector<int>& ages) { sort(ages.begin(), ages.end()); int n = ages.size(), y = 0, x = 1, sum = 0, ind, a; while( y < n - 1 ) { if( ages[x] * 0.5 + 7.0 >= ages[y] ) { y++; x = y + 1; } else { ind = upper_bound(ages.begin(), ages.end(), ages[y]) - ages.begin(); if(ind - y > 0) { sum += (ind - y - 1) * 2; } x = ind; a = (( ages[y] - 7 ) << 1); ind = lower_bound(ages.begin(), ages.end(), a) - ages.begin(); if(ind - x > 0) { sum += ind - x; } y++; x = y+1; } } return sum; // (y-7)*2 > x >= y && (y<=100||x>=100) } };
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 8:08:20

Stellarium天文软件故障诊断与预防指南

引言&#xff1a;从"为什么又崩溃了"到"我懂怎么修了" 【免费下载链接】stellarium Stellarium is a free GPL software which renders realistic skies in real time with OpenGL. It is available for Linux/Unix, Windows and macOS. With Stellarium, …

作者头像 李华
网站建设 2026/6/10 8:12:52

Spring Boot与Vue.js全栈开发终极指南:打造现代化Web应用架构

想要构建一个既具备强大后端能力又拥有流畅前端体验的现代化Web应用吗&#xff1f;Spring Boot与Vue.js的完美结合为你提供了理想的解决方案。这个全栈开发组合让你能够快速搭建高性能的企业级应用&#xff0c;同时享受前端开发的灵活性和易用性。 【免费下载链接】spring-boot…

作者头像 李华
网站建设 2026/6/10 8:18:06

Teachable Machine终极指南:零代码快速构建AI图像识别模型

Teachable Machine终极指南&#xff1a;零代码快速构建AI图像识别模型 【免费下载链接】teachablemachine-community Example code snippets and machine learning code for Teachable Machine 项目地址: https://gitcode.com/gh_mirrors/te/teachablemachine-community …

作者头像 李华
网站建设 2026/6/10 8:19:11

3个关键步骤掌握FaceFusion人脸掩码:告别融合毛边与背景干扰

3个关键步骤掌握FaceFusion人脸掩码&#xff1a;告别融合毛边与背景干扰 【免费下载链接】facefusion Next generation face swapper and enhancer 项目地址: https://gitcode.com/GitHub_Trending/fa/facefusion FaceFusion作为下一代人脸交换与增强工具&#xff0c;通…

作者头像 李华