Algorithm Notes
  • Introduction
  • Search & Backtracking 搜索与回溯
    • Tree 与 BackTracking 的比较
    • Subsets, Combination 与 Permutation
    • Subsets & Combinations & Combination Sum
    • 枚举法
    • N 皇后 + 矩阵 Index Trick
    • Sudoku 数独 + 矩阵 Index Trick
    • Word Ladder I & II
    • Number of ways 类
    • DFS flood filling
    • Strobogrammatic 数生成
    • String 构造式 DFS + Backtracking
    • Word Pattern I & II
    • (G) Binary Watch
    • (FB) Phone Letter Combination
    • 常见搜索问题的迭代解法
  • String,字符串类
    • 多步翻转法
    • Substring 结构和遍历
    • Palindrome 问题
    • Palindrome Continued
    • String / LinkedList 大数运算
    • 序列化与压缩
    • 5/24 String 杂题
    • Knuth–Morris–Pratt 字符串匹配
    • Lempel–Ziv–Welch 字符串压缩算法
    • (G) Decode String
    • (G) UTF-8 Validation
  • Binary Tree,二叉树
    • 各种 Binary Tree 定义
    • LCA 类问题
    • 三序遍历,vertical order
    • Post order traversal 的应用
    • Min/Max/Balanced Depth
    • BST
    • 子树结构
    • Level Order traversal
    • Morris 遍历
    • 修改结构
    • 创建 / 序列化
    • 子树组合,BST query
    • 路径与路径和
    • NestedInteger 类
    • (FB) 从 Binary Tree Path 看如何递归转迭代
    • (FB) Binary Tree Path 比较路径大小
    • 比较好玩的 Binary Tree 概率题
  • Segment & Fenwick Tree,区间树
    • Segment Tree 基础操作
    • Segment Tree 的应用
    • Fenwick Tree (Binary Indexed Tree)
    • Range Sum Query 2D - Immutable
  • Union-Find,并查集
    • Union-Find,并查集基础
    • Union-Find, 并查集应用
  • Dynamic Programming, 动态规划
    • 6/20, 入门 House Robber
    • 7/12, Paint Fence / House
    • 6/24, 滚动数组
    • 6/24, 记忆化搜索
    • 6/24, 博弈类 DP
    • 博弈类DP, Flip Game
    • 6/25, 区间类DP
    • 6/27, subarray 划分类,股票
    • 7/2, 字符串类
    • Bomb Enemies
    • 8/2,背包问题
    • (G) Max Vacation
    • (11/4新增) AST 子树结构 DP
  • LinkedList,链表
    • 6/9, LinkedList,反转与删除
    • 6/11, LinkedList 杂题
    • (FB) 链表的递归与倒序打印
  • LinkedIn 面经,算法题
    • 6/17, LinkedIn 面经题
    • 6/28, LinkedIn 面经题
    • 7/6, LinkedIn 面经
    • Shortest Word Distance 类
    • DFA Parse Integer
  • Two Pointers,双指针
    • 3 Sum, 3 Sum Closest / Smaller, 4 Sum
    • 对撞型,灌水类
    • 对撞型,partition类
    • Wiggle Sort I & II
    • 双指针,窗口类
    • 双指针,窗口类
    • Heap,排序 matrix 中的 two pointers
  • Bit & Math,位运算与数学
    • Bit Manipulation,对于 '1' 位的操作
    • Math & Bit Manipulation, Power of X
    • 坐标系 & 数值计算类
    • Add Digits
    • 用 int 做字符串 signature
  • Interval 与 扫描线
    • Range Addition & LCS
    • 7/5, Interval 类,扫描线
  • Trie,字典树
    • 6/9, Trie, 字典树
  • 单调栈,LIS
    • 4/13 LIS
    • 栈, 单调栈
    • Largest Divisible Subset
  • Binary Search 类
    • Matrix Binary Search
    • Array Binary Search
    • Find Peak Element I & II
    • **Median of Two Sorted Arrays
  • Graph & Topological Sort,图 & 拓扑排序
    • 有向 / 无向 图的基本性质和操作
    • 拓扑排序, DFS 做法
    • 拓扑排序, BFS 做法
    • Course Schedule I & II
    • Alien Dictionary
    • Undirected Graph, BFS
    • Undirected Graph, DFS
    • 矩阵,BFS 最短距离探索
    • 欧拉回路,Hierholzer算法
    • AI, 迷宫生成
    • AI, 迷宫寻路算法
    • (G) Deep Copy 无向图成有向图
  • 括号与数学表达式的计算
  • Iterator 类
  • Majority Element,Moore's Voting
  • Matrix Inplace Operations
  • 常见数据结构设计
  • (G) Design / OOD 类算法题
  • 随机算法 & 数据结构
  • (FB) I/O Buffer
  • (FB) Simplify Path, H-Index I & II
  • (FB) Excel Sheet, Remove Duplicates
  • Integer 的构造,操作,序列化
  • Frequency 类问题
  • Missing Number 类,元素交换,数组环形跳转
  • 8/10, Google Tag
  • (FB) Rearrange String k Distance Apart
  • Abstract Algebra
    • Chap1 -- Why Abstract Algebra ?
    • Chap2 -- Operations
    • Chap3 -- The Definition of Groups
