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在后 ...
随机推荐
- LibreOJ #116. 有源汇有上下界最大流
二次联通门 : LibreOJ #116. 有源汇有上下界最大流 /* LibreOJ #116. 有源汇有上下界最大流 板子题 我也就会写写板子题了.. 写个板子第一个点还死活过不去... 只能打个 ...
- PHP 之Html标签转义与反转义
1.htmlentities()函数转义html 2.html_entity_decode()函数反转义html 我这里是用来反转义富文本编辑器的内容
- python3编程基础之一:注释模块和包
1.注释 python中的注释和其他任何编程语言中的注释都不一样,有的注释有特殊要求,而是还是有用的. 1).单行注释:注释以#开始到语句结尾,#号后一般跟一个空格 2).多行注释:文档注释,以&qu ...
- java四种对象引用类型
java四种对象引用类型 对象的强.软.弱和虚引用 在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.也就是说,只有对象处于可触及(reachable)状态,程序 ...
- Spring框架各模块功能介绍
一. Spring是什么? Spring由Rod johnson开发: 是一个非常活跃的开源框架: 它帮助分离项目组件(对象)之间的依赖关系: 它的主要目的是简化企业开发 二. Spring的核心概念 ...
- ES6展开运算符数组合并,函数传参
定义: .展开运算符允许一个表达式在某处展开. 使用场景 1.展开函数在多个参数的地方使用 .意指用于函数传参 2.多个元素的地方使用,意指用于数组字面量 3.多个边框的地方使用,意指用于解构赋值 函 ...
- TweenJS----前端常用的js动画库,缓动画和复制动画的制作,效果非常好。
TweenJS类库主要用来调整和动画HTML5和Javascript属性,提供了简单并且强大的tweening接口. http://www.createjs.cc/tweenjs/ http://ww ...
- tensorflow cpu问题
返回: -- ::] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 ...
- 29 Flutter Dialog AlertDialog 、SimpleDialog、showModalBottomSheet、showToast
pubspec.yaml fluttertoast: ^ Dialog.dart import 'package:flutter/material.dart'; import 'package:flu ...
- python中的列表推导式——轻量级循环
列表推导式(list comprehension)是利用其他列表创建新列表(类似于数学术语中的集合推导式)的一种方法.它的工作方式类似于for循环,也很简单. 列表推导式书写形式: [表达式 for ...