Skip to content

Commit

Permalink
330v0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
jackqiang123 authored and Lian Lu committed May 31, 2016
1 parent 8a621ef commit 4b9c23e
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 9 deletions.
90 changes: 87 additions & 3 deletions lc321.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum number of length k <= m + n from digits of the two. The relative order of the digits from the same array must be preserved. Return an array of the k digits. You should try to optimize your time and space complexity.
// Given two arrays of length m and n with digits 0-9 representing two numbers.
// Create the maximum number of length k <= m + n from digits of the two.
// The relative order of the digits from the same array must be preserved.
// Return an array of the k digits. You should try to optimize your time and space complexity.
//
// Example 1:
// nums1 = [3, 4, 6, 5]
Expand All @@ -20,7 +23,88 @@
//

public class Solution {
public int[] maxNumber(int[] nums1, int[] nums2, int k) {
public int[] maxNumber(int[] nums1, int[] nums2, int k){
int len1 = nums1.length;
int len2 = nums2.length;
int res = new int[k];
for (int i = Math.max(0, k-len2); i <= k && i <= len1; i++){
int []candidate = merge(getNumber(nums1, nums1.length() - 1, i), getNumber(nums2, nums2.length() - 1, k - i));
if (!greater(res, candidate)) res = candidate;
}
return res;
}

private int[]merge(int l, int r){
String left = String.valueOf(l);
String right = String.valueOf(r);
int res[] = new int[left.length() + right.length()];
int pos = 0;
int i = 0; int j = 0;
while(pos < res.length){
if (i < left.length() && j < right.length()){
if (left.charAt(i) > right.charAt(j))
res[pos++] = left.charAt(i++) - '0';
else res[pos++] = right.charAt(j++) - '0';
}
else if (i < left.length()){
res[pos++] = left.charAt(i++) -'0';
}
else {
res[pos++] = right.charAt(j++) - '0';
}
}
}
return res;
}

private boolean greater(int []c1, int []c2){
for (int i = 0; i < c1.length; i++){
if (c1[i] > c2[i]) return true;
else if (c1[i] < c2[i]) return false;
}
return false;
}


private int getNumber(int[]nums, int end, int k){
int [][]dp = new int[end+1][k+1];
for (int i = 0; i <= end; i++){
for (int j = 0; j < dp[0].length; j++){
if (j == 0) dp[i][j] = 0;
else if (i == 0) dp[i][j] = nums[i];
else dp[i][j] = Math.max(dp[i-1][j], dp[i-1][j-1]*10+nums[i]);
}
}
return dp[end][k];
}

public int[] maxNumberUsingDP(int[] nums1, int[] nums2, int K) {
int len1 = nums1.length;
int len2 = nums2.length;
int [][][] dp = new int[len1 + 1][len2 + 1][K+1];
for (int k = 0; k <= K; k++){
for (int i = 0; i <= len1; i++){
for (int j = 0; j <= len2; j++){
if (k == 0 || (i == 0 && j == 0) || (i+j < k)) dp[i][j][k] = 0;
else if (i == 0){
dp[i][j][k] = getNumber(nums2, j - 1, k);
}
else if (j == 0){
dp[i][j][k] = getNumber(nums1, i - 1, k);
}
else {
dp[i][j][k] = Math.max(dp[i-1][j][k], dp[i][j-1][k]);
int n1 = dp[i-1][j][k-1]*10 + nums1[i-1];
int n2 = dp[i][j-1][k-1]*10 + nums2[j-1];
dp[i][j][k] = Math.max(dp[i][j][k], Math.max(n1,n2));
}
}
}
}
int num = dp[len1][len2][K];
int len = String.valueOf(num).length();
int res[] = new int[len];
for (int i = len - 1; i >= 0; i--)
{ res[i] = num%10; num /= 10;}
return res;
}
}
16 changes: 14 additions & 2 deletions lc322.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
// You are given coins of different denominations and a total amount of money amount.
// Write a function to compute the fewest number of coins that you need to make up that amount.
// If that amount of money cannot be made up by any combination of the coins, return -1.
//
// Example 1:
// coins = [1, 2, 5], amount = 11
Expand All @@ -12,6 +14,16 @@
// You may assume that you have an infinite number of each kind of coin.
public class Solution {
public int coinChange(int[] coins, int amount) {

int []dp = new int[amount + 1];
if (amount == 0) return 0;
for (int i = 1; i <= amount; i++){
dp[i] = Integer.MAX_VALUE;
for (int c : coins){
if (i - c >= 0 && dp[i -c] < Integer.MAX_VALUE){
dp[i] = Math.min(dp[i] , dp[i - c] + 1);
}
}
}
return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
}
}
30 changes: 30 additions & 0 deletions lc323.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,34 @@
// You can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges.

