답안 :
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Solution {
public int[] solution(String today, String[] terms, String[] privacies) {
List<Integer> list = new ArrayList<>();
Map<Character, Integer> map = new HashMap<>();
StringBuffer sb = new StringBuffer();
String[] todayPrats = today.split("\\.");
int todayYear = Integer.parseInt(todayPrats[0]);
int todayMonth = Integer.parseInt(todayPrats[1]);
int todayDay = Integer.parseInt(todayPrats[2]);
// map 약관맵핑
for (String item : terms) {
char c = item.charAt(0);
String tempMonth = item.substring(2, item.length());
int x = Integer.parseInt(tempMonth);
map.put(c, x);
}
for (int i = 0; i < privacies.length; i++) {
String temp = privacies[i].substring(0, privacies[i].length() - 2);
String[] privaciesParts = temp.split("\\.");
int privaciesYear = Integer.parseInt(privaciesParts[0]);
int privaciesMonth = Integer.parseInt(privaciesParts[1]);
int privaciesDay = Integer.parseInt(privaciesParts[2]);
int validityPeriod = map.get(privacies[i].charAt(privacies[i].length() - 1));
privaciesMonth += validityPeriod;
while (privaciesMonth >= 13) {
privaciesMonth -= 12;
privaciesYear++;
}
if (todayYear > privaciesYear) {
list.add(i + 1);
continue;
} else if (todayYear == privaciesYear && todayMonth > privaciesMonth) {
list.add(i + 1);
continue;
} else if (todayYear == privaciesYear && todayMonth == privaciesMonth && todayDay >= privaciesDay) {
list.add(i + 1);
continue;
}
}
int[] answer = list.stream().mapToInt(Integer::intValue).toArray();
return answer;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution s = new Solution();
String today = "2020.10.15";
String[] terms = { "A 100" };
String[] privacies = { "2018.06.16 A","2008.02.15 A" };
s.solution(today, terms, privacies);
}
}
String.spilt() 메소드를 통해 분리하여 년도,월,일자 기준으로 저장틀린답안 :
package answer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Solution {
public int[] solution(String today, String[] terms, String[] privacies) {
List<Integer> list = new ArrayList<>();
Map<Character, Integer> map = new HashMap<>();
StringBuffer sb = new StringBuffer();
today = today.replace(".", "");
// map 약관맵핑
for (String item : terms) {
char c = item.charAt(0);
String tempMonth = item.substring(2, item.length());
int x = Integer.parseInt(tempMonth);
map.put(c, x);
}
for (int i = 0; i < privacies.length; i++) {
char term = privacies[i].charAt(privacies[i].length() - 1);
String temp = privacies[i].substring(0, privacies[i].length() - 2);
String[] words1 = temp.split("\\.");
int month = map.get(term) + Integer.parseInt(words1[1]);
if (month >= 13) {
words1[0] = String.valueOf(Integer.parseInt(words1[0]) + 1);
month -= 12;
if (month <= 9) {
words1[1] = "0" + String.valueOf(month);
} else {
words1[1] = String.valueOf(month);
}
} else {
if (month <= 9) {
words1[1] = "0" + String.valueOf(month);
} else {
words1[1] = String.valueOf(month);
}
}
for (String item : words1) {
sb.append(item);
}
int x = Integer.parseInt(sb.toString());
int y = Integer.parseInt(today);
if (y >= x) {
list.add(i + 1);
}
sb.delete(0, sb.length());
}
int[] answer = list.stream().mapToInt(Integer::intValue).toArray();
return answer;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution s = new Solution();
String today = "2022.05.19";
String[] terms = { "A 6", "B 12", "C 3" };
String[] privacies = { "2021.05.02 A", "2021.07.01 B", "2022.02.19 C", "2022.02.20 C" };
s.solution(today, terms, privacies);
}
}
답안 :
SELECT Year(o1.SALES_DATE) as 'YEAR',Month(o1.SALES_DATE) as 'MONTH',count(distinct u1.USER_ID) as 'PURCHASED_USERS',
Round(COUNT (DISTINCT o1.USER_ID)/(SELECT COUNT (*) FROM USER_INFO WHERE JOINED LIKE '2021%'),1) AS PUCHASED_RATIO
from USER_INFO u1
join ONLINE_SALE o1
on u1.USER_ID=o1.USER_ID
where Year(u1.JOINED)='2021'
group by 1,2
order by 1,2
SALES_DATE 컬럼에서 연도와 월을 추출.count(distinct u1.USER_ID) 를 카운트하여 컬럼생성
- 테이블 내에서 low_fats 와 recyvlacle 컬럼이 'Y' 인 행을 찾는 문제
답안 :
select product_id
from Products
where low_fats = 'Y' AND recyclable = 'Y'