问题一:

pi表示取第i个单,di表示送第i个单。di不能在pi的前面。给一个取单送单的顺序,问是否是valid顺序。

 public boolean isValidOrderList(List<String> list) {
Set<String> set = new HashSet<>();
for (String item : list) {
if (item.startsWith("P")) {
set.add(item);
} else {
String parent = getParent(item);
if (!set.contains(parent)) {
return false;
}
set.remove(parent);
}
}
return set.isEmpty();
} private String getParent(String dId) {
if (dId == null || dId.length() < || dId.charAt() != 'D') {
throw new IllegalArgumentException("invalid input:" + dId);
} int id = Integer.parseInt(dId.substring());
return "P" + id;
}

问题二:

pi表示取第i个单,di表示送第i个单。di不能在pi的前面。给一个n,显示所有正确的顺序。

 public List<List<String>> print(int n) {
List<List<String>> result = new ArrayList<>();
List<List<String>> tempList = new ArrayList<>();
for (int j = ; j <= n; j++) {
if (j == ) {
result.add(Arrays.asList("p1", "d1"));
continue;
}
for (int i = ; i < result.size(); i++) {
String[] temp = new String[j * ];
for (int p = ; p < * j; p++) {
for (int q = p + ; q < * j; q++) {
clearArray(temp);
temp[p] = "p" + j;
temp[q] = "d" + j;
fillInArray(result.get(i), temp);
tempList.add(arrayToList(temp));
}
}
}
result = new ArrayList<>(tempList);
tempList.clear();
} return result;
} private void clearArray(String[] arr) {
for (int i = ; i < arr.length; i++) {
arr[i] = null;
}
} private void fillInArray(List<String> result, String[] temp) {
int index = ;
for (String str : result) {
while(temp[index] != null) {
index++;
}
temp[index] = str;
}
} private List<String> arrayToList(String[] arr) {
List<String> list = new ArrayList<>();
for (String str : arr) {
list.add(str);
}
return list;
}

问题3:

给你一个数字,问你有多少种接单和送单的顺序。

比如

n = 1, only 1 possible, p1 d1

n = 2,  6 possible

p1 d1 p2 d2

p1 p2 d1 d2

p1 p2 d2 d1

p2 p1 d1 d2

p2 p1 d2 d1

p2 d2 p1 d1

 int totalCount(int n) {
if (n == ) return ;
int prevCount = ;
for (int i = ; i <= n; i++) {
int totalSlots = * i;
prevCount = sum(totalSlots - ) * prevCount;
}
return prevCount;
} int sum(int n) {
int total = ;
for (int i = ; i <= n; i++) {
total += i;
}
return total;
}

order pick-up and delivery problem的更多相关文章

  1. Order to Cash Process

    order to cash process steps can be listed as below · Enter the Sales Order · Book the Sales Order · ...

  2. Order&Shipping Transactions Status Summary

    Order&Shipping Transactions Status Summary Step Order Header Status Order Line Status Order Flow ...

  3. SPOJ ORDERSET - Order statistic set

    ORDERSET - Order statistic set   In this problem, you have to maintain a dynamic set of numbers whic ...

  4. How to Configure Nginx for Optimized Performance

    Features Pricing Add-ons Resources | Log in Sign up   Guides & Tutorials Web Server Guides Nginx ...

  5. Enhancing the Scalability of Memcached

    原文地址: https://software.intel.com/en-us/articles/enhancing-the-scalability-of-memcached-0 1 Introduct ...

  6. topcoder算法练习2

    Problem Statement      In most states, gamblers can choose from a wide variety of different lottery ...

  7. [SinGuLaRiTy] COCI 2011~2012 #2

    [SinGuLaRiTy-1008] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 测试题目 对于所有的题目:Time Limit:1s   ...

  8. (转)db2top详解

    原文:https://blog.csdn.net/lyjiau/article/details/47804001 https://www.ibm.com/support/knowledgecenter ...

  9. SD从零开始25-28

    SD从零开始25 装运的组织单元(Organizational Units in Shipping) 组织结构-后勤Organizational Structure-Logistics Plant在后 ...

随机推荐

  1. TensorFlow(八):tensorboard可视化

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data from tensorflow.c ...

  2. getchar 和EOF

    本文章基于:http://www.cnblogs.com/QLinux/articles/2465329.html,稍作了修改. 大师级经典的著作,要字斟句酌的去读,去理解.以前在看K&R的T ...

  3. CF891C Envy【最小生成树】

    题目链接 我们知道,根据Kruskal的贪心,对于最小生成树,每一种权值的边数是一样的,而且如果将\(\leq x\)的边做最小生成树,合法方案的联通性是一样的.所以我们可以对于所有边分开考虑. 对于 ...

  4. 【原创】go语言学习(五)函数详解1

    目录 1.函数介绍 2.多返回值和可变参数 3.defer语句 4.内置函数介绍 1.函数介绍 1.1定义: 有输⼊入.有输出,⽤用来执⾏行行⼀一个指定任务的代码块. func functionnam ...

  5. js 删除节点

    亲身经历,寻得此法,告知大家=============== 在javascript操作dom树的时候可能会经常遇到增加,删除节点的事情,比如一个输入框后一个增加按钮,一个删除按钮,点击增加就增加 个输 ...

  6. Spring学习随笔(1):为什么要使用Spring

    寒冷的冬天,一周两节课,掏出买了一年没翻过的<Spring实战>. 刚刚接触spring的我对它还不是很熟悉,对各种知识的认知也比较浅薄,但我会学习的过程通过随笔记录下来,监督自己学下去. ...

  7. Linux工程管理器——make

    一.定义 工程管理器,顾名思义,是指管理较多的文件 Make工程管理器也就是个“自动编译管理器”,这里的“自动”是指它能构根据文件时间戳自动发现更新过的文件而减少编译的工作量,同时,它通过读入Make ...

  8. 关于form与表单提交操作的一切

    原文链接:http://caibaojian.com/form.html 你知道,一个表单里面只要有form元素,如果没有给action加一个默认值,为空白的时候,当你刷新页面时,会弹出一个警告框提示 ...

  9. VIM快速掌握

    vi/vim 基本使用方法 vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令.由于对Unix及Linux系统的 ...

  10. Tosca TestCases: Update all,Checkin all,Checkout,Checkout Tree

    #记录一下TestCases  Module 红框里面这几个button的功能 #Update all 从数据库里把别人最新checkin的东西给拽出来查看,也就是拿最新版本 #Checkin all ...