我有一个关于Hackkerrank的挑战如下
示例
票=[8,5,4,8,4]
已排序的有效子序列为{4,4,5}和{8,8}。这些子序列的m值分别为3和2。返回3。
功能描述
在下面的编辑器中完成maxTickets函数。
int tickets[n]: an array of integers
int: an integer that denotes the maximum possible value of m
1 ≤ n ≤ 10^5
1 ≤ tickets[i] ≤ 10^9
采样输入0
STDIN函数
4张票[]大小n=4
2
3
示例输出0
import java.util.*;
public class MaxTickets {
public static void main(String[] args) {
test(4,13,2,4,3);
test(10,1,5,2,5,5,2,6,3,5,3);
test(10,1,5,1,2,5,5,2,6,3,5,3);
test(9,1,4,5,2,2,3,1,6,9,10);
test(1,2,4,7,8,11,22);
test(1,2,2,2,3,4,7,8,11,14,22);
test(1,1,2,4,4,4,7,8,11,22);
}
static int maxTickets(List<Integer> tickets) {
Collections.sort(tickets);
tickets.forEach(num -> System.out.print(num + " "));
List<Integer> list = new ArrayList<>();
int len = tickets.size();
int distance;
int curDistance = Integer.MIN_VALUE;
int lastDistance = Integer.MIN_VALUE;
boolean first = true;
for(int i = 1; i < len; i++) {
int left = tickets.get(i-1);
int right = tickets.get(i);
distance = right - left;
if(i == 1) {
updateSum(list, 2);
} else {
if(lastDistance == 0 && first) {
updateSum(list, 1);
}
else if(distance == 0 ||
distance == curDistance) {
updateSum(list, 1);
} else if(distance != curDistance) {
list.add(1);
}
}
if(distance != 0) {
curDistance = distance;
}
lastDistance = distance;
if(i >= 2) {
first = false;
}
}
return
list.stream()
.max(Comparator.comparingInt(i -> i))
.get();
}
private static void updateSum(List<Integer> list, int value) {
if(list.isEmpty()) {
list.add(value);
} else {
int lastIndex = list.size() - 1;
list.set(lastIndex, list.get(lastIndex) + value);
}
}
private static void test(Integer... numbers) {
List<Integer> list = Arrays.asList(numbers);
int result = maxTickets(list);
System.out.println("\n" + result);
}
}
输出:
2 3 4 4 13
4
1 2 2 3 3 5 5 5 5 6 10
5
1 1 2 2 3 3 5 5 5 5 6 10
6
1 1 2 2 3 4 5 6 9 9 10
8
1 2 4 7 8 11 22
2
1 2 2 2 3 4 7 8 11 14 22
6
1 1 2 4 4 4 7 8 11 22
3
我的代码能用吗?有没有我的代码失败的情况?你能为这个挑战提供一个更好的算法吗?
解决这个问题最简单(也是最快)的方法是使用最简单、最直接的算法。下面是一个描述它的伪代码。
Function unbroken_range(arr) {
n = arr.length;
sorted_arr = sort(arr);
range_start = 0;
best_len = 0;
for i from 1 to n-1:
if sorted_arr[i] - sorted_arr[i-1] > 1 then {
current_len = i - range_start;
if current_len > best_len then {
best_len = current_len;
}
range_start = i;
}
}
return max(n - range_start, best_len);
}
从输出的外观来看,似乎是您的程序完成了这项工作。我没有详细讨论它,但您可以将它与伪代码进行比较,看看算法结构是否相似(应该如此,这确实是处理它的最简单的方法)。
问题内容: 这是我要使用的。.length方法对我尝试的任何操作均无效,因此我什至不知道从哪里开始。 问题答案: 您正在尝试遍历单个数组而不是字符串数组。更改 至 以便通过字符串列表循环,收集每个字符串和存储它诠释的,你以后。
问题内容: 查找整数数组中的第一个重复元素。 例如: 问题答案: 简单的解决方案是使用两个循环。外循环将遍历循环,内循环将检查元素是否重复,但此解决方案的时间复杂度为 o(n^2)。 另一种解决方案是创建另一个数组并对其进行排序。从原始数组中选取元素并使用二进制搜索在排序数组中查找元素,但此解决方案的时间复杂度为 o(n^logn)。 我们能做得更好吗? 是的,我们可以从右到左迭代并使用HashS
问题内容: 我编写了以下代码段来计算每个元素的出现次数。有可能以更短的方式实现这一目标吗? 另外,我只想显示出现1次以上的元素。所以我尝试如下修改,这导致了错误。 正确的方法是什么? 问题答案: 对于后一个问题,您必须进行更改 至 对于第一部分,尚不清楚为什么需要第一条管道,然后需要第二条管道。如果目的是将转换为,请使用: 正如Dici所建议的,您还可以将Collectors链接起来,将每个数字与
问题内容: 我们需要在分配中递归地找到一个数组中的第二个最小整数。但是,为了更好地理解该主题,我想先通过本网站进行迭代,然后自己进行递归。 不幸的是,迭代地进行相当混乱。我知道该解决方案很简单,但我无法解决。 到目前为止,以下是我的代码: 这适用于一些数字,但不是全部。数字会变化,因为内部if条件的效率不如外部if条件的效率。 禁止阵列重排。 问题答案: 试试这个。当最小的数字是第一个时,第二个条
我收集了一些作者的文章。每个作者都有一个独特的签名或链接,出现在他们所有的文本中。 Author1的示例: Author1的预期输出为: Author2的示例: Author2的预期输出为: 请特别注意,没有可靠的识别字符(或位置)表示签名的开始或结束。它可以是一个url,一个Twitter地址,任何类型的纯文本,等等,任何长度,包含字符串开头、结尾或中间出现的任何字符序列。 我正在寻找一种方法,
我正在尝试解决这个算法问题: https://dunjudge.me/analysis/problems/469/ 为了方便起见,我总结了下面的问题陈述。 给定一个长度为 ( 多数元素定义为发生的元素 时限:1.5s 例如: 如果给定的数组是[1,2,1,2,3,2], 答案是5,因为从位置1到5 (0索引)的长度为5的子数组[2,1,2,3,2]具有出现为3的数字2 首先想到的是一个明显的强力(