小编viv*_*_23的帖子

数组的最小和分区

问题陈述:

给定一个数组,任务是将其分为两组S1和S2,以使它们的和之间的绝对差最小。

采样输入

[1,6,5,11]=> 1。2个子集为{1,5,6}{11},总和为1211。因此答案是1

[36,7,46,40]=> 23。2个子集为{7,46}{36,40},总和为5376。因此答案是23

约束条件

1 <=数组大小<= 50

1 <= a [i] <= 50

我的努力:

int someFunction(int n, int *arr) {
    qsort(arr, n, sizeof(int), compare);// sorted it for simplicity
    int i, j;
    int dp[55][3000]; // sum of the array won't go beyond 3000 and size of array is less than or equal …
Run Code Online (Sandbox Code Playgroud)

c algorithm dynamic-programming

2
推荐指数
1
解决办法
1194
查看次数

php array_column 函数的逆函数?

给定一个数组,如

$x = array(array('a', 'aa'), array('b', 'bb'), array('c', 'cc'));
Run Code Online (Sandbox Code Playgroud)

array_column返回

array_column($x, 0) === array('a', 'b', 'c')

or 

array_column($x, 1) === array('aa', 'bb', 'cc') 
Run Code Online (Sandbox Code Playgroud)

现在,有反面吗?一个可以做的功能:

array_putoneaftertheother(array('a', 'b', 'c'), array('aa', 'bb', 'cc')) === array(array('a', 'aa'), array('b', 'bb'), array('c', 'cc')) 
Run Code Online (Sandbox Code Playgroud)

没有想到我...

实现起来很容易,但我很惊讶有这么array_*多功能,PHP 没有原生版本?!

php arrays

2
推荐指数
1
解决办法
271
查看次数

为什么我的队列排序输入字符串与先进先出基础不同?

我正在尝试利用队列,但队列正在重新组织,我不知道为什么

使用队列的每个人似乎都工作得很好,似乎没有人遇到这个问题

public static void main(String[] args){
    PriorityQueue<String> cue = new PriorityQueue<String>();
    cue.offer("this");
    cue.offer("that");
    cue.offer("then");
    System.out.printf("%s \n", cue);
    System.out.printf("%s \n", cue.peek());
    cue.poll();
    System.out.printf("%s \n", cue);
}
Run Code Online (Sandbox Code Playgroud)

我希望它打印:

[this, that, then]
this
[that, then]
Run Code Online (Sandbox Code Playgroud)

但相反它打印:

[that, this, then]
that
[then, this]
Run Code Online (Sandbox Code Playgroud)

我不知道为什么

java priority-queue

0
推荐指数
1
解决办法
50
查看次数

标签 统计

algorithm ×1

arrays ×1

c ×1

dynamic-programming ×1

java ×1

php ×1

priority-queue ×1