import java.util.Arrays;
import java.util.Scanner;
public class problem148 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int size = in.nextInt();
int cut = in.nextInt();
int [] arr = new int [size];
for(int i = 0; i <size; i++){
arr[i] = in.nextInt();
}
Arrays.sort(arr);
// 1 3 5 6 10
// arr의 배열 값의 차이를 구한다.
// -1 을 해주는 이유는 배열의 값을 빼면, length가 하나 줄어든다.
int [] dif = new int [size-1];
for(int i = 0; i < size - 1; i++){
dif[i] = arr[i+1] - arr[i];
}
// dif 배열을 오름차순으로 정리함으로써, 차이의 최솟값이 오름차순정렬된다.
Arrays.sort(dif);
int result = 0;
for(int i = 0; i < size - cut; i++){
result += dif[i];
}
System.out.print(result);
}
}
import java.util.Arrays;
import java.util.Scanner;
public class problem149 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int size = in.nextInt();
int [][] arr = new int[size][2];
for(int i = 0; i < size; i++){
arr[i][0] = in.nextInt(); // 남은 일수
arr[i][1] = in.nextInt(); // 과제 점수
}
Arrays.sort(arr, (o1,o2)->{
// 과제 점수에 따른 내림차순
return o2[1] - o1[1];
});
// 정수 범위
int []score = new int [1001];
for(int i = 0; i <size; i++){
for(int j = arr[i][0]; j > 0; j--){
// score 배열 안에 값이 없으면(0),
// 이미 점수에 따라 내림차순 되어있음으로, 배열에 값을 바인딩한다.
if(score[j] == 0){
score[j] = arr[i][1];
break;
}
}
}
int result = 0;
for(int i = 0; i < score.length; i++){
result += score[i];
}
System.out.print(result);
}
}
import java.util.*;
public class problem150 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int size = in.nextInt();
// 이동 비용이 최대인곳에서 출발하지 않으면 최소 비용
int max = 0;
int sum = 0;
for(int i = 0; i < size; i++){
int n = in.nextInt();
sum += n;
max = Math.max(max, n);
}
sum -= max;
System.out.print(sum);
}
}
import java.util.*;
public class problem151 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
// 지정되어있는 주파수보다, 현재 주파수에서 출발하는경우가 최소값일경우
int min = Math.abs(n-m);
int size = in.nextInt();
int [] arr = new int [size];
for(int i = 0; i < size; i++){
arr[i] = in.nextInt();
// 지정된 주파수와 현재 주파수의 최소값에서 min을 구해준다.
min = Math.min(Math.abs(arr[i]- m) + 1, min);
}
System.out.print(min);
}
}
import java.util.*;
public class problem152 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
int boxCount = 0;
for(int i = 0; i < t; i++) {
int c = in.nextInt();
int b = in.nextInt();
Integer [] arr = new Integer[b];
for(int j = 0; j < b; j++){
int n = in.nextInt();
int m = in.nextInt();
arr[j] = n * m;
}
// 최대한 큰 박스크기 부터 담아야 최소값
Arrays.sort(arr, Comparator.reverseOrder());
for(int k = 0 ; k < arr.length; k++){
// 박스 크기 만큼 사탕을 담을수있다. (박스 공간이 남아도된다)
c = c - arr[k];
boxCount++;
if(c <=0){
break;
}
}
}
System.out.print(boxCount);
}
}
import java.util.*;
public class problem152 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
int boxCount = 0;
for(int i = 0; i < t; i++) {
int c = in.nextInt();
int b = in.nextInt();
Integer [] arr = new Integer[b];
for(int j = 0; j < b; j++){
int n = in.nextInt();
int m = in.nextInt();
arr[j] = n * m;
}
// 최대한 큰 박스크기 부터 담아야 최소값
Arrays.sort(arr, Comparator.reverseOrder());
for(int k = 0 ; k < arr.length; k++){
// 박스 크기 만큼 사탕을 담을수있다. (박스 공간이 남아도된다)
c = c - arr[k];
boxCount++;
if(c <=0){
break;
}
}
System.out.println(boxCount);
}
}
}
프로그래머스 SQL GROUP BY (소마 2차테스트 준비)
SELECT CAR_ID,
## CASE WHEN (조건식)
## THEN (반환값) ELSE
## END (조건에 만족하지 않을때 반환값)
CASE
WHEN CAR_ID IN (SELECT CAR_ID
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
WHERE '2022-10-16' BETWEEN START_DATE AND END_DATE) THEN '대여중'
ELSE '대여가능' END "AVAILABILITY"
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
ORDER BY CAR_ID DESC;
class Solution {
public long solution(int n) {
// n은 2000이하의 자연수 임으로 배열을 2000개로 정의
long [] arr = new long[2001];
arr[1] = 1;
arr[2] = 2;
for(int i = 3 ; i < arr.length; i++){
// 문제에서 나눈 나머지를 리턴
arr[i] = (arr[i-2] + arr[i-1]) % 1234567;
}
return arr[n];
}
}
class Solution {
public int solution(int[] arr) {
int answer = 0;
if(arr.length == 1) return arr[0];
// 일단 arr[0], arr[1] 의 배열 값의 최소 공배수를 구해준다.
int l = lcm(arr[0], arr[1]);
if(arr.length > 2){
for(int i = 2 ; i < arr.length; i++){
// 최소 공배수를 갱신 하여 대입해준다.
l = lcm(l, arr[i]);
answer = lcm(l, arr[i]);
}
}
return answer;
}
static int gcd(int a, int b){
if(b == 0) return a;
return gcd(b, a% b);
}
static int lcm(int a, int b){
return a * b / gcd(a,b);
}
}
import java.util.*;
class Solution {
public int solution(String[][] clothes) {
int answer = 1;
HashMap<String, Integer> map = new HashMap<>();
for(String[] cl : clothes){
// 옷의 이름이 아니라 종류를 map에 담아준다.
// getOrDefault(,1)을 해준 이유는 아무것도 입지않은 상태가 존재할수있다.
map.put(cl[1], map.getOrDefault(cl[1], 1) + 1);
}
// 아무것도 입지 않은 상태도 포함해서 갯수를 세어준다.
for(String st : map.keySet()){
answer *= map.get(st);
}
// 종류 별로 하나도 안입은 상태는 없음으로 1가지 경우를 빼준다.
return answer - 1;
}
}