我正在努力解决几年前我遇到的一个面试问题,为即将到来的面试做准备。问题在此处的 pdf 中进行了概述。我使用 DFS 编写了一个简单的解决方案,该解决方案适用于文档中概述的示例,但我无法使程序满足以下条件
对于包含 10,000 个已占用 Geos 的 10,000 x 10,000 GeoBlock,您的代码应在不到一秒的时间内生成正确答案。
为了测试这一点,我生成了一个包含 10000 个随机条目的 CSV 文件,当我针对它运行代码时,平均只需 2 秒多一点就可以找到其中的最大地理块。除了在更快的笔记本电脑上运行之外,我不确定可以对我的方法进行哪些改进以将运行时间减少一半以上。从我的调查来看,搜索本身似乎只需要大约 8ms,所以也许我将数据加载到内存中的方式是低效的部分?
我非常感谢有关如何改进的建议。见下面的代码:
地理块分析器
package analyzer.block.geo.main;
import analyzer.block.geo.model.Geo;
import analyzer.block.geo.result.GeoResult;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;
import java.util.*;
public class GeoBlockAnalyzer {
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private final int width;
private final int height;
private final String csvFilePath;
private GeoResult result …Run Code Online (Sandbox Code Playgroud) 我一直在尝试从react-bootstrap-typeahead库集成AsyncTypeahead功能。我正在尝试与TheMovieDB搜索api一起使用。从开发人员控制台输出中可以看到,我正在成功发出api请求,并且结果被传递回AsyncTypeahead组件,但是它的意思是“未找到匹配项”。在我的屏幕上。
我已经从他们的github复制了用于AsyncTypeahead实现的大多数代码,甚至可以看到响应结果正在传递到Typeahead组件的选项中。因此,我不确定要像在他们的示例中那样将它们显示在屏幕上的缺失。
我的搜索实现组件
import React from 'react';
import {AsyncTypeahead} from 'react-bootstrap-typeahead';
import axios from 'axios';
import SearchResultMenuItem from './SearchResultMenuItem';
//Needs to be a class based component, as we have to handle state
class SearchBar extends React.Component {
state = {
term: '',
options: [],
isLoading: false
};
onFormSubimit = (event) => { //Arrow function makes sure the value of 'this' is always the instance of the search bar
event.preventDefault(); //Stops browser from submitting form automatically and …Run Code Online (Sandbox Code Playgroud)