uk
Feedback
Leetcode with dani

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
Підписники
+124 години
Немає даних7 днів
+730 день
Архів дописів
this is an interesting question , try it after that u can see my solution here
class Solution:
    def canJump(self, nums: List[int]) -> bool:
        maxReach = 0
        for i, jump in enumerate(nums):
            if i > maxReach:
                return False
            maxReach = max(maxReach, i + jump)
            if maxReach >= len(nums) - 1:
                return True
        return False
class Solution:
    def canJump(self, nums: List[int]) -> bool:
        last = len(nums)-1
        for i in range(len(nums)-2,-1,-1):
            if nums[i]+i >= last:
                last = i
        return nums[0] >= last

▎Problem: 462. Minimum Moves to Equal Array Elements II Difficulty: Medium Topics: Array, Greedy, Median Companies: [Google, Facebook, Microsoft] ▎Problem Description Given an integer array nums of size n , return the minimum number of moves required to make all array elements equal. In one move, you can increment or decrement an element of the array by 1. Note: Test cases are designed so that the answer will fit in a 32-bit integer. ▎Examples Example 1: Input:
nums = [1, 2, 3]
Output:
2
Explanation:
Only two moves are needed (each move increments or decrements one element):

• Step 1: [1, 2, 3] => [2, 2, 3]

• Step 2: [2, 2, 3] => [2, 2, 2]

---

Example 2:

Input:
nums = [1, 10, 2, 9]
Output:
16
▎Constraints

•  n = nums.length 

•  1 ≤ n ≤ 10⁵ 

•  -10⁹ ≤ nums[i] ≤ 10⁹ 

▎Explanation

To minimize the number of moves, you want to choose a target value that minimizes the sum of the absolute differences between each element and that target. It can be proven that the best target value is the median of the array.

