看电影,尽量看多的电影
[start,end],start 电影开始的时间,end 电影结束的时间

public class Movice {
public int start;
public int end;
public Movice(int start,int end){
this.start = start;
this.end = end;
}
public boolean isIn(int o){
if(o < end && o > start){
return true;
}
return false;
}
}
public class FindMaxMovices {
public int findMaxMovices(ArrayList<Movice> movices){
Comparator<Movice> comparator = new Comparator<Movice>() {
@Override
public int compare(Movice o1, Movice o2) {
return o1.end - o2.end;
}
};
int count = 0;
Collections.sort(movices,comparator);
for (int i = 0 ; i < movices.size();i++){
count++;
int end = movices.get(i).end;
for(int j = i + 1; j < movices.size(); j++){
if(movices.get(j).isIn(end)){
i = j;
}
}
}
return count;
}
public static void main(String[] args) {
FindMaxMovices findMaxMovices = new FindMaxMovices();
int[][] movice = {{1,3},{2,4},{4,7},{5,6},{6,10},{3,9}};
ArrayList<Movice> movices = new ArrayList<>();
for(int i = 0 ; i < movice.length; i++){
movices.add(new Movice(movice[i][0],movice[i][1]));
}
System.out.println(findMaxMovices.findMaxMovices(movices));
}
}
找到二叉树两个节点的最长距离
编程之美原题
解法一:
对于任意一个节点,以该节点为根,假设这个根有K个孩子节点,那么相距最远的两个节点U和V之间的路径与这个根节点的关系有两种情况: 1.若路径经过根Root,则U和V是属于不同子树的,且它们都是该子树中到根节点最远的节点,否则跟它们的距离最远相矛盾。 2.如果路径不经过Root,那么它们一定属于根的K个子树之一。并且它们也是该子树中相距最远的两个顶点。
因此,问题就可以转化为在子树上的解,从而能够利用动态规划来解决。
设第K棵子树中相距最远的两个节点:Uk和Vk,其距离定义为d(Uk,Vk),那么节点Uk或Vk即为子树K到根节点Rk距离最长的节点。不失一般性,我们设Uk为子树K中到根节点Rk距离最长的节点,其到根节点的距离定义为d(Uk,R)。取d(Ui,R)(1≤i≤k)中最大的两个值max1和max2,那么经过根节点R的最长路径为max1+max2+2,所以树R中相距最远的两个点的距离为:max{d(U1,V1),…,d(Uk,Vk),max1+max2+2}。
只需要遍历所有的节点一次,时间复杂度为O(|E|)=O(|V|-1),其中V为点的集合,E为边的集合。
package tree;
/**
* Created by mac on 2018/7/10.
*/
public class FindMaxLen {
int nMaxLen = 0;
//寻找树中最长的两段距离
void FindMaxLen(Node pRoot){
if(pRoot == null){
return;
}
//如果左子树为空,那么该节点的左边最长距离为0
if(pRoot.pLeft == null){
pRoot.nMaxLeft = 0;
}
//如果右子树为空,那么该节点的右边最长距离为0
if(pRoot.pRight == null){
pRoot.nMaxRight = 0;
}
//如果左子树不为空,递归寻找左子树最长距离
if(pRoot.pLeft != null){
FindMaxLen(pRoot.pLeft);
}
//如果右子树不为空,递归寻找右子树最长距离
if(pRoot.pRight != null){
FindMaxLen(pRoot.pRight);
}
//计算左子树最长节点距离
if(pRoot.pLeft != null){
int nTempMax = 0;
if(pRoot.pLeft.nMaxLeft > pRoot.pLeft.nMaxRight){
nTempMax = pRoot.pLeft.nMaxLeft;
}else{
nTempMax = pRoot.pLeft.nMaxRight;
}
pRoot.nMaxLeft = nTempMax + 1;
}
//计算右子树最长节点距离
if(pRoot.pRight != null){
int nTempMax = 0;
if(pRoot.pRight.nMaxLeft > pRoot.pRight.nMaxRight){
nTempMax = pRoot.pRight.nMaxLeft;
}else{
nTempMax = pRoot.pRight.nMaxRight;
}
pRoot.nMaxRight = nTempMax + 1;
}
if(pRoot.nMaxLeft + pRoot.nMaxRight > nMaxLen){
nMaxLen = pRoot.nMaxLeft + pRoot.nMaxRight;
}
}
}
改变两个数字,a,b,不用额外的空间
a = a + b;
b = a - b;
a = a - b;
用hashmap,arraylist实现set,实现方法insert,delete,getrandom 复杂度O(1)
leetcode原题 Insert Delete GetRandom O(1)
两个hashmap
class RandomizedSet {
HashMap<Integer, Integer> map1;
HashMap<Integer, Integer> map2;
Random rand;
/** Initialize your data structure here. */
public RandomizedSet() {
map1 = new HashMap<Integer, Integer>();
map2 = new HashMap<Integer, Integer>();
rand = new Random(System.currentTimeMillis());
}
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
public boolean insert(int val) {
if(map1.containsKey(val)){
return false;
}else{
map1.put(val, map1.size());
map2.put(map2.size(), val);
}
return true;
}
/** Removes a value from the set. Returns true if the set contained the specified element. */
public boolean remove(int val) {
if(map1.containsKey(val)){
int index = map1.get(val);
//remove the entry from both maps
map1.remove(val);
map2.remove(index);
if(map1.size()==0){
return true;
}
//if last is deleted, do nothing
if(index==map1.size()){
return true;
}
//update the last element's index
int key1 = map2.get(map2.size());
map1.put(key1, index);
map2.remove(map2.size());
map2.put(index, key1);
}else{
return false;
}
return true;
}
/** Get a random element from the set. */
public int getRandom() {
if(map1.size()==0){
return -1;
}
if(map1.size()==1){
return map2.get(0);
}
return map2.get(new Random().nextInt(map1.size()));
//return 0;
}
}
/**
* Your RandomizedSet object will be instantiated and called as such:
* RandomizedSet obj = new RandomizedSet();
* boolean param_1 = obj.insert(val);
* boolean param_2 = obj.remove(val);
* int param_3 = obj.getRandom();
*/
//一个hashmap 一个 arraylist
class RandomizedSet {
Map<Integer, Integer> map;
List<Integer> list;
Random random;
/** Initialize your data structure here. */
public RandomizedSet() {
map = new HashMap<>();
random = new Random();
list = new ArrayList<>();
}
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
public boolean insert(int val) {
if (!map.containsKey(val)) {
map.put(val, list.size());
list.add(val);
return true;
}
return false;
}
/** Removes a value from the set. Returns true if the set contained the specified element. */
public boolean remove(int val) {
if (!map.containsKey(val)) return false;
int index = map.get(val);
if (index < list.size() - 1) {
int num = list.get(list.size() - 1);
list.set(index, num);
map.put(num, index);
}
map.remove(val);
list.remove(list.size() - 1);
return true;
}
/** Get a random element from the set. */
public int getRandom() {
return list.get(random.nextInt(list.size()));
}
}
/**
* Your RandomizedSet object will be instantiated and called as such:
* RandomizedSet obj = new RandomizedSet();
* boolean param_1 = obj.insert(val);
* boolean param_2 = obj.remove(val);
* int param_3 = obj.getRandom();
*/
java 和 javascript的比较
由Netscape,Inc。开发的JavaScript编程语言不是Java平台的一部分。
JavaScript不会创建applet或独立应用程序。 在最常见的形式中,JavaScript驻留在HTML文档中,并且可以为使用简单HTML无法实现的网页提供交互级别。
Java和JavaScript之间的主要区别:
- Java是一种OOP编程语言,而Java Script是一种OOP脚本语言。
- Java创建在虚拟机或浏览器中运行的应用程序,而JavaScript代码仅在浏览器上运行。
- 需要编译Java代码,而JavaScript代码全部都是文本。
- 它们需要不同的插件。
javascript的4个特性
1.浏览器支持 要访问Flash内容,您需要在浏览器中安装Flash插件。但是要使用javascript,您根本不必使用任何插件。这是因为所有浏览器都接受了javascript作为它们的脚本语言,并为它提供了集成支持。您需要做的就是正确处理一些依赖于不同浏览器的DOM(文档对象模型)的任务。
2.可以在客户端和服务器端使用 由于javascript可以访问浏览器的Document对象模型,因此您可以在运行时实际更改Web页面的结构。因此,javascript可用于为网页添加不同的效果。另一方面,javascript也可以在服务器端使用。例如,在Alfresco这是一个流行的开源企业内容管理系统,javascript用于创建webscripts。这使得向露天添加自定义任务变得非常简单。
3.函数式编程语言 在javascript中,函数可以像任何其他数据类型一样分配给变量。不仅如此,函数还可以接受另一个函数作为参数,也可以返回一个函数。你也可以拥有没有名字的功能。显然,这使您能够以函数式编程风格进行编码。
4.支持对象 Javascript是一种面向对象的语言。但是,javascript处理对象和继承的方式与传统的面向对象编程语言(如Java)略有不同。因此,javascript支持大多数面向对象的概念,同时易于学习和使用。
这些功能使javascript能够处理简单和复杂的任务。因此,javascript一直是最流行的编程语言。对于想要学习计算机编程的人来说,它也是一门很好的语言,因为它支持面向对象以及功能概念并使用它,你只需要一个浏览器和一个文本编辑器。