04 Sorting
题目 1:MaxProductOfThree
A non-empty zero-indexed array A consisting of N integers is given. The product of triplet (P, Q, R) equates to A[P] * A[Q] * A[R] (0 ≤ P < Q < R < N).
For example, array A such that:
A[0] = -3 A[1] = 1 A[2] = 2 A[3] = -2 A[4] = 5 A[5] = 6
contains the following example triplets:
- (0, 1, 2), product is −3 * 1 * 2 = −6
- (1, 2, 4), product is 1 * 2 * 5 = 10
- (2, 4, 5), product is 2 * 5 * 6 = 60
Your goal is to find the maximal product of any triplet.
Write a function:
int solution(vector<int> &A);
that, given a non-empty zero-indexed array A, returns the value of the maximal product of any triplet.
Assume that:
- N is an integer within the range [3..100,000];
- each element of array A is an integer within the range [−1,000..1,000].
Complexity:
- expected worst-case time complexity is O(N*log(N));
- expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).
#include <algorithm>// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;int solution(vector<int> &A) {// write your code in C++11int n=A.size();sort(A.begin(),A.end());int sum1=A[0]*A[1]*A[n-1];int sum2=A[n-1]*A[n-2]*A[n-3]; return sum1>sum2?sum1:sum2;
}
题目 2:Distinct
Write a function
int solution(vector<int> &A);
that, given a zero-indexed array A consisting of N integers, returns the number of distinct values in array A.
Assume that:
- N is an integer within the range [0..100,000];
- each element of array A is an integer within the range [−1,000,000..1,000,000].
For example, given array A consisting of six elements such that:
A[0] = 2 A[1] = 1 A[2] = 1A[3] = 2 A[4] = 3 A[5] = 1
the function should return 3, because there are 3 distinct values appearing in array A, namely 1, 2 and 3.
Complexity:
- expected worst-case time complexity is O(N*log(N));
- expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
#include <algorithm>
#include <set>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;int solution(vector<int> &A) {// write your code in C++11//sortint n=A.size();sort(A.begin(),A.end());int count=0;for(int i=0;i<n;i++){if(i==0||A[i]!=A[i-1])count++;}return count;//set/* #define FOR(i,n) for(int i=0;i<(int)n;++i)int n=A.size();set<int> st;FOR(i,n){st.insert(A[i]);}return st.size();*/
}
题目 3:Triangle
A zero-indexed array A consisting of N integers is given. A triplet (P, Q, R) is triangular if 0 ≤ P < Q < R < N and:
- A[P] + A[Q] > A[R],
- A[Q] + A[R] > A[P],
- A[R] + A[P] > A[Q].
For example, consider array A such that:
A[0] = 10 A[1] = 2 A[2] = 5 A[3] = 1 A[4] = 8 A[5] = 20
Triplet (0, 2, 4) is triangular.
Write a function:
int solution(vector<int> &A);
that, given a zero-indexed array A consisting of N integers, returns 1 if there exists a triangular triplet for this array and returns 0 otherwise.
Assume that:
- N is an integer within the range [0..100,000];
- each element of array A is an integer within the range [−2,147,483,648..2,147,483,647].
Complexity:
- expected worst-case time complexity is O(N*log(N));
- expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
// you can use includes, for example:#include <algorithm>// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;int solution(vector<int> &A) {// write your code in C++11int n=A.size();sort(A.begin(),A.end());for(int i=0;i<n-2;i++){if(A[i+2]-A[i+1]<A[i])return 1;}return 0;
}
题目 4:NumberOfDiscIntersections
We draw N discs on a plane. The discs are numbered from 0 to N − 1. A zero-indexed array A of N non-negative integers, specifying the radiuses of the discs, is given. The J-th disc is drawn with its center at (J, 0) and radius A[J].
We say that the J-th disc and K-th disc intersect if J ≠ K and the J-th and K-th discs have at least one common point (assuming that the discs contain their borders).
The figure below shows discs drawn for N = 6 and A as follows:
A[0] = 1 A[1] = 5 A[2] = 2 A[3] = 1 A[4] = 4 A[5] = 0
There are eleven (unordered) pairs of discs that intersect, namely:
- discs 1 and 4 intersect, and both intersect with all the other discs;
- disc 2 also intersects with discs 0 and 3.
Write a function:
int solution(vector<int> &A);
that, given an array A describing N discs as explained above, returns the number of (unordered) pairs of intersecting discs. The function should return −1 if the number of intersecting pairs exceeds 10,000,000.
Assume that:
- N is an integer within the range [0..100,000];
- each element of array A is an integer within the range [0..2,147,483,647].
Complexity:
- expected worst-case time complexity is O(N*log(N));
- expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
int solution(vector<int> &A) {// write your code in C++11int n=A.size();vector<pair<long,int> > plane;for(int i=0;i<n;i++){plane.push_back(make_pair((long)i-A[i],-1));plane.push_back(make_pair((long)i+A[i],1));}sort(plane.begin(),plane.end());long count=0,allinsert=0;for(int i=0;i<2*n;i++){if(plane[i].second==-1){allinsert+=count;if(allinsert>1e7)return -1;count++;}elsecount--;}return allinsert;
}
04 Sorting相关推荐
- 全国计算机等级考试题库二级C操作题100套(第04套)
第04套: 程序通过定义学生结构体变量,存储了学生的学号.姓名和3门课的成绩.所有学生数据均以二进制方式输出到文件中.函数fun的功能是从形参filename所指的文件中读入学生数据,并按照学号从小到 ...
- 《计算之魂》读书笔记 04
<计算之魂>读书笔记 04 1.4 关于排序的讨论 [1.4.3]针对特殊情况,我们是否还有更好的答案? [附录]为什么排序算法的复杂度不可能小于 O(nlogn)O(nlogn)O(nl ...
- 英语读书笔记-Book Lovers Day 04
英语读书笔记-Book Lovers Day 04 Part 1 When we were teenagers, we'd spend hours sorting through junk for g ...
- Ubuntu16.04安装cuckoo sandbox
一.官方文档 https://cuckoo.sh/docs/ 二.沙盒简介 在计算机安全领域,沙盒(英语:sandbox,又译为沙箱)是一种安全机制,为运行中的程序提供的隔离环境.通常是作为一些来源不 ...
- ubuntu18.04 ros-melodic 在安装ros依赖包时总是出现E软件包无法定位
ubuntu18.04 ros-melodic 在安装ros依赖包时总是出现E软件包无法定位 鱼香ROS介绍: 鱼香ROS是由机器人爱好者共同组成的社区,欢迎一起参与机器人技术交流. 进交流群请加机器 ...
- 在Ubuntu18.04上安装USTC-TK2016
在Ubuntu18.04上安装USTC-TK2016 1.安装依赖 1. 安装Mono sudo apt install gnupg ca-certificates sudo apt-key adv ...
- yipin project 04
day04 今⽇内容: ● 1- ODS补充⼀张店铺表 ● 2- ODS层增量数据导⼊操作 ● 3- 分桶表的相关内容 ● 4- DWD层相关的操作 0 ODS补充⼀个店铺表 -- 店铺表 DROP ...
- 【OpenStack】在ubuntu20.04上部署openstack(Ussuri版)
在ubuntu20.04上部署openstack(Ussuri版) 文章目录 在ubuntu20.04上部署openstack(Ussuri版) 一.在虚拟机上安装的网络配置(服务器看二) 二.在服务 ...
- Pancake Sorting问题研究与优化
摘要 烙饼问题最初是在1975年由雅各布·e·古德曼在美国数学月刊上提出的,名为"Harry Dweighter"(或"忙碌的服务员").[1]在接下来的几年里 ...
最新文章
- python投资组合
- 什么是锚文本以及锚文本连接
- python 数据结构包_Python Redistr包_程序模块 - PyPI - Python中文网
- Shiro的Base64和MD5加密的使用
- pwa程序,清单文件测试有效,为什么不起效果?
- 关于MyEclipse项目的名字的修改对项目导入导出的影响
- 8086算术移位指令SAL和SAR
- java集成开发工具项目_Java项目开发(一)-不借助集成工具创建Java项目并编写编译执行脚本...
- Mark Cuban投资组合中60%为BTC,30%为ETH
- Spring MVC —— form表单post提交出现乱码
- Fortran编程:(一)认识Fortran
- 分享一款好用的英语词频统计软件
- hibernate笔记(三)
- 吴伯凡-认知方法论-聪明人如何避免犯错误
- Spring中的DataSource
- 为什么我们不能坚持?
- Python2.7爬虫——爬取微信公众号文章
- 一次 WebResource.axd 异常处理经历
- Fedora和Red Hat Enterprise Linux实用指南(第6版)(上、下册)( 入行必读的Linux圣经)...
- 开学!可怕的日本,无情到令人感到恐惧!
热门文章
- Leecode 738. 单调递增的数字 贪心
- 作曲与计算机音乐,论计算机音乐与计算机作曲
- PS5运行Linux,索尼发布新驱动 PS5手柄现已支持Linux系统用户
- UiPath RPA 工具中的键盘快捷键
- Database Meets AI: A Survey
- 欢迎空心字怎么写_的的空心字怎么写
- css教程 school,CSS 教程
- 苹果MAC OS 系统 安装指南 手册 步骤
- #红绿蓝颜色编码RGB
- MacBook Pro(13 英寸,2011 年末)A1278 老机器换了 好几个版本的系统,一直没有隔空投送,请高手指点!