LeetCode 645 | Set Mismatch | Python In-Place Index Marking or Counting | AlgoYogi
Welcome to AlgoYogi! π Start Your Smart Coding Prep at π https://algoyogi.io We solve LeetCode 645: Set Mismatch using Python. You are given nums from 1 to n where one number is duplicated and one is missing. Return [duplicate, missing]. Approaches: 1) In-place index marking - For each value x in nums, use idx = abs(x) β 1 - If nums[idx] is negative, x is the duplicate - Otherwise set nums[idx] = βnums[idx] - After marking, the index i where nums[i] is positive corresponds to missing = i + 1 2) Counting map (simpler but uses extra space) - Count frequencies; the one with count 2 is duplicate, the one with count 0 is missing π Problem Link: https://leetcode.com/problems/set-mismatch/ --- ### β± Timestamps 0:00 Introduction 0:20 Problem Statement 1:00 In-Place Index Marking 14:00 Python Code Walkthrough 16:40 Time and Space Complexity --- ### π‘ Why Watch? - Classic trick: use the array itself as a hash using signs - O(n) time, O(1) extra space for the in-place method - Easy, interview-friendly reasoning and implementation #LeetCode #SetMismatch #Python #AlgoYogi #Array #InPlace
Download
0 formatsNo download links available.