摘要:
Solution 1: Math Let f(n,k) denote the position of the survivor. After the kth person is killed, we're left with a circle of n-1, and we start the nex 阅读全文
摘要:
Solution: Use Stack, O(N) space my another solution: don't create new node, modify the existed node Solution 3: reverse the inputs, add one, then reve 阅读全文
摘要:
参考:https://discuss.leetcode.com/topic/48424/java-o-n-incredibly-short-yet-easy-to-understand-ac-solution the problem seems to have many cases a>0, a=0 阅读全文
摘要:
Analysis: Solution 1: The greedy algorithm is that in each step, select the char with highest remaining count if possible (if it is not in the waiting 阅读全文
摘要:
本题精妙之处在于:1. 如何最快找到possible的line的x axis(我最开始想到要用quickselect find median的方法,结果别人有min max方法) 2. 如何最方便确定一个点关于该line的reflection是否存在,由于既有x又有y,不太好处理,别人有个聪明的办法 阅读全文
摘要:
HashMap 阅读全文
摘要:
HashSet + Queue: 吃东西的时候保留尾巴,不吃的时候删去尾巴 one case to notice蛇转弯的时候,要先删去尾巴,再把新的点加进去。如果这个顺序不对的话,本来不会撞上尾巴的结果会判断会撞上 如果没有39行,程序会说dir没有initialize, 其实更好的写法应该是 阅读全文
摘要:
我自己的backtracking做法 最开始把cur设置为一个dummy value 0 最好的DFS with Optimization beat 97%: https://discuss.leetcode.com/topic/46260/java-dfs-solution-with-clear- 阅读全文
摘要:
我的做法:维护一个window,r移动到超出k distinct character限制是更新max,然后移动l使distinct character <=k; 这种做法更新max只发生在超出限制的时候,有可能永远都没有超出限制,所以while loop完了之后要补上一个max的更新 别人非常棒的做 阅读全文
摘要:
Hint: 看了hint之后想到:既然用数组的话,一行一列都是一个element来代表,估计这个element是要用sum了,那么,能不能用sum来代表一行,使它有且只有一种可能,全部是player1完成的/全部是Player2;所以想到了是Player1就+1,是player2就-1,看最后sum 阅读全文
摘要:
Queue method: Array method: 非常聪明 阅读全文
摘要:
Inspired by: https://discuss.leetcode.com/topic/49041/no-depth-variable-no-multiplication Instead of multiplying by depth, add integers multiple times 阅读全文
摘要:
DFS: 阅读全文
摘要:
refer to https://discuss.leetcode.com/topic/36995/share-my-o-n-java-code-with-brief-explanation-and-comments/2 这道题不好从root到leaf一层一层限制subtree取值范围,因为有可能p 阅读全文
摘要:
这道题应该是hard,首先思想上面参考了https://discuss.leetcode.com/topic/32861/3-lines-python-with-explanation-proof I put the smaller half of the numbers on the even i 阅读全文
摘要:
Bucket Sort + HashMap HashMap+ Heap+Wrapper Class 阅读全文
摘要:
recursively find the node that needs to be deleted Once the node is found, have to handle the below 4 cases node doesn't have left nor right - return 阅读全文
摘要:
The key of this problem is to think of using Stack 阅读全文
摘要:
DP use HashMap: 根据string的长度sort,然后维护每个string的longest chain,default为1,如果删除某个char生成的string能提供更长的chain,则更新 阅读全文
摘要:
DFS & BFS: 关键在于构造graph 如果想把String[] array转化为2d char array, code如下,写的时候这里出了点小问题。friends是String[] Union Find 阅读全文
摘要:
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this array. Cou... 阅读全文
摘要:
my solution: put integer A[i] at index A[i]-1 unless A[i] already equals i +1 or A[i] == A[A[i]-1] later scan again if at index i, A[i] != i+1, then A 阅读全文
摘要:
the same with word ladder 写的时候语法上出了一些问题 第5行不用给char数组赋大小 第20行用stringbuilder的时候曾经写成:String afterMutation = new StringBuilder(cur).setCharAt(i, c).toStri 阅读全文
摘要:
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely... 阅读全文
摘要:
refer to https://discuss.leetcode.com/topic/68896/java-solution-using-hashmap-with-detailed-explanation After solving several "Game Playing" questions 阅读全文
摘要:
这道题应该没法用DP等解,只能brute force 或者 KMP(为深究) BruteForce, Best Solution for now except KMP 作为Encode String with Shortest Length的subproblem KMP解法未研究,https://d 阅读全文
摘要:
Solution 1: Greedy, Time:O(nlogn) Just assign the cookies starting from the child with less greediness to maximize the number of happy children . Solu 阅读全文
摘要:
我觉得这道题是hard,难点第一是要想到用stack,第二是要维护一个这样子的min-max序列:So at any time in the stack, non-overlapping Pairs are formed in descending order by their min value, 阅读全文
摘要:
参考了https://discuss.leetcode.com/topic/67413/detailed-explanation-for-java-o-n-2-solution 这道题DP思路还是能想出来,Time O(N^2), Space O(N^2) T(i, d), which denote 阅读全文
摘要:
Just like meeting point problem, find the median elements in the array after sorting, so Solution 1: Sort, find the median: O(NlogN) Solution 2: Quick 阅读全文
摘要:
一开始想了一个O(n^3), space O(N)的做法,后来发现还可以优化 Solution: time O(N^2), space O(N^2) 阅读全文
摘要:
第二遍做法:参考https://discuss.leetcode.com/topic/64624/concise-easy-to-understand-java-5ms-solution-with-explaination Actually this is a denary tree (each n 阅读全文
摘要:
我的Greedy+Heap做法: Array按start moment升序sort,Heap里按end moment升序sort,一旦到Heap里结束时间最早的气球的end,就要扔arrow,这时在heap里的气球都会被引爆 改进:因为一旦气球爆了的话,Heap里面的元素要全部清空,不需要知道Hea 阅读全文
摘要:
idea 1: suppose it takes y steps to equal each elements, then this equation stands: (min + y) * num.length == sum + (num.length-1)*y, where min + y is 阅读全文
摘要:
Solution: Use HashTable, Time: O(N^2), Space: O(N) 我的:注意14行是有value个重复distance,表示有value个点,他们跟指定点距离都是distance,需要选取2个做permutation, 所以是value * (value-1) 别 阅读全文
摘要:
count is the # of level, sum is the accumulated coins Better Solution: Binary Search, 因为怕溢出,所以(1+m)m/2表示成了line6那种样子. 用m去估计最后返回的row 阅读全文
摘要:
Add the prefix sum to the hashMap, and check along path if hashMap.contains(pathSum+cur.val-target); My Solution 一个更简洁的solution: using HashMap to stor 阅读全文
摘要:
Solution: O(1) time complexity 解题思路主要参考了网友ivancjw的帖子,数据结构参考了https://discuss.leetcode.com/topic/65634/java-ac-all-strict-o-1-not-average-o-1-easy-to-re 阅读全文
摘要:
Solution 1: TreeMap, Time complexity: O(NlogN) 像这种在一个集合里面寻找有没有比某个数小的数,一般要么treeMap要么treeSet。Interval的题经常需要用treeMap, Data Stream as Disjoint Intervals 就 阅读全文
摘要:
Actually, the problem is the same as "Given a collection of intervals, find the maximum number of intervals that are non-overlapping." (the classic Gr 阅读全文