/blog/images/avatar-icon.png

5 Basic Design Patterns You Need to Know (1/2): Singleton, Factory, Observer

Brief Singleton pattern: This pattern ensures that only one instance of a class is created and provides a global point of access to that instance. Factory pattern: This pattern provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. Observer pattern: This pattern defines a one-to-many dependency between objects, so that when one object changes state, all its dependents are notified and updated automatically.

Median of Two Sorted Arrays in Python with Binary Search (No Slicing) Real O(log(min(m, n)))

Median of Two Sorted Arrays Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: Input: nums1 = [1,3], nums2 = [2] Output: 2.00000 Explanation: merged array = [1,2,3] and median is 2. Example 2: Input: nums1 = [1,2], nums2 = [3,4] Output: 2.50000 Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.

LeetCode Weekly Contest Medium (Sliding Window) 2537. Count the Number of Good Subarrays

Count the Number of Good Subarrays Given an integer array nums and an integer k, return the number of good subarrays of nums. A subarray arr is good if it there are at least k pairs of indices (i, j) such that i < j and arr[i] == arr[j]. A subarray is a contiguous non-empty sequence of elements within an array. Example 1: Input: nums = [1,1,1,1,1], k = 10 Output: 1 Explanation: The only good subarray is the array nums itself.

Amortized analysis - Implement Queue using Stacks vs. LinkedLIst

What’s Amortized Analysis The motivation for amortized analysis is that looking at the worst-case run time can be too pessimistic. Instead, amortized analysis averages the running times of operations in a sequence over that sequence.1 Example Implement Queue Linked List 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 class QNode: def __init__(self, v=0, _next=None): self.