[Algorithm🧬] キャッシュ
質問する / 答えを出す。py , 答えを出す。swift
コードはほぼ似ていて、リンクとしてのみ添付されています.特に採点に時間がかかるという問題です.swiftが遅いからかも…?
python
def solution(cacheSize, cities):
time = 0
cache = []
for city in cities:
city = city.lower()
# 처음에 이 경우를 생각을 안했다.
# caccheSize 가 0 이면 miss 만 일어남.
if cacheSize == 0:
time += 5
# 아니라면
else:
# hit
if city in cache:
# LRU 이기 때문에 그냥 삭제했다가 다시 넣었음.
cache.remove(city)
cache.append(city)
time += 1
# miss
else:
if len(cache) == cacheSize:
cache.pop(0)
cache.append(city)
time += 5
return time
+講義JavaScript
function solution(cacheSize, cities) {
let time = 0;
let cache = [];
for (let i = 0; i < cities.length; i++) {
let city = cities[i].toLowerCase();
let index = cache.indexOf(city);
if (index !== -1) {
// hit
cache.splice(index, 1);
cache.push(city);
time += 1;
} else {
// miss
time += 5;
cache.push(city);
if (cacheSize < cache.length) {
cache.shift();
}
}
}
return time;
}
0127 swift
コードはほぼ似ていて、リンクとしてのみ添付されています.特に採点に時間がかかるという問題です.swiftが遅いからかも…?
Reference
この問題について([Algorithm🧬] キャッシュ), 我々は、より多くの情報をここで見つけました https://velog.io/@ddosang/Algorithm-캐시テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol