Leetcode - Maximum Non Negative Product in a Matrix
Problem Statement You are given a rows x cols matrix grid. Initially, you are…
May 15, 2019
It is one of a simple algorithm to study for a beginner to understanding sorting algorithms.
In this algorithm, we start with 0th index value. We compare it in whole array to find if any other number is smaller than this. So effectively, we find the minimum number through rest of the array from our starting index.
And, we swap the numbers of the initial number and the smalles number found in array.
See the code here:
public void sort(int[] arr) {
int l = arr.length;
for (int i=0; i<l-1; i++) {
int smallestNumIndex = i;
for (int j=i+1; j<l; j++) {
if (arr[smallestNumIndex] > arr[j]) {
smallestNumIndex = j;
}
}
if (i != smallestNumIndex) {
//swap
int temp = arr[i];
arr[i] = arr[smallestNumIndex];
arr[smallestNumIndex] = temp;
}
}
}
The algorithm runs on O(n^2) in worst/average case.
Problem Statement You are given a rows x cols matrix grid. Initially, you are…
This algorithm is very efficient one, and is classic example of Divide and…
Problem Statement Given an array, rotate the array to the right by k steps…
Problem Statement Say you have an array prices for which the ith element is the…
Problem Statement Write a function that reverses a string. The input string is…
** Inversion There is an array(a) and two indexes i and j. Inversion is the…
In this post, we will see some of the frequently used concepts/vocabulary in…
System design interview is pretty common these days, specially if you are having…
Introduction You are given an array of integers with size N, and a number K…
Graph Topological Sorting This is a well known problem in graph world…
Problem Statement Given a Binary tree, print out nodes in level order traversal…
Problem Statement Given an array nums of n integers and an integer target, are…