반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 27 | 28 | 29 | 30 | 31 |
Tags
- springbatch error
- java 여러개 버전
- No tests found for given includes
- log error
- JUnit
- java 1.8 11
- maybe not public or not valid?
- Medium
- OpenFeign
- aws
- Java 1.8
- java 11
- parse
- AWS CLI
- property or field 'jobparameters' cannot be found on object of type
- el1008e
- java 버전 변경
- java version
- xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools)
- java
- yum install java
- no sources given
- LeetCode
- querydsl no sources given
- error
- easy
- 스프링부트테스트
- mac os git error
- springboot
- springboottest
Archives
- Today
- Total
쩨이엠 개발 블로그
[ Leetcode ] 1409. Queries on a Permutation With Key - Java 본문
728x90
반응형
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules:
- In the beginning, you have the permutation P=[1,2,3,...,m].
- For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i].
Return an array containing the result for the given queries.
Example 1:
Input: queries = [3,1,2,1], m = 5
Output: [2,1,2,1]
Explanation: The queries are processed as follow:
For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2,
then we move 3 to the beginning of P resulting in P=[3,1,2,4,5].
For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1,
then we move 1 to the beginning of P resulting in P=[1,3,2,4,5].
For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2,
then we move 2 to the beginning of P resulting in P=[2,1,3,4,5].
For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1,
then we move 1 to the beginning of P resulting in P=[1,2,3,4,5].
Therefore, the array containing the result is [2,1,2,1].
Example 2:
Input: queries = [4,1,2,2], m = 4
Output: [3,1,2,0]
Example 3:
Input: queries = [7,5,5,8,3], m = 8
Output: [6,5,0,7,5]
Constraints:
- 1 <= m <= 10^3
- 1 <= queries.length <= m
- 1 <= queries[i] <= m
Solution
import java.util.*;
class Solution {
public int[] processQueries(int[] queries, int m) {
int[] answer = new int[queries.length];
List<Integer> P = new ArrayList<>();
for(int i=1; i<= m;i++){
P.add(i);
}
for(int i=0; i< queries.length; i++){
int value = queries[i];
int order = P.indexOf(value);
answer[i] = order;
P.remove(order);
P.add(0, value);
}
return answer;
}
}
1. P를 만들어준다 1부터 m까지
2. 값에 대한 순서를 찾아서 answer에 넣고 맨 앞으로 꺼낸다.
++
import java.util.*;
class Solution {
public int[] processQueries(int[] queries, int m) {
int length = queries.length;
int[] answer = new int[length];
List<Integer> P = new ArrayList<>();
for(int i=1; i<= m;i++){
P.add(i);
}
for(int i=0; i< length; i++){
int value = queries[i];
int order = P.indexOf(value);
answer[i] = order;
P.remove(order);
P.add(0, value);
}
return answer;
}
}
queries.length를 미리 length에 담으면 메모리는 좀 더 줄어든다 !
728x90
반응형
'개발 > Programming' 카테고리의 다른 글
[ Leetcode ] 1630. Arithmetic Subarrays - Java (0) | 2021.02.02 |
---|---|
[ Leetcode ] 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers - Java (0) | 2021.01.30 |
[ Leetcode ] 1476. Subrectangle Queries - Java (0) | 2021.01.29 |
[ Leetcode ] 1470. Shuffle the Array - Java (0) | 2021.01.29 |
[ Leetcode ] 1672. Richest Customer Wealth - Java (0) | 2021.01.28 |
Comments