ABOUT ME

오늘 방문자
어제 방문자
전체
  • [Codility]MaxCounters
    알고리즘 2019. 5. 6. 21:27

    문제출처

     https://app.codility.com/programmers/lessons/4-counting_elements/max_counters/


    문제요약

     연산방법 2가지가 주어짐

     increase  :  배열 요소 값중 일부거 1증가

     max counter : 배열요소 값중 가장큰 값으로 모든 배열 요소를 치환



    코드

    44점 정답률100%, 퍼포먼스 모두 time out

    https://app.codility.com/demo/results/trainingT3R5JY-QZA/


    아무래도 stream이 문제인거 같음..


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    import java.util.*;
     
    class Solution {
        public int[] solution(int N, int[] A) {
           int[] arr = new int[N];
     
            int max = 0 ;
     
            List<Integer> list ;
     
            for(int i=0 ;i<A.length;i++){
     
                if(1<=A[i] && A[i]<=N){
                    arr[A[i]-1++;
                    list = Arrays.stream(arr).boxed().collect(Collectors.toList());
                    max = (int)Collections.max(list);
                }else{
                    Arrays.fill(arr, max);
                }
            }
     
            return arr;
        }
    }
    cs



    100점

    https://app.codility.com/demo/results/trainingH7XYK4-AXN/


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    import java.util.*;
     
    // you can write to stdout for debugging purposes, e.g.
    // System.out.println("this is a debug message");
     
    class Solution {
        public int[] solution(int N, int[] A) {
            int[] result = new int[N];
            int max = 0// increase 연산시 최대값을 저장할 변수
            int allMax = 0// max 연산시 max를 여기에 넣을 거임
     
            for (int i = 0; i < A.length; i++) {
                if (A[i] == N + 1) {    // max
                    allMax = max; // 최대값을 저장해 놓음
                } else {    //increase
                    result[A[i]-1= Math.max(allMax, result[A[i]-1]) + 1// 1씩 증가
                    max = Math.max(max, result[A[i]-1]);
                }
            }
     
            for (int i = 0; i < N; i++) {
                result[i] = Math.max(result[i], allMax);
            }
            return result;
        }
    }
    cs


    '알고리즘' 카테고리의 다른 글

    댓글

Designed by Tistory.