배열 array의 i번째 숫자부터 j번째 숫자까지 자르고 정렬했을 때, k번째에 있는 수를 구하려 합니다.
예를 들어 array가 [1, 5, 2, 6, 3, 7, 4], i = 2, j = 5, k = 3이라면
array의 2번째부터 5번째까지 자르면 [5, 2, 6, 3]입니다.
1에서 나온 배열을 정렬하면 [2, 3, 5, 6]입니다.
2에서 나온 배열의 3번째 숫자는 5입니다.
배열 array, [i, j, k]를 원소로 가진 2차원 배열 commands가 매개변수로 주어질 때,
commands의 모든 원소에 대해 앞서 설명한 연산을 적용했을 때
나온 결과를 배열에 담아 return 하도록 solution 함수를 작성해주세요.
array의 길이는 1 이상 100 이하입니다.
array의 각 원소는 1 이상 100 이하입니다.
commands의 길이는 1 이상 50 이하입니다.
commands의 각 원소는 길이가 3입니다.
array | commands | return |
---|---|---|
[1, 5, 2, 6, 3, 7, 4] | [[2, 5, 3], [4, 4, 1], [1, 7, 3]] | [5, 6, 3] |
[1, 5, 2, 6, 3, 7, 4]를 2번째부터 5번째까지 자른 후 정렬합니다. [2, 3, 5, 6]의 세 번째 숫자는 5입니다.
[1, 5, 2, 6, 3, 7, 4]를 4번째부터 4번째까지 자른 후 정렬합니다. [6]의 첫 번째 숫자는 6입니다.
[1, 5, 2, 6, 3, 7, 4]를 1번째부터 7번째까지 자릅니다. [1, 2, 3, 4, 5, 6, 7]의 세 번째 숫자는 3입니다.
import java.util.Arrays;
public class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
for (int i = 0; i < answer.length; i++) {
int[] command = commands[i];
int from = command[0] - 1; // 0-base
int to = command[1]; // exclusive
int k = command[2] - 1; // 0-base
int[] sub = Arrays.copyOfRange(array, from, to);
Arrays.sort(sub);
answer[i] = sub[k];
}
return answer;
}
}
def solution(array, commands):
answer = []
for i, j, k in commands:
new_arr=array[i-1:j]
new_arr.sort()
answer.append(new_arr[k-1])
return answer
0 또는 양의 정수가 주어졌을 때, 정수를 이어 붙여 만들 수 있는 가장 큰 수를 알아내 주세요.
예를 들어, 주어진 정수가 [6, 10, 2]라면
[6102, 6210, 1062, 1026, 2610, 2106]를 만들 수 있고, 이중 가장 큰 수는 6210입니다.
0 또는 양의 정수가 담긴 배열 numbers가 매개변수로 주어질 때,
순서를 재배치하여 만들 수 있는 가장 큰 수를 문자열로 바꾸어 return 하도록
solution 함수를 작성해주세요.
numbers의 길이는 1 이상 100,000 이하입니다.
numbers의 원소는 0 이상 1,000 이하입니다.
정답이 너무 클 수 있으니 문자열로 바꾸어 return 합니다.
numbers | return |
---|---|
[6, 10, 2] | "6210" |
[3, 30, 34, 5, 9] | "9534330" |
import java.util.Arrays;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
for (int i = 0; i < answer.length; i++) {
int[] command = commands[i];
int from = command[0] - 1;
int to = command[1];
int k = command[2] - 1;
int[] sub = Arrays.copyOfRange(array, from, to);
Arrays.sort(sub);
answer[i] = sub[k];
}
return answer;
}
}
def solution(numbers):
numbers = sorted(list(map(str, numbers)), key=lambda x:x*3, reverse=True)
return str(int(''.join(numbers)))
문제 설명
정수 배열 numbers가 주어집니다. numbers에서 서로 다른 인덱스에 있는 두 개의 수를 뽑아 더해서 만들 수 있는 모든 수를 배열에 오름차순으로 담아 return 하도록 solution 함수를 완성해주세요.
제한사항
numbers의 길이는 2 이상 100 이하입니다.
numbers의 모든 수는 0 이상 100 이하입니다.
입출력 예
numbers | result |
---|---|
[2,1,3,4,1] | [2,3,4,5,6,7] |
[5,0,2,7] | [2,5,7,9,12] |
import java.util.Set;
import java.util.HashSet;
class Solution {
public int[] solution(int[] numbers) {
Set<Integer> set = new HashSet<>();
for (int i = 0; i < numbers.length; i++) {
for (int j = i + 1; j < numbers.length; j++) {
set.add(numbers[i] + numbers[j]);
}
}
return set.stream().mapToInt(Integer::intValue).sorted().toArray();
}
}
from itertools import combinations
def solution(numbers):
answer = set()
selects = list(combinations(numbers, 2))
for select in selects:
(a, b) = select
answer.add(a + b)
return sorted(answer)