You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/Activity Selection Problem.md
+29-36Lines changed: 29 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,7 +39,6 @@ displayTitle: Activity Selection Problem
39
39
- **Stability**: Not applicable.
40
40
- **Greedy Choice Property**: Selecting the activity with the earliest finish time never eliminates a globally optimal set.
41
41
- **Optimal Substructure**: After selecting activity $k$, the remaining problem is the same — find max activities compatible with $k$ from the remaining set.
42
-
43
42
-# How It Works
44
43
collapsed:: true
45
44
- ## The Core Idea
@@ -59,7 +58,7 @@ displayTitle: Activity Selection Problem
59
58
F --> D
60
59
G --> D
61
60
D --> H["✅ Selected set = maximum non-overlapping activities"]
@@ -307,7 +302,6 @@ displayTitle: Activity Selection Problem
307
302
- ## ❌ Avoid Simple Greedy When
308
303
- Activities have **different profits/weights** — switch to Weighted Job Scheduling DP.
309
304
- The constraint is not just non-overlap but involves **dependencies** between activities.
310
-
311
305
-# Key Takeaways
312
306
collapsed:: true
313
307
- **Earliest Finish First** — The greedy choice is always picking the activity with the earliest finish time, maximizing remaining time for future activities.
@@ -316,11 +310,10 @@ displayTitle: Activity Selection Problem
316
310
- **Weighted Variant Needs DP** — When activities have different profits, greedy fails. Use DP + binary search ([[Dynamic Programming Concepts]]) for the weighted version.
317
311
- **Interval Coloring Dual** — The minimum number of machines = the maximum overlap depth at any point (use min-heap of finish times).
318
312
- **LeetCode Applications** — "Non-overlapping Intervals" (#435), "Meeting Rooms II" (#253), "Minimum Number of Arrows to Burst Balloons" (#452).
Copy file name to clipboardExpand all lines: pages/Constructor.md
+2-5Lines changed: 2 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,7 +30,6 @@ displayTitle: Constructor
30
30
```
31
31
-
32
32
- > [!important] In Python, `__new__()` allocates memory and `__init__()` initializes it. You almost always only define `__init__`. In C++, the constructor does both.
33
-
34
33
-# Types of Constructors
35
34
collapsed:: true
36
35
- | Type | Description | Example |
@@ -49,10 +48,10 @@ displayTitle: Constructor
49
48
CT --> COP["Copy\n(from existing object)"]
50
49
CT --> DEL["Delegating / Chaining\n(calls another constructor)"]
51
50
```
52
-
53
51
-# Constructor Chaining
54
52
collapsed:: true
55
53
- One constructor can call another constructor of the same class to avoid code duplication:
54
+
- :::code-tabs
56
55
- ```python
57
56
# Python — via __init__ default arguments
58
57
class Connection:
@@ -87,7 +86,7 @@ displayTitle: Constructor
87
86
}
88
87
}
89
88
```
90
-
89
+
:::
91
90
-# Implementation
92
91
collapsed:: true
93
92
- > [!note] A `DatabaseConnection` class demonstrating all constructor types, chaining, validation, and C++ initializer list.
@@ -368,15 +367,13 @@ displayTitle: Constructor
368
367
```
369
368
370
369
:::
371
-
372
370
-# Key Takeaways
373
371
collapsed:: true
374
372
- A constructor **runs automatically** at object creation — you never call it explicitly.
375
373
- Always **validate inputs** in the constructor to guarantee a valid initial state.
376
374
- Use **constructor chaining** (`this(...)` in Java, `super(...)` in child classes, default args in Python) to avoid duplication.
377
375
- **C++ initializer lists** (`: field_(value)`) initialize members before the constructor body runs — more efficient than assignment inside the body.
378
376
- **Factory class methods** (`@classmethod` in Python, `static` in Java/C#) are alternative constructors with semantic names (`from_url()`, `local()`).
- **Key Insight:** Sort queries by (block of L, then R for even blocks / R descending for odd blocks). This minimizes total pointer movement across all queries to O((N + Q)√N).
203
203
- **Use cases:** Range sum/product queries, count distinct elements in range, offline interval problems.
204
-
- 6) [[Distinct elements in subarray using Mo's Algorithm]]
204
+
- 6) [[Distinct elements in subarray using Mos Algorithm]]
205
205
- **Purpose:** Count **distinct elements** across many arbitrary subarrays efficiently using MO's ordering.
206
206
- **Time:** O((N + Q) × √N) · **Space:** O(N) for frequency map
207
207
- **Key Insight:** Extend/shrink the window by one element at a time, updating a frequency table. Increment the distinct count only when a new frequency goes from 0 → 1, decrement when it goes 1 → 0.
Copy file name to clipboardExpand all lines: pages/Distinct elements in subarray using Mos Algorithm.md
+3-5Lines changed: 3 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,7 @@ seoTitle: Count Distinct Elements in Subarray – MO's Algorithm Variant | In-De
3
3
description: "Complete guide to counting distinct elements in subarrays using MO's Algorithm. Covers frequency map add/remove, offline query processing, O((N+Q)√N) complexity, and implementations in Python, C++, Java, JavaScript, and C#."
4
4
keywords: "distinct elements subarray, Mo's algorithm, count distinct, offline queries, frequency map, square root decomposition, range query, VR-Rathod, Code-Note, code note vr"
5
5
displayTitle: Distinct elements in subarray using Mo's Algorithm
6
+
title: Distinct elements in subarray using Mos Algorithm
6
7
---
7
8
8
9
> [!info] What is this problem?
@@ -54,7 +55,6 @@ displayTitle: Distinct elements in subarray using Mo's Algorithm
54
55
| **Time** | O((N + Q) × √N) |
55
56
| **Space** | O(N) for frequency map + O(Q) for answers |
56
57
| **Constraint** | Offline — all queries known upfront |
57
-
58
58
-# Implementation
59
59
collapsed:: true
60
60
- > [!note] Full MO's-based distinct count: frequency map, Mo ordering, pointer management, and result retrieval.
@@ -107,7 +107,7 @@ displayTitle: Distinct elements in subarray using Mo's Algorithm
107
107
# Expand right
108
108
while right < q.r: right += 1; add(right)
109
109
# Expand left
110
-
while left > q.l: left -= 1; add(left)
110
+
while left > q.l: left -= 1; add(left)
111
111
# Shrink right
112
112
while right > q.r: remove(right); right -= 1
113
113
# Shrink left
@@ -312,17 +312,15 @@ displayTitle: Distinct elements in subarray using Mo's Algorithm
312
312
```
313
313
314
314
:::
315
-
316
315
-# Key Takeaways
317
316
collapsed:: true
318
317
- The core add/remove pattern: `freq[x]++; if freq[x]==1: distinct++` / `freq[x]--; if freq[x]==0: distinct--`.
319
318
- This O(1) add/remove makes MO's work for this problem — total cost = number of pointer moves = O((N+Q)√N).
320
319
- **Verification trick:** After computing, cross-check `len(set(arr[l:r+1]))` to verify answers.
321
320
- This is a direct application of [[MOs Algorithm (Query square root decomposition)]] specialised to the distinct-count aggregate.
0 commit comments