Posts

StringBuilder IV

Image
Not necessarily needed StringBuilder here, but the manipulation becomes much easier in strings than numbers, and since we need to swap elements to check for the IsAlmostEqual, it fits well here. Code is down below, cheers from Paddington, England! ACC Count Almost Equal Pairs I - LeetCode 3265. Count Almost Equal Pairs I Medium 43 6 Add to List Share You are given an array  nums  consisting of positive integers. We call two integers  x  and  y  in this problem  almost equal  if both integers can become equal after performing the following operation  at most once : Choose  either   x  or  y  and swap any two digits within the chosen number. Return the number of indices  i  and  j  in  nums  where  i < j  such that  nums[i]  and  nums[j]  are  almost equal . Note  that it is allowed for an integer to have leading zeros after performing an oper...

Golang vs. C#: performance characteristics (simple case study)

Image
This is an interesting example to study the performance characteristics of Golang compared to C# for a very particular case (not generalized though).  The same code was written in C# and Golang (literally translated from one to the other). Below you'll be able to see the source code for both. The problem is a simple post-order DFS in a tree-like graph. The code in C# times out (TLE = Time Limited Exceeded). The code in Golang not only works but beats 100% of the other Golang submissions. Take a look the image below: Some potential reasons for the performance gains from Go in this particular example are: 1. Language and Runtime Differences:    Go is compiled directly to machine code, while C# typically runs on a virtual machine (CLR).    Go has a simpler runtime with less overhead compared to the .NET runtime.    Go's garbage collector is designed for low latency, which can lead to better performance in some scenarios. 2. Data Structures:    G...