114Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place.

For example,
Given

angledark0123 發表在 痞客邦 留言(1) 人氣()

764Largest Plus Sign

In a 2D grid from (0, 0) to (N-1, N-1), every cell contains a 1, except those cells in the given list mines which are 0. What is the largest axis-aligned plus sign of 1s contained in the grid? Return the order of the plus sign. If there is none, return 0.

An "axis-aligned plus sign of 1s of order k" has some center grid[x][y] = 1 along with 4 arms of length k-1 going up, down, left, and right, and made of 1s. This is demonstrated in the diagrams below. Note that there could be 0s or 1s beyond the arms of the plus sign, only the relevant area of the plus sign is checked for 1s.

Examples of Axis-Aligned Plus Signs of Order k:

Order 1:

angledark0123 發表在 痞客邦 留言(0) 人氣()

316Remove Duplicate Letters

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.

Example:

Given "bcabc"
Return "abc"

angledark0123 發表在 痞客邦 留言(0) 人氣()

140. Word Break II

Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. You may assume the dictionary does not contain duplicate words.

Return all such possible sentences.

For example, given
s = "catsanddog",

angledark0123 發表在 痞客邦 留言(0) 人氣()

題目落落長 https://codejam.withgoogle.com/codejam/contest/dashboard?c=8384486#s=p2

簡化來說就是要判斷如果是任意 字母排序,判斷某word 是否可以在中間

Input

The first line of the input gives the number of test cases, TT test cases follow. Each begins with one line containing one integer L: the length of each candidate's name. Then, there is one line with three different strings Ni of uppercase letters of the English alphabet; the i-th of these is the name of the i-th candidate.

Output

angledark0123 發表在 痞客邦 留言(0) 人氣()

785. Is Graph Bipartite?

Given a graph, return true if and only if it is bipartite.

Recall that a graph is bipartite if we can split it's set of nodes into two independent subsets A and B such that every edge in the graph has one node in A and another node in B.

The graph is given in the following form: graph[i] is a list of indexes j for which the edge between nodes i and j exists.  Each node is an integer between 0 and graph.length - 1.  There are no self edges or parallel edges: graph[i] does not contain i, and it doesn't contain any element twice.

Example 1:

angledark0123 發表在 痞客邦 留言(0) 人氣()

670Maximum Swap

Given a non-negative integer, you could swap two digits at most once to get the maximum valued number. Return the maximum valued number you could get.


Example 1:


Input: 2736
Output: 7236

angledark0123 發表在 痞客邦 留言(0) 人氣()

269Alien Dictionary

There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of non-empty words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.


Example 1:
Given the following words in dictionary,
[

angledark0123 發表在 痞客邦 留言(0) 人氣()

127Word Ladder

Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:

  1. Only one letter can be changed at a time.

  2. Each transformed word must exist in the word list. Note that beginWord is not a transformed word.

angledark0123 發表在 痞客邦 留言(0) 人氣()

261Graph Valid Tree

Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree.

For example:

Given n = 5 and edges = [[0, 1], [0, 2], [0, 3], [1, 4]], return true.

Given n = 5 and edges = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]], return false.

angledark0123 發表在 痞客邦 留言(0) 人氣()

337House Robber III

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.

Determine the maximum amount of money the thief can rob tonight without alerting the police.

Example 1:

3

angledark0123 發表在 痞客邦 留言(0) 人氣()

23Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

就像書上所說,如果結果不注重中間的排序,只在意最小或是最大,可以用heap

這邊就是可以視作先把各個listnode 塞進去,然後一個一個用 min 拉出來

因為是用到heap,time complexity是O(nlogk),space complexity是O(k)

angledark0123 發表在 痞客邦 留言(0) 人氣()

Blog Stats
⚠️

成人內容提醒

本部落格內容僅限年滿十八歲者瀏覽。
若您未滿十八歲,請立即離開。

已滿十八歲者,亦請勿將內容提供給未成年人士。