public class Solution {
// can also be done in union find algorithm
boolean visit[];
Map<Integer,List<Integer>> map;
public int countComponents(int n, int[][] edges) {
visit[] = new int[n];
map = new HashMap();
for (int i = 0; i < edges.length; i++){
int s = edges[i][0];
int v = edges[i][1];
if (map.get(s) == null) map.put(s, new ArrayList());
if (map.get(v) == null) map.put(v, new ArrayList());
map.get(s).add(v);
map.get(v).add(s);
}
int res = 0;
for (int i = 0; i < n; i++){
if (!visit[i]){
res++; dfs(i);
}
}
return res;
}
private void dfs(int i){
if (visit[i]) return;
visit[i] = true;
if (map.get(i) == null) return;
for (int nb : map.get(i)){
dfs(nb);
}
}
}
14 changes: 13 additions & 1 deletion lc324.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@

public class Solution {
public void wiggleSort(int[] nums) {

Arrays.sort(nums);
int len = nums.length;
int mid = (len-1)/2;
int []temp = new int[len];
for (int i = 0; i < len; i++)
temp[i] = nums[i];
int i = mid;
int j = len - 1;
int pos = 0;
while(true){
nums[pos++] = temp[mid--];
nums[pos++] = temp[j--];
}
}
}
56 changes: 53 additions & 3 deletions lc332.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,57 @@
// Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"]. But it is larger in lexical order.
//
public class Solution {
public List<String> findItinerary(String[][] tickets) {
class City{
String name;
boolean visit;
public City(String name){
this.name = name; visit = false;
}
}
List<String> res;
Map<String, List<City>> map;
public List<String> findItinerary(String[][] tickets) {
map = new HashMap();
res = new ArrayList();
for (int i = 0; i < tickets.length; i++){
String from = tickets[i][0];
String to = tickets[i][1];
if (map.get(from) == null) map.put(from, new ArrayList<City>());
map.get(from).add(new City(to));
}
List<String> cur = new ArrayList<String>();
cur.add("JFK");
dfs(cur, tickets.length + 1);
return res;
}

}
}
private void dfs(List<String> cur, int count){
if (res.size() != 0)
return;
else if (cur.size() == count){
res.addAll(cur);
}
else {
String curCity = cur.get(cur.size()-1);
List<City> nextHop = map.get(curCity);
if (nextHop == null) return;
Collections.sort(nextHop, new Comparator<City>(){
public int compare(City c1, City c2){
return c1.name.compareTo(c2.name);
}
});
int size = nextHop.size();
for (int i = 0; i < size; i++){
City next = nextHop.get(i);
if (next.visit) continue;
if (i == 0 || !nextHop.get(i-1).name.equals(nextHop.get(i).name) || nextHop.get(i).visit){
next.visit = true;
cur.add(next.name);
dfs(cur, count);
cur.remove(cur.size()-1);
next.visit = false;
}
}
}
}
}

0 comments on commit 4b9c23e

Please sign in to comment.