-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ✨feat: LTC_2300_Successful_Pairs_of_Spells_and_Potions 알고리즘 풀이 * ✨feat: LTC_238_Product_of_Array_Except_Self 알고리즘 풀이
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
jinny-l/week_24/LTC_2300_Successful_Pairs_of_Spells_and_Potions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |