寒い

  • Home

  • About

  • Tags

  • Categories

  • Archives

333 Largest BST Subtree

Posted on 2019-09-30 | In LeetCode

333 Largest BST Subtree

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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.

Example:

Input: [10,5,15,1,8,null,7]

10
/ \
5 15
/ \ \
1 8 7

Output: 3
Explanation: The Largest BST Subtree in this case is the highlighted one.
The return value is the subtree's size, which is 3.

基础

Posted on 2019-09-28 | Edited on 2019-09-29 | In 操作系统

秋招面的本来就不多,听到的最多的就是:你基础不好。对着屏幕从题库里跳出一大堆概念问题,对面背的不够熟练或者漏了一条,然后就是你基础不好。:-(

进程和线程

  • 段程序的执行过程,是进程实体的运行过程(进程实体)=程序+数据+PCB(进程控制块)),大多数时候我们把进程实体叫做进程。(进程是具有独立功能的程序在数据集合上的运行过程)
  • 进程是分配系统资源的单位,各个进程拥有的内存地址空间相互独立。
  • 线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位。
  • 进程在执行过程中拥有独立的内存单元,而多个线程共享进程的内存。

区别:

  • 进程是资源分配的基本单位,线程是独立调度的基本单位。
  • 在同一进程中,线程的切换不会引起进程切换。
  • 由于创建或撤销进程时,系统都要为之分配或回收资源,如内存空间、I/O 设备等,所付出的开销远大于创建或撤销线程时的开销。而线程开销小。
Read more »

ThreadPool

Posted on 2019-09-27 | Edited on 2019-09-28 | In Java

ThreadPool

蓄水池算法

Posted on 2019-09-24 | Edited on 2019-09-28 | In algorithm

从数据流中等概率抽取一定的结果。

  1. 数据总量为n,需要取k个
  2. 构建一个k数组,前k个元素放入数组中
  3. 从第k+1个开始,k/i(i为当前的数量)的概率决定是否留在数组中,随机挑一个移出队列。

证明:

Read more »

reverse list

Posted on 2019-09-22 | Edited on 2019-09-29 | In LeetCode

reverse list

今天面试反转链表竟然能写错,真的想杀了自己,每次都在关键时刻掉链子,难的能写出来,简单的缺写错。做链表题的时候,遍历的时候注意循环的条件。到底是p==null还是p.next==null ,然后考虑循环条件满足的时候,循环体中的变量有没有满足。碰到循环或者递归不要一步一步去模拟,这样没完没了。

1
2
3
4
5
6
7
8
9
10
11
12
public ListNode reverse(ListNode head){
if(head==null)return null;
ListNode pre=null;
ListNode cur=head;
while(cur!=null){
ListNode next=cur.next;
cur.next=pre;
pre=cur;
cur=next;
}
return pre;
}
Read more »

substring

Posted on 2019-09-19 | In LeetCode

substring

1
2
3
4
5
6
7
8
9
10
11
12
13
string minWindow(string s, string t) {
vector<int> map(128,0);
for(auto c: t) map[c]++;
int counter=t.size(), begin=0, end=0, d=INT_MAX, head=0;
while(end<s.size()){
if(map[s[end++]]-->0) counter--; //in t
while(counter==0){ //valid
if(end-begin<d) d=end-(head=begin);
if(map[s[begin++]]++==0) counter++; //make it invalid
}
}
return d==INT_MAX? "":s.substr(head, d);
}

797 All Paths From Source to Target

Posted on 2019-09-15 | In LeetCode

All Paths From Source to Target

1
2
3
4
5
6
7
8
9
10
11
12
13
Given a directed, acyclic graph of N nodes.  Find all possible paths from node 0 to node N-1, and return them in any order.

The graph is given as follows: the nodes are 0, 1, ..., graph.length - 1. graph[i] is a list of all nodes j for which the edge (i, j) exists.

Example:
Input: [[1,2], [3], [3], []]
Output: [[0,1,3],[0,2,3]]
Explanation: The graph looks like this:
0--->1
| |
v v
2--->3
There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.

直接从0节点,DFS找到N-1就行了。因为是无环图也不需要判断有没有遍历过。

在找的时候会有重复的节点被找多次,可以采用Map<Integer,List<List> 来存储已经遍历过的节点。但提交的时候时间更长了,估计测试样例中没有多个节点重复找的情况比较少。

这种会重复路径的问题,类似DP,要记得试着去记录下来

525 Contiguous Array

Posted on 2019-09-13

525 Contiguous Array

1
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1.

找到连续的子数组,包含相同个数的0和1.

看到解法才想到之前看到过的微软的一到面试题,找出数组中的最长的和为0的字数组(好像是这样,忘记了),

如果遍历数组的话,找到一段符合要求的还要判断能不能和前面满足要求的子数组连起来,求最大值。

求和,如果当前sum(i)等于之前某个位置的sum(j),说明这中间为0,sum(i)-sum(j)就行了。

回到这一题,按照原来的想法看,思路完全是一样的,当前子数组都有可能和之前的子数组连起来。把0变成-1就和之前的题一模一样了。

1
2
3
4
5
6
7
8
for(int i;i<length;i++){
sum+=nums[i];
if(map.containsKey(sum)){
max=Math.max(max,i-map.get(sum));
}else{
map.put(sum,i);
}
}

329 Longest Increasing Path in a Matrix

Posted on 2019-09-12

Longest Increasing Path in a Matrix

1
2
3
Given an integer matrix, find the length of the longest increasing path.

From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed).

对每个格子找出以此为起点用dfs找出最长的,之后,找出所有的各自的最长的。对每个格子找出最长的时候记录下来,因为格子不一定是起点,有更小的话找到该格子,直接加上就行了,不用在找一遍。

背包

Posted on 2019-09-12

背包问题

  1. n个物体,体积Ci,价值Wi,有一个容量为V的包,求价值最大。
  • 前i个物体放入容量为v的背包可以获得的最大值

    • F[i,v]=max{F[i-1,v],F[i-1,v-Ci]+Wi}
    • 第i个要么不放,要么放
    • O(Vn)
  • 因为只跟前一个状态i-1有关,空间可以优化.

    • F[v]跟F[v-Ci]有关,也就是数组后面的跟前面的有关,如果顺序计算,那么前面是更新后的,在计算后面的就不是前一次的状态的了,所以要递减来计算。

    • 1
      2
      3
      4
      5
      for(int i=0;i<n;i++){
      for(int j=V;j>=Ci;j--){
      ……
      }
      }
  • 初始化:

    • F[0]=0,其余MIN,
    • 如果不要求签好装满背包,均为0
    Read more »
12…4
YAHIKO

YAHIKO

34 posts
5 categories
16 tags
© 2019 YAHIKO
Powered by Hexo v3.7.1
|
Theme – NexT.Gemini v6.4.2