From d9de5957df230dc9451d6920956180994fa0cdcf Mon Sep 17 00:00:00 2001 From: Prakash-sa Date: Fri, 2 Oct 2020 10:24:40 +0530 Subject: [PATCH 1/3] largest rectangular area histogram --- .../Largest_Rectangular_Area_Histogram.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Algorithms/Largest_Rectangular_Area_Histogram.java diff --git a/Algorithms/Largest_Rectangular_Area_Histogram.java b/Algorithms/Largest_Rectangular_Area_Histogram.java new file mode 100644 index 00000000..129490b9 --- /dev/null +++ b/Algorithms/Largest_Rectangular_Area_Histogram.java @@ -0,0 +1,62 @@ +//Java program to find maximum rectangular area in linear time + +import java.util.Stack; + +public class RectArea_Histogram +{ + static int getMaxArea(int hist[], int n) + { + // The stack holds indexes of hist[] array + Stack s = new Stack<>(); + + int max_area = 0; // Initialize max area + int top; // To store top of stack + int area_with_top; // To store area with top bar as the smallest bar + + int i = 0; + while (i < n) + { + // If this bar is higher than the bar on top stack, push it to stack + if (s.empty() || hist[s.peek()] <= hist[i]) + s.push(i++); + + // If this bar is lower than top of stack, then calculate area of rectangle + // with stack top as the smallest bar. + else + { + top = s.peek(); + s.pop(); + + // Calculate the area with hist[tp] stack as smallest bar + area_with_top = hist[top] * (s.empty() ? i : i - s.peek() - 1); + + // update max area, if needed + if (max_area < area_with_top) + max_area = area_with_top; + } + } + + // Now pop the remaining bars from stack and calculate area with every + // popped bar as the smallest bar + while (s.empty() == false) + { + top = s.peek(); + s.pop(); + area_with_top = hist[top] * (s.empty() ? i : i - s.peek() - 1); + + if (max_area < area_with_top) + max_area = area_with_top; + } + + return max_area; + + } + + + public static void main(String[] args) + { + int hist[] = { 6, 1, 7, 4, 5, 9, 2 }; + System.out.println(getMaxArea(hist, hist.length)); + } +} + From 80f72582036eec059bbb4aff236d945124824176 Mon Sep 17 00:00:00 2001 From: Prakash-sa Date: Fri, 2 Oct 2020 11:14:16 +0530 Subject: [PATCH 2/3] knn classifier --- Machine Learning/knn_classifier.java | 49 ++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Machine Learning/knn_classifier.java diff --git a/Machine Learning/knn_classifier.java b/Machine Learning/knn_classifier.java new file mode 100644 index 00000000..8330b82b --- /dev/null +++ b/Machine Learning/knn_classifier.java @@ -0,0 +1,49 @@ +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; + +import weka.classifiers.Classifier; +import weka.classifiers.lazy.IBk; +import weka.core.Instance; +import weka.core.Instances; + +/* +kNN is also provided by Weka as a class "IBk". IBk implements kNN. It uses normalized distances for all attributes so that attributes on different scales have the same impact on the distance function. It may return more than k neighbors if there are ties in the distance. Neighbors are voted to form the final classification. + +First prepare your data by creating a txt file "ads.txt": +*/ + +public class KNN { + public static BufferedReader readDataFile(String filename) { + BufferedReader inputReader = null; + + try { + inputReader = new BufferedReader(new FileReader(filename)); + } catch (FileNotFoundException ex) { + System.err.println("File not found: " + filename); + } + + return inputReader; + } + + public static void main(String[] args) throws Exception { + BufferedReader datafile = readDataFile("ads.txt"); + + Instances data = new Instances(datafile); + data.setClassIndex(data.numAttributes() - 1); + + //do not use first and second + Instance first = data.instance(0); + Instance second = data.instance(1); + data.delete(0); + data.delete(1); + + Classifier ibk = new IBk(); + ibk.buildClassifier(data); + + double class1 = ibk.classifyInstance(first); + double class2 = ibk.classifyInstance(second); + + System.out.println("first: " + class1 + "\nsecond: " + class2); + } +} From b0598015a82310c0d323a8df7392fbd05bfe08db Mon Sep 17 00:00:00 2001 From: Prakash Saini <35262074+Prakash-sa@users.noreply.github.com> Date: Fri, 2 Oct 2020 11:17:50 +0530 Subject: [PATCH 3/3] Delete knn_classifier.java --- Machine Learning/knn_classifier.java | 49 ---------------------------- 1 file changed, 49 deletions(-) delete mode 100644 Machine Learning/knn_classifier.java diff --git a/Machine Learning/knn_classifier.java b/Machine Learning/knn_classifier.java deleted file mode 100644 index 8330b82b..00000000 --- a/Machine Learning/knn_classifier.java +++ /dev/null @@ -1,49 +0,0 @@ -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.FileReader; - -import weka.classifiers.Classifier; -import weka.classifiers.lazy.IBk; -import weka.core.Instance; -import weka.core.Instances; - -/* -kNN is also provided by Weka as a class "IBk". IBk implements kNN. It uses normalized distances for all attributes so that attributes on different scales have the same impact on the distance function. It may return more than k neighbors if there are ties in the distance. Neighbors are voted to form the final classification. - -First prepare your data by creating a txt file "ads.txt": -*/ - -public class KNN { - public static BufferedReader readDataFile(String filename) { - BufferedReader inputReader = null; - - try { - inputReader = new BufferedReader(new FileReader(filename)); - } catch (FileNotFoundException ex) { - System.err.println("File not found: " + filename); - } - - return inputReader; - } - - public static void main(String[] args) throws Exception { - BufferedReader datafile = readDataFile("ads.txt"); - - Instances data = new Instances(datafile); - data.setClassIndex(data.numAttributes() - 1); - - //do not use first and second - Instance first = data.instance(0); - Instance second = data.instance(1); - data.delete(0); - data.delete(1); - - Classifier ibk = new IBk(); - ibk.buildClassifier(data); - - double class1 = ibk.classifyInstance(first); - double class2 = ibk.classifyInstance(second); - - System.out.println("first: " + class1 + "\nsecond: " + class2); - } -}