If the array is sorted, the median minimizes the sum of absolute deviations. Thus, the minimum number of moves is given by:
moves = ∑ᵢ₌₀ⁿ⁻¹ | nums[i] - median |
Sample Python Implementation
def minMoves2(nums):
    # Sort the array to find the median.
    nums.sort()
    n = len(nums)
    median = nums[n // 2]  # Get the median value
    
    # Compute the total moves as the sum of absolute differences.
    moves = sum(abs(num - median) for num in nums)
    return moves

# Example usage:
nums1 = [1, 2, 3]
print(minMoves2(nums1))  # Output: 2

nums2 = [1, 10, 2, 9]
print(minMoves2(nums2))  # Output: 16
This implementation efficiently calculates the minimum number of moves required to make all elements in the array equal by leveraging the properties of the median. The sorting step ensures we can easily access the median value for our calculations.

Problem: 453. Minimum Moves to Equal Array Elements Difficulty: Medium Topics: Array, Greedy, Mathematics ▎Problem Description Given an integer array nums of size n , return the minimum number of moves required to make all array elements equal. In one move, you can increment n - 1 elements of the array by 1. ▎Examples Example 1: Input:
nums = [1, 2, 3]
Output:
3
Explanation: Only three moves are needed (remember each move increments two elements): • Step 1: [1, 2, 3] => [2, 3, 3] • Step 2: [2, 3, 3] => [3, 4, 3] • Step 3: [3, 4, 3] => [4, 4, 4] --- Example 2: Input:
nums = [1, 1, 1]
Output:
0
Constraints • n = nums.length • 1 ≤ n ≤ 10⁵ • -10⁹ ≤ nums[i] ≤ 10⁹ The answer is guaranteed to fit in a 32-bit integer. ▎Explanation The key insight is to realize that incrementing n - 1 elements by 1 is equivalent to decrementing a single element by 1. Therefore, the problem can be reinterpreted as finding the total number of decrement operations required to make all elements equal to the minimum element in the array. This is because each move essentially reduces the difference between an element and the minimum value. If minVal is the minimum value in nums, then the number of moves is given by:
moves = ∑ᵢ₌₀ⁿ⁻¹ (nums[i] - minVal)
Sample Python Implementation
def minMoves(nums):
    # Find the minimum value in the array.
    minVal = min(nums)
    # Compute the total number of moves required.
    moves = sum(num - minVal for num in nums)
    return moves

# Example usage:
nums = [1, 2, 3]
print(minMoves(nums))  # Output: 3
For more answers and solutions, visit LeetCode.

For more answers and solutions, visit LeetCode.

Problem: 2602. Minimum Operations to Make All Array Elements Equal Difficulty: Medium Topics: Array, Sorting, Binary Search, Prefix Sum ▎Problem Description You are given an array nums consisting of positive integers. You are also given an integer array queries of size m. For the ith query, you want to make all the elements of nums equal to queries[i]. You can perform the following operation on the array any number of times: Operation: Increase or decrease an element of the array by 1. Return an array answer of size m where answer[i] is the minimum number of operations required to make all elements of nums equal to queries[i]. Note: After each query, the array is reset to its original state. ▎Examples Example 1: Input:
nums = [3, 1, 6, 8]
queries = [1, 5]
Output:
[14, 10]
Explanation:

• For the first query (q = 1):

    • Decrease nums[0] 2 times: from 3 to 1.

    • Decrease nums[2] 5 times: from 6 to 1.

    • Decrease nums[3] 7 times: from 8 to 1.

    • Total operations = 2 + 5 + 7 = 14.

• For the second query (q = 5):

    • Increase nums[0] 2 times: from 3 to 5.

    • Increase nums[1] 4 times: from 1 to 5.

    • Decrease nums[2] 1 time: from 6 to 5.

    • Decrease nums[3] 3 times: from 8 to 5.

    • Total operations = 2 + 4 + 1 + 3 = 10.

---

Example 2:

Input:
nums = [2, 9, 6, 3]
queries = [10]
Output:
[20]
Explanation:

• Increase each element in the array to 10:

    • Operations for each element: 

        • 8 (from 2 to 10),

        • 1 (from 9 to 10),

        • 4 (from 6 to 10),

        • 7 (from 3 to 10).

    • Total operations = 8 + 1 + 4 + 7 = 20.

▎Constraints

• n = nums.length

• m = queries.length

• 1 ≤ n, m ≤ 10⁵

• 1 ≤ nums[i], queries[i] ≤ 10⁹

---

What is the object-oriented method to get rich? Inherit it! 😂😂"

Repost from Codeforces Official
Codeforces Round 1007 (Div. 2) will take place on the 28th of February at 14:35 UTC.  Please, join by the link https://codeforces.com/contests/2071?locale=en

it is started

Repost from Codeforces Official
Educational Codeforces Round 175 (rated for Div. 2) starts in ~2 hours.  Please, join by the link https://codeforces.com/contests/2070

Top 10 Non-Technical Interview Questions for FAANG Companies: Insights and Preparation Tips ▎🧠 Behavioral Leadership 1. "Tell me about a time you failed and what you learned." Tests self-awareness and growth mindset. 2. "Describe a team conflict you resolved." Assesses emotional intelligence and mediation skills. 3. "Share an example of showing leadership without authority." Evaluates initiative and influence. ▎🎯 Career Motivation 4. "Why do you want to work here specifically?" Probes company research and cultural fit. 5. "Where do you see yourself in 3-5 years?" Checks alignment with company growth paths. ▎🌟 Strengths Values 6. "What unique value would you bring to this team?" Reveals self-assessment accuracy. 7. "Describe your most innovative professional contribution." Assesses creativity and business impact. ▎🛠 Work Approach 8. "How do you prioritize when facing multiple deadlines?" Tests organizational and decision-making skills. 9. "Tell me about a project requiring deep analysis." Examines problem-solving methodology. ▎🤝 Cultural Fit 10. "What does ideal team collaboration look like to you?" Matches working style with company culture. --- ▎Key Preparation Tips: • Use the STAR (Situation-Task-Action-Result) format for behavioral answers. • Align responses with FAANG leadership principles. • Practice concise storytelling (90-120 seconds per answer). • Research specific company values (e.g., Amazon's 16 Leadership Principles). For 53 additional common questions and answer frameworks, refer to: FAANG Behavioral Guide (https://igotanoffer.com/blogs/tech/faang-interview-questions) Non-Technical Question Strategies (https://www.indeed.com/career-advice/interviewing/non-tech-interview-questions)

Did you understand Floyd’s Cycle-Finding Algorithm for Linked List? It’s pretty cool and clever!

class Solution:
    def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
        p1 = head
        p2 = head
        while p2 and p2.next:
            p1=p1.next
            p2 = p2.next.next
        return p1

class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        dummy = ListNode(-1,head)
        p1 = dummy
        p2 = dummy.next
        count = 0
        while p2:
            if count <n:
                p2 = p2.next
                count+=1
            else:
                p1 = p1.next
                p2 = p2.next
        if p1:
            p1.next = p1.next.next
        return dummy.next

class Solution:
    def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
        dummy = ListNode(0,head)
        p1 = dummy
        p2 = dummy.next
        while p2:
            if p2.val==val:
                p1.next = p2.next
                p2  = p2.next
            else:
                p1 = p1.next
                p2 = p2.next
        return dummy.next

https://leetcode.com/problems/remove-linked-list-elements/ Easy question for beginner to linked list

Repost from Codeforces Official
Educational Codeforces Round 175 (rated for Div. 2) starts on the 27th of February at 14:35 UTC. Please, join by the link https://codeforces.com/contests/2070

🚀 A2SV Internship Program – Level Up Your Tech Skills! 🚀 Gain hands-on experience in Frontend, Backend, Mobile Development, UI/UX, and Product Management while working on real projects with industry experts. Perfect for students looking to grow and launch their tech careers! 📌 Requirements: Must be a continuing student in their internship phase and have completed or be enrolled in A2SV’s G6 program. 📍 Location: Abrehot Library (In-person) ⏰ Hours: 9 AM - 12 PM, 2 PM - 6 PM (G6: 2 PM - 4 PM) Apply now and start your journey with A2SV! 🚀