Posts

Showing posts from 2025

A linear solution taking advantage of sorted indexes

Image
 This is an interesting problem: you'll be taking advantage that the indexes in an array by definition are already sorted. Hence, although you'll be using the fact that the indexes are sorted, you won't be sorting anything leading to a linear optimized solution. The idea is to keep a list of indexes for each value in the array. The list by definition will be sorted. Then do a linear scan of each list, calculating the distance and storing the overall min one. Code is down below, cheers, ACC. Minimum Distance Between Three Equal Elements II - LeetCode ou are given an integer array  nums . A tuple  (i, j, k)  of 3  distinct  indices is  good  if  nums[i] == nums[j] == nums[k] . The  distance  of a  good  tuple is  abs(i - j) + abs(j - k) + abs(k - i) , where  abs(x)  denotes the  absolute value  of  x . Return an integer denoting the  minimum  possible  distance  of a  go...

ABCDE Problem

Image
Problem proposed by Kamal Jain: Solution can be implemented with a simple backtracking, with a bit of pruning during the Check. Not the most efficient solution due to the string operations, but did the trick, and the answer is: 09754,14754,15078,15080,15104,20078,20080,20104 Gemini 2.5 Flash as the clear winner. Code is down below, cheers. List < string > solutions = new List < string >(); Combination ( "" , 5 , ref solutions ); for ( int i = 0 ; i < solutions . Count ; i ++)     Console . Write ( "{0}{1}" , solutions [ i ], i < solutions . Count - 1 ? ',' : ' ' ); void Combination ( string current ,                   int MAX ,                   ref List < string > retVal ) {     if ( current . Length == MAX )     {         if ( Check ( current , MAX , 20320 ))             retVal . A...

Prefix Sum IX

Image
Classic prefix sum to accomplish a linear time solution, with a linear space usage too. Execution time is ~0ms according to LC. Code is down below, cheers, ACC. Equal Score Substrings - LeetCode You are given a string  s  consisting of lowercase English letters. The  score  of a string is the sum of the positions of its characters in the alphabet, where  'a' = 1 ,  'b' = 2 , ...,  'z' = 26 . Determine whether there exists an index  i  such that the string can be split into two  non-empty   substrings   s[0..i]  and  s[(i + 1)..(n - 1)]  that have  equal  scores. Return  true  if such a split exists, otherwise return  false .   Example 1: Input:   s = "adcb" Output:   true Explanation: Split at index  i = 1 : Left substring =  s[0..1] = "ad"  with  score = 1 + 4 = 5 Right substring =  s[2..3] = "cb"  with  score = 3 + 2 = 5 Both substrings ...