Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions C++/Data-Structures/LinkedList/LRU_cache
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// This can be optimized by storing a reference (iterator) to each key in a hash map.

#include <bits/stdc++.h>
using namespace std;

class LRUCache {
// store keys of cache
list<int> dq;

// store references of key in cache
unordered_map<int, list<int>::iterator> ma;
int csize; // maximum capacity of cache

public:
LRUCache(int);
void refer(int);
void display();
};

// Declare the size
LRUCache::LRUCache(int n)
{
csize = n;
}

// Refers key x with in the LRU cache
void LRUCache::refer(int x)
{
// not present in cache
if (ma.find(x) == ma.end()) {
// cache is full
if (dq.size() == csize) {
// delete least recently used element
int last = dq.back();

// Pops the last elmeent
dq.pop_back();

// Erase the last
ma.erase(last);
}
}

// present in cache
else
dq.erase(ma[x]);

// update reference
dq.push_front(x);
ma[x] = dq.begin();
}

// Function to display contents of cache
void LRUCache::display()
{

// Iterate in the deque and print
// all the elements in it
for (auto it = dq.begin(); it != dq.end();
it++)
cout << (*it) << " ";

cout << endl;
}

// Driver Code
int main()
{
LRUCache ca(4);

ca.refer(1);
ca.refer(2);
ca.refer(3);
ca.refer(1);
ca.refer(4);
ca.refer(5);
ca.display();

return 0;
}
79 changes: 79 additions & 0 deletions CONTRIBUTING_JAP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#貢献

このプロジェクトに貢献するためには、次のとおりです。

- バグを報告する
- コードの現在の状態について議論する
- 修正提出する
- 新機能の提案する
- 管理者になる


##貢献のためのコース

*あなたが作業する問題について言及します。他の人に作業する案件が割り当てられていないことを確認するようにします。

*もしアルゴリズムが足りないと思われる場合は、問題を作成します。

### Pull Request作成

> - PRを作ろうとする課題に割り当てられていることを確認します。
> - もし割り当てられる前にPRを作成する場合には、 `invalid(無効)`で表示され、マージされずに終了します。

*ストアをフォークして、あなたのコンピュータにコピーします。
*コピーされたストレージのメインbranchにupstream linkをよりしてください。
`` `
git remote add upstream https://github.com/div-bargali/Data-Structures-and-Algorithms.git
`` `
* Upstreamから取得Cloneストアを最新の状態に維持します。 (この操作は、新しい変更をコミット中にマージの競合を防ぐこともできます)。
`` `
git pull upstream main https://github.com/div-bargali/Data-Structures-and-Algorithms.git
`` `
*あなたのfeature branchを作りなさい。
`` `
git checkout -b <feature-name>
`` `
*すべての変更にコミットしてください。
`` `
git commit -m "Meaningful commit message"
`` `
*レビューのために変更をpushしてください。
`` `
git push origin <branch-name>
`` `

* GitHubのプロジェクトのリポジトリにPRを作成します。

###その他の注意事項

*コードは、読みやすさのために適切に作成する必要があります。
*もしテストが必要なコードを追加する場合は、コメントにtestsを追加できます。
* Pythonの場合は、テストを提供するためにdocstringsを使用します。
*あなたのコードが正しくフォーマットされていることを確認してください。
*適切なディレクトリ構造を維持していることを確認してください:
`` `
<Language> / <Algorithms> / <Algorithm Paradigm> / <。 。 >
`` `
`` `
<Language> / <Data Structures> / <Data Structure name> / <。 。 >
`` `
*このPull requestを問題化してください!

##問題提起/バグを報告する

問題を作成するときに、すでに提示されたことがないことを確認します。さらに変更について適切な説明を提供しています。
もしいくつかのコードの改善事項について提示する場合には、その改善点の詳細を提示(提供)してください。

**大きい問題製の**は、以下の事項を持っている必要があります:

- 変更の迅速な要約
- 再処理の過程で生ずるもバグについて
- 具体的これ!
- 可能な場合、サンプルコードを提供する。
- 予想日(問題)
- 実際に発生した日(問題)
- メモ(おそらくあなたがこれが起こるだろうと思ったことを含むか、試みたが、動作していないこと)

##参考

この文書は、[フェイスブックのDraft(ドラフト)](https://github.com/facebook/draft-js/blob/a9316a723f9e918afde44dea68b5f9f39b7d9b00/CONTRIBUTING.md)のオープンソースの貢献ガイドラインから適用された。
27 changes: 27 additions & 0 deletions merge-sort.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- # Binary Search in Haskell
-- This is an implementation of simple Binary Search in Haskell on any type that can be ordered.
-- Binary Search is an effective searching algorithm, that can be used to find an element in a sorted list in O(lg(n)) time.

-- Binary search with index carry
bsWithIndex :: (Ord a) => [a] -> a -> Int -> Maybe Int
bsWithIndex list n i
| n == head list = Just i
| len == 1 = Nothing -- only candidate in list is not the right elem
| n < head ys = bsWithIndex xs n i
| otherwise = bsWithIndex ys n (i + half)
where
len = length list
half = len `div` 2
(xs, ys) = splitAt half list

-- Binary search returning index, or -1 if element not in list
bs :: (Ord a) => [a] -> a -> Int
bs list n = case bsWithIndex list n 0 of
Just x -> x
Nothing -> -1

main :: IO ()
main = do
let intList = [1,4,7,10,25,30]
print $ bs intList 29 -- 29 -> -1 as not in list
print $ bs intList 7 -- 7 -> 2 as in list