Upgrade to Pro — share decks privately, control downloads, hide ads and more …

競技プログラミングサイトでPythonを学ぶ.pdf

ksnt
December 25, 2019

 競技プログラミングサイトでPythonを学ぶ.pdf

ksnt

December 25, 2019
Tweet

More Decks by ksnt

Other Decks in Programming

Transcript

  1. 代表的な競技プログラな競技プログラミ競技プログラミングプログラミングサイトで (私見) • AtCoder • Topcoder • Codeforces • LeetCode

    (面接対策?) └ 過去問 └ mock (模擬試験?) └ コンテストで • HackerRank (面接対策?) • ProjectEuler (数学ぶ) • Kaggle (データサイエンス) ※黄色くマーカーを引くマーカーを引いマーカーを学ぶ引いたものはやっいたものはやったことがある ※日本人ははAtCoderを学ぶやっている人はを学ぶよくマーカーを引い見かける 参考) コーディング面接対策のために解きたい解きたいきたいLeetCode 60問 https://1kohei1.com/leetcode/
  2. どのプラットでフォームを正確にでやり早くはじめ ればいいの? • 数学ぶが趣味の人 の人は => ProjectEuler • データサイエンスが好き き

    => Kaggle • 面接対策 企業面接の過去問が解きたいきたい => LeetCode 過去問でな競技プログラミくマーカーを引いてもよい => HackerRank • 楽しいからしそう => どれでもOK!
  3. 問題が出題され、よ Given a fixed length array arr of integers, duplicate

    each occurrence of zero, shifting the remaining elements to the right. Note that elements beyond the length of the original array are not written. Do the above modifications to the input array in place, do not return anything from your function. class Solution: def duplicateZeros(self, arr: List[int]) -> None: """ Do not return anything, modify arr in-place instead. """
  4. LRUキャッシュの実装の実装 Design and implement a data structure for Least Recently

    Used (LRU) cache. It should support the following operations: get and put. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. put(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item. The cache is initialized with a positive capacity. Follow up: Could you do both operations in O(1) time complexity? class LRUCache: def __init__(self, capacity: int) def get(self, key: int) -> int:: def put(self, key: int, value: int) -> None: Example: LRUCache cache = new LRUCache( 2 /* capacity */ ); cache.put(1, 1); cache.put(2, 2); cache.get(1); // returns 1 cache.put(3, 3); // evicts key 2 cache.get(2); // returns -1 (not found) cache.put(4, 4); // evicts key 1 cache.get(1); // returns -1 (not found) cache.get(3); // returns 3 cache.get(4); // returns 4
  5. 解きたい答 import collections class LRUCache: def __init__(self, capacity: int): self.capacity

    = capacity self.cache = collections.OrderedDict() def get(self, key: int) -> int: if key in self.cache.keys(): tmp = self.cache[key] self.cache.pop(key) self.cache[key] = tmp return tmp else: return -1 def put(self, key: int, value: int) -> None: if key not in self.cache.keys(): if len(self.cache) >= self.capacity: self.cache.popitem(last=False) # last=False -> FIFO else: self.cache.pop(key) self.cache[key] = value # Your LRUCache object will be instantiated and called as such: # obj = LRUCache(capacity) # param_1 = obj.get(key) # obj.put(key,value)