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
المشتركون
-124 ساعات
-37 أيام
-1730 أيام
أرشيف المشاركات
1 240
28 ديسمبر 2024, 01:15
28 ديسمبر 2024, ~01:15
1 240
2461. Maximum Sum of Distinct Subarrays With Length K
You are given an integer array nums and an integer k. Find the maximum subarray sum of all the subarrays of nums that meet the following conditions: The length of the subarray is k, and All the elements of the subarray are distinct. Return the maximum subarray sum of all the subarrays that meet the conditions. If no subarray meets the conditions, return 0. A subarray is a contiguous non-empty sequence of elements within an array. Example 1: Input: nums = [1,5,4,2,9,9,9], k = 3 Output: 15 Explanation: The subarrays of nums with length 3 are: - [1,5,4] which meets the requirements and has a sum of 10. - [5,4,2] which meets the requirements and has a sum of 11. - [4,2,9] which meets the requirements and has a sum of 15. - [2,9,9] which does not meet the requirements because the element 9 is repeated. - [9,9,9] which does not meet the requirements because the element 9 is repeated. We return 15 because it is the maximum subarray sum of all the subarrays that meet the conditions Example 2: Input: nums = [4,4,4], k = 3 Output: 0 Explanation: The subarrays of nums with length 3 are: - [4,4,4] which does not meet the requirements because the element 4 is repeated. We return 0 because no subarrays meet the conditions. Constraints: 1 <= k <= nums.length <= 105 1 <= nums[i] <= 105
28 ديسمبر 2024, 01:13
1 240
"""
Question Description
Given an input string s, reverse the order of the words.
A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.
Return a string of the words in reverse order concatenated by a single space.
Note that s may contain leading or trailing spaces or multiple spaces between two words.
The returned string should only have a single space separating the words. Do not include any extra spaces.
Example 1:
Input: s = "the sky is blue"
Output: "blue is sky the"
Example 2:
Input: s = " hello world "
Output: "world hello"
Explanation: Your reversed string should not contain leading or trailing spaces.
Example 3:
Input: s = "a good example"
Output: "example good a"
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
Constraints:
1 <= s.length <= 104
s contains English letters (upper-case and lower-case), digits, and spaces ' '.
There is at least one word in s.
"a good example"
["a","good","example"]
L R
[example,"good","a"]
"""
#will s will split
#variables left->0 rght ->len(splited)-1
#will have a while l<L left+=1 righ-=1
def revercing_word(s):
words=[]
for word in s.split(" "):
if word:
words.append(word)
left=0
right=len(words)-1
while left<right:
words[left],words[right]= words[right],words[left]
left+=1
right-=1
return " ".join(words)
print(revercing_word("hello world"))27 ديسمبر 2024, 12:56
1 240
Todays interview question
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string s, return true if it is a palindrome, or false otherwise. Example 1: Input: s = "A man, a plan, a canal: Panama" Output: true Explanation: "amanaplanacanalpanama" is a palindrome. Example 2: Input: s = "race a car" Output: false Explanation: "raceacar" is not a palindrome. Example 3: Input: s = " " Output: true Explanation: s is an empty string "" after removing non-alphanumeric characters. Since an empty string reads the same forward and backward, it is a palindrome. Constraints: 1 <= s.length <= 2 * 105 s consists only of printable ASCII characters.
27 ديسمبر 2024, 12:54
1 240
Today's Interview Question You are given a string s, which contains stars *. In one operation, you can: Choose a star in s. Remove the closest non-star character to its left, as well as remove the star itself. Return the string after all stars have been removed. Note: The input will be generated such that the operation is always possible. It can be shown that the resulting string will always be unique. Example 1: Input: s = "leet**cod*e" Output: "lecoe" Explanation: Performing the removals from left to right: - The closest character to the 1st star is 't' in "leet**cod*e". s becomes "lee*cod*e". - The closest character to the 2nd star is 'e' in "lee*cod*e". s becomes "lecod*e". - The closest character to the 3rd star is 'd' in "lecod*e". s becomes "lecoe". There are no more stars, so we return "lecoe". Example 2: Input: s = "erase*****" Output: "" Explanation: The entire string is removed, so we return an empty string. Constraints: 1 <= s.length <= 10**5 s consists of lowercase English letters and stars *. The operation above can be performed on s.```
def fun(s):
stack = []
for c in s:
if c != "*":
stack.append(c)
else:
stack.pop()
return "".join(stack)
print(fun("erase*****"))
`27 ديسمبر 2024, 00:28
1 240
Iterating through a string and appending to an empty string in Python has O(n²) time complexity because strings are immutable, and each append involves copying the entire string so far.
Optimized Approach: Use a list to collect characters and join them at the end:
result = []
for char in string:
result.append(char)
final_string = ''.join(result)
This reduces the time complexity to O(n).25 ديسمبر 2024, 13:33
1 240
24 ديسمبر 2024, 11:43
1 240
24 ديسمبر 2024, 11:42
26 ديسمبر 2024, ~14:31
1 240
2760. Longest Even Odd Subarray With Threshold
You are given a 0-indexed integer array nums and an integer threshold.
Find the length of the longest subarray of nums starting at index l and ending at index r (0 <= l <= r < nums.length) that satisfies the following conditions:
nums[l] % 2 == 0
For all indices i in the range [l, r - 1], nums[i] % 2 != nums[i + 1] % 2
For all indices i in the range [l, r], nums[i] <= threshold
Return an integer denoting the length of the longest such subarray.
Note: A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [3,2,5,4], threshold = 5
Output: 3
Explanation: In this example, we can select the subarray that starts at l = 1 and ends at r = 3 => [2,5,4]. This subarray satisfies the conditions.
Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length.
Example 2:
Input: nums = [1,2], threshold = 2
Output: 1
Explanation: In this example, we can select the subarray that starts at l = 1 and ends at r = 1 => [2].
It satisfies all the conditions and we can show that 1 is the maximum possible achievable length.
Example 3:
Input: nums = [2,3,4,5], threshold = 4
Output: 3
Explanation: In this example, we can select the subarray that starts at l = 0 and ends at r = 2 => [2,3,4].
It satisfies all the conditions.
Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length.
Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 100
1 <= threshold <= 100
you can test on the leetcode
23 ديسمبر 2024, 12:17
1 240
if u want the answers for the questions, ask in the comment section
22 ديسمبر 2024, 12:51
1 240
🌟 Check Out Someones Journey! 🌟
Hey everyone! I want to take a moment to spotlight one of our amazing community members, Samirawit. If you’re looking for valuable insights, tips, and resources in the tech world, you definitely need to check out her channel!
🔗 Someones Journey (https://t.me/sam12rawit789)
Let’s support each other and grow together! Don’t forget to subscribe and show some love! ❤️
22 ديسمبر 2024, 12:44
26 ديسمبر 2024, ~14:31
1 240
do also matrix questions this one of question asked in the interview
"""
Given an array of integer arrays arrays where each arrays[i] is sorted in strictly increasing order,
return an integer array representing the longest common subsequence among all the arrays.
A subsequence is a sequence that can be derived from another sequence by deleting some elements
(possibly none) without changing the order of the remaining elements.
Example 1:
Input: arrays = [[1,3,4],
[1,4,7,9]]
Output: [1,4]
Explanation: The longest common subsequence in the two arrays is [1,4].
Example 2:
Input: arrays = [[2,3,6,8],
[1,2,3,5,6,7,10],
[2,3,4,6,9]]
Output: [2,3,6]
Explanation: The longest common subsequence in all three arrays is [2,3,6].
Example 3:
Input: arrays = [[1,2,3,4,5],
[6,7,8]]
Output: []
Explanation: There is no common subsequence between the two arrays.
"""22 ديسمبر 2024, 12:35
1 240
Join our group and let's build a community of tech friends. The best thing about networking is that you might meet someone who shares a job opportunity with you, helping you land your first job. That friend could support you on your tech journey, or you could be that friend for someone else. I also want to be part of this group, so feel free to connect with me.
https://t.me/+m835nLa3A0c2MjBk
22 ديسمبر 2024, 03:30
1 240
I am also practicing for the A2SV interview, so please wish me good luck😁
22 ديسمبر 2024, 02:54
1 240
Given an integer array nums of 2n integers, group these integers into n pairs (a1, b1), (a2, b2), ..., (an, bn) such that the sum of min(ai, bi) for all i is maximized. Return the maximized sum. Example 1: Input: nums = [1,4,3,2] Output: 4 Explanation: All possible pairings (ignoring the ordering of elements) are: 1. (1, 4), (2, 3) -> min(1, 4) + min(2, 3) = 1 + 2 = 3 2. (1, 3), (2, 4) -> min(1, 3) + min(2, 4) = 1 + 2 = 3 3. (1, 2), (3, 4) -> min(1, 2) + min(3, 4) = 1 + 3 = 4 So the maximum possible sum is 4. Example 2: Input: nums = [6,2,6,5,1,2] Output: 9 Explanation: The optimal pairing is (2, 1), (2, 5), (6, 6). min(2, 1) + min(2, 5) + min(6, 6) = 1 + 2 + 6 = 9. 1 2 2 5 6 6 Constraints: 1 <= n <= 104 nums.length == 2 * n -104 <= nums[i] <= 104
22 ديسمبر 2024, 01:44
1 240
Repost from A2SV - Discussion
Hello fellow coders,
After a brief break, we've returned! Get ready for our fun weekly community coding contest happening every Saturday 🗓. It's your chance to compete with other awesome programmers, learn more about coding, and, most importantly, have a great time! 🥳
🏆 Contest Details:
📅 Date: December 21, 2024
⏰ Time: 6:00 PM
🔗 Link for Contest: Community Weekly Contest - Contest No 6
P.S. Remember to fill out this form to secure your spot for our upcoming community classes and lectures. It’s your golden ticket to enriching experiences you won’t want to miss. 📚✨
21 ديسمبر 2024, 09:20
10 يوليو 2025, ~05:41
