Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffrey-xiao committed Apr 11, 2019
1 parent 39845bf commit 2590219
Show file tree
Hide file tree
Showing 1,795 changed files with 23,242 additions and 23,349 deletions.
14 changes: 7 additions & 7 deletions src/codebook/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Template {
static PrintWriter out;
static StringTokenizer st;

public static void main (String[] args) throws IOException {
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
// br = new BufferedReader(new FileReader("in.txt"));
Expand All @@ -22,29 +22,29 @@ public static void main (String[] args) throws IOException {
out.close();
}

static String next () throws IOException {
static String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine().trim());
return st.nextToken();
}

static long readLong () throws IOException {
static long readLong() throws IOException {
return Long.parseLong(next());
}

static int readInt () throws IOException {
static int readInt() throws IOException {
return Integer.parseInt(next());
}

static double readDouble () throws IOException {
static double readDouble() throws IOException {
return Double.parseDouble(next());
}

static char readCharacter () throws IOException {
static char readCharacter() throws IOException {
return next().charAt(0);
}

static String readLine () throws IOException {
static String readLine() throws IOException {
return br.readLine().trim();
}
}
16 changes: 8 additions & 8 deletions src/codebook/algorithms/ArrayRotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public class ArrayRotation {

static void rotate1 (int[] a, int lo, int mid, int hi) {
static void rotate1(int[] a, int lo, int mid, int hi) {
int next = mid;
while (lo != next) {
swap(a, lo++, next++);
Expand All @@ -23,13 +23,13 @@ else if (lo == mid)
}
}

static void rotate2 (int[] a, int lo, int mid, int hi) {
static void rotate2(int[] a, int lo, int mid, int hi) {
reverse(a, lo, mid);
reverse(a, mid, hi);
reverse(a, lo, hi);
}

static void rotate3 (int[] a, int lo, int mid, int hi) {
static void rotate3(int[] a, int lo, int mid, int hi) {
int n = hi - lo, jump = mid - lo;
int gcf = gcf(jump, n), cycle = n / gcf;
for (int i = 0; i < gcf; i++) {
Expand All @@ -44,23 +44,23 @@ static void rotate3 (int[] a, int lo, int mid, int hi) {
}
}

static int gcf (int a, int b) {
static int gcf(int a, int b) {
return b == 0 ? a : gcf(b, a % b);
}

static void reverse (int[] a, int i, int j) {
static void reverse(int[] a, int i, int j) {
while (i != j && i != --j)
swap(a, i++, j);
}

static void swap (int[] a, int i, int j) {
static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}

public static void main (String[] args) throws IOException {
int[] a = new int[] {1, 2, 3, 4, 5, 6, 7, 8};
public static void main(String[] args) throws IOException {
int[] a = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
rotate3(a, 0, 5, 8);
System.out.println(Arrays.toString(a));

Expand Down
18 changes: 9 additions & 9 deletions src/codebook/algorithms/Backtracking.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Backtracking {
static int[] row;
static boolean[] used, diag1, diag2;

public static void main (String[] args) throws IOException {
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
//br = new BufferedReader(new FileReader("in.txt"));
Expand All @@ -39,7 +39,7 @@ public static void main (String[] args) throws IOException {
out.close();
}

static int backtrack (int i, int n) {
static int backtrack(int i, int n) {
if (i == n) {
for (int j = 0; j < n; j++)
for (int k = j + 1; k < n; k++)
Expand All @@ -58,7 +58,7 @@ static int backtrack (int i, int n) {
return total;
}

static int branchAndBound (int i, int n) {
static int branchAndBound(int i, int n) {
if (i == n)
return 1;
int total = 0;
Expand All @@ -72,29 +72,29 @@ static int branchAndBound (int i, int n) {
return total;
}

static String next () throws IOException {
static String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine().trim());
return st.nextToken();
}

static long readLong () throws IOException {
static long readLong() throws IOException {
return Long.parseLong(next());
}

static int readInt () throws IOException {
static int readInt() throws IOException {
return Integer.parseInt(next());
}

static double readDouble () throws IOException {
static double readDouble() throws IOException {
return Double.parseDouble(next());
}

static char readCharacter () throws IOException {
static char readCharacter() throws IOException {
return next().charAt(0);
}

static String readLine () throws IOException {
static String readLine() throws IOException {
return br.readLine().trim();
}
}
4 changes: 2 additions & 2 deletions src/codebook/algorithms/BinaryExponentiation.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
package codebook.algorithms;

public class BinaryExponentiation {
public long modpow (long b, long p, long m) {
public long modpow(long b, long p, long m) {
if (p == 0)
return 1;
if (p == 1)
Expand All @@ -17,7 +17,7 @@ public long modpow (long b, long p, long m) {
return b * modpow(b * b % m, p / 2, m) % m;
}

public long pow (long b, long p) {
public long pow(long b, long p) {
if (p == 0)
return 1;
if (p == 1)
Expand Down
8 changes: 4 additions & 4 deletions src/codebook/algorithms/BinarySearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
package codebook.algorithms;

public class BinarySearch {
public static int bsearch (int[] a, int lo, int hi, int val) {
public static int bsearch(int[] a, int lo, int hi, int val) {
while (lo <= hi) {
int mid = lo + ((hi - lo) >> 1);
if (a[mid] == val)
Expand All @@ -20,7 +20,7 @@ else if (a[mid] < val)
return -1;
}

public static int bsearchCeil (int[] a, int lo, int hi, int val) {
public static int bsearchCeil(int[] a, int lo, int hi, int val) {
while (lo <= hi) {
int mid = lo + ((hi - lo) >> 1);
if (a[mid] < val)
Expand All @@ -31,7 +31,7 @@ public static int bsearchCeil (int[] a, int lo, int hi, int val) {
return lo;
}

public static int bsearchFloor (int[] a, int lo, int hi, int val) {
public static int bsearchFloor(int[] a, int lo, int hi, int val) {
while (lo <= hi) {
int mid = lo + ((hi - lo) >> 1);
if (a[mid] <= val)
Expand All @@ -42,7 +42,7 @@ public static int bsearchFloor (int[] a, int lo, int hi, int val) {
return hi;
}

public static void main (String[] args) {
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 4, 4, 4, 4, 4, 6, 7, 8};
System.out.println(bsearchCeil(a, 0, 11, 9));
System.out.println(bsearchFloor(a, 0, 11, 4));
Expand Down
46 changes: 23 additions & 23 deletions src/codebook/algorithms/BranchAndBound.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class BranchAndBound {
static ArrayList<Schedule> curr = new ArrayList<Schedule>();
static ArrayList<Schedule> candidates = new ArrayList<Schedule>();

public static void main (String[] args) throws IOException {
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
//br = new BufferedReader(new FileReader("in.txt"));
Expand All @@ -59,7 +59,7 @@ public static void main (String[] args) throws IOException {
out.close();
}

static void solve (int i) {
static void solve(int i) {
if (min == 17)
return;
if (i == 0) {
Expand Down Expand Up @@ -93,44 +93,44 @@ static void solve (int i) {
}
}

static class Schedule implements Comparable<Schedule> {
int start, interval, occ;

Schedule (int start, int interval) {
this.start = start;
this.interval = interval;
this.occ = (59 - start) / interval + 1;
}

@Override
public int compareTo (Schedule s) {
return s.occ - occ;
}
}

static String next () throws IOException {
static String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine().trim());
return st.nextToken();
}

static long readLong () throws IOException {
static long readLong() throws IOException {
return Long.parseLong(next());
}

static int readInt () throws IOException {
static int readInt() throws IOException {
return Integer.parseInt(next());
}

static double readDouble () throws IOException {
static double readDouble() throws IOException {
return Double.parseDouble(next());
}

static char readCharacter () throws IOException {
static char readCharacter() throws IOException {
return next().charAt(0);
}

static String readLine () throws IOException {
static String readLine() throws IOException {
return br.readLine().trim();
}

static class Schedule implements Comparable<Schedule> {
int start, interval, occ;

Schedule(int start, int interval) {
this.start = start;
this.interval = interval;
this.occ = (59 - start) / interval + 1;
}

@Override
public int compareTo(Schedule s) {
return s.occ - occ;
}
}
}
50 changes: 25 additions & 25 deletions src/codebook/algorithms/ClosestTwoPoints.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@

public class ClosestTwoPoints {

static Point[] findClosestPair (Point[] points) {
final static ComparatorX cmpx = new ComparatorX();
final static ComparatorY cmpy = new ComparatorY();

static Point[] findClosestPair(Point[] points) {
Point[] ret = new Point[2];
Arrays.sort(points, cmpx);
solve(points, 0, points.length - 1, ret, 1 << 30);
return ret;
}

static int solve (Point[] points, int l, int r, Point[] ret, int minDist) {
static int solve(Point[] points, int l, int r, Point[] ret, int minDist) {
if (l == r)
return 1 << 30;
int mid = (l + r) >> 1;
Expand Down Expand Up @@ -47,58 +50,55 @@ static int solve (Point[] points, int l, int r, Point[] ret, int minDist) {
return minDist;
}

static int findClosestPairSlow (Point[] points) {
static int findClosestPairSlow(Point[] points) {
int ret = 1 << 30;
for (int i = 0; i < points.length; i++)
for (int j = i + 1; j < points.length; j++)
ret = Math.min(ret, dist(points[i], points[j]));
return ret;
}

static int dist (Point a, Point b) {
static int dist(Point a, Point b) {
int dx = a.x - b.x;
int dy = a.y - b.y;
return dx * dx + dy * dy;
}

public static void main(String[] args) {
for (int iter = 0; iter < 100; iter++) {
int N = 100;
Point[] points = new Point[N];
for (int i = 0; i < N; i++)
points[i] = new Point((int) (Math.random() * 1000), (int) (Math.random() * 1000));

Point[] res = findClosestPair(points);
int d1 = dist(res[0], res[1]);
int d2 = findClosestPairSlow(points);

assert d1 == d2;
}
}

static class Point {
int x, y;

Point (int x, int y) {
Point(int x, int y) {
this.x = x;
this.y = y;
}
}

final static ComparatorX cmpx = new ComparatorX();
final static ComparatorY cmpy = new ComparatorY();

static class ComparatorX implements Comparator<Point> {
public int compare (Point a, Point b) {
public int compare(Point a, Point b) {
if (a.x == b.x)
return a.y - b.y;
return a.x - b.x;
}
}

static class ComparatorY implements Comparator<Point> {
public int compare (Point a, Point b) {
public int compare(Point a, Point b) {
return a.y - b.y;
}
}

public static void main (String[] args) {
for (int iter = 0; iter < 100; iter++) {
int N = 100;
Point[] points = new Point[N];
for (int i = 0; i < N; i++)
points[i] = new Point((int)(Math.random() * 1000), (int)(Math.random() * 1000));

Point[] res = findClosestPair(points);
int d1 = dist(res[0], res[1]);
int d2 = findClosestPairSlow(points);

assert d1 == d2;
}
}
}
Loading

0 comments on commit 2590219

Please sign in to comment.