Posts

Leetcode 120. Triangle Solution

Image
  Leetcode 120. Triangle Solution Hi everybody, Today we can discuss a Leetcode question. Question : 120. Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [ 2 ], [ 3 ,4], [6, 5 ,7], [4, 1 ,8,3] ] The minimum path sum from top to bottom is  11  (i.e.,  2  +  3  +  5  +  1  = 11). We can start to understand the problem. First, we need to understand this question is a Dynamic Problem question. How? 1- We have a triangle. 2-) We need to traverse this triangle 3-) We can compute the min path through the Triangle. Second, we need to use an extra space like a 2 Dimensional array store to compute the adjacency number on the row below. Third, in the base case  t raverse and general traverse case. The base case will traverse fill the left side of our 2D Array. The general traverse case will fill the inside of o...

Making complex queries readable with the ActiveRecord

Image
Making complex queries readable with the ActiveRecord Sometimes, we need multiple tables to join in our project and these queries almost not be readable. The first thing that comes to our mind is of course Gems in Ruby on Rails. Today I will focus on how we can make complex queries more readable without using any library or Gem. I have some models in our current project on Ruby On Rails 5.2 : models/courier.rb class Courier < ApplicationRecord has_one :main_contract, :as => :customer has_many :customer_contracts #default pagination paginates_per 20 end models/main_contract.rb class MainContract < ApplicationRecord belongs_to :customer, polymorphic: true, optional: true belongs_to :product end models/customer_contract.rb class CustomerContract < ApplicationRecord belongs_to :product belongs_to :airline, optional: true belongs_to :airport, optional: true belongs_to :courier, optional: true end models/airport...

Easy Way of Finding Coordinates in Polygon

Image
(Ruby on Rails 6 only API, Postgres, Minitest) Hi everybody, Sometimes, we need specific features in our application. One of them may be “How to find Coordinates in Polygon” :) We suppose, we have some areas in our application. We define these areas by using Google Maps and every area has some specific attributes that are name , price, and coordinates. For example, we need information like this, when we click on a point on the map, what are the properties of this point? How to find if the dots are between the corresponding coordinates? GEMS If we are using Ruby on Rails, we think firstly that “is there any Gem about finding these points? Yes, there are some Gems. Like : https://github.com/geokit/geokit https://github.com/apneadiving/Google-Maps-for-Rails https://github.com/alexreisner/geocoder But these gems little huge :) Actually, we don’t need to use completely a gem. Because we just find a point in a polygon, technically. There is a cost of a gem for our a...