Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?
解法1:
C++
Java
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?
C++
Java
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.
Note that it is the kth smallest element in the sorted order, not the kth distinct element.
Example:
Note:
You may assume k is always valid, 1 ? k ? n2.
用heap的思想,先把第一排或者第一列放入queue中,然后操作k-1次,每一次拿出一个元素,并且把他下面一行的元素也推入queue中。最后在堆顶的就是所求的答案。
C++
Java
|
|
先确定最小值和最大值,知道了范围以后。找出中间点,然后统计有多少数小于他的值,如果总数小于k那么知道k个数一定在右半边。以此类推
O(NlogM): N是行数,M是search range = max - min
Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i, which can be called that j is on the “right” of i.
For any interval i, you need to store the minimum interval j’s index, which means that the interval j has the minimum start point to build the “right” relationship for interval i. If the interval j doesn’t exist, store -1 for the interval i. Finally, you need output the stored value of each interval as an array.
Note:
You may assume the interval’s end point is always bigger than its start point.
You may assume none of these intervals have the same start point.
Example 1:
Example 2:
Example 3:
这里遇到了一种binarysearch的用法,题目要求需要返回原始的index,而又有点像binarysearch
可以先用hashmap记录每一个当前的index,再对数组进行排序
排好序之后得到结果了再取map中找寻原始的index。
C++
Java
Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher’s h-index.
According to the definition of h-index on Wikipedia: “A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each.”
For example, given citations = [3, 0, 6, 1, 5], which means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively. Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, his h-index is 3.
Note: If there are several possible values for h, the maximum one is taken as the h-index.
要注意h的范围是在[0, N]之间。
只需要用一个array记录每一个数字出现的次数,对于数字大于等于N的记录在N
最后从后往前,累加所有的次数,当count >= i的时候说明就是我们要求的h-index
https://discuss.leetcode.com/topic/40765/java-bucket-sort-o-n-solution-with-detail-explanation/2
C++
Java
目标是找寻一个数i,使得比他引用次数多的paper的数量(包括和他一样的)要比i大就可以。统计每一个citation次数出现的次数,然后从大到小找到第一个满足条件的数字就可以了。
|
|
Given two sparse matrices A and B, return the result of AB.
You may assume that A’s column number is equal to B’s row number.
Example:
只要存下非0的数即可,然后遍历每一对非0的pair,看是否col=row,然后加和到对应的单元格里。
C++
Java
An abbreviation of a word follows the form
Assume you have a dictionary and given a word, find whether its abbreviation is unique in the dictionary. A word’s abbreviation is unique if no other word from the dictionary has the same abbreviation.
Example:
莫名其妙的一题。。
C++
Java
Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with value v at the given depth d. The root node is at depth 1.
The adding rule is: given a positive integer depth d, for each NOT null tree nodes N in depth d-1, create two tree nodes with value v as N’s left subtree root and right subtree root. And N’s original left subtree should be the left subtree of the new left subtree root, its original right subtree should be the right subtree of the new right subtree root. If depth d is 1 that means there is no depth d-1 at all, then create a tree node with value v as the new root of the whole original tree, and the original tree is the new root’s left subtree.
Example 1:
Example 2:
Note:
The given d is in range [1, maximum depth of the given tree + 1].
The given binary tree has at least one tree node.
先用bfs找到要加的层的上一层。然后对每一个node加上新的node,然后把原来的left assign给left,rightassign给right
C++
Java
Given n processes, each process has a unique PID (process id) and its PPID (parent process id).
Each process only has one parent process, but may have one or more children processes. This is just like a tree structure. Only one process has PPID that is 0, which means this process has no parent process. All the PIDs will be distinct positive integers.
We use two list of integers to represent a list of processes, where the first list contains PID for each process and the second list contains the corresponding PPID.
Now given the two lists, and a PID representing a process you want to kill, return a list of PIDs of processes that will be killed in the end. You should assume that when a process is killed, all its children processes will be killed. No order is required for the final answer.
Example 1:
Note:
The given kill id is guaranteed to be one of the given PIDs.
n >= 1.
先把问题条件转化为一个图,然后对图做一个BFS就可以了。
C++
Java
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree.
Especially, this path can be either increasing or decreasing. For example, [1,2,3,4] and [4,3,2,1] are both considered valid, but the path [1,2,4,3] is not valid. On the other hand, the path can be in the child-Parent-child order, where not necessarily be parent-child order.
Example 1:
Example 2:
Note: All the values of tree nodes are in the range of [-1e7, 1e7].
要先想想思路
一个node,返回已他为起点的最长的decreasing和increasing
那通过root的最长的一个path一定是increase + decrease - 1, 减1是去掉一次root
C++
Java
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it.
Note:
A subtree must include all of its descendants.
Here’s an example:
The Largest BST Subtree in this case is the highlighted one.
The return value is the subtree’s size, which is 3.
Follow up:
Can you figure out ways to solve it with O(n) time complexity?
C++
Java