zoukankan      html  css  js  c++  java
  • [CareerCup][Google Interview] 找最大序列

    A circus is designing a tower routine consisting of people standing atop one another’s
    shoulders. For practical and aesthetic reasons, each person must be both shorter and lighter than the person below him or her. Given the heights and weights of each person in the circus, write a method to compute the largest possible number of people
    in such a tower.
    EXAMPLE:
    Input (ht, wt): (65, 100) (70, 150) (56, 90) (75, 190) (60, 95) (68, 110)
    Output: The longest tower is length 6 and includes from top to bottom: (56, 90) (60,95) (65,100) (68,110) (70,150) (75,190)

    http://www.careercup.com/question?id=9339758

    先把数组按height或者weight排个序,然后就变为找最长上升子序列问题了。

     1 #include <iostream>
     2 #include <vector>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 struct Node
     7 {
     8     int height;
     9     int weight;
    10 
    11     Node(){}
    12     Node(int h, int w):height(h), weight(w){}
    13 };
    14 
    15 bool comp(const Node &lhs, const Node &rhs)
    16 {
    17     return lhs.height < rhs.height;
    18 }
    19 
    20 int solve(vector<Node> &a)
    21 {
    22     sort(a.begin(), a.end(), comp);
    23 
    24     vector<int> f(a.size());
    25 
    26     f[0] = 1;
    27 
    28     for(int i = 1; i < a.size(); i++)
    29     {
    30         f[i] = 1;
    31         for(int j = 0; j < i; j++)
    32             if (a[i].height > a[j].height && a[i].weight > a[j].weight)
    33                 f[i] = max(f[i], f[j] + 1);
    34     }
    35 
    36     int maxF = 0;
    37     for(int i = 0; i < f.size(); i++)
    38         maxF = max(maxF, f[i]);
    39 
    40     return maxF;
    41 }
    42 
    43 int main()
    44 {
    45     vector<Node> a;
    46     a.push_back(Node(65, 100));
    47     a.push_back(Node(70, 150));
    48     a.push_back(Node(56, 90));
    49     a.push_back(Node(75, 190));
    50     a.push_back(Node(60, 95));
    51     a.push_back(Node(68, 110));
    52 
    53     cout << solve(a) << endl;
    54 
    55     vector<Node> b;
    56     b.push_back(Node(1, 2));
    57     b.push_back(Node(1, 3));
    58 
    59     cout << solve(b) << endl;
    60 }
  • 相关阅读:
    主流数据库连接池性能比较 hikari druid c3p0 dbcp jdbc
    Dubbo 分布式事务一致性实现
    微服务实现事务一致性实例
    微服务间保持事务一致性
    海量积分数据实时排名处理方式介绍二
    Java两种方法实现循环报数
    MySQL 千万级 数据库或大表优化
    Linux 中 Nginx 重启关闭
    Linux 中 Oracle dmp 文件导入导出
    Linux 中 Oracle 数据库启动和关闭
  • 原文地址:https://www.cnblogs.com/chkkch/p/2744557.html
Copyright © 2011-2022 走看看