Leetcode with dani
رفتن به کانال در Telegram
Join us and let's tackle leet code questions together: improve your problem-solving skills Preparing for coding interviews learning new algorithms and data structures connect with other coding enthusiasts
نمایش بیشتر1 240
مشترکین
اطلاعاتی وجود ندارد24 ساعت
-47 روز
-2230 روز
آرشیو پست ها
1 240
class Solution:
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
def helper(qu):
if not qu:
return
i = len(qu)
temp = []
nextsize = 0
while i > 0 :
poped = qu.popleft()
temp.append(poped.val)
if poped.left:
qu.append(poped.left)
nextsize += 1
if poped.right:
qu.append(poped.right)
nextsize += 1
i -= 1
ans.append(temp)
helper(qu)
if not root:
return []
ans =[]
q = deque()
q.append(root)
helper(q)
return ans20 مارس 2025, 12:12
1 240
102. Binary Tree Level Order Traversal
Difficulty: Medium
▎Problem Statement
Given the root of a binary tree, return the level order traversal of its nodes' values (i.e., from left to right, level by level).
▎Examples
Example 1:
• Input:
root = [3,9,20,null,null,15,7]
• Output: [[3],[9,20],[15,7]]
Example 2:
• Input: root = [1]
• Output: [[1]]
Example 3:
• Input: root = []
• Output: []
▎Constraints
• The number of nodes in the tree is in the range [0, 2000].
• -1000 <= Node.val <= 100020 مارس 2025, 04:25
1 240
Here is our very first leetcode Tree problem (BFS). Answer will be posted at 6:00 PM
20 مارس 2025, 04:16
1 240
Here is our very first leetcode Tree problem (BFS). Answer will be posted at 6:00 PM
20 مارس 2025, 04:15
20 مارس 2025, ~04:16
1 240
19 مارس 2025, 08:58
19 مارس 2025, ~08:58
1 240
A big thank you to everyone at this channel and Alpha ,for a great session! I really enjoyed it.
17 مارس 2025, 15:40
1 240
Repost from Codeforces Official
Educational Codeforces Round 176
(rated for Div. 2) starts in ~2 hours.
Please, join by the link https://codeforces.com/contests/2075
17 مارس 2025, 08:44
1 240
Repost from Information Systems Hub 💻🔁
Get ready for a LeetCode problem-solving session with Daniel Mola today at 8:00 PM (2:00 LT)! Don’t forget to share with your friends💥
17 مارس 2025, 07:34
20 مارس 2025, ~09:38
1 240
16 مارس 2025, 04:20
1 240
Repost from Codeforces Official
Unfortunately, due to technical reasons, the round is postponed by 8.5 hours (to the usual scheduled time). We apologize for any inconvenience caused.
15 مارس 2025, 02:08
1 240
135. Candy
Hard
There are n children standing in a line. Each child is assigned a rating value given in the integer array ratings.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
Return the minimum number of candies you need to have to distribute the candies to the children.
Example 1:
Input: ratings = [1,0,2]
Output: 5
Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input: ratings = [1,2,2]
Output: 4
Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it satisfies the above two conditions.
Constraints:
n == ratings.length
1 <= n <= 2 * 104
0 <= ratings[i] <= 2 * 104
class Solution:
def candy(self, ratings: List[int]) -> int:
n = len(ratings)
candies = [1] * n # Give every child 1 candy initially
# Left-to-right pass: if a child has a higher rating than the left neighbor, give one more candy
for i in range(1, n):
if ratings[i] > ratings[i - 1]:
candies[i] = candies[i - 1] + 1
# Right-to-left pass: if a child has a higher rating than the right neighbor, ensure they have more candy
for i in range(n - 2, -1, -1):
if ratings[i] > ratings[i + 1]:
candies[i] = max(candies[i], candies[i + 1] + 1)
return sum(candies)
i didnt know it had a simple ans; my ans is like thousand lines14 مارس 2025, 17:12
1 240
135. Candy
Hard
There are n children standing in a line. Each child is assigned a rating value given in the integer array ratings.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
Return the minimum number of candies you need to have to distribute the candies to the children.
Example 1:
Input: ratings = [1,0,2]
Output: 5
Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input: ratings = [1,2,2]
Output: 4
Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it satisfies the above two conditions.
Constraints:
n == ratings.length
1 <= n <= 2 * 104
0 <= ratings[i] <= 2 * 104
class Solution:
def candy(self, ratings: List[int]) -> int:
n = len(ratings)
candies = [1] * n # Give every child 1 candy initially
# Left-to-right pass: if a child has a higher rating than the left neighbor, give one more candy
for i in range(1, n):
if ratings[i] > ratings[i - 1]:
candies[i] = candies[i - 1] + 1
# Right-to-left pass: if a child has a higher rating than the right neighbor, ensure they have more candy
for i in range(n - 2, -1, -1):
if ratings[i] > ratings[i + 1]:
candies[i] = max(candies[i], candies[i + 1] + 1)
return sum(candies)
i didnt know it had a simple ans my ans was like thousands line14 مارس 2025, 17:09
14 مارس 2025, ~17:10
1 240
135. Candy
Hard
There are n children standing in a line. Each child is assigned a rating value given in the integer array ratings.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
Return the minimum number of candies you need to have to distribute the candies to the children.
Example 1:
Input: ratings = [1,0,2]
Output: 5
Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input: ratings = [1,2,2]
Output: 4
Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it satisfies the above two conditions.
Constraints:
n == ratings.length
1 <= n <= 2 * 104
0 <= ratings[i] <= 2 * 104
class Solution:
def candy(self, ratings: List[int]) -> int:
total = 0
count = 0
i=0
ratings.append(ratings[-1])
cond = True
maxm = 0
while i<len(ratings)-1:
while i<len(ratings)-1 and ratings[i] > ratings[i+1]:
if cond or count:
total+= 1+count
count+=1
i+=1
if count==maxm:
count+=1
total+=1
cond = True
count = 0
maxm = 0
while i<len(ratings)-1 and ratings[i] < ratings[i+1]:
total+= 1+count
count+=1
i+=1
cond = False
maxm +=1
while i<len(ratings)-1 and ratings[i] == ratings[i+1]:
cond = True
i+=1
maxm = 0
count=0
return len(ratings)+total-114 مارس 2025, 17:00
14 مارس 2025, ~17:00
1 240
330. Patching Array
Difficulty: Hard
Problem Statement:
Given a sorted integer array nums and an integer n, add/patch elements to the array such that any number in the range [1, n] (inclusive) can be formed by the sum of some elements in the array.
Return the minimum number of patches required.
Example 1:
Input: nums = [1, 3], n = 6
Output: 1
Explanation:
The combinations of nums are [1], [3], [1, 3], which form possible sums of 1, 3, 4.
Now if we add/patch 2 to nums, the combinations become [1], [2], [3], [1, 3], [2, 3], [1, 2, 3], covering all sums 1, 2, 3, 4, 5, 6.
Thus, only 1 patch is needed.
Example 2:
Input: nums = [1, 5, 10], n = 20
Output: 2
Explanation:
The two patches can be [2, 4].
Example 3:
Input: nums = [1, 2, 2], n = 5
Output: 0
Constraints:
1 <= nums.length <= 1000
1 <= nums[i] <= 10^4
nums is sorted in ascending order.
1 <= n <= 2^31 - 1
Solution :
class Solution:
def minPatches(self, nums: List[int], n: int) -> int:
patches = 0
miss = 1
i = 0
while miss <= n:
if i < len(nums) and nums[i] <= miss:
miss += nums[i]
i += 1
else:
miss += miss
patches += 1
return patches14 مارس 2025, 16:47
