다 해봐야하는데 순서만 변경 가능한 상황에서 순열을 이용한다.
import java.util.*;
public class Main {
static boolean prev_permutation(int a[]) {
int i = a.length - 1;
while(i > 0 && a[i - 1] <= a[i]) {
i -= 1;
}
if(i <= 0) {
return false;
}
int j = a.length - 1;
while(a[j] >= a[i - 1]) {
j -= 1;
}
int temp = a[i - 1];
a[i - 1] = a[j];
a[j] = temp;
j = a.length - 1;
while(i < j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
i += 1;
j -= 1;
}
return true;
}
static boolean next_permutation(int a[]) {
int i = a.length - 1;
while(i > 0 && a[i - 1] >= a[i]) {
i -= 1;
}
if(i <= 0) {
return false;
}
int j = a.length - 1;
while(a[j] <= a[i - 1]) {
j -= 1;
}
int temp = a[i - 1];
a[i - 1] = a[j];
a[j] = temp;
j = a.length - 1;
while(i < j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
i += 1;
j -= 1;
}
return true;
}
static boolean check(int perm[], char a[]) {
for(int i = 0; i < a.length; i++) {
if(a[i] == '<' && perm[i] > perm[i + 1]) {
return false;
}
if(a[i] == '>' && perm[i] < perm[i + 1]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
char a[] = new char[k];
for(int i = 0; i < k; i++) {
a[i] = sc.next().charAt(0);
}
int small[] = new int[k + 1];
int big[] = new int[k + 1];
for(int i = 0; i <= k; i++) {
small[i] = i;
big[i] = 9 - i;
}
do { // 최솟값
if(check(small, a)) {
break;
}
} while(next_permutation(small));
do { // 최댓값
if(check(big, a)) {
break;
}
} while(prev_permutation(big));
for(int i = 0; i < big.length; i++) {
System.out.print(big[i]);
}
System.out.println();
for(int i = 0; i < small.length; i++) {
System.out.print(small[i]);
}
System.out.println();
}
}
import java.util.*;
public class Main {
static boolean next_permutation(int a[]) {
int i = a.length - 1;
while(i > 0 && a[i - 1] >= a[i]) {
i -= 1;
}
if(i <= 0) {
return false;
}
int j = a.length - 1;
while(a[j] <= a[i - 1]) {
j -= 1;
}
int temp = a[i - 1];
a[i - 1] = a[j];
a[j] = temp;
j = a.length - 1;
while(i < j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
i += 1;
j -= 1;
}
return true;
}
static int calc(int a[], int b[]) {
int n = a.length;
int answer = a[0];
for(int i = 1; i < n; i++) {
if(b[i - 1] == 0) {
answer += a[i];
} else if(b[i - 1] == 1) {
answer -= a[i];
} else if(b[i - 1] == 2) {
answer *= a[i];
} else {
answer /= a[i];
}
}
return answer;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n];
for(int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
int b[] = new int[n - 1];
int m = 0;
for(int i = 0; i < 4; i++) {
int count = sc.nextInt();
for(int k = 0; k < count; k++) {
b[m++] = i;
}
}
Arrays.sort(b);
ArrayList<Integer> result = new ArrayList<>();
do {
int temp = calc(a, b);
result.add(temp);
} while(next_permutation(b));
Collections.sort(result);
m = result.size();
System.out.println(result.get(m - 1));
System.out.println(result.get(0));
}
}
import java.util.*;
public class AlertMail {
public static int getTime(String time) {
int h = Integer.parseInt(time.split(":")[0]);
int m = Integer.parseInt(time.split(":")[1]);
return h * 60 + m;
}
public static String[] solution(String[] reports, int time) {
HashMap<String, Integer> inT = new HashMap<>();
HashMap<String, Integer> sumT = new HashMap<>();
for(String x : reports) {
String a = x.split(" ")[0];
String b = x.split(" ")[1];
String c = x.split(" ")[2];
if(c.equals("in")) {
inT.put(a, getTime(b));
} else {
sumT.put(a, sumT.getOrDefault(a, 0) + (getTime(b) - inT.get(a)));
}
}
ArrayList<String> res = new ArrayList<>();
for(String name : sumT.keySet()) {
if(sumT.get(name) > time) {
res.add(name);
}
}
res.sort((a, b) -> a.compareTo(b));
String answer[] = new String[res.size()];
for(int i = 0; i < res.size(); i++) {
answer[i] = res.get(i);
}
return answer;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(AlertMail.solution(new String[]{"john 09:30 in", "daniel 10:05 in", "john 10:15 out", "luis 11:57 in",
"john 12:03 in", "john 12:20 out", "luis 12:35 out", "daniel 15:05 out"}, 60)));
}
}