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 262
订阅者
无数据24 小时
+17 天
+830 天
帖子存档
1 262
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums = sorted(nums)
list1 = []
for i in range(len(nums)-1,-1,-1):
j = i - 1
left =0
if (i>1 and i!=(len(nums)-1) and nums[i]==nums[i+1]):
continue
while(j>-1 and left<=i-2 and j!=left):
total = nums[left] + nums[i] + nums[j]
if ( total)== 0:
list1.append([nums[left],nums[i],nums[j]])
left+=1
while(left<j and nums[left]==nums[left-1]):
left+=1
elif total > 0:
j-=1
else:
left+=1
return list11 262
✨ Master Python Division:
int(x / y) vs. x // y ✨
Ever wonder about the difference between int(x / y) and x // y in Python? Let’s break it down! 🧠👇
int(x / y):
- Performs regular division first (x / y), giving a floating-point result.
- Then, truncates the result to an integer using int(), removing the decimal part.
- Example:
- int(7 / 3) ➡️ 2 (since 7 / 3 ≈ 2.3333, truncates to 2).
- int(-7 / 3) ➡️ -2 (since -7 / 3 ≈ -2.3333, truncates to -2).
x // y (Floor Division):
- Directly performs floor division, rounding down to the nearest integer.
- Example:
- 7 // 3 ➡️ 2 (largest integer ≤ 2.3333).
- -7 // 3 ➡️ -3 (largest integer ≤ -2.3333).
Key Differences:
- Rounding:
- int(x / y) truncates towards zero.
- x // y rounds down towards negative infinity.
- Negative numbers:
- For positive results, both are the same.
- For negative results, int(x / y) truncates, but x // y rounds down.
Example with Negative Numbers:
- int(-7 / 3) ➡️ -2
- -7 // 3 ➡️ -31 262
When working with division in Python, you might encounter two common ways to divide numbers: int(x / y) and x // y. Here’s how they differ:
int(x / y):
This performs regular division first (x / y), giving a floating-point result.
It then converts that result to an integer using int(), which truncates the decimal part (rounds towards zero).
Examples:
int(7 / 3) results in 2 (as 7 / 3 equals approximately 2.3333).
int(-7 / 3) results in -2 (as -7 / 3 equals approximately -2.3333).
x // y (Floor Division):
This directly performs floor division, which divides x by y and rounds down to the nearest integer (towards negative infinity).
Examples:
7 // 3 results in 2 (the largest integer less than or equal to 2.3333).
-7 // 3 results in -3 (the largest integer less than or equal to -2.3333).
Key Differences:
Rounding Direction:
int(x / y) truncates towards zero.
x // y rounds down towards negative infinity.
Handling Negative Numbers:
For positive numbers, both methods yield the same result.
For negative numbers, int(x / y) truncates, while x // y rounds down.
Example with Negative Numbers:
int(-7 / 3) gives -2 (truncation).
-7 // 3 gives -3 (floor division).
1 262
The difference between
int(x / y) and x // y in Python lies in how they handle division and what they return as a result.
### 1. `int(x / y)`:
- Operation: First performs regular (floating-point) division x / y, which gives a floating-point result.
- Casting: The result is then explicitly converted to an integer using int(), which truncates the decimal part (rounds towards zero).
- Result: The result is an integer value.
Example:
int(7 / 3) # Result: 2
int(-7 / 3) # Result: -2
- 7 / 3 gives 2.3333..., and int() truncates it to 2.
- -7 / 3 gives -2.3333..., and int() truncates it to -2.
### 2. `x // y` (Floor Division):
- Operation: Performs floor division, which means it divides x by y and rounds down the result to the nearest integer towards negative infinity.
- Result: Returns the largest integer less than or equal to the result of x / y.
Example:
7 // 3 # Result: 2
-7 // 3 # Result: -3
- 7 // 3 gives 2 because it’s the largest integer less than or equal to 2.3333....
- -7 // 3 gives -3 because floor division rounds towards negative infinity, so -2.3333... becomes -3.
### Key Differences:
1. Rounding Direction:
- int(x / y) truncates (rounds towards zero).
- x // y uses floor division (rounds down towards negative infinity).
2. Handling Negative Numbers:
- For positive numbers, both yield the same result.
- For negative numbers, int(x / y) truncates towards zero, while x // y rounds down.
### Example with negative numbers:
int(-7 / 3) # Result: -2
-7 // 3 # Result: -3
In this case, int(-7 / 3) gives -2 (truncating towards zero), while -7 // 3 gives -3 (rounding towards negative infinity).1 262
In my opinion, if you're preparing for an interview and have no enough time left, it’s best not to spend more than 45 minutes on a single problem. Instead, I believe you should focus on understanding different patterns and concepts. This approach can help you become more versatile and better equipped to tackle various questions during the interview. what do you think?
1 262
in my opinion, If you're preparing for an interview and have enough time left, avoid spending more than 45 minutes or on a single problem. Instead, focus on understanding different patterns and concepts. This approach will help you become more versatile and better equipped to tackle various questions during the interview.
1 262
#Q20 #leet_codeQ15 Medium title. 3Sum
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets.
Example 1:
Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Explanation:
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
Example 2:
Input: nums = [0,1,1]
Output: []
Explanation: The only possible triplet does not sum up to 0.
Example 3:
Input: nums = [0,0,0]
Output: [[0,0,0]]
Explanation: The only possible triplet sums up to 0.
1 262
Your go-to source for IELTS preparation. We offer tips, practice exercises, and expert advice to help you excel in listening, reading, writing, and speaking. Achieve your target score with our step-by-step guidance in both modules!!!
https://t.me/IELTSBoostt
1 262
ስቴም ፖወር !
ኢትዮጵያ ለሚገኙ ወጣት ሴቶች ለ3ተኛ ዙር የቀረበ ጥሪ !
ስቴም ፖወር (STEMpower) ከፊንላንድ ኤምባሲ (Embassy of Finland Ethiopia) እና ከአይቢኤም (IBM) ጋር በመተባበር በኢትዮጵያ ከ1,000 በላይ ሥራ አጥ ወጣት ሴቶችን ተግባራዊ የሥራ ክህሎት ለመስጠት ያለመ " ትጋት " የተሰኘ ፕሮግራም አስተዋውቀዋል።
ባለፉት 2 ዙሮች 1700 በላይ ለሚሆኑ ሴቶች በ Project Management, Web Development , Cyber Security, Digital Marketing , Data Analytics , Information Technology , Job Readiness , Work Readiness ስልጠና የሰጠ ከመሆኑ በተጨማሪ የስራ እድል በመፈጠር ተጠቃሚ እንዲሆኑ እድል አመቻችቷል፡፡
#ነጻ በሚሰጠውና ዓለም አቀፍ እውቅና ያለው ሰርተፊኬት በሚያስገኘው በዚህ ስልጠና እነዚህን ኮርሶች ለመውሰድ ፍላጎት ያላችሁ ሴቶች ምዝገባ የጀመርን መሆኑን እያሳወቅን ምዝገባው የሚቆየው ከመስከረም 30 እስከ ጥቅምት 10 , 2017 ዓ/ም ሲሆን ቀድማችሁ በመመዝገብ የእድሉ ተጠቃሚ እንድትሆኑ እንጋብዛለን፡፡
እነዚህን ኮርሶች ለመውሰድ ፍላጎት ያላችሁ ከስር ባለውን ሊንክ ይመዝገቡ
👉Link: https://forms.office.com/r/bJedLrdqmd
1 262
Enjoy our content? Advertise on this channel and reach a highly engaged audience! 👉🏻
It's easy with Telega.io. As the leading platform for native ads and integrations on Telegram, it provides user-friendly and efficient tools for quick and automated ad launches.
⚡️ Place your ad here in three simple steps:
1 Sign up
2 Top up the balance in a convenient way
3 Create your advertising post
If your ad aligns with our content, we’ll gladly publish it.
Start your promotion journey now!
1 262
Ultrapro የሚባል ልክ እንደ Binance Exchage በ App መጥቶላችኋል 👌
✅አዲስ እንደመሆናቸው መጠን ለአዲስ ተመዝጋቢ በነፃ 25$ እየሰጡ ነው 🥳 ይህን Giveaway መካፈል የምትችሉት እስከ ለሊቱ 6 ሰአት ብቻ ነው so ቤተሰብ ይህ እድል እንዲያመልጣችሁ አልፈልግም
✅የዚህ App ስም Ultrapro exchange " ይባላል ከ playstore ላይ ታገኙታላችው ነገር ግን አፑ እኛ አገር ስለማይሰራ በ VPN ተጠቅማችሁ አውርዱት 👌 ነገር ግን የ USA Address አትጠቀሙ
✅ቀጥሎ የምታደርጉት sign up በማድረግ በ Email regester አድርጉ
✅Refferal code ከጠየቃችሁ ይሄን አስገቡ
➡️
UTX23975490
✅Withdraw ማድረግ የሚጀምረው እንደተናገሩት ከሆነ ከነገ ጀምሮ ነው ነገር ግን KYC ነገ መጠየቅ ስለሚጀምሩ ግዴታ ነው 🤏
✅KYC ከጨረሳችሁ ገንዘባችሁን ወደፈለጋችሁት ማውጣት ትችላላችው
ነገ Appu launch ስለሚደረግ በቻላችሁት መጠን ፍጠኑ
APP LINK ➡️ https://play.google.com/store/apps/details?id=com.application.ultrapro1 262
https://t.me/catizenbot/gameapp?startapp=rp_29599749
kezih befit dogs airdrop recommended argiyachu nebr yeheme ke 11 ken beuala list yderegal silver derega deresu
1 262
🚀 Explore Palindrome Challenges with the Two-Pointer Technique!🧑💻
Palindromes are a classic problem type that can be efficiently tackled using the two-pointer technique. Whether you're just starting or looking to refine your skills, these questions will help you master this approach. Check out these palindrome problems:
1.Valid Palindrome
🔗 [Link](https://leetcode.com/problems/valid-palindrome/)
Description: Determine if a string is a palindrome, considering only alphanumeric characters and ignoring cases.
2. Valid Palindrome II
🔗 [Link](https://leetcode.com/problems/valid-palindrome-ii/)
Description: Can the string become a palindrome by deleting just one character?
3. Palindrome Linked List
🔗 [Link](https://leetcode.com/problems/palindrome-linked-list/)
Description: Check if a singly linked list is a palindrome using two pointers and a bit of list manipulation.
4. Longest Palindromic Substring
🔗 [Link](https://leetcode.com/problems/longest-palindromic-substring/)
Description: Find the longest palindromic substring in a given string, with pointers expanding from each character.
5. Palindromic Substrings
🔗 [Link](https://leetcode.com/problems/palindromic-substrings/)
Description: Count how many palindromic substrings are present in the string using two pointers.
6. Shortest Palindrome.
🔗 [Link](https://leetcode.com/problems/shortest-palindrome/)
Description: Find the shortest palindrome by adding characters to the start of the string.
7. Palindrome Partitioning
🔗 [Link](https://leetcode.com/problems/palindrome-partitioning/)
Description: Partition a string into all possible palindromic substrings.
8. Palindrome Pairs
🔗 [Link](https://leetcode.com/problems/palindrome-pairs/)
Description: Given a list of words, find all pairs of distinct indices that form palindromes.
Ready to boost your problem-solving skills? Dive into these problems, practice your two-pointer technique, and ace those palindrome challenges! 💥
Happy Coding! 💻💡
1 262
▎1. 3Sum
def threeSum(nums):
nums.sort()
result = []
for i in range(len(nums) - 2):
if i > 0 and nums[i] == nums[i - 1]:
continue
left, right = i + 1, len(nums) - 1
while left < right:
total = nums[i] + nums[left] + nums[right]
if total < 0:
left += 1
elif total > 0:
right -= 1
else:
result.append([nums[i], nums[left], nums[right]])
while left < right and nums[left] == nums[left + 1]:
left += 1
while left < right and nums[right] == nums[right - 1]:
right -= 1
left += 1
right -= 1
return result
▎2. Minimum Window Substring
from collections import Counter
def minWindow(s, t):
if not t or not s:
return ""
dict_t = Counter(t)
required = len(dict_t)
l, r = 0, 0
formed = 0
window_counts = {}
ans = float("inf"), None, None
while r < len(s):
character = s[r]
window_counts[character] = window_counts.get(character, 0) + 1
if character in dict_t and window_counts[character] == dict_t[character]:
formed += 1
while l <= r and formed == required:
character = s[l]
if r - l + 1 < ans[0]:
ans = (r - l + 1, l, r)
window_counts[character] -= 1
if character in dict_t and window_counts[character] < dict_t[character]:
formed -= 1
l += 1
r += 1
return "" if ans[0] == float("inf") else s[ans[1]: ans[2] + 1]
▎3. Fruit Into Baskets
def totalFruits(fruits):
left, right = 0, 0
basket = {}
max_fruits = 0
while right < len(fruits):
basket[fruits[right]] = basket.get(fruits[right], 0) + 1
while len(basket) > 2:
basket[fruits[left]] -= 1
if basket[fruits[left]] == 0:
del basket[fruits[left]]
left += 1
max_fruits = max(max_fruits, right - left + 1)
right += 1
return max_fruits
1 262
1. 3Sum (Hard)
Answer: The solution involves sorting the array and using a two-pointer technique to find triplets that sum to zero. The time complexity is O(n^2).
2. Minimum Window Substring (Hard)
Answer: Use a sliding window approach to maintain a count of characters and expand/shrink the window until the minimum substring is found. The time complexity is O(n).
3. Fruit Into Baskets (Easy)
Answer: Utilize a sliding window to track the types of fruits and their counts, ensuring you only have two types at any time. The maximum length of the window gives the answer. The time complexity is O(n).
