Skip to content

Commit

Permalink
🔀merge: [Week24] 2문제 완료 (#86)
Browse files Browse the repository at this point in the history
* ✨feat: LTC_2300_Successful_Pairs_of_Spells_and_Potions 알고리즘 풀이

* ✨feat: LTC_238_Product_of_Array_Except_Self 알고리즘 풀이
  • Loading branch information
jinny-l committed Aug 9, 2023
1 parent f4eb593 commit e1f1694
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package week_24;

import java.util.Arrays;

public class LTC_2300_Successful_Pairs_of_Spells_and_Potions {

public int[] successfulPairs(int[] spells, int[] potions, long success) {
int[] answer = new int[spells.length];
// 이분 탐색을 위해 정렬
Arrays.sort(potions);

// spells 길이 만큼 반복
for (int i = 0; i < spells.length; i++) {
int left = 0;
int right = potions.length - 1;

// 이분탐색
while (left <= right) {
int mid = (left + right) / 2;

if ((long) potions[mid] * (long) spells[i] < success) {
left = mid + 1;
} else {
right = mid - 1;
}
}
answer[i] = potions.length - left;
}
return answer;
}
}
24 changes: 24 additions & 0 deletions jinny-l/week_24/LTC_238_Product_of_Array_Except_Self.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package week_24;

import java.util.Arrays;

public class LTC_238_Product_of_Array_Except_Self {
public int[] productExceptSelf(int[] nums) {
int[] answer = new int[nums.length];
// 1로 초기화
Arrays.fill(answer, 1);

// 왼쪽 곱셈
for(int i = 1; i < nums.length; i++) {
answer[i] = answer[i - 1] * nums[i - 1];
}

// 오른쪽 곱셈
int total = 1;
for (int i = nums.length - 1; i > 0; i--) {
total *= nums[i];
answer[i - 1] *= total;
}
return answer;
}
}

0 comments on commit e1f1694

Please sign in to comment.