Pick peaks

algorithms 2018. 1. 4. 13:06

codewars 4kyu짜리 문제.


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<integer>> 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.HashMap;
import java.util.List;
import java.util.ArrayList;

public class PickPeaks {
    
    public static Map<String,List<Integer>> getPeaks(int[] arr) {
        Map<String,List<Integer>> result = new HashMap<String, List<Integer>>();
        
        List<Integer> pos = new ArrayList<Integer>();
        List<Integer> peaks = new ArrayList<Integer>();
        
        for(int i=1; i<arr.length-1; i++){
            //Search peaks
            if(arr[i-1] < arr[i]){
                if(arr[i] > arr[i+1]){
                    pos.add(i);
                    peaks.add(arr[i]);
                }else if(arr[i] == arr[i+1]){
                    for(int j=i; j<arr.length-1; j++){
                        if(arr[j] == arr[j+1]){ continue; }
                        if(arr[j] < arr[j+1]){ break; }
                        if(arr[j] > arr[j+1]){
                            pos.add(i);
                            peaks.add(arr[i]);
                            break;
                        }
                    }
                }
            }//end of if
        }//end of for i
        
        result.put("pos", pos);
        result.put("peaks", peaks);
        
        return result;
    }
}

베스트 솔루션

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;
    }
}


미친 존나깔끔해

'algorithms' 카테고리의 다른 글

설탕 배달 문제  (0) 2018.02.23
Twice Linear  (0) 2018.01.08
정규식으로 필요한 문자열만 빼오기  (0) 2017.11.13
Mod4 Regex (Java)  (0) 2017.11.10
Regex for binary multiple of 3 (Java)  (0) 2017.11.07
Posted by 아마루아00
,