Takefumi Yamamura's blog

This blog is for my memorandum.

Takefumi Yamamura's b!og

This blog is for my memorandum

srm707 medium StepsConstruct

problem There is a rectangular board that is divided into n rows by m columns of cells. Each cell is either empty or it contains an obstacle. You are given the description of the board in the board. Each character in board represents one c…

srm707 easy Cross

problem There is a rectangular board that is divided into n rows by m columns of cells. Each cell is either black or white. You are given the description of the board in the board. Each character in board represents one cell. More precisel…

Delete Middle Node

problem Implment an algorithm to delete a node in the middle of a singly linked list. But you can only access to that node. note The solution is simply to copy the data from the next node over to the current node. code

RemoveDups

problem Write code to remove duplicates from an unsorted linked list without using a temporary buffer. how to solve If I can use a temporary buffer, I recommend to use hash table. But in this case, I have to compare two elements to check t…

Insertion

problem You are given two 32bit numbers n and m and two bit positions, i and j. Write a method to insert M into N such that M starts at bit j and ends at bit i. how to solve Clear the bits j through i in N Shift M so that it lines up with …

AnimalShelter

Problem An animal shelter, which holds only dogs and cats. Create the data structures to implement operations such as enqueue, dequeueAny, dequeueDog and dequeueCat. How to solve My approach is to simply use separate queues for dogs and ca…

Rotate Matrix

problem Given an image represented by an N*N matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place? how to solve We swap on each layer, starting from the outermost laye…

OneAway

Problem There are three types of edits that can be performed on strings: insert a character, remove a character, or replace a character. Given two strings, write a function to check if they are one edit (or zero edits) away. How to solve I…

SortedMerge

problem You are given two sorted arrays, A and B, where A has a large enough buffer at the end to hold B. Write a method B into A in sorted order. how to solve In this problem, I can use array A which has a large enough buffer at the end t…

Queue via Stacks

problem Implement a MyQueue class which implements a queue using two stacks how to solve I implement two functions pop and push. The first one is getting and removing the oldest element in MyQueue. The second one is adding the element into…

Linear Regression Models with Logarithmic Transformations

Abstract 一言でいうと、線形回帰モデルを扱う際に、特徴量の尺度をそろえる方法として、 logを取ってあげるとうまくいくらしい。 http://www.kenbenoit.net/courses/ME104/logmodels2.pdf stats.stackexchange.com そうなる理由は↑を詳しくは参照 もとはと…

srm705 SuperUserDo

Problem Statement Fox Ciel just used the command "sudo" (super-user do) to gain administrative privileges in the UNIX-based operating system on her computer. She now wants to install several new programs. Each program has some dependencies…

276. Paint Fence (Google, LeetCode)

Problem There is a fence with n posts, each post can be painted with one of the k colors. You have to paint all the posts such that no more than two adjacent fence posts have the same color. Return the total number of ways you can paint th…

57. Insert Interval(Google, LeetCode)

# Problem Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1:Given intervals [1,3],[6,9…

317. Shortest Distance from All Buildings(Google, LeetCode)

problem You want to build a house on an empty land which reaches all buildings in the shortest amount of distance. You can only move up, down, left and right. You are given a 2D grid of values 0, 1 or 2, where: Each 0 marks an empty land w…

BFS 320. Generalized Abbreviation (Google, LeetCode)

Problem Write a function to generate the generalized abbreviations of a word. Example: Given word = "word", return the following list (order does not matter): ["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w…

302. Smallest Rectangle Enclosing Black Pixels

problem An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally and vertically. Given the location (…

200. Number of Islands(Leet Code, Google)

Problem Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are…

316. Remove Duplicate Letters (Leet Code, Google)

problem Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Exam…

340. Longest Substring with At Most K Distinct Characters (Leet Code, Google)

https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ problems Given a string, find the length of the longest substring T that contains at most k distinct characters. For example, Given s = “eceba” and k = 2, …

AtCoder Regular Contest 023 B - 謎の人物X

問題 arc023.contest.atcoder.jp note dpを使用した。i歩進むときの最大のたこ焼きの値段をdp[i]とした。 code

AtCoder Regular Contest 044 B - 最短路問題

問題 B: 最短路問題 - AtCoder Regular Contest 044 | AtCoder note 数学の問題 最短距離がnの場所の場合の数は、最短経路がn-1である場所の数と、最短距離がnの場所の数で決まることを利用して貪欲にとく。 code

累積和とってソートしてBluteForce AtCoder Regular Contest 035 B - アットコーダー王国のコンテスト事情

問題 arc035.contest.atcoder.jp note ペナルティの小さい順にソートして小さい順にペナルティをうけていけばよい。 コード

いもす法 累積和 AtCoder Regular Contest 048 B - AtCoderでじゃんけんを

Problem arc048.contest.atcoder.jp note あらかじめあるレーティングの人が何人いるのかの累積和と、あるレーティングである手をとるプレイヤーが何人いるかを記録しておく。 そうすると、それぞれの勝敗が次のようにO(1)でもとまる。 int win = dp[players…

2通りの全列挙 AtCoder Regular Contest 049 B - 高橋ノルム君

Problem arc049.contest.atcoder.jp Note 想定解のやり方と違うみたい。 nこの頂点から最も遠い2組の点が最小になるようにすればよいわけで、ある2点に対して、その2点間の間にあってそれぞれの点と距離が等しくなる任意の点との距離は、 (a.c * b.c) / (a…

div2 srm 704 hard ModEquationEasy

Problem Statement You are given the s n, K, and v. Consider the following modular equation with n variables: (x[0] * x[1] * x[2] * ... * x[n-1]) mod K = v. How many solutions does it have? Formally, we want to find the number of sequences …

srm704 middium ConnectedComponentConstruction

Problem Statement Any undirected graph can be decomposed into connected components. Two vertices u and v belong to the same connected component if we can travel from u to v by following a sequence of zero or more consecutive edges. The siz…

segment Treeを使用してRMQをとく。

RMQとは 数列a_0,a_1,...a_nがあるとき次の二つの処理をO(log n)で行う。 s, tが与えられたとき、a_s, a_s+1, .... , a_tの最小値を求める i, xが与えられたとき、a_iの値をxに変更する SegmentTreeとは プログラミングコンテストでのデータ構造 from Takuya…

いもす法を二回する AtCoder Regular Contest 045 B - ドキドキデート大作戦高橋君

問題 arc045.contest.atcoder.jp note まず累積和を使って、担当が1人しかいない掃除場所を探し出す。 その後、担当が1人しかいない場所を1、それよりも多い掃除場所を0として累積和をとればO(1)で任意の掃除の区間をサボれるかわかる code

ハッシュテーブルをbitで管理 AtCoder Regular Contest 053 B - 回文分割

問題 arc053.contest.atcoder.jp note 回文になる文字列は出現回数が奇数の文字が1回以下の場合。 したがってアルファベット26文字が偶数か奇数かを管理すればよい。