Alright, this post will be my first blog post covering how I solve code challenges. For the first couple of these posts (and more perhaps), I will be using challenges available at LeetCode.com. So, here we go!
I will be solving this problem using C++ as you may be able to tell from my boiler plate or from the annotations in the top left of my browser text editor. I’m using C++ for a couple different reasons: First, I like the idea of having access to address and pointers, and I would love more practice using them. Secondly, C++ is a very performant OOP language, which makes it a great option for competitive coding problems, or computing problems that require performant code and hardware, like 3d rendering or computer vision.
So, based on the title here, I know we are going to be looking at triangles. We are passed an address to the integer vector named “nums” so I know that we will want to look through the Vector library in the C++ docs. For now, let’s just logic it out.
I am going to use comments to use “Pseudo code” to break up my problem into smaller problems. Here, I understand from the problem that we need to find the perimeter of the triangle by taking the sum of each of the sides. Also, we will need to validate the triangle sides, and make sure the triangle can exist. Lets take the second part first, since if the triangle doesn’t exist, we may save some computing time by exiting the method early.
Based on the provided example and a little understanding of trigonometry, we will say that a triangle can exist if the sum of two sides is always greater than the remaining side. (I will test this logic against my program and the sample sets later, I’m just trying to keep it simple, in case the simplest answer is the right answer.)
For now, lets build a logical statement that reflects what we just surmised above: