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
- leetcode 1721
- 리트코드
- 프로그래머스
- 프로그래머스 java
- leetcode
- dfs
- 자바 리트코드
- 파이썬
- 스택
- 자바
- java 프로그래머스
- java leetcode
- 구현
- 스프링 에러
- 코딩테스트
- 분할정복
- Java
- 백준 16935
- daily challenge
- 백준
- BFS
- 코테
- 카카오
- 그래프 자바
- 리트코드 자바
- DP
- 자바 5464
- 리트코드 1557
- 백준 18222
- 인텔리제이 에러
Archives
- Today
- Total
레벨업 일지
[Java] leetcode 1626. Best Team With No Conflicts 본문
문제
https://leetcode.com/problems/best-team-with-no-conflicts/
Given two lists, scores and ages, where each scores[i] and ages[i] represents the score and age of the ith player, respectively, return the highest overall score of all possible basketball teams.
알아야 할 개념
- dfs 메모이제이션
풀이
풀이는 다음과 같다.
- ArrayList에 n번째 플레이어의 정보[score, age] 를 저장한다.
- age 순으로 오름차순 정렬 한다.
- 같은 age이면 score 순 오름차순 정렬
- 재귀 함수를 만들어서 각 n번째 플레이어를 넣을지 말지 2가지 행동을 한다.
- 메모이제이션으로 ( 현재 n번째 플레이어, max 플레이어 ) 값을 저장해 중복 탐색을 피한다.
- 답을 리턴한다.
재귀함수의 해당 레벨에서 값을 구하면, (해당 인덱스와, max 인덱스) 위치의 값을 저장해야 중복 탐색을 피할 수 있다.
prev 는 이전 max 인덱스 위치 를 나타내고 초기값을 -1 로 지정하기 때문에, 액세스할때 + 1 을 해준다.
코드
자바
class Solution {
int memo[][];
public int bestTeamScore(int[] s, int[] age) {
List<int[]> l = new ArrayList<>();
int n = s.length;
memo = new int[n][n];
for(int i = 0 ;i < n ;i++){
l.add(new int[]{age[i],s[i]});
}
l.sort((a,b)->a[0]-b[0]==0 ? a[1]-b[1]: a[0]-b[0]);
return dfs(l,0,-1);
}
public int dfs(List<int[]> l , int cur, int prev){
if(cur > l.size()-1)return 0;
if(memo[cur][prev+1]>0)return memo[cur][prev+1];
if(prev==-1 || l.get(prev)[1] <= l.get(cur)[1] ){
return memo[cur][prev+1] = Math.max(dfs(l,cur+1, cur)+l.get(cur)[1], dfs(l,cur+1,prev));
}
return memo[cur][prev+1] = dfs(l,cur+1,prev);
}
}
'알고리즘 > leetcode' 카테고리의 다른 글
[Java] leetcode 1523. Count Odd Numbers in an Interval Range (0) | 2023.02.13 |
---|---|
[Java] leetcode 1626. Best Team With No Conflicts (0) | 2023.02.12 |
[Java/Python] leetcode 1137. N-th Tribonacci Number (0) | 2023.01.31 |
[Java] leetcode 352. Data Stream as Disjoint Intervals (0) | 2023.01.28 |
[Java] leetcode 787. Cheapest Flights Within K Stops (0) | 2023.01.27 |
Comments