Powered by GitBook
On this page
  • Simplify Path
  • H-Index
  • 这题有官方答案
  • 从图的方式理解的话,h-index 是图中最大正方形的边长。
  • h-index 的定义是,至少有 h 篇 paper 的 citation 数都大于等于 h.
  • 对于给定 array, h-index 有且只有一个。
  • 正确的做法是从 N 到 0 递减遍历一遍,才能保证得到的 h-index 是最大的,但是我们需要在扫描过程中知道对于每一个切分点 val 来讲,到底有多少个 >= val 的 paper.
  • 换句话说,这是一种类似 “后缀和” 的计数算法,先记录所有可能的切分点;如果一个 paper 的引用次数大于 N ,则直接记录在 count[n] 上,代表 “我不管到底是什么,反正大于 N 了”,其他位置各自计算。然后从后向前扫,依次记录后缀和即可。
  • H-Index II
  • 需要注意的细节是,这里要做 left = mid + 1 ,而不是 right = mid - 1,不然在 [0] 的 testcase 上会 TLE.
  • 同时用 mid + 1 和 mid - 1 会在 [1,2] 的情况下 WA. 所以指针的推动和错位是要根据题意制定的;这题我们唯一能确定的只有两个情况:

Was this helpful?

(FB) Simplify Path, H-Index I & II

Previous(FB) I/O BufferNext(FB) Excel Sheet, Remove Duplicates

Last updated 4 years ago

Was this helpful?

没啥特别好说的~ 用双端队列比 stack 省事很多。

收尾的时候注意下,如果字符串为空的话,需要留个 "/" 作为默认情况。

public class Solution {
    public String simplifyPath(String path) {
        String[] ops = path.split("/");
        Deque<String> deque = new LinkedList<String>();

        for(String str : ops){
            if(str.equals(".") || str.equals("")) continue;
            if(str.equals("..")) deque.pollFirst();
            else deque.offerFirst(str);
        }

        StringBuilder sb = new StringBuilder();

        while(!deque.isEmpty()){
            String str = deque.pollLast();
            sb.append('/');
            sb.append(str);
        }

        return (sb.length() == 0) ? "/" : sb.toString();
    }
}

从图的方式理解的话,h-index 是图中最大正方形的边长。

h-index 的定义是,至少有 h 篇 paper 的 citation 数都大于等于 h.

对于给定 array, h-index 有且只有一个。

因此对于 n 篇 paper 的输入, h-index 的值一定在【0,n】区间。 一个需要特别考虑的例子是下面这个:

[0,0,4,4] 的 h-index 是 2.

一开始尝试了下排序然后从右往左扫,然而遇到上面那个 case 就比较麻烦,因为数组给定的 citations 之间间隔可能很大,而这种循环做法只考虑 citation 的值作为切分点是不对的。

但是排序 + 遍历依然可以做,代码如下:

public class Solution {
    public int hIndex(int[] citations) {
        if(citations == null || citations.length == 0) return 0;
        // Maxium hindex is n, number of papers
        int n = citations.length;
        Arrays.sort(citations);
        int hindex = 0;

        for(int i = n - 1; i >= 0; i--){
            if(citations[i] > hindex) hindex++;
        }

        return hindex;
    }
}

正确的做法是从 N 到 0 递减遍历一遍,才能保证得到的 h-index 是最大的,但是我们需要在扫描过程中知道对于每一个切分点 val 来讲,到底有多少个 >= val 的 paper.

换句话说,这是一种类似 “后缀和” 的计数算法,先记录所有可能的切分点;如果一个 paper 的引用次数大于 N ,则直接记录在 count[n] 上,代表 “我不管到底是什么,反正大于 N 了”,其他位置各自计算。然后从后向前扫,依次记录后缀和即可。

public class Solution {
    public int hIndex(int[] citations) {
        if(citations == null || citations.length == 0) return 0;
        // Maxium hindex is n, number of papers
        int n = citations.length;
        int[] counts = new int[n + 1];
        for(int i = 0; i < n; i++){
            if(citations[i] > n) counts[n]++;
            else counts[citations[i]]++;
        }

        int biggerCount = 0;
        for(int index = n; index >= 0; index--){
            biggerCount += counts[index];
            if(biggerCount >= index) return index;
        }

        return 0;
    }
}

试着用 left + 1 < right 的 binary search 搞这题,搞了好半天才 AC .. corner case 一大堆。。

之前用 left + 1 < right 是相对于“元素 index” 来讲的,为的是避免越界。这里面既然我们已知最终 h-index 区间 [0, N] 了,可以直接以这两点开始,以 pointer 重合结束。

需要注意的细节是,这里要做 left = mid + 1 ,而不是 right = mid - 1,不然在 [0] 的 testcase 上会 TLE.

同时用 mid + 1 和 mid - 1 会在 [1,2] 的情况下 WA. 所以指针的推动和错位是要根据题意制定的;这题我们唯一能确定的只有两个情况:

  • c[mid] == N - mid ,当前位置就是完美正方形,一定是解;

  • c[mid] < N - mid,当前位置的 bar 太低,正解要在右边找,而且一定不会是 mid 的位置,mid + 1;

public class Solution {
    public int hIndex(int[] citations) {
        if(citations == null || citations.length == 0) return 0;

        int N = citations.length;
        int left = 0;
        int right = N;

        while(left < right){
            int mid = left + (right - left) / 2;

            if(citations[mid] == N - mid){
                return N - mid;
            } else if(citations[mid] < N - mid){
                left = mid + 1;
            } else {
                right = mid;
            }
        }

        return N - left;
    }
}

这题有

Simplify Path
H-Index
官方答案
H-Index II