-
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
ashetkar
committed
Feb 20, 2015
1 parent
57997da
commit d44fb30
Showing
5 changed files
with
389 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package com.leetcode.largestnumber; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* Given a list of non negative integers, arrange them such that they form the | ||
* largest number. | ||
* | ||
* For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. | ||
* | ||
* Note: The result may be very large, so you need to return a string instead of | ||
* an integer. | ||
* | ||
* @author ashetkar | ||
* | ||
*/ | ||
public class Solution { | ||
|
||
/** | ||
* Handles all integer values. Integer.MAX_VALUE is 2.147483647 * 10^9. That's | ||
* why value 9 below. | ||
*/ | ||
private static final int MAX_POWER = 9; | ||
|
||
private boolean illustrate = true; | ||
|
||
private ArrayList<Integer>[] solutionList = new ArrayList[10]; | ||
|
||
public Solution() { | ||
} | ||
|
||
/** | ||
* @param args | ||
*/ | ||
public static void main(String[] args) { | ||
Solution ln = new Solution(); | ||
System.out.println("Largest number: " | ||
+ ln.largestNumber(new int[] { 0, 0 })); | ||
} | ||
|
||
public String largestNumber(int[] num) { | ||
boolean isNonZero = false; | ||
for (int i = 0; i < num.length; i++) { | ||
for (int j = 0; j <= MAX_POWER; j++) { | ||
int divider = (int) Math.pow(10, j); | ||
if (num[i] != 0) { | ||
isNonZero = true; | ||
} | ||
if (num[i] < (divider * 10)) { | ||
int idx = num[i]/divider; | ||
|
||
if (this.solutionList[idx] == null) { | ||
this.solutionList[idx] = new ArrayList<Integer>(); | ||
} | ||
updateList(num[i], this.solutionList[idx]); | ||
break; | ||
} | ||
} | ||
} | ||
return isNonZero ? constructSolution() : "0"; | ||
} | ||
|
||
private void updateList(int num, ArrayList<Integer> list) { | ||
if (list.isEmpty()) { | ||
list.add(num); | ||
} else { | ||
boolean done = false; | ||
for (int i = 0; i < list.size(); i++) { | ||
if (compare(num, list.get(i))) { | ||
list.add(i, num); | ||
done = true; | ||
break; | ||
} | ||
} | ||
if (!done) { | ||
list.add(num); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Return true iff onetwo is greater than or equal to twoone. | ||
* | ||
* @param one | ||
* @param two | ||
* @return | ||
*/ | ||
private boolean compare(int one, int two) { | ||
StringBuilder onetwo = new StringBuilder().append(one).append(two); | ||
StringBuilder twoone = new StringBuilder().append(two).append(one); | ||
return onetwo.toString().compareTo(twoone.toString()) >= 0; | ||
} | ||
|
||
private String constructSolution() { | ||
StringBuilder sb = new StringBuilder(""); | ||
boolean first = true; | ||
for (int i = 9; i >= 0; i--) { | ||
if (solutionList[i] != null) { | ||
for (int j : solutionList[i]) { | ||
if (illustrate) { | ||
if (!first) { | ||
sb.append(","); | ||
} | ||
first = false; | ||
} | ||
sb.append(String.valueOf(j)); | ||
} | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
} |
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,38 @@ | ||
package com.leetcode.lrucache; | ||
|
||
/** | ||
* Design and implement a data structure for Least Recently Used (LRU) cache. It | ||
* should support the following operations: get and set. | ||
* | ||
* get(key) - Get the value (will always be positive) of the key if the key | ||
* exists in the cache, otherwise return -1. set(key, value) - Set or insert the | ||
* value if the key is not already present. When the cache reached its capacity, | ||
* it should invalidate the least recently used item before inserting a new | ||
* item. | ||
* | ||
* @author ashetkar | ||
* | ||
*/ | ||
public class LRUCache { | ||
|
||
public LRUCache() { | ||
} | ||
|
||
/** | ||
* @param args | ||
*/ | ||
public static void main(String[] args) { | ||
|
||
} | ||
|
||
public LRUCache(int capacity) { | ||
} | ||
|
||
public int get(int key) { | ||
return -1; | ||
} | ||
|
||
public void set(int key, int value) { | ||
|
||
} | ||
} |
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,40 @@ | ||
package com.leetcode.maxpointsline; | ||
|
||
/** | ||
* Given n points on a 2D plane, find the maximum number of points that lie on | ||
* the same straight line. | ||
* | ||
* @author ashetkar | ||
* | ||
*/ | ||
public class Solution { | ||
|
||
public Solution() { | ||
} | ||
|
||
/** | ||
* @param args | ||
*/ | ||
public static void main(String[] args) { | ||
|
||
} | ||
|
||
public int maxPoints(Point[] points) { | ||
return -1; | ||
} | ||
} | ||
|
||
class Point { | ||
int x; | ||
int y; | ||
|
||
Point() { | ||
x = 0; | ||
y = 0; | ||
} | ||
|
||
Point(int a, int b) { | ||
x = a; | ||
y = b; | ||
} | ||
} |
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,153 @@ | ||
package com.leetcode.mediansortedarrays; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* | ||
* There are two sorted arrays A and B of size m and n respectively. Find the | ||
* median of the two sorted arrays. The overall run time complexity should be | ||
* O(log (m+n)). | ||
* | ||
* @author ashetkar | ||
* | ||
*/ | ||
public class Solution { | ||
|
||
public Solution() { | ||
} | ||
|
||
/** | ||
* @param args | ||
*/ | ||
public static void main(String[] args) { | ||
int[][] aoa = new int[][] { | ||
{ 3, 5, 7 }, { 2, 4, 6 }, | ||
{ 3, 5, 7 }, { 9, 11, 13}, | ||
{ 3, 5, 7 }, { 5, 6, 8, 10 }, | ||
{ 7, 5, 3 }, { 2, 4, 6 }, | ||
{ 7, 5, 3 }, { 8, 11 }, | ||
{ 7, 5, 3 }, { 5, 6, 8, 10 }, | ||
{ 7, 5, 3 }, { 6, 4, 2 }, | ||
{ 7, 5, 3 }, { 11, 8 }, | ||
{ 7, 5, 3 }, { 10, 8, 6, 5 }}; | ||
|
||
for (int i = 0; i < aoa.length; i += 2) { | ||
System.out.println("Median of " + printArray(aoa[i]) + ", " | ||
+ printArray(aoa[i + 1]) + ": " | ||
+ new Solution().findMedianSortedArrays(aoa[i], aoa[i + 1])); | ||
} | ||
System.out.println("------"); | ||
|
||
for (int i = 0; i < aoa.length; i += 2) { | ||
System.out.println("Median of " + printArray(aoa[i+1]) + ", " | ||
+ printArray(aoa[i]) + ": " | ||
+ new Solution().findMedianSortedArrays(aoa[i+1], aoa[i])); | ||
} | ||
} | ||
|
||
private static String printArray(int[] a) { | ||
StringBuilder sb = new StringBuilder("{ "); | ||
boolean first = true; | ||
for (int e : a) { | ||
if (!first) { | ||
sb.append(", "); | ||
} | ||
first = false; | ||
sb.append(e); | ||
} | ||
return sb.append(" }").toString(); | ||
} | ||
|
||
public double findMedianSortedArrays(int A[], int B[]) { | ||
// handle empty array | ||
if (A.length == 0) { | ||
if (B.length % 2 == 0) { | ||
return (B[B.length/2 - 1] + B[B.length/2]) / 2d; | ||
} else { | ||
return B[B.length/2]; | ||
} | ||
} else if (B.length == 0) { | ||
if (A.length % 2 == 0) { | ||
return (A[A.length/2 - 1] + A[A.length/2]) / 2d; | ||
} else { | ||
return A[A.length/2]; | ||
} | ||
} | ||
|
||
boolean aAscending = A[0] <= A[A.length - 1]; | ||
boolean bAscending = B[0] <= B[B.length - 1]; | ||
|
||
if (aAscending) { | ||
if (bAscending) { | ||
if (A[A.length -1] <= B[0]) { | ||
return handleNoOverlap(A, B); | ||
} else if (B[B.length - 1] <= A[0]) { | ||
return handleNoOverlap(B, A); | ||
} | ||
} | ||
return mergeArrays(A, aAscending, B, bAscending); | ||
} else { | ||
if (!bAscending) { | ||
if (A[A.length -1] >= B[0]) { | ||
return handleNoOverlap(A, B); | ||
} else if (B[B.length - 1] >= A[0]) { | ||
return handleNoOverlap(B, A); | ||
} | ||
} | ||
return mergeArrays(A, aAscending, B, bAscending); | ||
} | ||
} | ||
|
||
// Both assumed to be either ascending or descending together. | ||
private double handleNoOverlap(int[] A, int[] B) { | ||
int total = A.length + B.length; | ||
int halfMark = total/2; | ||
if (total % 2 == 0) { // total length is even number | ||
if (A.length > halfMark) { | ||
return (A[halfMark - 1] + A[halfMark]) / 2d; | ||
} else if (A.length == halfMark) { | ||
return (A[A.length - 1] + B[0]) / 2d; | ||
} else { | ||
int idx = halfMark - A.length; | ||
return (B[idx - 1] + B[idx]) / 2d; | ||
} | ||
} else { // total length is odd number | ||
if (A.length > halfMark) { | ||
return A[halfMark]; | ||
} else { | ||
return B[halfMark-A.length]; | ||
} | ||
} | ||
} | ||
|
||
|
||
private double mergeArrays(int[] A, boolean aAscending, int[] B, | ||
boolean bAscending) { | ||
ArrayList<Integer> union = new ArrayList<Integer>(); | ||
int a = aAscending ? 0 : A.length-1; | ||
int b = bAscending ? 0 : B.length-1; | ||
|
||
for (; ;) { | ||
if (union.size() > (A.length + B.length)/2) { | ||
break; | ||
} | ||
boolean aDone = (a >= A.length && aAscending) || (a < 0 && !aAscending); | ||
boolean bDone = (b >= B.length && bAscending) || (b < 0 && !bAscending); | ||
|
||
if (aDone && bDone) { | ||
break; | ||
} | ||
|
||
if (bDone || (!aDone && A[a] < B[b])) { | ||
union.add(A[a]); | ||
a = aAscending ? a+1 : a-1; | ||
} else if (!bDone) { | ||
union.add(B[b]); | ||
b = bAscending ? b+1 : b-1; | ||
} | ||
} | ||
int s = A.length + B.length; //union.size(); | ||
return s % 2 == 0 ? (union.get((s / 2) - 1) + union.get(s / 2)) / 2d : union.get(s / 2); | ||
} | ||
|
||
} |
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,46 @@ | ||
package com.leetcode.twosum; | ||
|
||
import java.util.HashMap; | ||
|
||
/** | ||
* Given an array of integers, find two numbers such that they add up to a | ||
* specific target number. | ||
* | ||
* The function twoSum should return indices of the two numbers such that they | ||
* add up to the target, where index1 must be less than index2. Please note that | ||
* your returned answers (both index1 and index2) are not zero-based. | ||
* | ||
* You may assume that each input would have exactly one solution. | ||
* | ||
* Input: numbers={2, 7, 11, 15}, target=9 Output: index1=1, index2=2 | ||
* | ||
* @author ashetkar | ||
* | ||
*/ | ||
public class Solution { | ||
|
||
public Solution() { | ||
} | ||
|
||
/** | ||
* @param args | ||
*/ | ||
public static void main(String[] args) { | ||
int[] ans = new Solution().twoSum(new int[]{22, -5, 2, 7, 14, 2, 7, 11, 15}, 9); | ||
System.out.println("Solution: " + ans[0] + ", " + ans[1]); | ||
} | ||
|
||
public int[] twoSum(int[] numbers, int target) { | ||
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); | ||
map.put(numbers[0], 0); | ||
for (int i = 1; i < numbers.length; ++i) { | ||
Integer integer = map.get(target - numbers[i]); | ||
if (integer != null) { | ||
return new int[] {integer+1, i+1}; | ||
} else { | ||
map.put(numbers[i], i); | ||
} | ||
} | ||
return null; | ||
} | ||
} |