The shortest path problem is the problem of finding a path between two vertices in a graph such that the sum of the weights of its constituent edges is minimized. (Wikipedia) In this and coming posts some of the most common algorithms to solve the shortest path problems will be explained. Today’s topic is Dijkstra.

Continue reading “Shortest Path Algorithms I – Dijkstra”## [Leetcode problems]1477. Find Two Non-overlapping Sub-arrays Each With Target Sum

Given an array of integersContinue reading “[Leetcode problems]1477. Find Two Non-overlapping Sub-arrays Each With Target Sum”`arr`

and an integer`target`

. You have to findtwo non-overlapping sub-arraysof`arr`

each with sum equal`target`

. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays isminimum. Returnthe minimum sum of the lengthsof the two required sub-arrays, or returnif you cannot find such two sub-arrays.-1Example 1:Input:arr = [3,2,2,4,3], target = 3Output:2Explanation:Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.Example 2:Input:arr = [7,3,4,7], target = 7Output:2Explanation:Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.

## [Leetcode problems]1475. Final Prices With a Special Discount in a Shop

Given the arrayContinue reading “[Leetcode problems]1475. Final Prices With a Special Discount in a Shop”`prices`

where`prices[i]`

is the price of the`ith`

item in a shop. There is a special discount for items in the shop, if you buy the`ith`

item, then you will receive a discount equivalent to`prices[j]`

where`j`

is theminimumindex such that`j > i`

and`prices[j] <= prices[i]`

, otherwise, you will not receive any discount at all.Return an array where theExamples:`ith`

element is the final price you will pay for the`ith`

item of the shop considering the special discount.Input:prices = [8,4,6,2,3]Output:[4,2,4,2,3]Input:prices = [1,2,3,4,5]Output:[1,2,3,4,5]

## Snowboarding: From Newbie to CASI Level 1

I got my CASI Level 1 certificate this March, and it’s really a milestone for me and it helps me to explore snowboarding better. So if you like snowboarding and would like to advance your skills, this post would give you an idea about what CASI is and when you are ready for it. All resources mentioned could be found at the end.

Continue reading “Snowboarding: From Newbie to CASI Level 1”## [Leetcode problems] 45 Jump Game II

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps.Continue reading “[Leetcode problems] 45 Jump Game II”Example:Input:[2,3,1,1,4]Output:2Explanation:The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.Note:You can assume that you can always reach the last index.

## Top K problem – Sort, Heap, and Quickselect

Top K problem refers to those asking to find the Kth largest/smallest element or the top K largest/smallest items from an unsorted array. Sorting first and returning the Kth item definitely works, yet it yields O(n^2) or O(nlogn) time complexity. With heap or quickselect, O(n) is achievable.

Continue reading “Top K problem – Sort, Heap, and Quickselect”## [Leetcode problems] 72. Edit Distance

Given two wordsContinue reading “[Leetcode problems] 72. Edit Distance”word1andword2, find the minimum number of operations required to convertword1toword2. You have the following 3 operations permitted on a word:Inserta characterDeletea characterReplacea characterExample: word1 = 'horse', word2 = 'ros' horse -> rorse (replace 'h' with 'r') rorse -> rose (remove 'r') rose -> ros (remove 'e')

## Divide and Conquer – What is it and How to use it

What is it

Divide and Conquer is an algorithm design paradigm. It works by breaking the original problem into similar subproblems recursively. The solutions to subproblems are then combined to give the solution to the original problem. (Wikipedia)

Continue reading “Divide and Conquer – What is it and How to use it”