Problem Solving with Algorithms

반응형

[100%] FrogRiverOne

Find the earliest time when a frog can jump to the other side of a river.
Respectable


[100%] MaxCounters

Calculate the values of counters after applying all alternating operations: increase counter by 1; set value of all counters to current maximum.
Respectable

 

[100%] MissingInteger

Find the smallest positive integer that does not occur in a given sequence.
Painless

 

// you can also use imports, for example:
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[] A) {
        Arrays.sort(A);
        if(A[A.length-1] < 0) return 1;
        int ans = 0;
        int prev = 0;
        for(int i = 0; i < A.length; i++) {
            if(A[i] < 0) continue;
            if(A[i]-prev > 1) {
                ans = prev+1;
                break;
            }
            prev = A[i];
        }
        if(ans == 0) {
            ans = A[A.length-1]+1;
        }
        return ans;
    }
}

 

 

import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

class Solution {
    public int solution(int[] A) {
ArrayList<Integer> numbers = IntStream.of(A).boxed().filter(x->x>0).sorted().distinct().collect(Collectors.toCollection(ArrayList<Integer>::new));
		if (numbers.size() == 0)
			return 1;
		for (int i = 0; i < numbers.size(); i++) {
			if (numbers.get(i) != i+1)
				return i+1;
		}
		return numbers.size()+1;
    }
}

이건 시도는 좋았으나 타임아웃에러남


▶large_2shuffled sequence 1, 2, ..., 100000 (without minus)✘TIMEOUT ERRORrunning time: 1.272 sec., time limit: 1.024 sec.
1.1.272 sTIMEOUT ERROR, running time: 1.272 sec., time limit: 1.024 sec.2.1.240 sTIMEOUT ERROR, running time: 1.240 sec., time limit: 1.008 sec.
 

 

 

 

[100%] PermCheck

Check whether array A is a permutation.

반응형
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band