This is what I do after the kids go to bed and before Forensic Files.
I casually write code.
Claude vs ChatGPT: A Coder's Perspective on LLM Performance
Get link
Facebook
X
Pinterest
Email
Other Apps
-
In the rapidly evolving world of Large Language Models (LLMs), recent releases from OpenAI's ChatGPT have garnered significant attention. However, when it comes to coding tasks, Anthropic's Claude.ai continues to impress me with its speed and quality of output.
Benchmarking with LeetCode
While LeetCode problems aren't the definitive measure of an AI's coding capabilities (they're pre-designed puzzles, after all), they offer an interesting playground for comparison. Let's dive into a recent example:
3319. K-th Largest Perfect Subtree Size in Binary Tree
Medium
You are given the root of a binary tree and an integer k.
Return an integer denoting the size of the kthlargestperfect binarysubtree, or -1 if it doesn't exist.
A perfect binary tree is a tree where all leaves are on the same level, and every parent has two children.
Example 1:
Input:root = [5,3,6,5,2,5,7,1,8,null,null,6,8], k = 2
Output:3
Explanation:
The roots of the perfect binary subtrees are highlighted in black. Their sizes, in decreasing order are [3, 3, 1, 1, 1, 1, 1, 1]. The 2nd largest size is 3.
Example 2:
Input:root = [1,2,3,4,5,6,7], k = 1
Output:7
Explanation:
The sizes of the perfect binary subtrees in decreasing order are [7, 3, 3, 1, 1, 1, 1]. The size of the largest perfect binary subtree is 7.
Example 3:
Input:root = [1,2,3,null,4], k = 3
Output:-1
Explanation:
The sizes of the perfect binary subtrees in decreasing order are [1, 1]. There are fewer than 3 perfect binary subtrees.
Constraints:
The number of nodes in the tree is in the range [1, 2000].
1 <= Node.val <= 2000
1 <= k <= 1024
My Solution
I tackled this problem using a combination of Post-Order Depth-First Search (PO-DFS) and a Priority Queue. Here are the performance stats:
Runtime: 197 ms
Memory Usage: 103.6 MB
``` Marcelo's Solution:
public class Solution {
public int KthLargestPerfectSubtree(TreeNode root, int k)
{
PriorityQueue pQueue = new PriorityQueue(false, 2500);
Claude.ai consistently outperforms ChatGPT in this and other examples I've tested. Its approach:
Pruned version of PO-DFS
K-min Priority Queue
Performance:
Runtime: 164 ms
Memory Usage: 71.1 MB
``` Claude.ai Solution:
public class Solution
{
private PriorityQueue<int, int> topK;
private int k;
public int KthLargestPerfectSubtree(TreeNode root, int k)
{
this.k = k;
this.topK = new PriorityQueue<int, int>(k, Comparer<int>.Create((a, b) => a - b));
DFS(root);
return topK.Count == k ? topK.Peek() : -1;
}
private int DFS(TreeNode node)
{
if (node == null)
return 0;
int left = DFS(node.left);
int right = DFS(node.right);
if (left == right && IsPowerOfTwoMinusOne(left + right + 1))
{
int size = left + right + 1;
if (topK.Count < k)
{
topK.Enqueue(size, size);
}
else if (size > topK.Peek())
{
topK.Dequeue();
topK.Enqueue(size, size);
}
return size;
}
return -1;
}
private bool IsPowerOfTwoMinusOne(int n)
{
return (n & (n + 1)) == 0;
}
}
```
The Future of AI and Coding
It's astounding to think that we're only in the first decade of LLMs since the groundbreaking "Attention is All You Need" paper. The rapid progress we've seen raises exciting questions:
How will these models evolve in the coming decades?
What impact will they have on software development practices?
How can we best leverage AI assistants in our coding workflows?
As we continue to explore these questions, one thing is clear: the synergy between human creativity and AI capabilities is reshaping the landscape of software development.
To solve this Advent of Code (parts 1 and 2), I used Breadth-First Search (BFS) to keep moving the guard as well as Finite State Machine (FSM) to control the directions. This solves part 1 easily. Part 2 can be solved using the same technique, takes a little longer since you need to try every empty cell, but it eventually does the trick (takes a min or so). Code is down below, cheers, ACC. Day 6 - Advent of Code 2024 using System.IO; using System.Collections; using System; using System.Text; using System.Text.RegularExpressions; using System.ComponentModel.DataAnnotations; Process2(); void Process() { string fileName = "input.txt"; FileInfo fileInfo = new FileInfo(fileName); StreamReader sr = fileInfo.OpenText(); List map = new List (); int startRow = 0; int startCol = 0; while (!sr.EndOfStream) { string line = sr.ReadLine().Trim(); map.Add(new StringBuilder(line)); int indexStart = line.IndexOf('^'); ...
Problem today requires backtracking and eval (evaluating a mathematical expression in the format of a string). Glad that it used a left-2-right evaluation, makes for an easy parsing and not overly complicated evaluation function. Code is down below for parts 1 and 2. Cheers, ACC. Day 7 - Advent of Code 2024 using System.IO; using System.Collections; using System; using System.Text; using System.Text.RegularExpressions; using System.ComponentModel.DataAnnotations; Process2(); void Process() { string fileName = "input.txt"; long retVal = 0; FileInfo fileInfo = new FileInfo(fileName); StreamReader sr = fileInfo.OpenText(); while (!sr.EndOfStream) { string line = sr.ReadLine().Trim(); string[] parts1 = line.Split(':'); long target = Int64.Parse(parts1[0]); string[] numbers = parts1[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (TryAll1(numbers, target, "", 0)) r...
LeetCode Problem 3532 is an interesting one that tests understanding of graph theory and connected components. The problem gives us: An integer n representing nodes in a graph (labeled 0 to n-1) An array nums of length n in non-decreasing order A value maxDiff A series of queries asking if a path exists between two nodes Two nodes i and j have an edge between them if |nums[i] - nums[j]| <= maxDiff . For each query [u, v] , we need to determine if there's a path from node u to node v . The Key Insight: Connected Components This problem is essentially asking to determine if two nodes belong to the same connected component in an undirected graph. What makes this problem particularly approachable is that there's no need to perform a BFS or DFS for each query. Since nums is already sorted, this property can be exploited to pre-compute all connected components in linear time! public bool[] PathExistenceQueries(int n, int[] nums, int maxDiff, int[][] queries) { ...
Comments
Post a Comment