Java中用于时间戳而非索引的滑动窗口算法
我在考试中遇到了一个编码问题:在输入时,我有一个.csv文件,其中包含4列的交易数据行:Java中用于时间戳而非索引的滑动窗口算法,java,algorithm,Java,Algorithm,我在考试中遇到了一个编码问题:在输入时,我有一个.csv文件,其中包含4列的交易数据行: 09:30:01.034, 36.99, 100, V 09:30:11.034, 33.99, 205, V 09:30:21.034, 36.99, 300, V 09:30:21.334, 36.99, 76, V 09:30:21.534, 33.99, 100, D 09:30:25.034, 36.99, 201, V 09:30
09:30:01.034, 36.99, 100, V
09:30:11.034, 33.99, 205, V
09:30:21.034, 36.99, 300, V
09:30:21.334, 36.99, 76, V
09:30:21.534, 33.99, 100, D
09:30:25.034, 36.99, 201, V
09:30:28.034, 36.99, 202, V
09:30:51.034, 36.99, 100, D
09:31:01.554, 36.69, 120, V
09:32:51.034, 36.99, 100, D
09:33:01.034, 36.99, 99, V
09:33:05.734, 36.99, 100, D
第一列值是其事务时间戳的第二部分(hh:mm:ss.mmm);第四列表示事务的类型;第二个和第三个你可以忽略,我想;任务是开发一种算法,帮助我们分别找出在一分钟的某段时间(窗口)(非同一分钟)内发生的V交易的最大金额和D交易的最大金额
首先,我在谷歌上搜索了滑动窗口算法的问题,我发现了这个问题:(感谢Prerna Saini提供的Java版本的算法)
我当时试图实现滑动窗口技术,所以让我向您展示我的想法
公共类NormalMinuteWindow{
公共静态void main(字符串[]args){
最终整数窗口=60000;
List tradeInfo=new ArrayList();
int-bestResultV=0,bestResultD=0,localResultV,localResultD;
NormalMinuteWindow应用程序=新的NormalMinuteWindow();
app.readFile(贸易信息);
对于(int i=0;inext.getDate().getTime()){
if(next.getExchange().equals(true)){
LocalResultTV++;
}否则{
localResultD++;
}
如果(计数器
然而,据说我的算法给出了错误的结果,因为在一分钟内出现了最大数量的“D”和“V”。我这里有两个问题:
- 我的算法实际上是滑动窗口算法吗
- 我在哪里犯了一个错误,产生了错误的结果,不管它是O(m*n)算法还是O(n)算法
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class NormalMinuteWindow {
private static final int WINDOW = 60000;
private static final String PATH_TO_CSV_FILE = "/path/to/file/windowAlg.csv";
public static void main(String[] args) {
NormalMinuteWindow app = new NormalMinuteWindow();
List<TradeEntry> tradeInfo = Objects.requireNonNull(app.readFile(PATH_TO_CSV_FILE));
Window window = new Window(tradeInfo.get(0).date.getTime(), WINDOW);
//The sliding loop
// for (int x = 1; x < 2; x++) {
printAmountOfMatchesForCurrentWindow(tradeInfo, window); //show what's in the window
// window.slide(tradeInfo.get(x).date.getTime()); //change the window's position over the list
// }
}
private static final class Window {
private long currentStartOfWindow;
private long currentEndOfWindow;
private long windowSize;
Window(long currentStartOfWindow, long windowSize) {
this.windowSize = windowSize;
this.currentStartOfWindow = currentStartOfWindow;
this.currentEndOfWindow = this.currentStartOfWindow + this.windowSize;
}
void slide(long newStartOfWindow) {
this.currentStartOfWindow = newStartOfWindow;
this.currentEndOfWindow = this.currentStartOfWindow + this.windowSize;
}
}
private static final class TradeEntry {
private final Timestamp date;
private final Float price;
private final Short size;
private final Boolean exchange;
public TradeEntry(Timestamp date, Float price, Short size, Boolean exchange) {
this.date = date;
this.price = price;
this.size = size;
this.exchange = exchange;
}
}
private List<TradeEntry> readFile(String pathToFile) {
List<TradeEntry> list = new ArrayList<>();
String line = "";
String cvsSplitBy = ",";
try (BufferedReader br = new BufferedReader(new FileReader(pathToFile))) {
while ((line = br.readLine()) != null) {
String[] tradeInfo = line.split(cvsSplitBy);
boolean exch = tradeInfo[3].trim().toUpperCase().equals("V");
list.add(new TradeEntry(Timestamp.valueOf("2000-03-01 ".concat(tradeInfo[0])),
Float.parseFloat(tradeInfo[1]),
Short.parseShort(tradeInfo[2]),
exch));
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
//performance optimization method
private static int findIndexOfFirstElementInGivenWindow(List<TradeEntry> list, Window window) {
for (int j = 0; j < list.size(); j++) {
if (list.get(j).date.getTime() >= window.currentStartOfWindow) return j;
}
return 0;
}
//performance optimization method
private static int findIndexOfLastElementInGivenWindow(List<TradeEntry> list, Window window) {
for (int j = 0; j < list.size(); j++) {
if (list.get(j).date.getTime() >= window.currentEndOfWindow) return j;
}
return 0;
}
private static void printAmountOfMatchesForCurrentWindow(List<TradeEntry> list, Window window) {
int sumV = 0, sumD = 0;
for (int i = findIndexOfFirstElementInGivenWindow(list, window); i <= findIndexOfLastElementInGivenWindow(list, window); i++) {
TradeEntry next = list.get(i);
if (window.currentStartOfWindow <= next.date.getTime() && window.currentEndOfWindow >= next.date.getTime()) {
if (next.exchange) {
sumV++;
} else {
sumD++;
}
}
}
System.out.println(sumV);
System.out.println(sumD);
}
}
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
导入java.sql.Timestamp;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Objects;
公共类NormalMinuteWindow{
专用静态最终整数窗口=60000;
私有静态最终字符串路径到CSV文件=“/PATH/TO/FILE/windowAlg.CSV”;
公共静态void main(字符串[]args){
NormalMinuteWindow应用程序=新的NormalMinuteWindow();
List tradeInfo=Objects.requirennull(app.readFile(路径到CSV文件));
窗口=新窗口(tradeInfo.get(0.date.getTime(),窗口);
//滑动环
//对于(int x=1;x<2;x++){
printAmountOfMatchesForCurrentWindow(tradeInfo,window);//显示窗口中的内容
//slide(tradeInfo.get(x.date.getTime());//更改窗口在列表中的位置
// }
}
私有静态最终类窗口{
私人长时间窗口;
私人长假;
私有长窗口大小;
窗口(长当前开始窗口,长窗口大小){
this.windowSize=windowSize;
this.currentStartOfWindow=currentStartOfWindow;
this.currentEndOfWindow=this.currentStartOffWindow+this.WindowsSize;
}
空白幻灯片(长新闻起始窗口){
this.currentStartOfWindow=newstartOfWindows;
this.currentEndOfWindow=this.currentStartOffWindow+this.WindowsSize;
}
}
私有静态最终类TradeEntry{
私人最终时间戳日期;
私人最终浮动价格;
私人最终短尺寸;
私有最终布尔交换;
公共交易条目(时间戳日期、浮动价格、短码、布尔交换){
this.date=日期;
这个价格=价格;
这个。大小=大小;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class NormalMinuteWindow {
private static final int WINDOW = 60000;
private static final String PATH_TO_CSV_FILE = "/path/to/file/windowAlg.csv";
public static void main(String[] args) {
NormalMinuteWindow app = new NormalMinuteWindow();
List<TradeEntry> tradeInfo = Objects.requireNonNull(app.readFile(PATH_TO_CSV_FILE));
Window window = new Window(tradeInfo.get(0).date.getTime(), WINDOW);
//The sliding loop
// for (int x = 1; x < 2; x++) {
printAmountOfMatchesForCurrentWindow(tradeInfo, window); //show what's in the window
// window.slide(tradeInfo.get(x).date.getTime()); //change the window's position over the list
// }
}
private static final class Window {
private long currentStartOfWindow;
private long currentEndOfWindow;
private long windowSize;
Window(long currentStartOfWindow, long windowSize) {
this.windowSize = windowSize;
this.currentStartOfWindow = currentStartOfWindow;
this.currentEndOfWindow = this.currentStartOfWindow + this.windowSize;
}
void slide(long newStartOfWindow) {
this.currentStartOfWindow = newStartOfWindow;
this.currentEndOfWindow = this.currentStartOfWindow + this.windowSize;
}
}
private static final class TradeEntry {
private final Timestamp date;
private final Float price;
private final Short size;
private final Boolean exchange;
public TradeEntry(Timestamp date, Float price, Short size, Boolean exchange) {
this.date = date;
this.price = price;
this.size = size;
this.exchange = exchange;
}
}
private List<TradeEntry> readFile(String pathToFile) {
List<TradeEntry> list = new ArrayList<>();
String line = "";
String cvsSplitBy = ",";
try (BufferedReader br = new BufferedReader(new FileReader(pathToFile))) {
while ((line = br.readLine()) != null) {
String[] tradeInfo = line.split(cvsSplitBy);
boolean exch = tradeInfo[3].trim().toUpperCase().equals("V");
list.add(new TradeEntry(Timestamp.valueOf("2000-03-01 ".concat(tradeInfo[0])),
Float.parseFloat(tradeInfo[1]),
Short.parseShort(tradeInfo[2]),
exch));
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
//performance optimization method
private static int findIndexOfFirstElementInGivenWindow(List<TradeEntry> list, Window window) {
for (int j = 0; j < list.size(); j++) {
if (list.get(j).date.getTime() >= window.currentStartOfWindow) return j;
}
return 0;
}
//performance optimization method
private static int findIndexOfLastElementInGivenWindow(List<TradeEntry> list, Window window) {
for (int j = 0; j < list.size(); j++) {
if (list.get(j).date.getTime() >= window.currentEndOfWindow) return j;
}
return 0;
}
private static void printAmountOfMatchesForCurrentWindow(List<TradeEntry> list, Window window) {
int sumV = 0, sumD = 0;
for (int i = findIndexOfFirstElementInGivenWindow(list, window); i <= findIndexOfLastElementInGivenWindow(list, window); i++) {
TradeEntry next = list.get(i);
if (window.currentStartOfWindow <= next.date.getTime() && window.currentEndOfWindow >= next.date.getTime()) {
if (next.exchange) {
sumV++;
} else {
sumD++;
}
}
}
System.out.println(sumV);
System.out.println(sumD);
}
}