Posts

Showing posts from December, 2024

Dijkstra, Miller-Rabin and Caching

Image
This problem requires Dijkstra since it is a Breadth-First Search (BFS) but you always want to dequeue the element with the smallest path-sum first, hence instead of using a queue you'll need to use a priority queue. It also has a twist that it requires you to check for prime numbers along the way. My favorite method (although overkill here) is Miller-Rabin, but even Miller-Rabin leads to Time Limit Exceeded (TLE) hence you'll need to combine it with global caching. HashSet performs better than HashTable from my observation. Even with all that in place, it was still leading to TLE. Reducing the size of the priority queue from 20K to 10K did the trick. Code is down below. Greetings from the Big Island!! ACC. Digit Operations to Make Two Integers Equal - LeetCode You are given two integers  n  and  m  that consist of the  same  number of digits. You can perform the following operations  any  number of times: Choose  any  digit from  n...

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