Skip to content

Commit d8dcf0d

Browse files
committed
45주차
1 parent bb22dc9 commit d8dcf0d

File tree

7 files changed

+406
-16
lines changed

7 files changed

+406
-16
lines changed

src/main/java/org/example/_44week/SuccessfulPairsOfSpellsAndPotions.java

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,49 @@
55
public class SuccessfulPairsOfSpellsAndPotions {
66

77
public static void main(String[] args) {
8-
int[] spells = {5,1,3};
9-
int[] potions = {1,2,3,4,5};
8+
int[] spells = {5, 1, 3};
9+
int[] potions = {1, 2, 3, 4, 5};
1010
int success = 7;
1111

1212
int[] answers = successfulPairs(spells, potions, success);
13-
Arrays.stream(answers).forEach(i -> System.out.print(i+" "));
13+
Arrays.stream(answers).forEach(i -> System.out.print(i + " "));
1414
}
1515

16-
// 안부전해줘야되고 준우형이 보고싶어한다고 해줘야하고.
17-
// 안보고싶다하면 그렇게 말할줄 알았다고 말했다고 해야하고
1816
public static int[] successfulPairs(int[] spells, int[] potions, long success) {
19-
int[] sortedPotions = Arrays.stream(potions).sorted().toArray();
20-
17+
int[] answers = new int[spells.length];
18+
potions = Arrays.stream(potions).sorted().toArray();
19+
20+
for (int i = 0; i < spells.length; i++) {
21+
int sp = 0;
22+
int ep = potions.length - 1;
23+
24+
while (sp <= ep) {
25+
int mid = (sp + ep) / 2;
26+
if ((long) potions[mid] * spells[i] >= success) {
27+
ep = mid - 1;
28+
} else {
29+
sp = mid + 1;
30+
}
31+
}
32+
33+
answers[i] = potions.length - sp;
34+
}
2135

36+
return answers;
2237
}
2338

24-
private static class Spell implements Comparable<Spell>{
25-
int value;
26-
int idx;
27-
28-
@Override
29-
public int compareTo(Spell o) {
30-
return Integer.compare(this.value, o.value);
31-
}
32-
}
39+
// private static class Spell implements Comparable<Spell>{
40+
// int value;
41+
// int idx;
42+
//
43+
// public Spell(int value, int idx) {
44+
// this.value = value;
45+
// this.idx = idx;
46+
// }
47+
//
48+
// @Override
49+
// public int compareTo(Spell o) {
50+
// return Integer.compare(this.value, o.value);
51+
// }
52+
// }
3353
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package org.example._45week;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
6+
public class FindPeakElement {
7+
8+
private final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
10+
private final static int SLASH = 0;
11+
private final static int BACK_SLASH = 1;
12+
private final static int V = 2;
13+
private final static int = 3;
14+
15+
public static void main(String[] args) {
16+
// int[] nums = {1, 2, 1, 3, 5, 6, 4};
17+
int[] nums = {1, 2, 3, 1};
18+
19+
int peakElement = findPeakElement(nums);
20+
System.out.println(peakElement);
21+
}
22+
23+
public static int findPeakElement(int[] nums) {
24+
if(nums.length==1){
25+
return nums[0];
26+
}
27+
28+
int sp = -1;
29+
int ep = nums.length;
30+
31+
int mid = 0;
32+
while (sp < ep) {
33+
mid = (sp + ep) / 2;
34+
35+
int shape = getSahpe(mid, nums);
36+
if (shape == SLASH) {
37+
sp = mid + 1;
38+
} else if (shape == BACK_SLASH) {
39+
ep = mid - 1;
40+
} else if (shape == V) {
41+
sp = mid + 1;
42+
} else if (shape == ) {
43+
return mid;
44+
}
45+
}
46+
47+
return (sp+ep)/2;
48+
}
49+
50+
private static int getSahpe(int mid, int[] nums) {
51+
if (mid == 0) {
52+
if (nums[mid] < nums[mid + 1]) {
53+
return SLASH;
54+
} else if (nums[mid] > nums[mid + 1]) {
55+
return ;
56+
}
57+
} else if (mid == nums.length - 1) {
58+
if (nums[mid - 1] < nums[mid]) {
59+
return ;
60+
} else if (nums[mid - 1] > nums[mid]) {
61+
return BACK_SLASH;
62+
}
63+
} else {
64+
int left = nums[mid - 1] > nums[mid] ? 1 : -1;
65+
int right = nums[mid + 1] > nums[mid] ? 1 : -1;
66+
if (left > 0 && right > 0) {
67+
return V;
68+
} else if (left > 0 && right < 0) {
69+
return BACK_SLASH;
70+
} else if (left < 0 && right > 0) {
71+
return SLASH;
72+
} else {
73+
return ;
74+
}
75+
}
76+
77+
return -1;
78+
}
79+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.example._45week;
2+
3+
class HouseRobber {
4+
private final int O = 0;
5+
private final int X = 1;
6+
public int rob(int[] nums) {
7+
int[][] money = new int[2][nums.length];
8+
9+
money[O][0] = nums[0];
10+
money[X][0] = 0;
11+
12+
for(int i=1; i<nums.length; i++){
13+
money[O][i] = money[X][i-1] + nums[i];
14+
money[X][i] = Math.max(money[X][i-1],money[O][i-1]);
15+
}
16+
17+
return Math.max(money[O][nums.length-1],money[X][nums.length-1]);
18+
}
19+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.example._45week;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import java.util.Set;
7+
import java.util.stream.Collectors;
8+
9+
public class JewelShopping {
10+
11+
public static void main(String[] args) {
12+
// String[] gems = {"DIA", "RUBY", "RUBY", "DIA", "DIA", "EMERALD", "SAPPHIRE", "DIA"};
13+
String[] gems = {"AA", "AB", "AC", "AA", "AC"};
14+
int[] answers = solution(gems);
15+
for (int answer : answers) {
16+
System.out.print(answer + ", ");
17+
}
18+
}
19+
20+
public static int[] solution(String[] gems) {
21+
Set<String> gemCategories = Arrays.stream(gems).collect(Collectors.toSet());
22+
Map<String, Integer> countMap = new HashMap<>();
23+
24+
int start = 0;
25+
int end = 0;
26+
27+
int answerStart = 0;
28+
int answerEnd = Integer.MAX_VALUE;
29+
30+
for (end = 0; end < gems.length; end++) {
31+
String currentGem = gems[end];
32+
countMap.put(currentGem, countMap.getOrDefault(currentGem, 0) + 1);
33+
34+
// if (gems[start].equals(currentGem)) {
35+
// start++;
36+
// countMap.put(currentGem, countMap.get(currentGem) - 1);
37+
// }
38+
39+
// 종류가 한개씩 들어갈 때 까지
40+
if (countMap.size() == gemCategories.size()) {
41+
// 1. start 가능한 만큼 땡기기.
42+
start = updateStart(start, gems, countMap);
43+
// 2. 최고 기록과 비교
44+
// 3. 갱신.
45+
if ((answerEnd - answerStart) > (end - start)) {
46+
answerEnd = end;
47+
answerStart = start;
48+
}
49+
}
50+
}
51+
52+
53+
return new int[]{answerStart + 1, answerEnd + 1};
54+
}
55+
56+
private static int updateStart(int start, String[] gems, Map<String, Integer> countMap) {
57+
int newStart = 0;
58+
for (newStart = start; ; newStart++) {
59+
Integer count = countMap.get(gems[newStart]);
60+
61+
if (count > 1) {
62+
countMap.put(gems[newStart], count - 1);
63+
} else {
64+
break;
65+
}
66+
}
67+
68+
return newStart;
69+
}
70+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package org.example._45week;
2+
3+
public class LockAndKey {
4+
5+
private static int N = 0;
6+
private static int M = 0;
7+
8+
public static void main(String[] args) {
9+
// int[][] key = {{0, 0, 0}, {1, 0, 0}, {0, 1, 1}};
10+
// int[][] lock = {{0, 0, 0}, {1, 0, 0}, {0, 1, 1}};
11+
12+
int[][] key = {{0, 0}, {1, 0}};
13+
int[][] lock = {{0, 0, 0}, {1, 0, 0}, {0, 1, 1}};
14+
15+
boolean solution = solution(key, lock);
16+
System.out.println(solution);
17+
}
18+
19+
public static boolean solution(int[][] key, int[][] lock) {
20+
N = lock.length;
21+
M = key.length;
22+
23+
int[][][] keys = makeRotationKeys(key);
24+
int[][] newLock = makeExtensionLock(lock);
25+
26+
for (int row = 0; row < newLock.length; row++) {
27+
for (int col = 0; col < newLock.length; col++) {
28+
for (int rotation = 0; rotation < 4; rotation++) {
29+
boolean isMatch = isMatch(keys[rotation], newLock, row, col);
30+
if (isMatch) {
31+
return true;
32+
}
33+
}
34+
}
35+
}
36+
37+
return false;
38+
}
39+
40+
private static int[][] makeExtensionLock(int[][] lock) {
41+
int newLength = N + 2 * M - 2;
42+
int[][] newLock = new int[newLength][newLength];
43+
44+
for (int row = 0; row < newLength; row++) {
45+
for (int col = 0; col < newLength; col++) {
46+
newLock[row][col] = 3;
47+
}
48+
}
49+
50+
for (int row = M - 1, originalRow = 0; row < M - 1 + N; row++, originalRow++) {
51+
for (int col = M - 1, originalCol = 0; col < M - 1 + N; col++, originalCol++) {
52+
newLock[row][col] = lock[originalRow][originalCol];
53+
}
54+
}
55+
56+
return newLock;
57+
}
58+
59+
public static boolean isMatch(int[][] key, int[][] lock, int rowIndex, int colIndex) {
60+
for (int row = M - 1; row < M - 1 + N; row++) {
61+
for (int col = M - 1; col < M - 1 + N; col++) {
62+
if (lock[row][col] == 0){
63+
if (((row - rowIndex) < M - 1 || (row - rowIndex) > N + 2 * M - 2) || ((col - colIndex) < M - 1 || (col - colIndex) > N + 2 * M - 2)) {
64+
return false;
65+
}
66+
67+
68+
if(key[row - rowIndex][col - colIndex] == 0){
69+
return false;
70+
}
71+
}
72+
73+
if (lock[row][col] == 1 && key[row - rowIndex][col - colIndex] == 1) {
74+
return false;
75+
}
76+
}
77+
}
78+
79+
return true;
80+
}
81+
82+
public static int[][][] makeRotationKeys(int[][] key) {
83+
int[][][] keys = new int[4][key.length][key[0].length];
84+
keys[0] = key;
85+
86+
for (int i = 1; i < 4; i++) {
87+
for (int row = 0; row < key.length; row++) {
88+
for (int col = 0; col < key[0].length; col++) {
89+
keys[i][row][col] = keys[i - 1][key.length - 1 - col][row];
90+
}
91+
}
92+
}
93+
94+
return keys;
95+
}
96+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.example._45week;
2+
3+
public class MinimumFlipsToMakeAORBEqualToC {
4+
5+
public static void main(String[] args) {
6+
int answer = minFlips(8, 3, 5);
7+
System.out.println(answer);
8+
}
9+
10+
public static int minFlips(int a, int b, int c) {
11+
String binaryA = Integer.toBinaryString(a);
12+
String binaryB = Integer.toBinaryString(b);
13+
String binaryC = Integer.toBinaryString(c);
14+
String binaryAOrB = Integer.toBinaryString(a|b);
15+
16+
int maxLength = Math.max(Math.max(binaryA.length(),binaryB.length()),Math.max(binaryC.length(),binaryAOrB.length()));
17+
binaryA = convertBinaryString(binaryA,maxLength);
18+
binaryB = convertBinaryString(binaryB,maxLength);
19+
binaryC = convertBinaryString(binaryC,maxLength);
20+
binaryAOrB = convertBinaryString(binaryAOrB,maxLength);
21+
22+
System.out.println(binaryA);
23+
System.out.println(binaryB);
24+
System.out.println(binaryAOrB);
25+
System.out.println(binaryC);
26+
int count =0;
27+
for(int i = 0; i<maxLength; i++){
28+
if(binaryC.charAt(i) !=binaryAOrB.charAt(i)){
29+
if(binaryC.charAt(i)=='0'){
30+
if(binaryA.charAt(i)=='1'){
31+
count++;
32+
}
33+
if(binaryB.charAt(i)=='1'){
34+
count++;
35+
}
36+
}else{
37+
count++;
38+
}
39+
}
40+
}
41+
42+
return count;
43+
}
44+
45+
public static String convertBinaryString(String a, int maxLength){
46+
StringBuilder sb = new StringBuilder();
47+
for(int i=0; i< maxLength-a.length(); i++){
48+
sb.append(0);
49+
}
50+
51+
sb.append(a);
52+
53+
return sb.toString();
54+
}
55+
}

0 commit comments

Comments
 (0)