涓€銆侀棶棰樿儗鏅笌闇€姹傚垎鏋?/h2>
鍦ㄧ幇浠f櫤鎱у煄甯傚缓璁句腑锛?strong>瀹炴椂鍏叡浜ら€氳拷韪郴缁?/strong>鏄彁鍗囧競姘戝嚭琛屼綋楠岀殑鍏抽敭鍩虹璁炬柦銆傛湰鏂囧皢鎺㈣濡備綍璁捐涓€涓珮鏁堢殑浜ら€氳拷韪ā鍧楋紝鏀寔浠ヤ笅鏍稿績鍔熻兘锛?/p>
- 绾胯矾绔欑偣绠$悊锛氱淮鎶ゆ湁搴忕殑绔欑偣搴忓垪锛屾敮鎸佸崟鍚戣椹剁殑鍏氦绾胯矾
- 杞﹁締浣嶇疆鏌ヨ锛氭牴鎹溅杈嗙紪鍙峰揩閫熻幏鍙栧綋鍓嶆墍鍦ㄧ珯鐐?/li>
- 鏈€杩戣溅杈嗚绠?/strong>锛氳绠椾换鎰忕珯鐐硅窛绂绘渶杩戞潵杞﹁繕鏈夊嚑绔?/li>
浜屻€佹牳蹇冩暟鎹粨鏋勮璁?/h2>
涓轰簡瀹炵幇O(1)鏃堕棿澶嶆潅搴︾殑鏌ヨ鏁堢巼锛屾垜浠渶瑕佸阀濡欏湴閫夋嫨鏁版嵁缁撴瀯锛?/p>
2.1 绔欑偣绱㈠紩鏄犲皠
浣跨敤HashMap寤虹珛绔欑偣鍚嶇О鍒颁綅缃储寮曠殑鍙屽悜鏄犲皠锛岃繖鏍峰彲浠ュ湪甯告暟鏃堕棿鍐呭畬鎴愮珯鐐瑰畾浣嶏細
class TransitTracker {
private stationIndex: Map<string, number>; // 绔欑偣鍚?-> 浣嶇疆绱㈠紩
private stationList: string[]; // 绱㈠紩 -> 绔欑偣鍚?
private vehiclePositions: Map<number, string>; // 杞﹁締ID -> 褰撳墠绔欑偣
private nearestDistance: number[]; // 棰勫鐞嗭細姣忕珯鍒版渶杩戣溅杈嗙殑璺濈
constructor(routeString: string, vehicleLocations: Record<number, string>) {
// 瑙f瀽绾胯矾瀛楃涓诧紝寤虹珛绱㈠紩
this.stationList = routeString.split('-');
this.stationIndex = new Map();
this.stationList.forEach((station, idx) => {
this.stationIndex.set(station, idx);
});
// 瀛樺偍杞﹁締浣嶇疆
this.vehiclePositions = new Map(Object.entries(vehicleLocations)
.map(([id, station]) => [parseInt(id), station]));
// 棰勫鐞嗘渶杩戣溅杈嗚窛绂?
this.preprocessNearestVehicle();
}
}
2.2 璺濈棰勫鐞嗙畻娉?/h3>
鍏抽敭浼樺寲鍦ㄤ簬浠庡彸鍚戝乏鎵弿锛岄鍏堣绠楁瘡涓珯鐐瑰埌鏈€杩戞潵杞︾殑璺濈锛?/p>
private preprocessNearestVehicle(): void {
const n = this.stationList.length;
this.nearestDistance = new Array(n).fill(-1);
// 鏀堕泦鎵€鏈夎溅杈嗘墍鍦ㄧ珯鐐圭殑绱㈠紩
const vehicleIndices: number[] = [];
for (const station of this.vehiclePositions.values()) {
const idx = this.stationIndex.get(station);
if (idx !== undefined) {
vehicleIndices.push(idx);
}
}
// 浠庡彸鍚戝乏鎵弿锛岀淮鎶ゆ渶杩戣溅杈嗚窛绂?
let minDistFromRight = Infinity;
for (let i = n - 1; i >= 0; i--) {
// 妫€鏌ュ綋鍓嶇珯鐐规槸鍚︽湁杞﹁締
if (vehicleIndices.includes(i)) {
minDistFromRight = 0;
}
if (minDistFromRight !== Infinity) {
this.nearestDistance[i] = minDistFromRight;
}
minDistFromRight++;
}
}
馃挕 浼樺寲鎻愮ず锛?/strong>棰勫鐞嗗悗锛?code>nearestToStation()鏌ヨ鐨勬椂闂村鏉傚害闄嶄负O(1)锛岄潪甯搁€傚悎楂橀鏌ヨ鍦烘櫙銆?
涓夈€丄PI鎺ュ彛瀹炵幇
3.1 鏌ヨ鏈€杩戣溅杈嗚窛绂?/h3>
getNearestVehicleDistance(stationName: string): number {
const idx = this.stationIndex.get(stationName);
if (idx === undefined) return -1;
return this.nearestDistance[idx];
}
// 绀轰緥璋冪敤
// 绾胯矾: "涓灭珯-涓績骞垮満-绉戞妧鍥?澶у鍩?缁堢偣绔?
// 杞﹁締浣嶇疆: {101: "涓績骞垮満", 102: "澶у鍩?}
tracker.getNearestVehicleDistance("涓灭珯"); // -1 (鍓嶆柟鏃犺溅)
tracker.getNearestVehicleDistance("涓績骞垮満"); // 0 (褰撳墠绔欐湁杞?
tracker.getNearestVehicleDistance("绉戞妧鍥?); // 1 (璺濇渶杩戣溅1绔?
3.2 鏌ヨ杞﹁締褰撳墠浣嶇疆
getVehicleLocation(vehicleId: number): string {
return this.vehiclePositions.get(vehicleId) || "";
}
鍥涖€佸鏉傚害鍒嗘瀽
getNearestVehicleDistance(stationName: string): number {
const idx = this.stationIndex.get(stationName);
if (idx === undefined) return -1;
return this.nearestDistance[idx];
}
// 绀轰緥璋冪敤
// 绾胯矾: "涓灭珯-涓績骞垮満-绉戞妧鍥?澶у鍩?缁堢偣绔?
// 杞﹁締浣嶇疆: {101: "涓績骞垮満", 102: "澶у鍩?}
tracker.getNearestVehicleDistance("涓灭珯"); // -1 (鍓嶆柟鏃犺溅)
tracker.getNearestVehicleDistance("涓績骞垮満"); // 0 (褰撳墠绔欐湁杞?
tracker.getNearestVehicleDistance("绉戞妧鍥?); // 1 (璺濇渶杩戣溅1绔?
getVehicleLocation(vehicleId: number): string {
return this.vehiclePositions.get(vehicleId) || "";
}| 鎿嶄綔 | 鏃堕棿澶嶆潅搴?/th> | 绌洪棿澶嶆潅搴?/th> |
|---|---|---|
| 鍒濆鍖?/td> | O(n + m) | O(n + m) |
| 鏌ヨ鏈€杩戣溅杈?/td> | O(1) | O(1) |
| 鏌ヨ杞﹁締浣嶇疆 | O(1) | O(1) |
鍏朵腑 n 涓虹珯鐐规暟閲忥紝m 涓鸿溅杈嗘暟閲?/em>
浜斻€佹墿灞曟€濊€?/h2>
鈿狅笍 闈㈣瘯杩涢樁闂锛?/strong>
- 濡備綍鏀寔杞﹁締浣嶇疆鐨勫姩鎬佹洿鏂帮紵锛堝閲忔洿鏂伴澶勭悊鏁扮粍锛?/li>
- 濡備綍鎵╁睍鍒板弻鍚戣椹剁殑绾胯矾锛燂紙缁存姢涓や釜鏂瑰悜鐨勮窛绂绘暟缁勶級
- 濡備綍澶勭悊鐜舰绾胯矾锛燂紙鍙栨ā杩愮畻澶勭悊寰幆锛?/li>
鍏€佹€荤粨
- 濡備綍鏀寔杞﹁締浣嶇疆鐨勫姩鎬佹洿鏂帮紵锛堝閲忔洿鏂伴澶勭悊鏁扮粍锛?/li>
- 濡備綍鎵╁睍鍒板弻鍚戣椹剁殑绾胯矾锛燂紙缁存姢涓や釜鏂瑰悜鐨勮窛绂绘暟缁勶級
- 濡備綍澶勭悊鐜舰绾胯矾锛燂紙鍙栨ā杩愮畻澶勭悊寰幆锛?/li>
鏈枃浠嬬粛鐨?strong>瀹炴椂浜ら€氳拷韪郴缁?/strong>璁捐鏂规锛岄€氳繃鍝堝笇鏄犲皠鍜岄澶勭悊鎶€鏈紝瀹炵幇浜嗛珮鏁堢殑杞﹁締瀹氫綅鍜岃窛绂绘煡璇㈠姛鑳姐€傛牳蹇冭鐐瑰寘鎷細
- 浣跨敤鍙屽悜鏄犲皠蹇€熷畾浣嶇珯鐐?/li>
- 浠庡彸鍚戝乏鎵弿棰勫鐞嗘渶杩戣窛绂?/li>
- 绌洪棿鎹㈡椂闂达紝瀹炵幇O(1)鏌ヨ