Skip to content

Commit 4b9c23e

Browse files
jackqiang123Lian Lu
authored andcommitted
330v0.2
1 parent 8a621ef commit 4b9c23e

File tree

5 files changed

+197
-9
lines changed

5 files changed

+197
-9
lines changed

lc321.java

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
// 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.
1+
// Given two arrays of length m and n with digits 0-9 representing two numbers.
2+
// Create the maximum number of length k <= m + n from digits of the two.
3+
// The relative order of the digits from the same array must be preserved.
4+
// Return an array of the k digits. You should try to optimize your time and space complexity.
25
//
36
// Example 1:
47
// nums1 = [3, 4, 6, 5]
@@ -20,7 +23,88 @@
2023
//
2124

2225
public class Solution {
23-
public int[] maxNumber(int[] nums1, int[] nums2, int k) {
26+
public int[] maxNumber(int[] nums1, int[] nums2, int k){
27+
int len1 = nums1.length;
28+
int len2 = nums2.length;
29+
int res = new int[k];
30+
for (int i = Math.max(0, k-len2); i <= k && i <= len1; i++){
31+
int []candidate = merge(getNumber(nums1, nums1.length() - 1, i), getNumber(nums2, nums2.length() - 1, k - i));
32+
if (!greater(res, candidate)) res = candidate;
33+
}
34+
return res;
35+
}
2436

37+
private int[]merge(int l, int r){
38+
String left = String.valueOf(l);
39+
String right = String.valueOf(r);
40+
int res[] = new int[left.length() + right.length()];
41+
int pos = 0;
42+
int i = 0; int j = 0;
43+
while(pos < res.length){
44+
if (i < left.length() && j < right.length()){
45+
if (left.charAt(i) > right.charAt(j))
46+
res[pos++] = left.charAt(i++) - '0';
47+
else res[pos++] = right.charAt(j++) - '0';
48+
}
49+
else if (i < left.length()){
50+
res[pos++] = left.charAt(i++) -'0';
51+
}
52+
else {
53+
res[pos++] = right.charAt(j++) - '0';
54+
}
2555
}
26-
}
56+
return res;
57+
}
58+
59+
private boolean greater(int []c1, int []c2){
60+
for (int i = 0; i < c1.length; i++){
61+
if (c1[i] > c2[i]) return true;
62+
else if (c1[i] < c2[i]) return false;
63+
}
64+
return false;
65+
}
66+
67+
68+
private int getNumber(int[]nums, int end, int k){
69+
int [][]dp = new int[end+1][k+1];
70+
for (int i = 0; i <= end; i++){
71+
for (int j = 0; j < dp[0].length; j++){
72+
if (j == 0) dp[i][j] = 0;
73+
else if (i == 0) dp[i][j] = nums[i];
74+
else dp[i][j] = Math.max(dp[i-1][j], dp[i-1][j-1]*10+nums[i]);
75+
}
76+
}
77+
return dp[end][k];
78+
}
79+
80+
public int[] maxNumberUsingDP(int[] nums1, int[] nums2, int K) {
81+
int len1 = nums1.length;
82+
int len2 = nums2.length;
83+
int [][][] dp = new int[len1 + 1][len2 + 1][K+1];
84+
for (int k = 0; k <= K; k++){
85+
for (int i = 0; i <= len1; i++){
86+
for (int j = 0; j <= len2; j++){
87+
if (k == 0 || (i == 0 && j == 0) || (i+j < k)) dp[i][j][k] = 0;
88+
else if (i == 0){
89+
dp[i][j][k] = getNumber(nums2, j - 1, k);
90+
}
91+
else if (j == 0){
92+
dp[i][j][k] = getNumber(nums1, i - 1, k);
93+
}
94+
else {
95+
dp[i][j][k] = Math.max(dp[i-1][j][k], dp[i][j-1][k]);
96+
int n1 = dp[i-1][j][k-1]*10 + nums1[i-1];
97+
int n2 = dp[i][j-1][k-1]*10 + nums2[j-1];
98+
dp[i][j][k] = Math.max(dp[i][j][k], Math.max(n1,n2));
99+
}
100+
}
101+
}
102+
}
103+
int num = dp[len1][len2][K];
104+
int len = String.valueOf(num).length();
105+
int res[] = new int[len];
106+
for (int i = len - 1; i >= 0; i--)
107+
{ res[i] = num%10; num /= 10;}
108+
return res;
109+
}
110+
}

