![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法
水果妹妹踩前端的坑
这个作者很懒,什么都没留下…
展开
-
剑指 Offer 55 - II. 平衡二叉树(js)
剑指 Offer 55 - II. 平衡二叉树意节点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。示例 1:给定二叉树 [3,9,20,null,null,15,7] 3 / \ 9 20 / \ 15 7返回 true 。示例 2:给定二叉树 [1,2,2,3,3,null,null,4,4] 1 / \ 2 2 / \ 3 3 / \4 4返回false...原创 2021-05-18 14:26:40 · 220 阅读 · 0 评论 -
剑指 Offer 47. 礼物的最大价值
剑指 Offer 47. 礼物的最大价值题目描述:在一个 m*n 的棋盘的每一格都放有一个礼物,每个礼物都有一定的价值(价值大于 0)。你可以从棋盘的左上角开始拿格子里的礼物,并每次向右或者向下移动一格、直到到达棋盘的右下角。给定一个棋盘及其上面的礼物的价值,请计算你最多能拿到多少价值的礼物?示例 1:输入: [ [1,3,1], [1,5,1], [4,2,1]]输出: 12解释: 路径 1→3→5→2→1 可以拿到最多价值的礼物提示:0 < grid.lengt原创 2021-04-13 13:55:44 · 95 阅读 · 0 评论 -
剑指 Offer 42. 连续子数组的最大和
剑指 Offer 42. 连续子数组的最大和题目描述:输入一个整型数组,数组中的一个或连续多个整数组成一个子数组。求所有子数组的和的最大值。要求时间复杂度为O(n)。示例1:输入: nums = [-2,1,-3,4,-1,2,1,-5,4]输出: 6解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。提示:1 <= arr.length <= 10^5-100 <= arr[i] <= 100解题方法一:暴力解决,用2个for循环,但这里还增加了一个原创 2021-04-12 14:50:16 · 78 阅读 · 0 评论 -
1034 Head of a Gang (30 分)【连通分量dfs变形】
One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be t...原创 2018-11-03 13:38:30 · 163 阅读 · 0 评论 -
zcmu1757: 内部收益率【二分】
1757: 内部收益率DescriptionInputOutputSample Input1 -1 2 2 -8 6 9 0Sample Output1.00 0.50HINT 解题思路:这个函数是单调的,所以不会too many;CF0<0,且IRR为负数时要小于-1,也就是绝对值大于1,其他的CFi都大于0,所以一定有近似解,在求解...原创 2018-10-13 15:40:37 · 787 阅读 · 1 评论 -
1203: 逆序数【归并排序】
1203: 逆序数Description在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数不小于后面的数,那么它们就称为一个逆序。一个排列中逆序的总数就称为这个排列的逆序数。如2 4 3 1中,2 1,4 3,4 1,3 1是逆序,逆序数是4。给出一个整数序列,求该序列的逆序数。Input多组测试数据每组测试数据分两行,第一行一个正整数n(n <= 5000...原创 2018-09-22 20:08:46 · 319 阅读 · 0 评论 -
快速幂模板【快速幂+矩阵快速幂】
快速幂模板:引入:就是将幂以二进制数分解,比如5的6次方,6被分解为2,4,即110,110&1为0,不执行ans=ans*a%mod,但是a=a*a每循环一次就执行一次,现在a=5*5,下一次循环11&1==1,执行ans=1*25,a=25*25;1&1==1,执行ans=25*a(也就是25*25),a=a*a(a有点大了,懒得写数字),你只看ans,你就会发现a...原创 2018-09-19 19:18:42 · 229 阅读 · 0 评论 -
排序的模板【冒泡排序、选择排序、直接插入排序、归并排序、堆排序】(还有排序后面继续补)
目录冒泡排序:选择排序:直接插入排序:归并排序:堆排序:冒泡排序:第一种写法:for(int i=0;i<n-1;i++){ for(int j=0;j<n-1-i;j++) { if(a[j]>a[j-1]) swap(a[j],a[j-1]); }} 第二种写法:for(int i=0;i<n-1;i++)...原创 2018-09-23 19:05:55 · 240 阅读 · 0 评论 -
HDU--1248 ( 寒冰王座 ) 【完全背包】
题目链接Problem Description不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票(记住,只有一张钞票),为了防止自己在战斗中频繁的死掉,他决定给自己买一些道具,于是他来到了地精商店前.死亡骑士:"我要买道具!"地精商人:"我们这里有三种道具,血瓶150块一个,魔法药200块一个,无敌药水350块一个."死亡骑士:"好的,给我一个血瓶."说完他掏出那张N元的大钞递给地...原创 2018-09-13 16:31:25 · 225 阅读 · 0 评论 -
1118 Birds in Forest (25 分)【并查集的应用】
1118 Birds in Forest (25 分)Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in the same picture belong to the same tree. You are supposed to help the...原创 2018-11-11 20:09:55 · 186 阅读 · 0 评论 -
1029 Median (25 分)【边输入边输出】
1029 Median (25 分)Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1 = { 11, 12, 13, 14 } is 12, and the median of S2 = { 9,...原创 2018-11-13 22:03:19 · 317 阅读 · 0 评论 -
1057 Stack (30 分)【stack模拟+二分】
1057 Stack (30 分)Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an element onto the t...原创 2018-11-12 11:28:15 · 1380 阅读 · 4 评论 -
PAT甲级1044 Shopping in Mars (25 分)【二分查找】
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position ...原创 2019-02-25 20:23:03 · 305 阅读 · 1 评论 -
PAT甲级1010 Radix (25 分)【二分查找+进制转换】
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer isyes, if 6 is a decimal number and 110 is a binary number.Now for any pair of positive inte...原创 2019-03-19 12:03:38 · 255 阅读 · 0 评论 -
PAT甲级1111 Online Map (30 分)【Dijkstra+dfs】
Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is g...原创 2019-04-01 11:34:56 · 342 阅读 · 0 评论 -
PAT甲级题型整理
按题型整理最短路径类题目:1003 Emergency (Dijkstra)1018 Public Bike Management(Dijkstra+dfs 将所有最短路径都储存在vector中)1087 All Roads Lead to Rome(Dijkstra+dfs)1111 online Map(2次Dijkstra+dfs)二叉搜索树类题目:1043 Is...原创 2019-02-21 12:57:05 · 1160 阅读 · 0 评论 -
排序系列算法
冒泡排序它是一种较简单的排序算法。基本思想:它会遍历若干次要排序的数列,每次遍历时,它都会从前往后依次的比较相邻两个数的大小;如果前者比后者大,则交换它们的位置。这样,一次遍历之后,最大的元素就在数列的末尾! 采用相同的方法再次遍历时,第二大的元素就被排列在最大元素之前。重复此操作,直到整个数列都有序为止!每个排序都会讲思想,然后自己理解并动手敲,再看下面的代码。冒泡排序时间复杂...原创 2019-07-11 11:08:11 · 209 阅读 · 0 评论 -
zcmu-- 中医药院校程序设计竞赛备赛一【前缀和,博弈】
目录2959--Problem B: Amity Assessment【思维】1986- Problem C: 周期串plus【周期】1980-- Problem D: 不存在的泳池【最大公因数】1907-- Problem E: k倍区间【前缀和优化】1916-Problem F: E.当然是选...原创 2018-09-18 14:46:20 · 523 阅读 · 0 评论 -
HDU--2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活【多重背包】
Problem Description急!灾区的食物依然短缺!为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品,其价格不等,并且只能整袋购买。请问:你用有限的资金最多能采购多少公斤粮食呢?后记:人生是一个充满了变数的生命过程,天灾、人祸、病痛是我们生命历程中不可预知的威胁。月有阴晴圆缺,人有旦夕祸福...原创 2018-09-13 10:53:21 · 160 阅读 · 0 评论 -
HDU--1114Piggy-Bank【完全背包】
题目链接Problem DescriptionBefore ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM)...原创 2018-09-13 10:04:43 · 147 阅读 · 0 评论 -
PAT甲级--1007 Maximum Subsequence Sum (25)(25 分)【最大子序列和及其起始和终点】
1007 Maximum Subsequence Sum (25)(25 分)Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A continuous subsequence is defined to be { N~i~, N~i+1~, ..., N~j~ } where 1 <= i <= j <= K...原创 2018-08-22 10:19:13 · 247 阅读 · 0 评论 -
zcmu--4922: 后缀子串排序【substr+字符串转为下标排序】
4922: 后缀子串排序Time Limit: 1 Sec Memory Limit: 32 MBSubmit: 8 Solved: 4[Submit][Status][Web Board]Description对于一个字符串,将其后缀子串进行排序,例如grain其子串有:grainrainaininn然后对各子串按字典顺序排序,即:ain,grain,in,n...原创 2018-08-27 20:40:46 · 464 阅读 · 0 评论 -
PAT甲级--1146 Topological Order(25 分)【判断是否为拓扑序列】
1146 Topological Order(25 分)This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topological order obtained from the given directed graph? Now you are suppo...原创 2018-08-28 10:16:19 · 1486 阅读 · 0 评论 -
PAT甲级--1003 Emergency (25)(25 分)
1003 Emergency (25)(25 分)As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue t...原创 2018-08-24 13:13:44 · 274 阅读 · 0 评论 -
zcmu-1624: 最短路【模板-最短路径】
1624: 最短路Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 279 Solved: 70[Submit][Status][Web Board]Description有n个城市编号为1—n,m条路,告诉你每条路的长度,求给定两点的最短路径长度。 InputT 组数据每组数据第一行有两个正整数n,m(n<=10...原创 2018-08-21 18:54:39 · 321 阅读 · 0 评论 -
PAT甲级--1114 Family Property(25 分)【并查集】
1114 Family Property(25 分)This time, you are supposed to help us collect the data for family-owned property. Given each person's family members, and the estate(房产)info under his/her own name, we nee...原创 2018-09-04 15:53:57 · 497 阅读 · 0 评论 -
PAT甲级--1107 Social Clusters(30 分)【并查集】
1107 Social Clusters(30 分)When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A social cluster is a set of ...原创 2018-09-04 19:12:37 · 560 阅读 · 0 评论 -
zcmu4940: 畅通工程 【并查集】
4940: 畅通工程Time Limit: 1 Sec Memory Limit: 32 MBSubmit: 4 Solved: 4[Submit][Status][Web Board]Description某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要...原创 2018-09-08 20:35:40 · 175 阅读 · 0 评论 -
超有爱的并查集~【真的超有趣】
例子就是杭电上的畅通工程:http://acm.hdu.edu.cn/showproblem.php?pid=1232首先在地图上给你若干个城镇,这些城镇都可以看作点,然后告诉你哪些对城镇之间是有道路直接相连的。最后要解决的是整幅图的连通性问题。比如随意给你两个点,让你判断它们是否连通,或者问你整幅图一共有几个连通分支,也就是被分成了几个互相独立的块。像畅通工程这题,问还需要修几条路,实质...转载 2018-09-03 19:20:20 · 257 阅读 · 0 评论 -
zcmu--4926: 还是畅通工程【最小生成树(Kruskal)+并查集】
4926: 还是畅通工程Time Limit: 1 Sec Memory Limit: 32 MBSubmit: 17 Solved: 12[Submit][Status][Web Board]Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路...原创 2018-09-09 10:56:14 · 209 阅读 · 0 评论 -
【计蒜客】2018ICPC徐州赛区网络赛H Ryuji doesn't want to study【树状数组】
题目链接Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]a[i]a[i].Unfortunately, the longer he learns, the fewer he get...原创 2018-09-12 10:34:54 · 150 阅读 · 0 评论 -
HDU--2602--Bone Collector 【01背包】
题目链接Problem DescriptionMany years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the ...原创 2018-09-12 16:15:43 · 123 阅读 · 0 评论 -
ACM-ICPC 2018 焦作赛区网络预赛
A. Magic MirrorJessie has a magic mirror.Every morning she will ask the mirror: 'Mirror mirror tell me, who is the most beautiful girl in the world?' If the mirror says her name, she will praise the...原创 2018-09-16 20:47:23 · 462 阅读 · 0 评论 -
CSU--1547: Rectangle【01背包】
1547: Rectangle Time Limit: 1 Sec Memory Limit: 256 Mb Submitted: 1281 Solved: 366 DescriptionNow ,there are some rectangles. The area of these rectangles is 1* x or 2 * ...原创 2018-09-12 18:39:52 · 139 阅读 · 0 评论 -
zcmu--1261: 采药【01背包】
1261: 采药Description 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师。为此,他想拜附近最有威望的医师为师。医师为了判断他的资质,给他出了一个难题。医师把他带到一个到处都是草药的山洞里对他说:“孩子,这个山洞里有一些不同的草药,采每一株都需要一些时间,每一株也有它自身的价值。我会给你一段时间,在这段时间里,你可以采到一些草药。如果你是一个聪明的孩子,你应该可以...原创 2018-09-17 09:27:08 · 215 阅读 · 0 评论