-
Notifications
You must be signed in to change notification settings - Fork 1
1-2 k-number #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
1-2 k-number #4
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,39 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| /* | ||
| 2. K번째 수 | ||
| https://programmers.co.kr/learn/courses/30/lessons/42748 | ||
| */ | ||
| class Solution { | ||
|
|
||
| public static void main(String[] args) { | ||
| Solution sol = new Solution(); | ||
|
|
||
| int[] arr = { 1, 5, 2, 6, 3, 7, 4 }; | ||
| int[][] commands = { { 2, 5, 3 }, { 4, 4, 1 }, { 1, 7, 3 } }; | ||
|
|
||
| System.out.println(Arrays.toString(sol.solution(arr, commands))); | ||
| } | ||
|
|
||
| public int[] solution(int[] array, int[][] commands) { | ||
| return null; | ||
|
|
||
| List<Integer> answer = new ArrayList<Integer>(); | ||
|
|
||
| for (int[] command : commands) { | ||
|
|
||
| int startIndex = command[0] - 1; | ||
| int endIndex = command[1]; | ||
| int pickIndex = command[2] - 1; | ||
|
|
||
| int[] commandCopy = Arrays.copyOfRange(array, startIndex, endIndex); | ||
| Arrays.sort(commandCopy); | ||
|
|
||
| answer.add(commandCopy[pickIndex]); | ||
| } | ||
|
|
||
| return answer.stream().mapToInt(i -> i).toArray(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. List를 배열로 만들기 위해 stream api를 거치는 것 보다 toArrray() 메서드를 이용하는게 좋을것 같습니다. |
||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| function solution(array = [], commands = [[], []]) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 클로저의 내부 변수는 외부에서 참조가 가능해지므로 가비지 컬렉션에 의해 사라지지 않아 함수의 실행 이후 지속적으로 참조가 가능합니다. 다만 자원을 점유하게 되는데요. |
||
| return commands.map((command) => { | ||
| const [startIndex, endIndex, pickIndex] = command; | ||
|
|
||
| return array.slice(startIndex - 1, endIndex).sort((a, b) => a - b)[ | ||
| pickIndex - 1 | ||
| ]; | ||
| }); | ||
| } | ||
|
|
||
| console.log( | ||
| solution( | ||
| [1, 5, 2, 6, 3, 7, 4], | ||
| [ | ||
| [2, 5, 3], | ||
| [4, 4, 1], | ||
| [1, 7, 3], | ||
| ] | ||
| ) | ||
| ); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
만들어질 배열의 길이를 알 수 있는데 List를 사용할 필요가 있을까요