Skip to content

Commit 30bfa8e

Browse files
committed
new soln
1 parent 9f1bcc2 commit 30bfa8e

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

2300.SuccessfulPairs.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// 2300. Successful Pairs of Spells and Potions
2+
// You are given two positive integer arrays spells and potions, of length n and m respectively, where spells[i] represents the strength of the ith spell and potions[j] represents the strength of the jth potion.// You are also given an integer success. A spell and potion pair is considered successful if the product of their strengths is at least success.
3+
// Return an integer array pairs of length n where pairs[i] is the number of potions that will form a successful pair with the ith spell.
4+
5+
// Example 1:
6+
// Input: spells = [5,1,3], potions = [1,2,3,4,5], success = 7
7+
// Output: [4,0,3]
8+
// Explanation:
9+
// - 0th spell: 5 * [1,2,3,4,5] = [5,10,15,20,25]. 4 pairs are successful.
10+
// - 1st spell: 1 * [1,2,3,4,5] = [1,2,3,4,5]. 0 pairs are successful.
11+
// - 2nd spell: 3 * [1,2,3,4,5] = [3,6,9,12,15]. 3 pairs are successful.
12+
// Thus, [4,0,3] is returned.
13+
// Example 2:
14+
// Input: spells = [3,1,2], potions = [8,5,8], success = 16
15+
// Output: [2,0,2]
16+
// Explanation:
17+
// - 0th spell: 3 * [8,5,8] = [24,15,24]. 2 pairs are successful.
18+
// - 1st spell: 1 * [8,5,8] = [8,5,8]. 0 pairs are successful.
19+
// - 2nd spell: 2 * [8,5,8] = [16,10,16]. 2 pairs are successful.
20+
// Thus, [2,0,2] is returned.
21+
22+
// Constraints:
23+
// n == spells.length
24+
// m == potions.length
25+
// 1 <= n, m <= 105
26+
// 1 <= spells[i], potions[i] <= 105
27+
// 1 <= success <= 1010
28+
public class Solution {
29+
public int[] SuccessfulPairs(int[] spells, int[] potions, long success) {
30+
// Sort the potions array
31+
Array.Sort(potions);
32+
33+
// Initialize the array to store the results
34+
int[] successPair = new int[spells.Length];
35+
36+
// Iterate through each spell
37+
for(int i = 0; i < spells.Length; i++){
38+
// Perform a binary search to find the minimum index of the potion that meets the success criteria
39+
int min = BinarySearch(potions, spells[i], success);
40+
// Calculate the number of successful pairs
41+
successPair[i] = potions.Length - min;
42+
}
43+
44+
// Return the array with the counts of successful pairs
45+
return successPair;
46+
}
47+
48+
// Helper method to perform binary search
49+
private int BinarySearch(int[] potions, int spell, long success) {
50+
int left = 0;
51+
int right = potions.Length;
52+
53+
// Perform binary search
54+
while (left < right) {
55+
int mid = left + (right - left) / 2;
56+
if ((long) spell * potions[mid] >= success) {
57+
right = mid; // Look for a smaller index on the left side
58+
} else {
59+
left = mid + 1; // Look for a larger index on the right side
60+
}
61+
}
62+
63+
return left;
64+
}
65+
}

826.MaxProfitAssignment.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// 826. Most Profit Assigning Work
2+
// You have n jobs and m workers. You are given three arrays: difficulty, profit, and worker where:
3+
4+
// difficulty[i] and profit[i] are the difficulty and the profit of the ith job, and
5+
// worker[j] is the ability of jth worker (i.e., the jth worker can only complete a job with difficulty at most worker[j]).
6+
// Every worker can be assigned at most one job, but one job can be completed multiple times.
7+
// For example, if three workers attempt the same job that pays $1, then the total profit will be $3. If a worker cannot complete any job, their profit is $0.
8+
// Return the maximum profit we can achieve after assigning the workers to the jobs.
9+
10+
// Example 1:
11+
// Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7]
12+
// Output: 100
13+
// Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get a profit of [20,20,30,30] separately.
14+
// Example 2:
15+
// Input: difficulty = [85,47,57], profit = [24,66,99], worker = [40,25,25]
16+
// Output: 0
17+
18+
// Constraints:
19+
// n == difficulty.length
20+
// n == profit.length
21+
// m == worker.length
22+
// 1 <= n, m <= 104
23+
// 1 <= difficulty[i], profit[i], worker[i] <= 105
24+
25+
public class Solution {
26+
public int MaxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
27+
28+
Array.Sort(difficulty, profit);
29+
Array.Sort(worker);
30+
31+
int netProfit = 0;
32+
int maxProfit = 0;
33+
int k = 0;
34+
35+
foreach (int w in worker) {
36+
while (k < difficulty.Length && w >= difficulty[k]) {
37+
maxProfit = Math.Max(maxProfit, profit[k]);
38+
k++;
39+
}
40+
netProfit += maxProfit;
41+
}
42+
43+
return netProfit;
44+
}
45+
}
46+
47+
48+
public class Solution {
49+
public int MaxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
50+
List<int[]> diffProfit = new List<int[]>();
51+
for (int i = 0; i < difficulty.Length; i++) {
52+
diffProfit.Add(new int[] { difficulty[i], profit[i] });
53+
}
54+
55+
// Sort by difficulty first, then by profit in descending order
56+
diffProfit.Sort((a, b) => {
57+
if (a[0] == b[0]) return b[1].CompareTo(a[1]);
58+
return a[0].CompareTo(b[0]);
59+
});
60+
61+
Array.Sort(worker);
62+
63+
int netProfit = 0;
64+
int maxProfit = 0;
65+
int k = 0;
66+
67+
foreach (int w in worker) {
68+
while (k < diffProfit.Count && w >= diffProfit[k][0]) {
69+
maxProfit = Math.Max(maxProfit, diffProfit[k][1]);
70+
k++;
71+
}
72+
netProfit += maxProfit;
73+
}
74+
75+
return netProfit;
76+
}
77+
}

0 commit comments

Comments
 (0)