October 7, 2024
Today I decided to embrace the AI revolution and try out Cursor. I'm going to write a quick LeetCode solution, then dive into some project ideas.
Since we have some help from Cursor tonight, we wil be solving a hard problem. Today's problem is 4. Median of Two Sorted Arrays. The problem is as follows:
Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.
The overall run time complexity should be O(log (m+n)).
Example 1: Input: nums1 = [1,3], nums2 = [2] Output: 2.00000 Explanation: merged array = [1,2,3] and median is 2.
Example 2: Input: nums1 = [1,2], nums2 = [3,4] Output: 2.50000 Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
Let's answer using the UMPIRE method:
1class Solution:2 def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:3 # Ensure nums1 is the smaller array4 if len(nums1) > len(nums2):5 nums1, nums2 = nums2, nums167 m, n = len(nums1), len(nums2)8 total_length = m + n9 half = (total_length + 1) // 21011 left, right = 0, m1213 while left <= right:14 partition1 = (left + right) // 215 partition2 = half - partition11617 left1 = nums1[partition1 - 1] if partition1 > 0 else float('-inf')18 right1 = nums1[partition1] if partition1 < m else float('inf')19 left2 = nums2[partition2 - 1] if partition2 > 0 else float('-inf')20 right2 = nums2[partition2] if partition2 < n else float('inf')2122 if left1 <= right2 and left2 <= right1:23 # We have found the correct partition24 if total_length % 2 == 0:25 return (max(left1, left2) + min(right1, right2)) / 226 else:27 return max(left1, left2)28 elif left1 > right2:29 right = partition1 - 130 else:31 left = partition1 + 13233 raise ValueError("Input arrays are not sorted")
Cursor generates duplicate responses. I had to remove unnecessary text. It does not appear to elaborate on its reasoning. Elaborating on specific parts is easy using the annotated keyboard commands. Using cursor to write LeetCode solutions is great, but it won't be useful for interviews. Hopefully it will be as useful for projects. It would be nice if I could tab through the suggestions, rather than using tab to accept current suggestion. Cursor has been a drop-in replacement for Code, requiring no configuration to continue prior development workflow.
I had a few ideas for projects I want to build.
I will use the wallpaper app to learn Next.js and Tailwind. I don't expect to spend much time on it, as it can only be monetized through ads. I will use the scholarship essay assistant to learn about:
Another LeetCode Blog Post
Scholarships Plus