Leetcode with dani – Telegram
Leetcode with dani
1.31K subscribers
197 photos
14 videos
56 files
240 links
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
Download Telegram
How do you convert a tuple into a list in Python?
a) tuple.toList()
b) list(tuple)
c) tuple.list()
What will be the output of the following code?

print('Hello' * 3)
Anonymous Quiz
69%
a) HelloHelloHello
8%
b) HelloHelloHelloHello
9%
c) Hello, Hello, Hello
14%
D) Error
Leetcode with dani
https://youtu.be/jM2dhDPYMQM?si=WG3yrnoQvRyEvnkm
i recommend this video for those who want to learn sliding window algorithm , we will be working on this algorithm in the coming days
#leet_code_Q_13 #easy #Sliding_window #Q_219 219. Contains Duplicate II
Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k.



Example 1:

Input: nums = [1,2,3,1], k = 3
Output: true
Example 2:

Input: nums = [1,0,1,1], k = 1
Output: true
Example 3:

Input: nums = [1,2,3,1,2,3], k = 2
Output: false
👍2❤‍🔥1
out of topic I just watched an amazing video that dives into the theory of relativity and how time works.

It’s fascinating how scientists always talk about light, and I used to think they had a beef with speed of light 😂 But now I understand why speed of light is so special and its profound connection to time. https://youtu.be/5BBHEZFearI?si=dGWI303LG9XXgQYh
👍5👀1
share your ideas
Websites To Increase Your Problem-Solving Skills

LeetCode: LeetCode is widely used for practicing coding problems, especially for preparing for technical interviews. It has a vast collection of problems categorized by difficulty and topic.

HackerRank: HackerRank offers challenges in various domains like algorithms, data structures, artificial intelligence, and more. It is also a good platform for preparing for coding interviews.

CodeSignal: CodeSignal provides a variety of coding challenges and also has a feature to simulate technical interviews.

Codeforces: Codeforces is a competitive programming platform that hosts regular contests. It’s a great place to practice algorithmic problems and improve your speed and accuracy.

AtCoder: AtCoder is a Japanese competitive programming site that offers regular contests and a rich set of problems to solve.

TopCoder: TopCoder is one of the original competitive programming platforms. It offers Single Round Matches (SRMs) and has a large problem archive.

Exercism: Exercism offers exercises in various programming languages with mentor feedback.

Codewars: Codewars provides a community-driven platform where you can solve coding challenges and compare solutions with others.
Leetcode with dani
#leet_code_Q_13 #easy #Sliding_window #Q_219 219. Contains Duplicate II Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k. …
## Intuition
To solve this problem, we can use a sliding window approach along with a hash map (dictionary) to efficiently check for duplicate elements within a given range. The idea is to maintain a window of size k and check if any element within this window is duplicated.

## Algorithm
1. Initialize an empty hash map (dictionary) to store the most recent index of each element.
2. Iterate over the array nums:
- For each element nums[i]:
- If nums[i] is already in the hash map and the difference between the current index i and the stored index j is less than or equal to k, return true.
- Update the hash map to store the current index i of nums[i].
3. If no duplicates within the range k are found, return false.

## Code
def containsNearbyDuplicate(nums, k):
num_indices = {}

for i, num in enumerate(nums):
if num in num_indices and i - num_indices[num] <= k:
return True
num_indices[num] = i

return False

# Example 1
print(containsNearbyDuplicate([1, 2, 3, 1], 3)) # Output: True

# Example 2
print(containsNearbyDuplicate([1, 0, 1, 1], 1)) # Output: True

# Example 3
print(containsNearbyDuplicate([1, 2, 3, 1, 2, 3], 2)) # Output: False


## Complexity Analysis
- Time Complexity: O(n), where n is the length of the array nums. We only need to iterate through the array once.
- Space Complexity: O(min(n, k)), where n is the length of the array. The space used by the hash map is proportional to the number of unique elements within any window of size k. In the worst case, this is O(n), but usually, it's limited by k.

