Posts
Dynamic Programming
It’s an advanced Algorithm In computer science, dynamic programming is an algorithmic method for solving optimization problems. The concept was introduced in the early 1950s by Richard Bellman. At the time, the term « programming » means planning and scheduling
The most used techniques for dynamic programming are:
read morePosts
Find All Anagrams in a String
PROBLEM Given two strings s and p, return an array of all the start indices of p’s anagrams in s. You may return the answer in any order.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1 *Input*: s = "cbaebabacd", p = "abc" *Output*: [0,6] *Explanation*: The substring with start index = 0 is "cba", which is an anagram of "abc".
read morePosts
Group Anagrams
PROBLEM Given an array of strings strs, group the anagrams together. You can return the answer in any order.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example1 *Input*: strs = ["eat","tea","tan","ate","nat","bat"] *Output*: [["bat"],["nat","tan"],["ate","eat","tea"]] Example2 *Input*: strs = [""] *Output*: [ [""] ] Example3 *Input*: strs = ["a"] *Output*: [ ["a"] ] SOLVING We’ll use Hash and Array to solve this problem
read morePosts
House Robber
PROBLEM You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.
read morePosts
Interval List Intersections
PROBLEM You are given two lists of closed intervals, firstList and secondList, where firstList[i] = [starti, endi] and secondList[j] = [startj, endj]. Each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
A closed interval [a, b] (with a<=b) denotes the set of real numbers x with a<=x<=b.
The intersection of two closed intervals is a set of real numbers that are either empty or represented as a closed interval.
read morePosts
Is Power Of Two
PROBLEM Given an integer n, return true if it is a power of two. Otherwise, return false.
An integer n is a power of two, if there exists an integer x such that n = 2x.=
Example1 *Input*: n = 1 *Output*: true Explanation: 20 = 1 Example2 *Input*: n = 16 *Output*: true Explanation: 24 = 16 Example3 *Input*: n = 3 *Output*: false SOLVING we’ll use Bit manipulation method.
read morePosts
Letter Case Permutation
PROBLEM Given a string s, you can transform every letter individually to be lowercase or uppercase to create another string.
Return a list of all possible strings we could create. Return the output in any order.
Example1 *Input*: s = "a1b2" *Output*: ["a1b2","a1B2","A1b2","A1B2"] Example2 *Input*: s = "3z4" *Output*: ["3z4","3Z4"] Constraints s consits of lowercase English letters, uppercase English letters and digits SOLVING we’ll use Backtracking method.
Steps If the index equal to size push to the result the string Call once the method with the exact same string and just the index + 1 If the current element is an alpha change uppercase <–> lowercase and call a second time the method Return the result Code class Solution { public: vector<string> letterCasePermutation(string s) { vector<string> result; backtrack(s, 0, s.
read morePosts
Linked List
INFORMATION In computer science, a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes which together represent a sequence.
There are several types of linked lists:
Singly linked list Doubly linked list Circular linked list Circular doubly linked list IMPLEMENTATION C Language .
read morePosts
Longest Substring Without Repeating Characters
PROBLEM Given a string s, find the length of the longest substring without repeating characters.
Example1 *Input*: s = "abcabcbb" *Output*: 3 Explanation: The answer is "abc", with the length of 3. Example2 *Input*: s = "bbbbb" *Output*: 1 Explanation: The answer is "b", with the length of 1. Example3 *Input*: s = "pwwkew" *Output*: 3 Explanation: The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
read morePosts
Matrix Distance Nearest 0
PROBLEM Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell.
The distance between two adjacent cells is 1.
Example1 *Input*: mat = [[0,0,0],[0,1,0],[0,0,0]] *Output*: [[0,0,0],[0,1,0],[0,0,0]] Example2 *Input*: mat = [[0,0,0],[0,1,0],[1,1,1]] *Output*: [[0,0,0],[0,1,0],[1,2,1]] SOLVING we’ll use Breadth-first Search method
Steps Create matrix result with the same size filled by 0 Create a queue Parcor the list if there is a 1 next to a 0 : Add in result 1 at it’s position (because it’s at one cell from a zero) And push to the queue it’s position While queue not empty: Pop the queu to get the current value Check the neighboors (up, down, right, left) If it’s equal to 1 and equal to 0 in result (That means we didn’t see this cell before): Push to the queue it’s position Add in result the parent value in result + 1 Return result Code class Solution { public: vector<vector<int>> updateMatrix(vector<vector<int>> &mat) { m = mat.
read more