import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Main {
public int solution(int[] nums, int m){
int ans = 0;
Arrays.sort(nums);
int left = 0;
int right = nums.length - 1;
while(left <= right){
if(nums[left] + nums[right] <= m) {
ans++;
left++;
right--;
} else {
ans++;
right--;
}
}
return ans;
}
public static void main(String[] args) throws IOException {
Main T = new Main();
System.out.println(T.solution(new int[]{90, 50, 70, 100, 60}, 140));
System.out.println(T.solution(new int[]{10, 20, 30, 40, 50, 60, 70, 80, 90}, 100));
}
}
- python
2) 가격책정
- java
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Main {
public int solution(int[] prices, int d, int k){
int ans = 0;
int n = prices.length;
Arrays.sort(prices);
if(prices[n-1] - prices[0] <= d) return getAverage(prices, 0, n-1);
else if(prices[n-2] - prices[1] <= d) return getAverage(prices, 1, n-2);
for(int i=0; i<n-k; i++){
if(prices[i+k-1] - prices[i] <= d) return getAverage(prices, i, i+k-1);
}
return prices[(n-1)/2];
}
public int getAverage(int[] prices, int a, int b){
int sum = 0;
for(int i=a; i<=b; i++){
sum += prices[i];
}
return sum / (b-a+1);
}
public static void main(String[] args) throws IOException {
Main T = new Main();
System.out.println(T.solution(new int[]{4,5,6,7,8}, 4, 3));
System.out.println(T.solution(new int[]{8,4,5,7,6}, 1, 3));
}
}
- python
3) 팀구성
- java
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Main {
public int solution(int[] abilites, int k){
int ans = 0;
int n = abilites.length;
Integer[] tmp;
if(n%2 == 1){
n++;
tmp = new Integer[n];
for(int i=0; i<abilites.length; i++) tmp[i] = abilites[i];
tmp[n-1] = 0;
} else {
tmp = new Integer[n];
for(int i=0; i<abilites.length; i++) tmp[i] = abilites[i];
}
Arrays.sort(tmp, Collections.reverseOrder());
Integer[] diff = new Integer[n/2];
for(int i=0; i<n/2; i++) diff[i] = tmp[i*2] - tmp[i*2+1];
Arrays.sort(diff, Collections.reverseOrder());
for(int i=0; i<n/2; i++) ans += tmp[i*2+1];
for(int i=0; i<k; i++) ans += diff[i];
return ans;
}
public static void main(String[] args) throws IOException {
Main T = new Main();
System.out.println(T.solution(new int[]{2,8,3,6,1,9,1,9},2));
System.out.println(T.solution(new int[]{7,6,8,9,10},1));
}
}
- python
4) 선긋기
- java
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Main {
public int solution(int[][] nums){
int answer=0;
Arrays.sort(nums, (a, b) -> a[0]==b[0]?a[1]-b[1]:a[0]-b[0]);
int s=nums[0][0];
int e=nums[0][1];
for(int i=1; i<nums.length; i++){
if(nums[i][0]<=e && nums[i][1]>e){
e=nums[i][1];
}
else if(nums[i][0]>e){
answer+=(e-s);
s=nums[i][0];
e=nums[i][1];
}
}
answer+=(e-s);
return answer;
}
public static void main(String[] args) throws IOException {
Main T = new Main();
int [][] arr = new int[][]{{1,3}, {2,5}, {7,10}};
System.out.println(T.solution(new int[][]{{1,3}, {2,5}, {7,10}}));
System.out.println(T.solution(new int[][]{{5,6}, {1,3}, {7,8}, {9,10}}));
}
}
- python
5) 회의실 배정
- java
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Main {
public int solution(int[][] times){
int answer=0;
Arrays.sort(times, (a, b)->a[1]==b[1]?a[0]-b[0]:a[1]-b[1]);
int et=0;
for(int[] x : times){
if(x[0]>=et){
answer++;
et=x[1];
}
}
return answer;
}
public static void main(String[] args){
Main T = new Main();
System.out.println(T.solution(new int[][]{{1, 4}, {2, 3}, {3, 5}, {4, 6}, {5, 7}}));
}
}
- python
6) 결혼식
- java
package ch05;
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
class Time implements Comparable<Time>{
public int time;
public int state;
Time(int time, int state) {
this.time = time;
this.state = state;
}
@Override
public int compareTo(Time ob){
if(this.time==ob.time) return ob.state-this.state;
else return this.time-ob.time;
}
}
public class 결혼식 {
public int solution(int[][] times){
int answer=Integer.MIN_VALUE;
ArrayList<Time> list = new ArrayList<>();
for(int[] x : times){
list.add(new Time(x[0], 1));
list.add(new Time(x[1], 2));
}
Collections.sort(list);
int cnt=0;
for(Time ob : list){
System.out.println(ob.time+" "+ob.state);
if(ob.state==1) cnt++;
else cnt--;
answer=Math.max(answer, cnt);
}
return answer;
}
public static void main(String[] args){
결혼식 T = new 결혼식();
System.out.println(T.solution(new int[][]{{14, 18}, {12, 15}, {15, 20}, {20, 30}, {5, 14}}));
}
}
- python
7) 씨름선수
- java
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Main {
public int solution(int[][] body){
int ans = 0;
int maxW = 0;
Arrays.sort(body, (a, b) -> b[0]-a[0]);
for(int[] x : body){
if(x[1] > maxW) {
ans++;
maxW = x[1];
}
}
return ans;
}
public static void main(String[] args){
Main T = new Main();
System.out.println(T.solution(new int[][]{{172,67}, {183,65}, {180,70}, {170,72}, {181,60}}));
}
}
- python
8) 최대수입스케줄
- java
public int solution(int[][] nums){
int ans = 0;
PriorityQueue<Integer> q = new PriorityQueue<>(Collections.reverseOrder());
Arrays.sort(nums, (a,b) -> b[1]-a[1]);
int maxD = nums[0][1];
int j=0;
for(int i=maxD; i>=1; i--){
for(; j<nums.length; j++){
if(nums[j][1] < i) break;
q.offer(nums[j][0]);
}
if(!q.isEmpty()) ans += q.poll();
}
return ans;
}
public static void main(String[] args){
Main T = new Main();
System.out.println(T.solution(new int[][]{{50,2}, {20,1}, {40,2}, {60,3}, {30,3}, {30,1}}));
}