⭐ Shop on Amazon to support me: [ Ссылка ]
⭐ NordVPN to protect your online privacy: [ Ссылка ]
⭐ NordPass to help manage all of your passwords: [ Ссылка ]
LeetCode 15: 3Sum: [ Ссылка ]
Solution explained:
1. A direct intuition is the brute force way of tackling this problem: use a three layer nested for loop to iterate through the original input array, this way, we can find all possible triplets that add up to zero, then filter out the duplicate ones.
This is of course a "workable" solution, but is that acceptable/optimal?
It renders O(n^3) time complexity which is not ideal if not horrible. 🤪
2. We can do better than that for sure. Sorting is the rescue!
We could sort this array first, then use the famous two pointer technique to shrink the array to scanning from both ends.
Sorting: O(nlogn)
Iteration: O(n^2)
So total time complexity becomes: O(n^2), this is way better than O(n^3) when n is significant large. 😆
⭐ Support my channel and connect with me:
[ Ссылка ]
// TOOLS THAT I USE:
○ Memory Foam Set Keyboard Wrist Rest Pad - [ Ссылка ]
○ Electric Height Adjustable Standing Desk - [ Ссылка ]
○ Apple Magic Keyboard (Wireless, Rechargable) - [ Ссылка ]
○ Apple Magic Trackpad 2 (Wireless, Rechargable) - [ Ссылка ]
○ Apple MacBook Pro - [ Ссылка ]
○ All-In One Printer - [ Ссылка ]
○ Apple AirPods Pro - [ Ссылка ]
○ My new favorite Apple Watch - [ Ссылка ]
// MY FAVORITE BOOKS:
○ Introduction to Algorithms - [ Ссылка ]
○ Designing Data-Intensive Applications - [ Ссылка ]
○ Head First Java - [ Ссылка ]
○ Design Patterns - [ Ссылка ]
Follow me on Github for complete LeetCode solutions: [ Ссылка ]
Support me on Patreon: [ Ссылка ]
My ENTIRE Programming Equipment and Computer Science Bookshelf:
[ Ссылка ]
And make sure you subscribe to my channel!
Your comments/thoughts/questions/advice will be greatly appreciated!
#softwareengineering #leetcode #algorithms #coding #interview #SDE #SWE #SiliconValley #programming #datastructures
LeetCode 15: 3Sum - Interview Prep Ep 20
Теги
fisher coderfishercoderjava programmingleetcode in javaleetcodejava leetcodehacker rankcracking the coding interviewcodingprogrammingcomputer sciencetechnologymathscienceeducationtutorialhow tojavasoftware engineeringalgorithmsdata structuresinterviewtechnical interviewcoding questionscoding interviews3Sumthree sumFacebook Coding Interview QuestionLeetCode 15