Skip to content

Commit

Permalink
so many things
Browse files Browse the repository at this point in the history
  • Loading branch information
haidmoham committed Nov 7, 2016
1 parent edc667f commit 4c535d6
Show file tree
Hide file tree
Showing 17 changed files with 507 additions and 91 deletions.
10 changes: 10 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions src/BubbleSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@

public class BubbleSort {
public static void main(String[] args){
long start = System.currentTimeMillis();
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int[] num = new int[n];
for (int i = 0; i < n; i++){
num[i] = s.nextInt();
System.out.print(num[i] + " ");
}
System.out.println("\n");
int l, r, temp;
l = 0;
r = 1;
Expand All @@ -26,7 +25,7 @@ public static void main(String[] args){
l++;
r++;
}
for (int i = 0; i < n; i++)
System.out.print(num[i] + " ");
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
55 changes: 55 additions & 0 deletions src/DFSStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Created by mohammad on 7/24/16.
*/
import java.util.*;

public class DFSStack {
static int dfs(ArrayList<ArrayList<Integer>> graph, int start, int end){
int[] dist = new int[graph.size()];

Stack<Integer> stack = new Stack<>();
Set<Integer> visited = new HashSet<>();

stack.push(start);
visited.add(start);
dist[start] = 0;

while(!stack.isEmpty()){
int current = stack.pop();
int d = dist[current];
if(current == end) return d;

/*for (int adj : graph.get(current)){
if (!visited.contains(adj)){
visited.add(adj);
stack.pop();
dist[adj] = d+1;
}
}*/
}
return -1;
}

public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n, e, s, b;
n = sc.nextInt();
e = sc.nextInt();
s = sc.nextInt();
b = sc.nextInt();

int u,v;
ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
for (int i = 0; i < n; i++)
graph.add(new ArrayList<Integer>());
for (int i = 0; i < e; i++){
u = sc.nextInt();
v = sc.nextInt();
graph.get(u).add(v);
graph.get(v).add(u);
}
int dist = dfs(graph, s, b);
int min = dist / 6 + (dist % 6 == 0 ? 0 : 1); //Not a part of bfs, specifically for Problem 2 of PCS
System.out.println(min + " " + dist);
}
}
12 changes: 6 additions & 6 deletions src/GridPaths.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
*/
import java.util.*;
public class GridPaths {
public static int paths(int R, int C) {
int[][] result = new int[R][C];
public static long paths(int R, int C) {
long[][] result = new long[R+1][C+1];
for (int i = 0; i < R; i++)
result[i][0] = 1;
for (int i = 0; i < C; i++)
result[0][i] = 1;
for (int i = 1; i < R; i++){
for (int j = 1; j < C; j++){
result[i][j] = result[i-1][j] + result[i][j-1];
for (int i = R; i >= 0; i++){
for (int j = C; j >= 0; j++){
result[i][j] = result[i+1][j] + result[i][j+1];
}
}
return result[R-1][C-1];
return result[R][C];
}

public static void main(String[] args){
Expand Down
89 changes: 89 additions & 0 deletions src/Hardy_Ramanujan_Partitions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Created by mo on 10/2/16.
*/

import java.util.*;
import java.io.*;
import static java.lang.Math.*;


public class Hardy_Ramanujan_Partitions {
static double e = E;
static double c = PI * (sqrt(2) / sqrt(3));
public static void main(String[] args) {
FastScanner sc = new FastScanner();
System.out.println(partition(sc.nextInt()));
}
public static double partition(int n){
/*
Calculates the number of unordered ways to partition a number
in constant time. Accurate in terms of percentage, but not as accurate on smaller cases.
Perhaps code two different versions, one for small numbers, one for large?
*/
double numerator = pow(e, c * sqrt(n));
double denominator = (4 * n * sqrt(3));
return numerator / denominator;
}

public static class FastScanner {
BufferedReader br;
StringTokenizer st;

public FastScanner(Reader in) {
br = new BufferedReader(in);
}

public FastScanner() {
this(new InputStreamReader(System.in));
}

String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}

int nextInt() {
return Integer.parseInt(next());
}

long nextLong() {
return Long.parseLong(next());
}

double nextDouble() {
return Double.parseDouble(next());
}

String readNextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}

int[] readIntArray(int n) {
int[] a = new int[n];
for (int idx = 0; idx < n; idx++) {
a[idx] = nextInt();
}
return a;
}

long[] readLongArray(int n) {
long[] a = new long[n];
for (int idx = 0; idx < n; idx++) {
a[idx] = nextLong();
}
return a;
}
}
}
11 changes: 3 additions & 8 deletions src/HeapSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,18 @@ public static void swap(int arr[], int i, int j){
}

public static void main(String[] args){
long start = System.currentTimeMillis();
Scanner s = new Scanner(System.in);
System.out.println("Heap Sort Test\n");
int n, i;
//# of elements
System.out.println("Enter number of elements: ");
n = s.nextInt();
//Array of n elements
System.out.println("Enter elements: ");
int[] arr = new int[n];
for (i = 0; i < n; i++)
arr[i] = s.nextInt();
//Sort array
sort(arr);
System.out.println("\nElements after sorting: ");
for (i = 0; i < n; i++)
System.out.print(arr[i] + " ");
System.out.println();
long end = System.currentTimeMillis();
System.out.println(end - start);
}

}
15 changes: 10 additions & 5 deletions src/InsertionSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@

public class InsertionSort {
public static void main(String[] args){
int[] arr1 = {10,34,2,56,7,67,88,42};
int[] arr2 = doInsertionSort(arr1);
for (int i:arr2)
System.out.print(i + " ");
long start = System.currentTimeMillis();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
int[] arr2 = doInsertionSort(arr);
long end = System.currentTimeMillis();
System.out.println(end - start);

}
public static int[] doInsertionSort(int[] input){
int temp;
Expand All @@ -20,7 +26,6 @@ public static int[] doInsertionSort(int[] input){
input[j-1] = temp;
}
}
System.out.println(" ");
}
return input;
}
Expand Down
98 changes: 98 additions & 0 deletions src/Kadane.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* Created by mo on 10/5/16.
*/

import java.util.*;
import java.io.*;

public class Kadane {
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int T = sc.nextInt();
for (int i = 0; i < T; i++){
int n = sc.nextInt();
int[] in = new int[n];
for (int j = 0; j < n; j++)
in[j] = sc.nextInt();
int out = biggestSubSum(in);
for (int j = 0; j < n; j++){
int temp = in[j];
in[j] = 0;
int check = biggestSubSum(in);
//System.err.println(check);
out = check > out ? check : out;
in[j] = temp;
}
System.out.println(out);
}
}
public static int biggestSubSum (int[] arr) {
int maxHere = arr[0], maxSoFar = arr[0];
for (int i = 0; i < arr.length; i++){
maxHere = Math.max(Integer.MIN_VALUE, maxHere + arr[i]);
maxSoFar = Math.max(maxSoFar, maxHere);
}
return maxSoFar;
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;

public FastScanner(Reader in) {
br = new BufferedReader(in);
}

public FastScanner() {
this(new InputStreamReader(System.in));
}

String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}

int nextInt() {
return Integer.parseInt(next());
}

long nextLong() {
return Long.parseLong(next());
}

double nextDouble() {
return Double.parseDouble(next());
}

String readNextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}

int[] readIntArray(int n) {
int[] a = new int[n];
for (int idx = 0; idx < n; idx++) {
a[idx] = nextInt();
}
return a;
}

long[] readLongArray(int n) {
long[] a = new long[n];
for (int idx = 0; idx < n; idx++) {
a[idx] = nextLong();
}
return a;
}
}
}
Loading

0 comments on commit 4c535d6

Please sign in to comment.