マツシタのお勉強

HashMap

Insert Delete GetRandom O(1) - Duplicates allowed

問題 通常のHashTableの機能に加え、テーブル内の要素をランダムで返すメソッドを実装されているデータ構造を作る。データ構造内の要素は重複が許されており、重複した要素の数はランダムな要素を返す関数に影響する。 leetcode.com ソースコード

Insert Delete GetRandom O(1)

問題 通常のハッシュテーブルの機能に加えて、テーブル内の要素をランダムで取得するメソッドが実装されたデータ構造をデザインする問題。 全ての操作はO(1)で行う必要がある。 leetcode.com 解法 HashMapとArrayListを用いることで解くことができる。問題は…

Least Recently Used (LRU) cache を実装する

問題 LRUキャッシュとはキーとバリューをマッピングするデータ構造で、キャパティを超えたら最後に使われたキャッシュが削除されるという挙動をする。 leetcode.com 解法 HashMapと双方向連結リストを利用することで実装することができる。 リストを用いるこ…

古い携帯電話 ガラケーのキーボード

問題 ガラケーのキーボードにおいて、入力値としてキーボード上の押した数字が与えられる(連打回数は無視)。 この時に、押した数字によって得られる単語の組合せの内、辞書に乗っているものを出力する問題。辞書は予め与えられ、データ構造は自由に設定でき…

平面上のN個の点を最も多く通る線を見つける

問題 xy平面上にN個の点が与えられる。最も多くの点を通るように直線を引いた時の点の最大値を求める問題 leetcode.com 解法 HashMapを用いることでO(N2)で解くことができる。forループを2回使い2つの点を全列挙する。2つの点のを結ぶ傾きをキーとして、H…

Google Code Jam 2016 B Rank and File 解法

問題 N×Nの行列を考える。各マスには数字が振られており、どの行もどの列も昇順になっている。 N×Nの行列では行と列の数は合計で2N個だが、この問題では2×N-1個の行か列のリストが与えられる。 取り除かれた行又は列のリストを出力する問題 Dashboard - Roun…

Longest Substring Without Repeating Characters

問題 文字列sが与えられ、以下の条件での最大の部分文字列の長さを出力する。 条件は、部分文字列内には同じ文字は被ってはいけない。 leetcode.com 解法 ハッシュマップを用いて、キーに文字をバリューにその文字が出現した場所(index)を保存することでO(N…

Knowledges about Java 【Interview preparation】

Introduction This article is for my preparation of technical interview. And this focuses on programming language “Java”. final keyword The final keyword in Java has different meaning depending on whether it is applied to a variable, class …

Maximum Size Subarray Sum Equals k

Problem discuss.leetcode.com Solution This problem can be solved by using HashMap effectively. First, we store each sum of range from 0 to i (0 <= i < N). While we traverse the array, we check if the (sum - k) is stored in HashMap. If the …

Count Path Sum in Binary Tree

Problem You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only fr…

LRU Cache

Problem leetcode.com Solution This problem can be solved by Double Linked List and HashMap. Source Code

About HashTable in Java

What’s HashMap HashTable is a data structure used to implement an associative array, a structure that can map keys to values. A hash table uses a hash function to compute an index into an array. Hash table - Wikipedia Structure Of HashMap …

Sort an array of strings by anagrams

Problem Write a method to sort an array of strings so that all the anagrams are next to each other. Solution This problem asks us to group the strings in arrays such that anagrams appear next to each other. How to find the anagram The feat…

If the string is a permutation of another string

Problem Given two strings, write a method to decide if one is a permutation of the other. How to Solve In oder to count of the number that how many times each characters appeared, I use array like HashMap. In case the given strings is ASCI…

316. Remove Duplicate Letters : Past Google Coding Interview

Problem leetcode.com How to Solve This problem is solved by the following steps. find out index that each letter appears int the last position have the indexes calculating at step 1 in a array sort the arrays created at step2 4 check the s…

Mapのキーに独自キーを割り当てる D - すぬけ君の塗り絵 / Snuke's Coloring

問題 D: すぬけ君の塗り絵 / Snuke's Coloring - AtCoder Beginner Contest 045 | AtCoder 解き方 ポイントは以下の通り。 Mapを用いて3×3の四角形とその範囲に存在する黒マスの数を管理する。 1つの黒マスが影響する四角形は9個(四角形の中央のマスを基…