Posts
Square Of A Sorted Array
PROBLEM Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order
Follow up: Squaring each element and sorting the new array is very trivial, could you find an O(n) solution using a different approach?
Example1 *Input*: nums = [-4,-1,0,3,10] *Output*: [0,1,9,16,100] *Explanation*: After squaring, the array becomes [16,1,0,9,100]. After sorting, it becomes [0,1,9,16,100]. Example 2 *Input*: nums = [-7,-3,2,3,11] *Output*: [4,9,9,49,121] SOLVING We will use Two Pointers technique.
read morePosts
Stack
INFORMATION Stack is a linear Data Structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first, comes out last.
IMPLEMENTATION C Language .h file typedef struct node_s { void *value; struct node_s *next; } node_t, *stack_t; /* informations */ unsigned int stack_get_size(stack_t stack); bool stack_is_empty(stack_t stack); /* Modifications */ bool stack_push(stack_t *stack_ptr, void *elem); bool stack_pop(stack_t *stack_ptr); void stack_clear(stack_t *stack_ptr); /* Access */ void *stack_top(stack_t stack); .
read morePosts
SubRectangleQueries
PROBLEM Implement the class SubrectangleQueries which receives a rows x cols rectangle as a matrix of integers in the constructor and supports two methods:
updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) Updates all values with newValue in the subrectangle whose upper left coordinate is (row1,col1) and bottom right coordinate is (row2,col2).
getValue(int row, int col) Returns the current value of the coordinate (row,col) from the rectangle.
You want to update element between row2 and row3 and between col3 and col6
read morePosts
Top K Frequent Elements
PROBLEM Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.
Example 1 *Input*: nums = [1,1,1,2,2,3], k = 2 *Output*: [1,2] Example 2 *Input*: nums = [1], k = 1 *Output*: [1] SOLVING We’ll use the Bucket Sort method.
Steps Create a Map to store the frequency of each letters, a Map buckets and the array for the result Iterate on the string use each letter as key for frequency and increase by one the value Fill the buckets by using the value of frequency as the key of buckets and add the key of frequency in the value of the bucket Iterate from the end of buckets: Iterate on each element in the bucket value: Add this element to result if result length is equal to k return result Example With the parameters nums = [1,1,1,2,2,3] and k = 2
read morePosts
Two Pointers
INFO The two pointers is a searching algorithm if you want at the same time:
Search two elements in an array Search and do something in an array Keep in mind an index when moving another Ect…
read morePosts
Two Sum || Array Sorted
PROBLEM Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1<=index1<index2<=numbers.length.
Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2.
The tests are generated such that there is exactly one solution.
read morePosts
Valid Parentheses
PROBLEM Given a string s containing just the characters '(' , ')' , '{' , '}' , '[' and ']', determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Every close bracket has a corresponding open bracket of the same type. Example1 *Input*: s = "()" *Output*: true Example2 *Input*: s = "([]){}" *Output*: true Example3 *Input*: s = "(]" *Output*: false SOLVING Steps Create a Stack Browse the string: If characters is an open parentheses push to the stack If characters is an close parentheses If the top of the stack is the open equivalent pop the stack Otherwise return false, the string isn’t closing the last open parentheses Otherwise return false, the character isn’t an open or close parentheses return true if the stack is empty otherwise false because all parentheses aren’t closed Code class Solution { public: bool isValid(string s) { const string open = "({["; const string close = ")}]"; stack<char> parenthesesOpen; for (int i = 0; i < s.
read morePosts
Valid Phone Numbers
PROBLEM Given a text file file.txt that contains a list of phone numbers (one per line), write a one-liner bash script to print all valid phone numbers.
You may assume that a valid phone number must appear in one of the following two formats: (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)
You may also assume each line in the text file must not contain leading or trailing white spaces.
read more