order pick-up and delivery problem
问题一:
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的更多相关文章
- Order to Cash Process
order to cash process steps can be listed as below · Enter the Sales Order · Book the Sales Order · ...
- Order&Shipping Transactions Status Summary
Order&Shipping Transactions Status Summary Step Order Header Status Order Line Status Order Flow ...
- SPOJ ORDERSET - Order statistic set
ORDERSET - Order statistic set In this problem, you have to maintain a dynamic set of numbers whic ...
- How to Configure Nginx for Optimized Performance
Features Pricing Add-ons Resources | Log in Sign up Guides & Tutorials Web Server Guides Nginx ...
- Enhancing the Scalability of Memcached
原文地址: https://software.intel.com/en-us/articles/enhancing-the-scalability-of-memcached-0 1 Introduct ...
- topcoder算法练习2
Problem Statement In most states, gamblers can choose from a wide variety of different lottery ...
- [SinGuLaRiTy] COCI 2011~2012 #2
[SinGuLaRiTy-1008] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 测试题目 对于所有的题目:Time Limit:1s ...
- (转)db2top详解
原文:https://blog.csdn.net/lyjiau/article/details/47804001 https://www.ibm.com/support/knowledgecenter ...
- SD从零开始25-28
SD从零开始25 装运的组织单元(Organizational Units in Shipping) 组织结构-后勤Organizational Structure-Logistics Plant在后 ...
随机推荐
- [Luogu] 外星密码
https://www.luogu.org/problemnew/show/P1928 沙比提 读清题目 #include <bits/stdc++.h> using namespace ...
- The Preliminary Contest for ICPC China Nanchang National Invitational
目录 Contest Info Solutions A. PERFECT NUMBER PROBLEM D. Match Stick Game G. tsy's number H. Coloring ...
- Spring Cloud Gateway(八):其它路由谓词工厂
本文基于 spring cloud gateway 2.0.1 6.基于Cookie的谓词工厂 CookieRoutePredicateFactory 是 Cookie 类型的路由断言工厂,接收两个参 ...
- 物聯網安全黑客松 IoT Security and Privacy Hackathon
感覺這次黑客松的程度屬於初階,但是節奏很快,內容緊湊.概念部分解說較多,以致實驗時間縮短,有些只能看demo有點遺憾.幸好有video-taped,事後回溯可以看看能不能replicate實驗.總體而 ...
- IDEA Junit FileNotFoundException: class path resource [spring/spring.xml] cannot be opened because it does not exist
今天打算写一个单元测试,但是已经有写好的单元测试无论怎么弄都提示文件不存在,自己一度以为是启动方式不正确.这里简单记录一下处理过程 1 异常信息: Caused by: org.springframe ...
- Tkinter 之Canvas画布
一.参数说明 参数 作用 background(bg) 指定 Canvas 的背景颜色 borderwidth(bd) 指定 Canvas 的边框宽度 closeenough 指定一个距离,当鼠标与画 ...
- 在CentOS7中安装zookeeper
参考:https://www.linuxidc.com/Linux/2016-09/135052.htm 1.zookeeper运行需要jdk环境,先确保有配置jdk,可以参考此处 2.下载解压zoo ...
- thymeleaf 声明
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd&qu ...
- 将bat文件注册成为系统服务
第一章 注册系统服务准备 1.1 注册系统服务前准备 1.1.1 涉及第三方软件 Bat_To_Exe_Converter.exe (将*.bat文件转化为可执行*.exe文件) insts ...
- LightGBM建模
LightGBM 1.读取csv数据并指定参数建模 # coding: utf-8 import json import lightgbm as lgb import pandas as pd fro ...