My Wi-Fi literally disappeared in the middle of the #LeetCode#WeeklyContest512 . ๐ญ I restarted it, turned it off and on, and it's still not coming back. I have no idea how I'm supposed to finish my contest. Somebody help! ๐ฅฒ @ASUS@ASUSIndia@LeetCode
Today's LeetCode POTD just killed me ๐ not even exaggerating ๐,4-5 attempts, 3 different approaches, and my first real submission passed only 2 out of 4 test cases ๐ญ๐ญ๐ญIf I drop dead today, you all know the reason....
#leetcode#POTD
checked if currentSum minus k had occurred before. If it had, the subarray between those two points sums to k
Time O(n), Space O(n)
That's Day 4 done โ
If this helped or you liked the approach, a like goes a long way in keeping this consistent
Back tomorrow with new questions ๐
Day 4 โ #StriversTrailUnlocked with @deep_zero1
Solved two problems today from Striver's A2Z Sheet
1. Find the number that appears once, and other numbers twice.
2. Longest Subarray with Sum K
Sharing my approach for both below ๐
Got a better solution? Drop it in the comments
That's O(nยฒ), too slow, and this array has negatives too so sliding window alone won't directly work
Used prefix sum with a hashmap instead
Kept a running sum while going through the array, and stored each sum with its index the first time it appeared. At every point.
the max value along the way so I don't need a separate pass at the end.
Time O(n), Space O(1) That's Day 3 done โ
If this helped or you liked the approach, a like goes a long way in keeping this consistent
Back tomorrow with new questions ๐
Day 3 โ #StriversTrailUnlocked with @deep_zero1
Solved two problems today from Striver's A2Z Sheet
1. Move Zeroes
2. Max Consecutive Ones
Sharing my approach for both below ๐
Got a better solution? Drop it in the comments
Problem 2, Max Consecutive Ones
Find the maximum number of consecutive 1s in a binary array . Straightforward one, just needed a running counter Went through the array once, incrementing a counter every time I saw a 1, and resetting it to 0 the moment I hit a 0
Kept updating ...
Landed on the reversal technique, reverse the first K elements, reverse the rest, then reverse the whole array .One clean pass instead of K repeated ones
Time O(n), Space O(1) ๐
If this helped or you liked the approach, a like goes a long way in keeping this consistent ๐๐
So I did it in place instead. Just store the first element separately, shift every other element one step to the left, then place the stored value at the end
Time O(n), Space O(1)
Problem 2, Left Rotate Array by K Places
My first instinct was to just repeat the "rotate by one"
logic K times in a loop
That works, but it means doing a full pass K times, so the time complexity becomes O(n*k), too slow when K is large Had to think of something that does it in a single pass regardless of K