Codewars里的 5kyu Kata。

题目说明:

In this kata, you will write a function that returns the positions and the values of the "peaks" (or local maxima) of a numeric array.

For example, the array arr = [0, 1, 2, 5, 1, 0] has a peak at position 3 with a value of 5(since arr[3] equals 5).

The output will be returned as a ``Map<String,List>with two key-value pairs:"pos"and"peaks". If there is no peak in the given array, simply return{"pos" => [], "peaks" => []}`.

Example: pickPeaks([3, 2, 3, 6, 4, 1, 2, 3, 2, 1, 2, 3]) should return {pos: [3, 7], peaks: [6, 3]} (or equivalent in other languages)

All input arrays will be valid integer arrays (although it could still be empty), so you won't need to validate the input.

The first and last elements of the array will not be considered as peaks (in the context of a mathematical function, we don't know what is after and before and therefore, we don't know if it is a peak or not).

Also, beware of plateaus !!! [1, 2, 2, 2, 1] has a peak while [1, 2, 2, 2, 3] does not. In case of a plateau-peak, please only return the position and value of the beginning of the plateau. For example: pickPeaks([1, 2, 2, 2, 1]) returns {pos: [1], peaks: [2]} (or equivalent in other languages)

Have fun!

解题代码:

import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;public class PickPeaks {public static Map<String,List<Integer>> getPeaks(int[] arr) {// Your code here!Map<String, List<Integer>> pos_peaks = new HashMap<String, List<Integer>>();List<Integer> pos = new ArrayList<Integer>();List<Integer> peaks = new ArrayList<Integer>();for(int i = 0; i<arr.length; i++) {if(i == 0 || i == arr.length-1) {continue;}else if(arr[i] > arr[i-1] && arr[i] > arr[i+1]) {pos.add(i);peaks.add(arr[i]);}else if(arr[i] > arr[i-1] && arr[i] == arr[i+1]) {for(int j = i; j<arr.length; j++) {if(arr[j] < arr[i]) {pos.add(i);peaks.add(arr[i]);}if(arr[j] != arr[i]) {i = j-1;break;}}}}pos_peaks.put("pos", pos);pos_peaks.put("peaks", peaks);return pos_peaks;}
}

Test Cases:

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
import java.util.*;
import java.util.stream.Collectors;public class SolutionTest {private static String[] msg = { "should support finding peaks","should support finding peaks, but should ignore peaks on the edge of the array","should support finding peaks; if the peak is a plateau, it should only return the position of the first element of the plateau","should support finding peaks; if the peak is a plateau, it should only return the position of the first element of the plateau","should support finding peaks, but should ignore peaks on the edge of the array","should support finding peaks, but should ignore peaks on the edge of the array","should support finding peaks, despite the plateau", "should support finding peaks","should return an object with empty arrays if the input is an empty array","should return an object with empty arrays if the input does not contain any peak" };private static int[][] array = { { 1, 2, 3, 6, 4, 1, 2, 3, 2, 1 }, { 3, 2, 3, 6, 4, 1, 2, 3, 2, 1, 2, 3 },{ 3, 2, 3, 6, 4, 1, 2, 3, 2, 1, 2, 2, 2, 1 }, { 2, 1, 3, 1, 2, 2, 2, 2, 1 }, { 2, 1, 3, 1, 2, 2, 2, 2 },{ 2, 1, 3, 2, 2, 2, 2, 5, 6 }, { 2, 1, 3, 2, 2, 2, 2, 1 },{ 1, 2, 5, 4, 3, 2, 3, 6, 4, 1, 2, 3, 3, 4, 5, 3, 2, 1, 2, 3, 5, 5, 4, 3 }, {}, { 1, 1, 1, 1 } };private static int[][] posS = { { 3, 7 }, { 3, 7 }, { 3, 7, 10 }, { 2, 4 }, { 2 }, { 2 }, { 2 }, { 2, 7, 14, 20 },{}, {} };private static int[][] peaksS = { { 6, 3 }, { 6, 3 }, { 6, 3, 2 }, { 3, 2 }, { 3 }, { 3 }, { 3 }, { 5, 6, 5, 5 },{}, {} };@Testpublic void sampleTests() {for (int n = 0; n < 5; n++) {final int[] p1 = posS[n], p2 = peaksS[n];Map<String, List<Integer>> expected = new HashMap<String, List<Integer>>() {{put("pos", Arrays.stream(p1).boxed().collect(Collectors.toList()));put("peaks", Arrays.stream(p2).boxed().collect(Collectors.toList()));}}, actual = PickPeaks.getPeaks(array[n]);assertEquals(msg[n], expected, actual);}}@Testpublic void moreTests() {for (int n = 5; n < msg.length; n++) {final int[] p1 = posS[n], p2 = peaksS[n];Map<String, List<Integer>> expected = new HashMap<String, List<Integer>>() {{put("pos", Arrays.stream(p1).boxed().collect(Collectors.toList()));put("peaks", Arrays.stream(p2).boxed().collect(Collectors.toList()));}}, actual = PickPeaks.getPeaks(array[n]);assertEquals(msg[n], expected, actual);}}private Random rand = new Random();@Testpublic void randomTests() {for (int n = 0; n < 40; n++) {int[] arr = new int[5 + rand.nextInt(25)];for (int i = 0; i < arr.length; i++)arr[i] = -5 + rand.nextInt(25);Map<String, List<Integer>> expected = SolPeaks.getPeaks(arr), actual = PickPeaks.getPeaks(arr);assertEquals(String.format("Testing for %s:", Arrays.toString(arr)), expected, actual);}}private static class SolPeaks {protected static Map<String, List<Integer>> getPeaks(int[] arr) {Map<String, List<Integer>> ans = new HashMap<String, List<Integer>>() {{put("pos", new ArrayList<Integer>());put("peaks", new ArrayList<Integer>());}};int posMax = 0;boolean matchAsc = false;for (int i = 1; i < arr.length; i++) {if (arr[i - 1] < arr[i]) {matchAsc = true;posMax = i;}if (matchAsc && arr[i - 1] > arr[i]) {matchAsc = false;ans.get("pos").add(posMax);ans.get("peaks").add(arr[posMax]);}}return ans;}}}

个人总结:

import java.util.*;public class PickPeaks {public static Map<String, List<Integer>> getPeaks(int[] arr) {Map<String, List<Integer>> ans = new HashMap<String, List<Integer>>() {{put("pos", new ArrayList<Integer>());put("peaks", new ArrayList<Integer>());}};int posMax = 0;boolean matchAsc = false;for (int i = 1; i < arr.length; i++) {if (arr[i - 1] < arr[i]) {matchAsc = true;posMax = i;}if (matchAsc && arr[i - 1] > arr[i]) {matchAsc = false;ans.get("pos").add(posMax);ans.get("peaks").add(arr[posMax]);}}return ans;}
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;public class PickPeaks {public static Map<String, List<Integer>> getPeaks(int[] arr) {ArrayHelper arrayHelper = new ArrayHelper(arr);HashMap<String, List<Integer>> posPeaksMap = new HashMap<>();List<Integer> pos = IntStream.range(1, arr.length).filter(arrayHelper::isPeak).boxed().collect(Collectors.toList());List<Integer> peaks = pos.stream().map(i -> arr[i]).collect(Collectors.toList());posPeaksMap.put("pos", pos);posPeaksMap.put("peaks", peaks);return posPeaksMap;}private static class ArrayHelper {private final int[] array;private ArrayHelper(int[] array) {this.array = array;}boolean isPeak(int index) {return isBiggerThanDirectPredecessor(index) && isBiggerThanAnySuccessor(index);}private boolean isBiggerThanDirectPredecessor(int index) {return array[index] > array[index - 1];}private boolean isBiggerThanAnySuccessor(int index) {int value = array[index];for (int succeedingIndex = index + 1; succeedingIndex < array.length; succeedingIndex++) {int succeedingValue = array[succeedingIndex];if (value == succeedingValue) {continue;}return value > succeedingValue;}return false;}}}
import java.util.*;public class PickPeaks {public static Map<String, List<Integer>> getPeaks(int[] array) {List<Integer> maxima = new ArrayList<>();List<Integer> positions = new ArrayList<>();Map<String, List<Integer>> map = new HashMap<>();// Input array must have atleast 3 elements in order to find a local maxima.if (array.length < 3) {map.put("peaks", maxima);map.put("pos", positions);return map;}int front, middle = array[0], end = array[1], index = 2, plateauStartPoint = 1, plateauEndPoint,dupeCounter = 0, dupeValue = array[1];while (index < array.length) {front = middle;middle = end;end = array[index];if (dupeValue == array[index])dupeCounter++;else {dupeCounter = 0;plateauStartPoint = index;}dupeValue = array[index];// Comparing the middle value with it's neighbours to see if it is the local// maxima.if (middle > front && middle > end) {maxima.add(middle);positions.add(index - 1);}/** DupeCounter will only ever register as more than 0 if a consectutive* duplicate occur. If a consectutive duplicate is detected then it could imply* a plateau.*/if (dupeCounter >= 1) {/** Set the end of the suspected plateau as the index of the next element if* there is one else set as end of array*/plateauEndPoint = ((index + 1) >= (array.length)) ? index : (index + 1);/** The start of the plateau will be index of the first element of the* consectutive duplicates. all the duplicates are treated as one and checked* against it's immediate neighbours.*/if (array[plateauStartPoint - 1] < dupeValue && array[plateauEndPoint] < dupeValue) {maxima.add(dupeValue);positions.add(plateauStartPoint);dupeCounter = 0;}}index++;}map.put("peaks", maxima);map.put("pos", positions);return map;}
}
import static java.util.stream.Collectors.toList;import java.util.ArrayList;
import java.util.Map;
import java.util.List;public class PickPeaks {public static Map<String, List<Integer>> getPeaks(int[] arr) {List<Integer> pos = new ArrayList<>();boolean ascending = false;int localPeakPos = -1;for (int i = 1; i < arr.length; i++) {if (arr[i] > arr[i - 1]) {ascending = true;localPeakPos = i;}if (arr[i] < arr[i - 1] && ascending) {pos.add(localPeakPos);ascending = false;}}return Map.of("pos", pos, "peaks", pos.stream().map(i -> arr[i]).collect(toList()));}
}
def pick_peaks(arr):peak, pos = [], []res = { "peaks":[], "pos":[] }for i in range(1, len(arr)) :if arr[i]>arr[i-1] :peak, pos = [arr[i]], [i]elif arr[i]<arr[i-1] :res["peaks"] += peakres["pos"] += pospeak, pos = [], []return res

一个压栈出栈的思路。

【Codewars】Pick peaks相关推荐

  1. 【总结】pick定理Farey序列

    pick定理及其证明 pick定理 结论 在一个平面直角坐标系内,以整点为顶点的简单多边形,设其内部整点数为aaa,边上(包括顶点)的整点数为bbb,则它的面积S=a+b2−1S = a+\frac ...

  2. 【CodeWars】 Pete, the baker

    CodeWars 里的5kyu题目 Pete,the baker 题目说明: Description: Pete likes to bake some cakes. He has some recip ...

  3. 【Codewars】Simple Pig Latin

    Codewars里的 5kyu Kata. 题目说明: Move the first letter of each word to the end of it, then add "ay&q ...

  4. 【Codewars】All Inclusive?

    Codewars里的 7kyu Kata. 题目说明: Description: Input: a string strng an array of strings arr Output of fun ...

  5. 【Codewars】<7kyu> Vowel Count

    一.题目: Return the number (count) of vowels in the given string. 这道题要实现的是返回字符串中的元音个数(a,e,i,o,u) 二.例子: ...

  6. 【Codewars】7×7 摩天大楼

    介绍 链接:7×7 Skyscrapers C#答案(原因:懒,但是完全可以转成C++):bajdcc/learnstl 题目(机翻): 在7乘7格的网格中,你只想在每个广场上放置一个摩天大楼,只有一 ...

  7. 【完结】中国12大AI研究院,高调的低调的你pick谁

    文章首发于微信公众号<有三AI> [完结]中国12大AI研究院,高调的低调的你pick谁 专栏国内的<AI研究院>正式完结了,在这个专题中,我们和大家一起分享了国内12大研究院 ...

  8. Linux内核跟踪之trace框架分析【转】

    转自:http://blog.chinaunix.net/uid-20543183-id-1930846.html ------------------------------------------ ...

  9. Linux内核抢占实现机制分析【转】

    Linux内核抢占实现机制分析 转自:http://blog.chinaunix.net/uid-24227137-id-3050754.html [摘要]本文详解了Linux内核抢占实现机制.首先介 ...

  10. Git - Tutorial官方【转】

    转自:http://www.vogella.com/tutorials/Git/article.html#git_rename_branch Lars Vogel Version 5.8 Copyri ...

最新文章

  1. python自动控制windows、Android的软件用来实现机器人流程自动化--RPA,外挂、自动化测试等方面的解决方案
  2. 实习就参与“服务过亿用户的项目”,是什么体验?
  3. how is Bean object created
  4. mysql 查询系统_使用select和show命令查看mysql数据库系统信息
  5. Hadoop集群安装部署_分布式集群安装_02
  6. cdn转发防攻击_一个既可以加速又可以防御的CDN产品
  7. 原生开发什么意思_什么是原生开发?什么是混合开发?两者有什么区别?
  8. ios 中文输入法 完成事件_让聊天更方便 百度输入法开启AI助聊功能
  9. [导入]设置wap服务器
  10. Kubernetes系列——Kubernetes 组件、对象(二)
  11. synchronized关键词保证线程安全
  12. 串口服务器是什么,有什么功能
  13. 周围剃光头顶留长发型_为什么很多秃顶的人,宁可留周围一圈头发,也不直接剃成光头?...
  14. lamp mysql 密码_lamp 如何修改mysql密码
  15. macOS下快速复制文件或文件夹路径
  16. 删了手机里的一个html文件,手机操作篇:手机上怎么删除pdf其中一页
  17. 1.试用期个人工作总结(篇一)
  18. DIAC-WOZ数据集(二)---Visual signals
  19. 蘑菇丁工学云打卡教程
  20. iPad 3 即将发布,网传价格,IPD2降价50$ 新功能猜测

热门文章

  1. Matplotlib绘制春节休假表
  2. 机器学习之集成学习(七)随机森林scikit-learn库
  3. 利用API爬取QQ音乐评论
  4. tfp.distributions.Multinomial().sample()用法简介
  5. 回溯法实现求解子集合和问题
  6. 力扣LeetCode刷题笔记总结1
  7. 封装HDLC协议实现两个路由器互相通信
  8. 英文版-恩光照耀-Shine Jesus Shine
  9. 一张照片让你的安卓手机崩溃
  10. hdu5442 后缀数组