Skip to content

Commit f7377a6

Browse files
authored
Merge pull request #1 from iamAntimPal/main
leet conde daily python solutions
2 parents 93afb75 + f606926 commit f7377a6

File tree

3 files changed

+205
-0
lines changed

3 files changed

+205
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
"""
3+
2115. Find All Possible Recipes from Given Supplies
4+
5+
You have information about n different recipes. You are given a string array recipes and a 2D string array ingredients. The ith recipe has the name recipes[i], and you can create it if you have all the needed ingredients from ingredients[i]. A recipe can also be an ingredient for other recipes, i.e., ingredients[i] may contain a string that is in recipes.
6+
7+
You are also given a string array supplies containing all the ingredients that you initially have, and you have an infinite supply of all of them.
8+
9+
Return a list of all the recipes that you can create. You may return the answer in any order.
10+
11+
Note that two recipes may contain each other in their ingredients.
12+
13+
14+
15+
Example 1:
16+
17+
Input: recipes = ["bread"], ingredients = [["yeast","flour"]], supplies = ["yeast","flour","corn"]
18+
Output: ["bread"]
19+
Explanation:
20+
We can create "bread" since we have the ingredients "yeast" and "flour".
21+
Example 2:
22+
23+
Input: recipes = ["bread","sandwich"], ingredients = [["yeast","flour"],["bread","meat"]], supplies = ["yeast","flour","meat"]
24+
Output: ["bread","sandwich"]
25+
Explanation:
26+
We can create "bread" since we have the ingredients "yeast" and "flour".
27+
We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread".
28+
Example 3:
29+
30+
Input: recipes = ["bread","sandwich","burger"], ingredients = [["yeast","flour"],["bread","meat"],["sandwich","meat","bread"]], supplies = ["yeast","flour","meat"]
31+
Output: ["bread","sandwich","burger"]
32+
Explanation:
33+
We can create "bread" since we have the ingredients "yeast" and "flour".
34+
We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread".
35+
We can create "burger" since we have the ingredient "meat" and can create the ingredients "bread" and "sandwich".
36+
37+
38+
Constraints:
39+
40+
n == recipes.length == ingredients.length
41+
1 <= n <= 100
42+
1 <= ingredients[i].length, supplies.length <= 100
43+
1 <= recipes[i].length, ingredients[i][j].length, supplies[k].length <= 10
44+
recipes[i], ingredients[i][j], and supplies[k] consist only of lowercase English letters.
45+
All the values of recipes and supplies combined are unique.
46+
Each ingredients[i] does not contain any duplicate values.
47+
"""
48+
49+
class Solution:
50+
def findAllRecipes(
51+
self, recipes: List[str], ingredients: List[List[str]], supplies: List[str]
52+
) -> List[str]:
53+
g = defaultdict(list)
54+
indeg = defaultdict(int)
55+
for a, b in zip(recipes, ingredients):
56+
for v in b:
57+
g[v].append(a)
58+
indeg[a] += len(b)
59+
q = supplies
60+
ans = []
61+
for i in q:
62+
for j in g[i]:
63+
indeg[j] -= 1
64+
if indeg[j] == 0:
65+
ans.append(j)
66+
q.append(j)
67+
return ans
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2206. Divide Array Into Equal Pairs
2+
'''
3+
You are given an integer array nums consisting of 2 * n integers.
4+
5+
You need to divide nums into n pairs such that:
6+
7+
Each element belongs to exactly one pair.
8+
The elements present in a pair are equal.
9+
Return true if nums can be divided into n pairs, otherwise return false.
10+
11+
12+
13+
Example 1:
14+
15+
Input: nums = [3,2,3,2,2,2]
16+
Output: true
17+
Explanation:
18+
There are 6 elements in nums, so they should be divided into 6 / 2 = 3 pairs.
19+
If nums is divided into the pairs (2, 2), (3, 3), and (2, 2), it will satisfy all the conditions.
20+
Example 2:
21+
22+
Input: nums = [1,2,3,4]
23+
Output: false
24+
Explanation:
25+
There is no way to divide nums into 4 / 2 = 2 pairs such that the pairs satisfy every condition.
26+
27+
28+
Constraints:
29+
30+
nums.length == 2 * n
31+
1 <= n <= 500
32+
1 <= nums[i] <= 500
33+
'''
34+
35+
36+
# Now start Solution
37+
# Solution
38+
class Solution:
39+
def divideArray(self, nums: list[int]) -> bool:
40+
return all(value % 2 == 0 for value in collections.Counter(nums).values())
41+
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
3+
2226. Maximum Candies Allocated to K Children
4+
5+
You are given a 0-indexed integer array candies. Each element in the array denotes a pile of candies of size candies[i]. You can divide each pile into any number of sub piles, but you cannot merge two piles together.
6+
7+
You are also given an integer k. You should allocate piles of candies to k children such that each child gets the same number of candies. Each child can be allocated candies from only one pile of candies and some piles of candies may go unused.
8+
9+
Return the maximum number of candies each child can get.
10+
11+
12+
13+
Example 1:
14+
15+
Input: candies = [5,8,6], k = 3
16+
Output: 5
17+
Explanation: We can divide candies[1] into 2 piles of size 5 and 3, and candies[2] into 2 piles of size 5 and 1. We now have five piles of candies of sizes 5, 5, 3, 5, and 1. We can allocate the 3 piles of size 5 to 3 children. It can be proven that each child cannot receive more than 5 candies.
18+
Example 2:
19+
20+
Input: candies = [2,5], k = 11
21+
Output: 0
22+
Explanation: There are 11 children but only 7 candies in total, so it is impossible to ensure each child receives at least one candy. Thus, each child gets no candy and the answer is 0.
23+
24+
25+
Constraints:
26+
27+
1 <= candies.length <= 105
28+
1 <= candies[i] <= 107
29+
1 <= k <= 1012
30+
'''
31+
*/
32+
33+
class Solution:
34+
def maximumCandies(self, candies: List[int], k: int) -> int:
35+
left, right = 1, max(candies)
36+
result = 0
37+
38+
while left <= right:
39+
mid = (left + right) // 2
40+
41+
children_count = sum(pile // mid for pile in candies)
42+
43+
if children_count >= k:
44+
result = mid
45+
left = mid + 1
46+
else:
47+
right = mid - 1
48+
49+
return result
50+
51+
# Time complexity : O(nlogm) where n is the number of piles and m is the maximum number of candies in a pile.
52+
# Space complexity : O(1) as we are using only constant space.
53+
54+
// Java code
55+
class Solution {
56+
public int maximumCandies(int[] candies, int k) {
57+
int left = 1, right = Arrays.stream(candies).max().getAsInt();
58+
int result = 0;
59+
60+
while (left <= right) {
61+
int mid = (left + right) / 2;
62+
63+
int childrenCount = Arrays.stream(candies).mapToInt(candy -> candy / mid).sum();
64+
65+
if (childrenCount >= k) {
66+
result = mid;
67+
left = mid + 1;
68+
} else {
69+
right = mid - 1;
70+
}
71+
}
72+
73+
return result;
74+
}
75+
}
76+
77+
// Time complexity : O(nlogm) where n is the number of piles and m is the maximum number of candies in a pile.
78+
// Space complexity : O(1) as we are using only constant space.
79+
'''
80+
'''
81+
82+
from typing import List
83+
84+
class Solution:
85+
def maximumCandies(self, candies: List[int], k: int) -> int:
86+
left, right = 1, max(candies)
87+
result = 0
88+
while left <= right:
89+
mid = (left + right) // 2
90+
children_count = sum(pile // mid for pile in candies)
91+
if children_count >= k:
92+
result = mid
93+
left = mid + 1
94+
else:
95+
right = mid - 1
96+
return result
97+
'''

0 commit comments

Comments
 (0)