doing leetcode everyday until I get a job (day 106)

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Best of luck thats the dedication I wish I had

just_exist_ezz
Автор

Hey john. Best of LUCK man You're gonna make it for sure. Keep going

hamadrehman
Автор

Nested (for) loops aren't indicative of quadratic time complexity.

Fixed Inner Loop:

for (let i = 0; i < n; i++) {
for (let j = 0; j < 10; j++) { // constant size
// do something
}
}
This is O(n) because the inner loop is constant time - it always runs 10 times regardless of n.

Decreasing Inner Loop:

for (let i = 0; i < n; i++) {
for (let j = 0; j < n - i; j++) {
// do something
}
}
This is actually O(n²/2), which simplifies to O(n²), but it's a good example of how not all nested loops do the same amount of work.

Halving Inner Loop:

for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j += i + 1) { // step size increases
// do something
}
}
This is O(n log n) because the inner loop makes fewer iterations as i increases.

Different Collection Sizes:

for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) { // different size
// do something
}
}
This is O(n*m), which could be better or worse than O(n²) depending on the relationship between n and m.
Time complexity depends on how many total iterations occur, not just the fact that loops are nested.

michaelharrington
Автор

hope u get a job, i got one yesterday

montralanca