package answer;
import java.util.ArrayList;
import java.util.Arrays;
public class Solution {
public int[] solution(int[] lottos, int[] win_nums) {
int bestcnt = 0;
ArrayList<Integer> best = new ArrayList<>();
ArrayList<Integer> worst = new ArrayList<>();
Arrays.sort(lottos);
Arrays.sort(win_nums);
// best, worst List 삽입
for (int i = 0; i < lottos.length; i++) {
if (lottos[i] != 0) {
best.add(lottos[i]);
}
for (int j = 0; j < win_nums.length; j++) {
if (lottos[i] == win_nums[j]) {
worst.add(lottos[i]);
bestcnt++;
break;
}
}
}
//best 근사치 값 삽입
for (int j = 0; j < win_nums.length; j++) {
if (!best.contains(win_nums[j]) && best.size() < 6) {
best.add(win_nums[j]);
bestcnt++;
}
}
//등수 계산, 6등 초과시 6등으로 고정
int[] answer = { 7 - bestcnt, 7 - worst.size() };
if (answer[0] > 6)
answer[0] = 6;
if (answer[1] > 6)
answer[1] = 6;
return answer;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution s = new Solution();
int lottos[] = { 0, 0, 0, 0, 0, 0 };
int win_nums[] = { 38, 19, 20, 40, 15, 25 };
System.out.println(s.solution(lottos, win_nums));
}
}
import java.util.HashMap;
import java.util.Map;
class Solution {
public int[] solution(int[] lottos, int[] win_nums) {
Map<Integer, Boolean> map = new HashMap<Integer, Boolean>();
int zeroCount = 0;
for(int lotto : lottos) {
if(lotto == 0) {
zeroCount++;
continue;
}
map.put(lotto, true);
}
int sameCount = 0;
for(int winNum : win_nums) {
if(map.containsKey(winNum)) sameCount++;
}
int maxRank = 7 - (sameCount + zeroCount);
int minRank = 7 - sameCount;
if(maxRank > 6) maxRank = 6;
if(minRank > 6) minRank = 6;
return new int[] {maxRank, minRank};
}
}
답안 :
SELECT HISTORY_ID, CAR_ID, date_format(START_DATE,'%Y-%m-%d') as 'START_DATE', date_format(END_DATE,'%Y-%m-%d') as 'END_DATE',
case when DATEDIFF(END_DATE,START_DATE)+1 >=30 then '장기 대여'
else '단기 대여' end as 'RENT_TYPE'
from CAR_RENTAL_COMPANY_RENTAL_HISTORY
where date_format(START_DATE,'%Y-%m')='2022-09'
order by 1 desc
DATE 간의 차이를 구하는 DATEDIFF()를 사용하면 간단히 해결할수 있음.
DATEDIFF(Start_Date, End_DATE) : 시작날짜와 끝날짜의 날짜수 구하기
END_DATE - START_DATE를 하게되면 날짜계산이 아닌 숫자계산이 되버리기 때문에 31을 초과하는 숫자가 나올시 따로 정의해줘야함.
답안 :
SELECT car_id,ROUND(AVG(DATEDIFF(END_DATE,START_DATE)+1),1) as 'AVERAGE_DURATION'
from CAR_RENTAL_COMPANY_RENTAL_HISTORY
group by 1
Having 7<=AVERAGE_DURATION
order by 2 desc, 1 desc