Posts

Showing posts from 2024

Advent of Code - Day 7, 2024: Backtracking and Eval

Image
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...

Advent of Code - Day 6, 2024: BFS and FSM

Image
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('^'); ...

Follow the hints! - Part 5

Image
In this problem the hints are very useful: first, make a hash table (or set) to access the elements of the array quickly. Second, try every element as if it was the outlier, do some math manipulation to check if that element is a valid candidate, and if so keep track of the max. Notice that you achieve linearity with multiple 1-passes. Code is down below, cheers, ACC. Identify the Largest Outlier in an Array - LeetCode 3371. Identify the Largest Outlier in an Array Medium 68 9 Add to List Share You are given an integer array  nums . This array contains  n  elements, where  exactly   n - 2  elements are  special  numbers . One of the remaining  two  elements is the  sum  of these  special numbers , and the other is an  outlier . An  outlier  is defined as a number that is  neither  one of the original special numbers  nor  the element representing the sum of those numbers. Note  ...

Upward Diagonals Sudoku (UDS) - an NP Problem

Image
There are two types of Sudoku games: the standard one, and the Diagonal Sudoku. For the latter, it includes the standard rules plus the additional rule that the two main diagonals must also only contain unique numbers. I'd like to propose a third type: Upward Diagonals Sudoku, or UDS. In an UDS, the standard Sudoku rules apply, but we add one more rule: all the upward diagonals, all of them, must also only have unique numbers. The solution to this problem is again using Backtracking, but I liked to create a specific easy, scalable function to add new rules, which I highlight here . This way, if we want to add a different rule in the future, all we need is to craftly modify this function. This seems to be (arguably) an NP problem. When I asked for solutions using both Claude.ai and ChatGPT o1, they really struggle and can't really find a solution for me. It is interesting that eventually they give up and produce a code (without me asking for a code) that would arguably generate ...

1500 LeetCode Problems

Image
 Optimizing 'Adjacent Increasing Subarrays Detection II' with Pre-computation and Caching Solving algorithmic problems efficiently often hinges on smart strategies like pre-computation and caching. The LeetCode problem “Adjacent Increasing Subarrays Detection II” (https://leetcode.com/problems/adjacent-increasing-subarrays-detection-ii/) serves as a perfect example of how these techniques can be applied to achieve a linear-time solution. This post explores how to implement these strategies to solve the problem effectively. Understanding the Problem The challenge is to find the maximum length of a subarray that increases continuously, under specific conditions. The naive approach might involve nested loops, resulting in higher time complexity. The goal, however, is to process the array in linear time, avoiding unnecessary computations. Strategy Overview The key to optimizing the solution lies in: - Pre-computing the lengths of all increasing subarrays. - Caching these lengths to...

Classic Dijkstra II

Image
For this problem (and the #3342), all we need to do is apply classic Dijkstra, there is only one tiny adjustment for the "weight" based on the time, everything else is standard. Code is down below, cheers, ACC. Find Minimum Time to Reach Last Room I - LeetCode 3341. Find Minimum Time to Reach Last Room I Medium 46 12 Add to List Share There is a dungeon with  n x m  rooms arranged as a grid. You are given a 2D array  moveTime  of size  n x m , where  moveTime[i][j]  represents the  minimum  time in seconds when you can  start moving  to that room. You start from the room  (0, 0)  at time  t = 0  and can move to an  adjacent  room. Moving between adjacent rooms takes  exactly  one second. Return the  minimum  time to reach the room  (n - 1, m - 1) . Two rooms are  adjacent  if they share a common wall, either  horizontally  or  vertically .   Ex...