Skip to content

Commit 4d81902

Browse files
add SplitCoin solution
1 parent 2dcdbd2 commit 4d81902

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

easy200/FastSki.java

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package easy200;
2+
3+
import java.text.DateFormat;
4+
import java.text.ParseException;
5+
import java.text.SimpleDateFormat;
6+
import java.util.Date;
7+
8+
public class FastSki {
9+
public String getLast(String[] times) {
10+
long max = -1 << 20;
11+
12+
for (int i = 0; i < times.length; i++) {
13+
String[] time = times[i].split("-");
14+
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
15+
Date reference = null, date1 = null, date2 = null;
16+
try {
17+
reference = dateFormat.parse("00:00:00");
18+
date1 = dateFormat.parse(time[0]);
19+
date2 = dateFormat.parse(time[1]);
20+
} catch (ParseException e) {
21+
e.printStackTrace();
22+
}
23+
24+
long secondsLeft = (date1.getTime() - reference.getTime()) / 1000L;
25+
long secondsRight = (date2.getTime() - reference.getTime()) / 1000L;
26+
max = Math.max(max, secondsRight - secondsLeft);
27+
}
28+
29+
int hours = 0, minutes = 0, seconds = 0;
30+
31+
while (max >= 3600) {
32+
hours++;
33+
max -= 3600;
34+
}
35+
while (max >= 60) {
36+
minutes++;
37+
max -= 60;
38+
}
39+
while (max > 0) {
40+
seconds++;
41+
max--;
42+
}
43+
44+
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
45+
}
46+
47+
public static void main(String[] args) {
48+
FastSki f = new FastSki();
49+
System.out.println(f.getLast(new String[] { "20:03:05-22:06:31", "04:20:05-15:51:14", "08:39:33-10:31:49" }));
50+
System.out.println(f.getLast(new String[] { "13:09:14-13:10:27", "12:47:19-20:40:58", "08:47:40-14:25:34",
51+
"13:44:31-14:17:07", "18:22:38-18:27:29" }));
52+
System.out.println(f.getLast(new String[] { "14:13:08-23:40:27", "10:35:30-17:01:21", "12:30:02-23:27:19",
53+
"01:41:40-14:08:05", "11:03:53-16:56:54", "03:07:59-08:08:06", "16:48:06-23:41:52", "10:56:14-20:25:58",
54+
"10:25:54-17:43:48" }));
55+
}
56+
}

medium300/SplitCoin.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package medium300;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class SplitCoin {
7+
public int split(int[] coins) {
8+
9+
int sum = 0;
10+
for (int i = 0; i < coins.length; i++)
11+
sum += coins[i];
12+
13+
Map<Integer, Integer> dp = new HashMap<>();
14+
dp.put(0, 1);
15+
for (int j = 0; j < coins.length; j++)
16+
for (int i = sum / 2; i >= 0; i--)
17+
if (dp.containsKey(i)) {
18+
// System.out.println(i + coins[j]);
19+
dp.put(i + coins[j], 1);
20+
}
21+
22+
for (int i = sum / 2; i >= 0; i--) {
23+
if (dp.containsKey(i))
24+
return Math.abs(i - (sum - i));
25+
}
26+
27+
return 0;
28+
}
29+
30+
public static void main(String[] args) {
31+
SplitCoin s = new SplitCoin();
32+
System.out.println(s.split(new int[] { 25, 5, 15, 62, 9, 87, 3, 35, 44, 9 }));
33+
System.out.println(s.split(new int[] { 1, 10000 }));
34+
}
35+
}

0 commit comments

Comments
 (0)