Leetcode 238: Product of Array Except Self

Problem Statement Description Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. You must write an algorithm that runs in O(n) time and without using the division operation. Examples Example 1: Input: nums = [1,2,3,4] Output: [24,12,8,6] Example 2: Input: nums = [-1,1,0,-3,3] Output: [0,0,9,0,0] Constraints 2 <= nums....

February 20, 2023 · 3 min · 509 words · Me

Leetcode 53: Maximum Subarray

Problem Statement Description Given an integer array nums, find the subarray with the largest sum, and return its sum. Examples Example 1: Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: The subarray [4,-1,2,1] has the largest sum 6. Example 2: Input: nums = [1] Output: 1 Explanation: The subarray [1] has the largest sum 1. Example 3: Input: nums = [5,4,-1,7,8] Output: 23 Explanation: The subarray [5,4,-1,7,8] has the largest sum 23....

January 24, 2023 · 4 min · 692 words · Me

Leetcode 217: Contains Duplicates

Problem Statement Description Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. Examples Example 1: Input: nums = [1,2,3,1] Output: true Example 2: Input: nums = [1,2,3,4] Output: false Example 3: Input: nums = [1,1,1,3,3,4,3,2,4,2] Output: true Constraints 1 <= nums.length <= 105 -109 <= nums[i] <= 109 Key Insights This is a very simple problem that introduces the concept of a hash set....

January 18, 2023 · 1 min · 201 words · Me

Leetcode 169: Majority Element

Problem Statement Description Given an array nums of size n, return the majority element. The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array. Examples Example 1: Input: nums = [3,2,3] Output: 3 Example 2: Input: nums = [2,2,1,1,1,2,2] Output: 2 Constraints n == nums.length 1 <= n <= 5 * 104 -109 <= nums[i] <= 109 Key Insights There are many different ways to solve this problem....

January 16, 2023 · 2 min · 249 words · Me

Leetcode 242: Valid Anagram

Problem Statement Given two strings s and t, return true if t is an anagram of s, and false otherwise. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. Example 1: Input: s = "anagram", t = "nagaram" Output: true Example 2: Input: s = "rat", t = "car" Output: false Solution Python Implementation from collections import Counter class Solution: def isAnagram(self, s: str, t: str) -> bool: """ T: O(N) = max(S + T) S: O(N) """ s_mp = Counter(s) t_mp = Counter(t) return s_mp == t_mp Leetcode 242: Valid Anagram

January 10, 2023 · 1 min · 109 words · Me