lc322.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
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.
1+
// You are given coins of different denominations and a total amount of money amount.
2+
// Write a function to compute the fewest number of coins that you need to make up that amount.
3+
// If that amount of money cannot be made up by any combination of the coins, return -1.
24
//
35
// Example 1:
46
// coins = [1, 2, 5], amount = 11
@@ -12,6 +14,16 @@
1214
// You may assume that you have an infinite number of each kind of coin.
1315
public class Solution {
1416
public int coinChange(int[] coins, int amount) {
15-
17+
int []dp = new int[amount + 1];
18+
if (amount == 0) return 0;
19+
for (int i = 1; i <= amount; i++){
20+
dp[i] = Integer.MAX_VALUE;
21+
for (int c : coins){
22+
if (i - c >= 0 && dp[i -c] < Integer.MAX_VALUE){
23+
dp[i] = Math.min(dp[i] , dp[i - c] + 1);
24+
}
25+
}
26+
}
27+
return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
1628
}
1729
}

lc323.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,34 @@
1616
// 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.
1717

1818
public class Solution {
19+
// can also be done in union find algorithm
20+
boolean visit[];
21+
Map<Integer,List<Integer>> map;
1922
public int countComponents(int n, int[][] edges) {
23+
visit[] = new int[n];
24+
map = new HashMap();
25+
for (int i = 0; i < edges.length; i++){
26+
int s = edges[i][0];
27+
int v = edges[i][1];
28+
if (map.get(s) == null) map.put(s, new ArrayList());
29+
if (map.get(v) == null) map.put(v, new ArrayList());
30+
map.get(s).add(v);
31+
map.get(v).add(s);
32+
}
33+
int res = 0;
34+
for (int i = 0; i < n; i++){
35+
if (!visit[i]){
36+
res++; dfs(i);
37+
}
38+
}
39+
return res;
40+
}
41+
private void dfs(int i){
42+
if (visit[i]) return;
43+
visit[i] = true;
44+
if (map.get(i) == null) return;
45+
for (int nb : map.get(i)){
46+
dfs(nb);
47+
}
48+
}
49+
}

lc324.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212

1313
public class Solution {
1414
public void wiggleSort(int[] nums) {
15-
15+
Arrays.sort(nums);
16+
int len = nums.length;
17+
int mid = (len-1)/2;
18+
int []temp = new int[len];
19+
for (int i = 0; i < len; i++)
20+
temp[i] = nums[i];
21+
int i = mid;
22+
int j = len - 1;
23+
int pos = 0;
24+
while(true){
25+
nums[pos++] = temp[mid--];
26+
nums[pos++] = temp[j--];
27+
}
1628
}
1729
}

lc332.java

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,57 @@
1313
// Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"]. But it is larger in lexical order.
1414
//
1515
public class Solution {
16-
public List<String> findItinerary(String[][] tickets) {
16+
class City{
17+
String name;
18+
boolean visit;
19+
public City(String name){
20+
this.name = name; visit = false;
21+
}
22+
}
23+
List<String> res;
24+
Map<String, List<City>> map;
25+
public List<String> findItinerary(String[][] tickets) {
26+
map = new HashMap();
27+
res = new ArrayList();
28+
for (int i = 0; i < tickets.length; i++){
29+
String from = tickets[i][0];
30+
String to = tickets[i][1];
31+
if (map.get(from) == null) map.put(from, new ArrayList<City>());
32+
map.get(from).add(new City(to));
33+
}
34+
List<String> cur = new ArrayList<String>();
35+
cur.add("JFK");
36+
dfs(cur, tickets.length + 1);
37+
return res;
38+
}
1739

18-
}
19-
}
40+
private void dfs(List<String> cur, int count){
41+
if (res.size() != 0)
42+
return;
43+
else if (cur.size() == count){
44+
res.addAll(cur);
45+
}
46+
else {
47+
String curCity = cur.get(cur.size()-1);
48+
List<City> nextHop = map.get(curCity);
49+
if (nextHop == null) return;
50+
Collections.sort(nextHop, new Comparator<City>(){
51+
public int compare(City c1, City c2){
52+
return c1.name.compareTo(c2.name);
53+
}
54+
});
55+
int size = nextHop.size();
56+
for (int i = 0; i < size; i++){
57+
City next = nextHop.get(i);
58+
if (next.visit) continue;
59+
if (i == 0 || !nextHop.get(i-1).name.equals(nextHop.get(i).name) || nextHop.get(i).visit){
60+
next.visit = true;
61+
cur.add(next.name);
62+
dfs(cur, count);
63+
cur.remove(cur.size()-1);
64+
next.visit = false;
65+
}
66+
}
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)