forked from RodneyShag/HackerRank_solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ac5cfb
commit 34e6656
Showing
26 changed files
with
859 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
10 Days of Statistics/Day 9 - Multiple Linear Regression/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
// Author: Rodney Shaghoulian | ||
// Github: github.com/rshaghoulian | ||
// HackerRank: hackerrank.com/rshaghoulian | ||
|
||
import java.util.Scanner; | ||
import java.util.Arrays; | ||
|
||
public class Solution { | ||
public static void main(String[] args) { | ||
/* Read input: Create and fill X,Y arrays */ | ||
Scanner scan = new Scanner(System.in); | ||
int m = scan.nextInt(); | ||
int n = scan.nextInt(); | ||
double [][] X = new double[n][m + 1]; | ||
double [][] Y = new double[n][1]; | ||
for (int row = 0; row < n; row++) { | ||
X[row][0] = 1; | ||
for (int col = 1; col <= m; col++) { | ||
X[row][col] = scan.nextDouble(); | ||
} | ||
Y[row][0] = scan.nextDouble(); | ||
} | ||
|
||
/* Calculate B */ | ||
double [][] xtx = multiply(transpose(X),X); | ||
double [][] xtxInv = invert(xtx); | ||
double [][] xty = multiply(transpose(X), Y); | ||
double [][] B = multiply(xtxInv, xty); | ||
|
||
int sizeB = B.length; | ||
|
||
/* Calculate and print values for the "q" feature sets */ | ||
int q = scan.nextInt(); | ||
for (int i = 0; i < q; i++) { | ||
double result = B[0][0]; | ||
for (int row = 1; row < sizeB; row++) { | ||
result += scan.nextDouble() * B[row][0]; | ||
} | ||
System.out.println(result); | ||
} | ||
} | ||
|
||
/* Multiplies 2 matrices in O(n^3) time */ | ||
public static double[][] multiply(double [][] A, double [][] B) { | ||
int aRows = A.length; | ||
int aCols = A[0].length; | ||
int bRows = B.length; | ||
int bCols = B[0].length; | ||
|
||
double [][] C = new double[aRows][bCols]; | ||
int cRows = C.length; | ||
int cCols = C[0].length; | ||
|
||
for (int row = 0; row < cRows; row++) { | ||
for (int col = 0; col < cCols; col++) { | ||
for (int k = 0; k < aCols; k++) { | ||
C[row][col] += A[row][k] * B[k][col]; | ||
} | ||
} | ||
} | ||
return C; | ||
} | ||
|
||
public static double[][] transpose(double [][] matrix) { | ||
/* Create new array with switched dimensions */ | ||
int originalRows = matrix.length; | ||
int originalCols = matrix[0].length; | ||
int rows = originalCols; | ||
int cols = originalRows; | ||
double [][] result = new double[rows][cols]; | ||
|
||
/* Fill our new 2D array */ | ||
for (int row = 0; row < originalRows; row++) { | ||
for (int col = 0; col < originalCols; col++) { | ||
result[col][row] = matrix[row][col]; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
/******************************************************************/ | ||
/* Matrix Inversion code (the 2 functions below) are from: */ | ||
/* http://www.sanfoundry.com/java-program-find-inverse-matrix/ */ | ||
/******************************************************************/ | ||
|
||
public static double[][] invert(double a[][]) | ||
{ | ||
int n = a.length; | ||
double x[][] = new double[n][n]; | ||
double b[][] = new double[n][n]; | ||
int index[] = new int[n]; | ||
for (int i=0; i<n; ++i) | ||
b[i][i] = 1; | ||
|
||
// Transform the matrix into an upper triangle | ||
gaussian(a, index); | ||
|
||
// Update the matrix b[i][j] with the ratios stored | ||
for (int i=0; i<n-1; ++i) | ||
for (int j=i+1; j<n; ++j) | ||
for (int k=0; k<n; ++k) | ||
b[index[j]][k] | ||
-= a[index[j]][i]*b[index[i]][k]; | ||
|
||
// Perform backward substitutions | ||
for (int i=0; i<n; ++i) | ||
{ | ||
x[n-1][i] = b[index[n-1]][i]/a[index[n-1]][n-1]; | ||
for (int j=n-2; j>=0; --j) | ||
{ | ||
x[j][i] = b[index[j]][i]; | ||
for (int k=j+1; k<n; ++k) | ||
{ | ||
x[j][i] -= a[index[j]][k]*x[k][i]; | ||
} | ||
x[j][i] /= a[index[j]][j]; | ||
} | ||
} | ||
return x; | ||
} | ||
|
||
// Method to carry out the partial-pivoting Gaussian | ||
// elimination. Here index[] stores pivoting order. | ||
|
||
public static void gaussian(double a[][], int index[]) | ||
{ | ||
int n = index.length; | ||
double c[] = new double[n]; | ||
|
||
// Initialize the index | ||
for (int i=0; i<n; ++i) | ||
index[i] = i; | ||
|
||
// Find the rescaling factors, one from each row | ||
for (int i=0; i<n; ++i) | ||
{ | ||
double c1 = 0; | ||
for (int j=0; j<n; ++j) | ||
{ | ||
double c0 = Math.abs(a[i][j]); | ||
if (c0 > c1) c1 = c0; | ||
} | ||
c[i] = c1; | ||
} | ||
|
||
// Search the pivoting element from each column | ||
int k = 0; | ||
for (int j=0; j<n-1; ++j) | ||
{ | ||
double pi1 = 0; | ||
for (int i=j; i<n; ++i) | ||
{ | ||
double pi0 = Math.abs(a[index[i]][j]); | ||
pi0 /= c[index[i]]; | ||
if (pi0 > pi1) | ||
{ | ||
pi1 = pi0; | ||
k = i; | ||
} | ||
} | ||
|
||
// Interchange rows according to the pivoting order | ||
int itmp = index[j]; | ||
index[j] = index[k]; | ||
index[k] = itmp; | ||
for (int i=j+1; i<n; ++i) | ||
{ | ||
double pj = a[index[i]][j]/a[index[j]][j]; | ||
|
||
// Record pivoting ratios below the diagonal | ||
a[index[i]][j] = pj; | ||
|
||
// Modify other elements accordingly | ||
for (int l=j+1; l<n; ++l) | ||
a[index[i]][l] -= pj*a[index[j]][l]; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Author: Rodney Shaghoulian | ||
// Github: github.com/rshaghoulian | ||
// HackerRank: hackerrank.com/rshaghoulian | ||
|
||
import java.util.Scanner; | ||
|
||
public class Solution { | ||
public static void main(String[] args) { | ||
Scanner scan = new Scanner(System.in); | ||
int n = scan.nextInt(); | ||
scan.close(); | ||
System.out.println(consecutiveOnes(n)); | ||
} | ||
|
||
public static int consecutiveOnes(int n) { | ||
int count = 0; | ||
int max = 0; | ||
while (n > 0) { | ||
if (n % 2 == 1) { | ||
count++; | ||
max = Math.max(max, count); | ||
} else { | ||
count = 0; | ||
} | ||
n = n >> 1; | ||
} | ||
return max; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Author: Rodney Shaghoulian | ||
// Github: github.com/rshaghoulian | ||
// HackerRank: hackerrank.com/rshaghoulian | ||
|
||
import java.util.Scanner; | ||
|
||
public class Solution { | ||
|
||
public static void main(String [] args) { | ||
Scanner scan = new Scanner(System.in); | ||
int arr[][] = new int[6][6]; | ||
for (int row = 0; row < 6; row++) { | ||
for (int col = 0; col < 6; col++) { | ||
arr[row][col] = scan.nextInt(); | ||
} | ||
} | ||
scan.close(); | ||
|
||
System.out.println(maxHourglass(arr)); | ||
} | ||
|
||
public static int maxHourglass(int [][] arr) { | ||
int max = Integer.MIN_VALUE; | ||
for (int row = 0; row < 4; row++) { | ||
for (int col = 0; col < 4; col++) { | ||
int sum = findSum(arr, row, col); | ||
max = Math.max(max, sum); | ||
} | ||
} | ||
return max; | ||
} | ||
|
||
private static int findSum(int [][] arr, int r, int c) { | ||
int sum = arr[r+0][c+0] + arr[r+0][c+1] + arr[r+0][c+2] | ||
+ arr[r+1][c+1] + | ||
arr[r+2][c+0] + arr[r+2][c+1] + arr[r+2][c+2]; | ||
return sum; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Author: Rodney Shaghoulian | ||
// Github: github.com/rshaghoulian | ||
// HackerRank: hackerrank.com/rshaghoulian | ||
|
||
class Student extends Person{ | ||
private int[] testScores; | ||
|
||
/* | ||
* Class Constructor | ||
* | ||
* @param firstName - A string denoting the Person's first name. | ||
* @param lastName - A string denoting the Person's last name. | ||
* @param id - An integer denoting the Person's ID number. | ||
* @param scores - An array of integers denoting the Person's test scores. | ||
*/ | ||
public Student(String firstName, String lastName, int id, int [] scores) { | ||
super(firstName, lastName, id); | ||
testScores = scores; | ||
} | ||
|
||
/* | ||
* Method Name: calculate | ||
* @return A character denoting the grade. | ||
*/ | ||
public char calculate() { | ||
double average = 0; | ||
for (int score : testScores) { | ||
average += score; | ||
} | ||
average /= testScores.length; | ||
|
||
if (average >= 90) { | ||
return 'O'; | ||
} | ||
else if (average >= 80) { | ||
return 'E'; | ||
} | ||
else if (average >= 70) { | ||
return 'A'; | ||
} | ||
else if (average >= 55) { | ||
return 'P'; | ||
} | ||
else if (average >= 40) { | ||
return 'D'; | ||
} | ||
else { | ||
return 'T'; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Author: Rodney Shaghoulian | ||
// Github: github.com/rshaghoulian | ||
// HackerRank: hackerrank.com/rshaghoulian | ||
|
||
class MyBook extends Book { | ||
int price; | ||
|
||
MyBook(String title, String author, int price) { | ||
super(title, author); | ||
this.price = price; | ||
} | ||
|
||
void display() { | ||
System.out.println("Title: " + title); | ||
System.out.println("Author: " + author); | ||
System.out.println("Price: " + price); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Author: Rodney Shaghoulian | ||
// Github: github.com/rshaghoulian | ||
// HackerRank: hackerrank.com/rshaghoulian | ||
|
||
class Difference { | ||
private int[] elements; | ||
public int maximumDifference; | ||
|
||
Difference (int [] elements) { | ||
this.elements = elements; | ||
} | ||
|
||
// O(n) runtime. O(1) space | ||
void computeDifference() { | ||
maximumDifference = 0; | ||
int min = elements[0]; | ||
int max = elements[0]; | ||
for (int num : elements) { | ||
min = Math.min(min, num); | ||
max = Math.max(max, num); | ||
} | ||
maximumDifference = max - min; | ||
} | ||
} // End of Difference class |
Oops, something went wrong.