为了提高我的编程技能,或者说我缺乏编程技巧,我正试图完成这一谦逊的挑战。挑战的细节在这里。
房间里有N根绳子和N个重量。每根绳子都与一个重量相连(只在一端),而且每根绳子都有一个特殊的耐用性−,这是它可以挂起的最大重量。还有一个挂钩,挂在天花板上。这些绳子可以在没有重量的情况下绑在钩子上。这些绳索也可以连接到其他重量上,也就是说,绳子和重物可以在一条链子中相互连接。如果与绳子直接或间接连接的重量之和大于其耐久性,绳子就会断裂。 我们知道我们想要附加N根绳子的顺序。更准确地说,我们知道绳子的参数(耐用性和重量)和每个附件的位置。在三个长度为N的零索引数组A、B、C中给出了耐久度、权重和位置。对于每一个i (0≤iA= 4,3,1 B= 2,2,1 C= -1,0,1 函数应该返回2,就好像我们附加了第三根绳子,那么一根绳子就会断掉,因为重量之和大于它的耐久性(2 +2+1=5和5> 4)。
下面是我尝试的解决方案。我有一个名为add_weights的助手函数,它返回True,如果添加最新的绳子不会导致任何其他绳子断裂,否则会出错。
def add_weights(A, ancestors, weights, weight, rope):
#add weight(int) to rope and ancestors
if (rope == -1):
return (True)
else:
weights[rope] += weight
if (A[rope] < weights[rope]):
print "Rope that breaks", rope
return False
ancestor = ancestors[rope]
print ancestor
add_weights(A, ancestors, weights, weight, ancestor)
def solution(A, B, C):
# write your code in Python 2.7
weights = {}
ancestors = {}
for i in range(len(B)):
weights[i] = B[i]
for i in range(len(C)):
#attaching rope i to rope x
x = C[i]
ancestors[i] = x
broke = add_weights(A, ancestors, weights, B[i], x)
if (not broke):
return i
return len(C)问题是在函数解决方案中的for循环的第二次迭代中(当我尝试添加rope 1时),当我可以清楚地看到add_weights返回True时,变量中断会以某种方式计算为零。我也用调试器对它进行了测试,所以我不完全确定发生了什么。欢迎任何帮助。
发布于 2014-12-30 07:26:46
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercise5_DurablityofRopes
{
class Program
{
static int[] A_Durability = new int[] { 15, 6, 2,3,1 };
static int[] B_Weight = new int[] { 2, 1, 2,3,1 };
static int[] C_Position = new int[] { -1, 0, 1 ,2,3};
static int no_of_ropes_attached = 0;
static int maxropeattached = 0;
static void Main(string[] args)
{
// first position cannot necessarily be -1 hence Checking for each position How many maximum ropes can be attached
for (int i = 0; i <= C_Position.Length - 1; i++)
{
int[] Copy_Durability = new int[A_Durability.Length];
for (int l = 0; l <= C_Position.Length - 1; l++)
{
Copy_Durability[l] = A_Durability[l];
}
AddNextRope(i, B_Weight[i], Copy_Durability);
Console.WriteLine("Total Number of ropes can be attached to " + C_Position[i] + " ropes are" + no_of_ropes_attached);
if (no_of_ropes_attached>=maxropeattached)
{
maxropeattached = no_of_ropes_attached;
}
no_of_ropes_attached = 0;
}
Console.WriteLine("Total Number of ropes can be attached is " + maxropeattached);
Console.ReadKey();
}
private static void AddNextRope(int currentRopePosition,int newWeight,int[] Durability)
{
if (currentRopePosition == C_Position.Length - 1) return;
// decrease same amount of weight from all ansestors from their durability and check if any of them breaks (durability <0) with new weight added
for (int k = currentRopePosition; k != 0; k--)
{
Durability[k] = Durability[k] - newWeight;
if(Durability[k]<0)
{
return;
}
}
no_of_ropes_attached = no_of_ropes_attached + 1;
for (int i = 0; i <= C_Position.Length - 1; i++)
{
if (C_Position[i] == C_Position[currentRopePosition] + 1)
{
if (A_Durability[i] > B_Weight[i])
{
AddNextRope(i, B_Weight[i], Durability);
}
else
{
return;
}
}
}
}
}
}发布于 2015-04-21 13:03:25
这是我的第一个解决方案O(N**2),它简单地运行锻造绳索和减轻每个连接的重量。
def solution(A, B, C):
num_ropes = len(C)
for i in range(num_ropes):
node = i
while(node != -1):
A[node] -= B[i]
if A[node] < 0:
return i
node = C[node]
return num_ropes这是O(N*log(N))的解决方案,其原理是,它不是去处理所有的绳子,而是使用具有一种张力的变形组。
def solution(A, B, C):
num_ropes = len(C)
for i in range(num_ropes):
A[i] -= B[i]
if A[i] < 0:
return i
pre_node = C[i]
while pre_node != -1:
A[pre_node] -= B[i]
if A[pre_node] < 0:
return i
if A[pre_node] <= A[i]:
C[i] = pre_node
A[i] = A[pre_node]
pre_node = C[pre_node]
return num_ropes它可以使用最后一部分或在https://app.codility.com/programmers/challenges/sulphur2014/上进行测试。
if __name__ == '__main__':
A = [5, 3, 6, 3, 3]
B = [2, 3, 1, 1, 2]
C = [-1, 0, -1, 0, 3]
assert solution(A, B, C) == 3
A = [4, 3, 1]
B = [2, 2, 1]
C = [-1, 0, 1]
assert solution(A, B, C) == 2发布于 2016-05-14 19:51:59
我没有时间将其转换为Python,但是通过查看这些JS代码,您可以有一个想法:
function keysOrderedByValues(array) {
var result = [];
for (var i = 0; i < array.length; i++) {
result[array[i] + 1] = i;
}
return result;
}
function findFirstPositiveElem(array) {
for (var i = 0; i < array.length; i++) {
if (array[i]>0) {
return i;
}
}
return null;
}
function accumulativeArray(array) {
var result = [];
var sum = 0;
for (var i = 0; i < array.length; i++) {
sum = sum + array[i];
result.push(sum);
}
return result;
}
Array.prototype.indexedByArray = function(array) {
result = [];
for (var i = 0; i < array.length; i++) {
result.push(this[array[i]])
}
return result;
}
function test(ds, ws, ords) {
return findFirstPositiveElem(ws.indexedByArray(keysOrderedByValues(ords)), ds);
}
console.log(test([4,3,1], [2,2,1], [-1,0,1]))https://stackoverflow.com/questions/26560868
复制相似问题