-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathWalmart.java
More file actions
164 lines (137 loc) · 4.75 KB
/
Copy pathWalmart.java
File metadata and controls
164 lines (137 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
/**
* Created with IntelliJ IDEA.
* User: windows 7
* Date: 12/14/14
* Time: 6:11 PM
* To change this template use File | Settings | File Templates.
*/
public class Walmart {
public static class Data {
int startTime;
int endTime;
int duration;
public Data(int start, int end) {
this.startTime = start;
this.endTime = end;
this.duration = endTime - startTime;
}
}
public static void main(String[] args) throws Exception {
/* String input = "";
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
input = bufferedReader.readLine();
String[] inputs = input.substring(1,input.length()-1).split(",");
Data[] inputData = new Data[inputs.length];
for(int x =0;x<inputs.length;x++){
String[] temp = inputs[x].split("#");
int start;
int end;
if(temp[0].endsWith("AM")){
start = Integer.parseInt(temp[0].substring(0,temp[0].length()-2));
}else{
start =12+ Integer.parseInt(temp[0].substring(0,temp[0].length()-2));
}
if(temp[1].endsWith("AM")){
end = Integer.parseInt(temp[1].substring(0, temp[1].length() - 2));
}else{
end =12+ Integer.parseInt(temp[1].substring(0, temp[1].length() - 2));
}
inputData[x] = new Data(start,end);
}*/
int length = args.length;
Data[] inputData = new Data[length];
for (int x = 0; x < length; x++) {
String[] temp = args[x].split("#");
int start;
int end;
if (temp[0].endsWith("AM")) {
start = Integer.parseInt(temp[0].substring(0, temp[0].length() - 2));
} else {
start = 12 + Integer.parseInt(temp[0].substring(0, temp[0].length() - 2));
}
if (temp[1].endsWith("AM")) {
end = Integer.parseInt(temp[1].substring(0, temp[1].length() - 2));
} else {
end = 12 + Integer.parseInt(temp[1].substring(0, temp[1].length() - 2));
}
inputData[x] = new Data(start, end);
}
/* Arrays.sort(inputData, new Comparator<Data>() {
@Override
public int compare(Data o1, Data o2) {
if (o1.endTime == o2.endTime) {
return o1.duration - o2.duration;
} else {
return o1.endTime - o2.endTime;
}
}
});*/
MergeSort mergeSort = new MergeSort(inputData);
mergeSort.mergeSort();
int endTime = -1;
int moneyEarned = 0;
for (int x = 0; x < inputData.length; x++) {
if (endTime == -1) {
endTime = inputData[x].endTime;
moneyEarned += 500;
} else {
if (endTime <= inputData[x].startTime) {
endTime = inputData[x].endTime;
moneyEarned += 500;
}
}
}
System.out.println(moneyEarned);
}
public static class MergeSort {
Data[] array;
Data[] tempArray;
int length;
public MergeSort(Data[] arr) {
this.array = arr;
this.length = arr.length;
this.tempArray = new Data[length];
}
public void mergeSort() {
mergeSortPrivate(0, length - 1);
}
private void mergeSortPrivate(int start, int end) {
if (start < end) {
int mid = (start + end) / 2;
mergeSortPrivate(start, mid);
mergeSortPrivate(mid + 1, end);
for (int x = start; x <= end; x++) {
tempArray[x] = array[x];
}
int m = start;
int n = mid + 1;
int k = start;
while (m <= mid && n <= end) {
if (tempArray[m].endTime < tempArray[n].endTime) {
array[k] = tempArray[m];
m++;
k++;
} else {
array[k] = tempArray[n];
n++;
k++;
}
}
while (m <= mid) {
array[k] = tempArray[m];
m++;
k++;
}
while (n <= end) {
array[k] = tempArray[n];
n++;
k++;
}
}
}
}
}