オットセイの経営日誌

データサイエンス系ベンチャーを経営してます。経営のこと、趣味のことつぶやきます。

LeetCode

LeetCode / Palindrome Linked List

最近、ニュースを見る度に過剰反応が報道されていていらついていたのでテレビを全く見なくなり、Youtube視聴に退避していたのですが、 ついに好きな旅行系Youtuberの方が投稿を休止される事態となり、残念、うんざりな思いが蓄積されていっている今日この頃…

LeetCode / Power of Two

https://leetcode.com/problems/power-of-two/ Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: true Explanation: 20 = 1 Example 2: Input: 16 Output: true Explanation: 24 = 16 Example 3: Inp…

LeetCode / Invert Binary Tree

初年度のベンチャーはどこもそうなんだと思いますが、特に平日は心身ともに擦り減るので、ベンチャーに休日はないと言えど強制的に休日を作らないとなかなかやっていけません。 と言っても、ただボーッとしてると仕事のことを考えてしまうので、LeetCodeでも…

LeetCode / Implement Stack using Queues, Implement Queue using Stacks

重要なデータ構造の1つにStack(スタック)とQueue(キュー)がありますが、これを実装しろという問題を取り上げます。 と言っても、こちらの解説記事にあるように、スタックもキューも配列で表現することができ、データ構造としては特別なものではありませ…

LeetCode / Contains Duplicate, Contains Duplicate Ⅱ

明日から海外旅行で浮き足立っていますが、リストから重複判定する問題シリーズを解きます。 https://leetcode.com/problems/contains-duplicate/ Given an array of integers, find if the array contains any duplicates. Your function should return tru…

LeetCode / Reversed Linked List

https://leetcode.com/problems/reverse-linked-list/ Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL 1つのLinked Listをsortして逆順にする問題。 解法1 Iterativeな解法。 # Definition for singly-lin…

LeetCode / Isomorphic Strings

https://leetcode.com/problems/isomorphic-strings/ Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with…

LeetCode / Count Primes

https://leetcode.com/problems/count-primes/ Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7. 与えられた値より小さい…

LeetCode / Remove Linked List Elements

https://leetcode.com/problems/remove-linked-list-elements/ Remove all elements from a linked list of integers that have value val. Example: Input: 1->2->6->3->4->5->6, val = 6 Output: 1->2->3->4->5 これがlistだったりsetだったら問題にもなら…

LeetCode / Happy Number

https://leetcode.com/problems/happy-number/ Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squar…

LeetCode / House Robber

プログラミングの技術ではなく、高校(中学?)数学の知識を頭の片隅から引っ張り出す必要がある問題。 https://leetcode.com/problems/house-robber/ You are a professional robber planning to rob houses along a street. Each house has a certain amou…

LeetCode / Find the Difference

シンプルな問題ですが、dictionary使ったり、Counter使ったり、アスキーコード使ったり、ビット演算使ったりと、解法が多岐にわたる良問かも。 https://leetcode.com/problems/find-the-difference/ Given two strings s and t which consist of only lowerc…

LeetCode / First Unique Character in a String

今日は、いくら計算量を頑張って削減してもPythonの超えられない壁があるよ、ということを身を以て伝えてくれる問題です。 https://leetcode.com/problems/first-unique-character-in-a-string/ Given a string, find the first non-repeating character in …

LeetCode / Ransom Note

リハビリその2。 https://leetcode.com/problems/ransom-note/ Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed fro…

LeetCode / Guess Number Higher or Lower

今年も残すところ後1週間程度ですね。 ここ数ヶ月は怒涛のように過ぎ去り、ちょっと自分の人としての器を超えた容量が押し寄せている感じで、リハビリの必要性を感じてます。 ということで、リハビリがてらプレーンな二分探索問題を解きます。 https://leetc…

LeetCode / Number of 1 Bits

ビット表現祭りその2。 https://leetcode.com/problems/number-of-1-bits/ Write a function that takes an unsigned integer and return the number of '1' bits it has (also known as the Hamming weight). Example 1: Input: 00000000000000000000000000…

LeetCode / Reverse Bits

ビット表現祭り。 https://leetcode.com/problems/reverse-bits/ Reverse bits of a given 32 bits unsigned integer. Example 1: Input: 00000010100101000001111010011100 Output: 00111001011110000010100101000000 Explanation: The input binary string…

LeetCode / Rotate Array

https://leetcode.com/problems/rotate-array/ Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: [1,2,3,4,5,6,7] and k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 steps to the right: [7…

LeetCode / Factorial Trailing Zeroes

今日は算数の問題です。大人より小学生の方が解けるかも? https://leetcode.com/problems/factorial-trailing-zeroes/ Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explanation: 3! = 6, no trailing …

LeetCode / Excel Sheet Column Number

https://leetcode.com/problems/excel-sheet-column-number/ Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ... 先日の記事はFrom列インデ…

過半数判定アルゴリズム(LeetCode / Majority Elementより)

過半数を獲得した候補者を効率的に特定するにはどんな方法が良いと思いますか? 今日の問題は、そう読み替えてみるとなかなか奥が深いです。 https://leetcode.com/problems/majority-element/ Given an array of size n, find the majority element. The ma…

LeetCode / Excel Sheet Column Title

https://leetcode.com/problems/excel-sheet-column-title/ Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB ... エクセルシートの列名を…

LeetCode / Intersection of Two Linked Lists

https://leetcode.com/problems/intersection-of-two-linked-lists/ Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: begin to intersect at node c1. Notes…

LeetCode / Min Stack

https://leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get…

LeetCode / Linked List Cycle II

https://leetcode.com/problems/linked-list-cycle-ii Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To represent a cycle in the given linked list, we use an integer pos which represents the po…

LeetCode / Linked List Cycle

Kaggle繋がりでTwitterでばったりフォローした人がばったり旧知の囲碁友だった件。 ちょうど今日の問題のLinked Listのようにぐるりと回って再会しました(無理矢理 https://leetcode.com/problems/linked-list-cycle/ Given a linked list, determine if it…

LeetCode / Single Number

昨夜実家の富山から帰ってきました。 台風さんがフェーン現象を巻き起こしたせいで、北陸は地獄のような気候になってます。 私が兼ねてから主張している国民総打ち水法の制定を真摯に考えるべきかと思います! ja.wikipedia.org さて、今日の問題。 https://…

LeetCode / Valid Palindrome

終日頭が重い@mhiro216です。 気象病ってやつなんだろうか。全部台風のせいだ。 www.d-yutaka.co.jp さて、軽い問題で頭をほぐします。 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome, considering onl…

LeetCode / Best Time to Buy and Sell Stock II @柏の葉コワーキングスペースKOIL

今日は柏の葉のコワーキングスペースに来てみました。 www.31ventures.jp 日本最大級らしいので、ここまでのクオリティが他でもあるわけではないと思いますが、 1日(9時-23時まで)で1500円 ということで、圧倒的なコスパに驚愕しております。 柏の葉というこ…

LeetCode / Best Time to Buy and Sell Stock

東京都内在住の私ですが、ひとり開発合宿なるものをやってみようと思い、とある県のホテルに泊まって、部屋から出ず黙々とコードの写経をしています。 しかし、ひとり開発合宿は、自分に対する甘えを断ち切れる精神の持ち主でないと、あまり向いていないです…