问题一:

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. 【原创】go语言学习(五)函数详解1

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

  2. 关于解决ruby源码安装 gem install报错问题

    因做redis集群需要安装ruby,源码安装过后gem install redis安装redis接口报错 解决方案: 确保主机安装zlib,没有安装执行 yum -y install zlib zli ...

  3. c 输出是自动显示输出类型

    显示0x i= print("%#x\n",i) 显示6位有效数字 i= print("l=%.6lf\n",i)

  4. Linux - /bin/sh^M: bad interpreter: No such file or directory

    问题 在Windows环境下用Notepad++写了个shell脚本,上传到Linux平台后运行报错如下: /bin/sh^M: bad interpreter: No such file or di ...

  5. [线性代数] 线性代数入门A Gentle Introduction

    An Overview: System of Linear Equations Basically, linear algebra solves system of linear equations ...

  6. C++删除字符串的前后空格

    函数: string trim(string& str) { str.erase(0, str.find_first_not_of(" \t")); // 去掉头部空格 s ...

  7. 面试题小议---BY gremount

    Problem 1:  两个烧杯,一个放糖一个放盐,用勺子舀一勺糖到盐,搅拌均匀,然后舀一勺混合物会放糖的烧杯,问你两个烧杯哪个杂质多? 提示:相同.(1)可以用一个特殊数据计算一下,可以得到两个烧杯 ...

  8. beyond compare秘钥被禁

    错误提示:This license key has been revoked xxxxx 即: Windows 系统: 解决方法: 删除以下目录中的所有文件即可. C:\Users\Administr ...

  9. idea JRebel

    JRebel 链接:https://pan.baidu.com/s/11LI0RkPtrfEWQENns6cWAA 提取码:ndsu settings -> plugins -> inst ...

  10. vue开发环境、正式环境的配置及原理

    修改prod.env.js里的内容,修改后的内容如下: 'use strict' module.exports = { NODE_ENV: '"production"', EVN_ ...