Problems Solved:
- #4 Median of Two Sorted Arrays
- #20 Valid Parentheses
This continues my daily series (Day 10: Sorting + Stack patterns). Hitting a 10βday streak π― feels great β the 5/10/15 day checkpoints help me keep momentum and show consistent practice.
π‘ What I Learned
Todayβs focus was on two fundamentals:
- For Median of Two Sorted Arrays, I implemented the straightforward mergeβthenβsort approach for clarity. Itβs not the optimal
O(log(m+n))
partition method, but itβs clean and correct. In C++, I also guarded against overflow by promoting tolong long
before averaging. - For Valid Parentheses, the classic stack trick: push the expected closing bracket when you see an opener; on any closer, check the stack top for a match or fail fast.
π§© #4 β Median of Two Sorted Arrays
Python (Sort & pick middle)
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
nums3 = sorted(nums1 + nums2)
n = len(nums3)
if len(nums3) % 2 == 1:
return float(nums3[n // 2])
else:
return (nums3[n // 2 - 1] + nums3[n // 2]) / 2
Why it works: Concatenate, sort, and take the middle(s). Itβs the simplest correct approach; great for readability and quick validation.
Time: O((m+n)Β·log(m+n))
Space: O(m+n)
(new merged list)
C++ (Inβplace merge & safe average)
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
nums1.insert(nums1.end(), nums2.begin(), nums2.end());
sort(nums1.begin(), nums1.end());
int n = (int)nums1.size();
if (n % 2 == 1) {
return static_cast<double>(nums1[n/2]);
} else {
long long a = nums1[n/2 - 1];
long long b = nums1[n/2];
return (a + b) / 2.0;
}
}
};
Why it works: Same idea as Python. Using long long
prevents overflow on the sum before dividing by 2.0
.
Time: O((m+n)Β·log(m+n))
Space: O(1)
extra (reuses nums1
βs buffer after insert
)
Note: The optimal approach is the partition method with binary search on the smaller array to achieve
O(log(m+n))
. I stuck to the clear version today to keep the streak focused on fundamentals.
π§© #20 β Valid Parentheses
Python (Expectedβcloser stack)
class Solution:
def isValid(self, s: str) -> bool:
stack = []
for ch in s:
if ch == '(':
stack.append(')')
elif ch == '{':
stack.append('}')
elif ch == '[':
stack.append(']')
elif not stack or stack.pop() != ch:
return False
return not stack
Why it works: Push the expected closing bracket for each opener. On a closer, pop and compare. Any mismatch or early closer fails.
Time: O(n)
Space: O(n)
C++ (Topβthenβpop)
class Solution {
public:
bool isValid(string s) {
stack<char> st;
for (char c : s) {
if (c == '(') {
st.push(')');
} else if (c == '{') {
st.push('}');
} else if (c == '[') {
st.push(']');
} else {
if (st.empty() || st.top() != c) return false;
st.pop();
}
}
return st.empty();
}
};
Why it works: Identical logic as Python. Always check top()
before pop()
; pop()
doesnβt return a value.
Time: O(n)
Space: O(n)
πΈ Achievements
- Median of Two Sorted Arrays: Python & C++ implementations validated.
- Valid Parentheses: Stack pattern implemented in both languages with edgeβcase handling (empty, early closers, leftover openers).
π¦ Complexity Recap
-
Median of Two Sorted Arrays:
O((m+n)Β·log(m+n))
time; Python spaceO(m+n)
, C++ extra spaceO(1)
after merge. -
Valid Parentheses:
O(n)
time,O(n)
space.
π£ Join the Journey
Day 10 streak β and counting! Iβm solving and documenting problems daily in both Python and C++, highlighting patterns and performance insights. Follow along if youβre into algorithms and clean, reliable implementations.
Links
- LinkedIn: https://www.linkedin.com/in/ertugrul-mutlu
- GitHub Series: https://github.com/Ertugrulmutlu/leetcode-series
Top comments (0)