This algorithm ensures that we efficiently find any duplicates within a specified range, making use of the hash map for quick lookups and updates.
👌4👍2
#leet_code_Q_14 #Q_2269 #easy #Sliding_window
2269. Find the K-Beauty of a Number
The k-beauty of an integer num is defined as the number of substrings of num when it is read as a string that meet the following conditions:

It has a length of k.
It is a divisor of num.
Given integers num and k, return the k-beauty of num.

Note:

Leading zeros are allowed.
0 is not a divisor of any value.
A substring is a contiguous sequence of characters in a string.



Example 1:

Input: num = 240, k = 2
Output: 2
Explanation: The following are the substrings of num of length k:
- "24" from "240": 24 is a divisor of 240.
- "40" from "240": 40 is a divisor of 240.
Therefore, the k-beauty is 2.
Constraints:

1 <= num <= 109
1 <= k <= num.length (taking num as a string)
👍32
#leet_code_Q15 #Q_209 #Medium #Sliding_window
209. Minimum Size Subarray Sum
Given an array of positive integers nums and a positive integer target, return the minimal length of a
subarray
whose sum is greater than or equal to target. If there is no such subarray, return 0 instead.

Example 1:

Input: target = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: The subarray [4,3] has the minimal length under the problem constraint.
Example 2:

Input: target = 4, nums = [1,4,4]
Output: 1
Example 3:

Input: target = 11, nums = [1,1,1,1,1,1,1,1]
Output: 0

Constraints:

1 <= target <= 109
1 <= nums.length <= 105
1 <= nums[i] <= 104

Follow up: If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log(n)).
👍41
What will be the output of the following
Python Code?
>>> a = 12 print(a in [x for x in range(12)]) Choose the correct option:-
Anonymous Quiz
21%
A. True
48%
B. False
29%
C. Error
2%
D. None of above
👍4
out of topic
You are in a room with three light switches. Each switch controls one of three light bulbs in an adjacent room, but you do not know which switch controls which bulb. You can only go into the room once to check the state of the bulbs. How can you determine which switch controls which bulb?
🤯4👍1
Leetcode with dani
#leet_code_Q15 #Q_209 #Medium #Sliding_window 209. Minimum Size Subarray Sum Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such…
Solution:


class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
minm = len(nums) + 1
left = 0
tot = 0

for i in range(len(nums)):
tot += nums[i]
while tot >= target:
minm = min(minm, i - left + 1)
tot -= nums[left]
left += 1

if minm > len(nums):
return 0
return minm

Explanation:
Initialize Variables:

minm is initialized to a value greater than the length of nums to ensure any valid subarray length found will be smaller.
left is the starting index of the current subarray.
tot is the sum of the current subarray.
Iterate Through nums:

Add each element to tot as we iterate through the array with index i.
Adjust left Pointer:

If tot is greater than or equal to target, adjust the left pointer to find the minimal subarray length.
Continuously subtract the value at nums[left] from tot and increment left until tot is less than target.
During this process, update minm with the length of the current valid subarray if it's smaller than the previously recorded minm.
Return Result:

If no valid subarray is found, minm will remain greater than len(nums) and the function will return 0.
Otherwise, it returns the minimal length of the subarray.
This approach ensures an efficient solution with a time complexity of O(n), where n is the length of the input array nums. The sliding window technique optimally finds the minimal subarray length in a single pass through the array.
3👍1
What is the output of the following code?

x = [1, 2, 3] y = [x, x, x] y[0][0] = 10 print(y)
Anonymous Quiz
44%
a) [[10, 2, 3], [1, 2, 3], [1, 2, 3]]
47%
b) [[10, 2, 3], [10, 2, 3], [10, 2, 3]]
9%
c) [[10, 2, 3], [1, 2, 3], [10, 2, 3]]
🔥6
x = 5
y = [x,x]
y[0] = 10 print(y)
Anonymous Quiz
53%
[10,5]
28%
[10,10]
20%
[5,5]