Intro to Cursor

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.

LeetCode Problem

The Problem

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.

The Solution

Let's answer using the UMPIRE method:

  • Understand
    • We have two sorted arrays, and we must merge them and find the median.
    • The median is the middle value of a sorted list. If the list has an even number of elements, the median is the average of the two middle elements.
  • Match
    • We can merge the two arrays and then find the median, but this would not be O(log(m+n)).
    • We can use a modified binary search to find the median.
      • Binary search is O(log n) and we can use it to find the median.
  • Plan
    • We can use a modified binary search to find the median.
    • We will use two pointers to traverse the arrays.
    • We will use a helper function to find the median of the two arrays.
  • Implement
1class Solution:
2 def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
3 # Ensure nums1 is the smaller array
4 if len(nums1) > len(nums2):
5 nums1, nums2 = nums2, nums1
6
7 m, n = len(nums1), len(nums2)
8 total_length = m + n
9 half = (total_length + 1) // 2
10
11 left, right = 0, m
12
13 while left <= right:
14 partition1 = (left + right) // 2
15 partition2 = half - partition1
16
17 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')
21
22 if left1 <= right2 and left2 <= right1:
23 # We have found the correct partition
24 if total_length % 2 == 0:
25 return (max(left1, left2) + min(right1, right2)) / 2
26 else:
27 return max(left1, left2)
28 elif left1 > right2:
29 right = partition1 - 1
30 else:
31 left = partition1 + 1
32
33 raise ValueError("Input arrays are not sorted")
  • Review
    • The code is clean, it uses descriptive variables and follows a logical flow.
    • The code is correct, it passes the test cases.
  • Evaluate
    • The code is efficient, it runs in O(log(m+n)) time.
    • We never iterate over the entire array, even in the worst case.

Notes

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.

Project Ideas

I had a few ideas for projects I want to build.

  • A wallpaper web app that scrapes image boards.
    • It would have authentication for user accounts.
      • Users can favorite wallpapers, saving them for later.
      • Users can download wallpapers, saving bandwidth compared to anonymous access.
    • It would hide NSFW wallpapers by parsing post text.
  • A scholarship essay assistant.
    • It would use RAG to import past essays as context during prompts.
    • It would use GPT-4o to proofread and improve essays.
    • It would track scholarship deadlines.
    • It would save essays to a user's Google Drive.

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:

  • RAG,
  • GPT-4o,
  • Google Drive API,
  • Docker,
  • AWS,
  • React,
  • and TypeScript.

Another LeetCode Blog Post

Scholarships Plus