我目前正在处理CodeFights Arcade的CodeChange级别。这是目标:
您将获得一个整数数组。在每一步中你都被允许
准确地增加其中一个元素。找到最小数量的
从输入中获得严格增加的序列所需的移动。
实施例
对于> org.cytoscape.app.internal.exception.AppStartupException: App constructor threw exception
at org.cytoscape.app.internal.manager.SimpleApp.start(SimpleApp.java:187)
at org.cytoscape.app.internal.manager.AppManager$1.onFileCreate(AppManager.java:502)
at org.apache.commons.io.monitor.FileAlterationObserver.doCreate(FileAlterationObserver.java:379)
at org.apache.commons.io.monitor.FileAlterationObserver.checkAndNotify(FileAlterationObserver.java:345)
at org.apache.commons.io.monitor.FileAlterationObserver.checkAndNotify(FileAlterationObserver.java:304)
at org.apache.commons.io.monitor.FileAlterationMonitor.run(FileAlterationMonitor.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.cytoscape.app.internal.manager.SimpleApp.start(SimpleApp.java:175)
... 6 more
Caused by: java.lang.ExceptionInInitializerError
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:181)
at Action.Producer.<init>(Producer.java:30)
at App.MyControlPanel.<init>(MyControlPanel.java:130)
at App.CytoVisProject.<init>(CytoVisProject.java:29)
... 11 more
Caused by: org.apache.kafka.common.config.ConfigException: Invalid value org.apache.kafka.clients.producer.internals.DefaultPartitioner for configuration partitioner.class: Class org.apache.kafka.clients.producer.internals.DefaultPartitioner could not be found.
at org.apache.kafka.common.config.ConfigDef.parseType(ConfigDef.java:255)
at org.apache.kafka.common.config.ConfigDef.define(ConfigDef.java:78)
at org.apache.kafka.common.config.ConfigDef.define(ConfigDef.java:94)
at org.apache.kafka.clients.producer.ProducerConfig.<clinit>(ProducerConfig.java:206)
... 15 more
org.cytoscape.app.internal.exception.AppStartupException: App constructor threw exception
at org.cytoscape.app.internal.manager.SimpleApp.start(SimpleApp.java:187)
at org.cytoscape.app.internal.manager.AppManager$1.onFileCreate(AppManager.java:502)
at org.apache.commons.io.monitor.FileAlterationObserver.doCreate(FileAlterationObserver.java:379)
at org.apache.commons.io.monitor.FileAlterationObserver.checkAndNotify(FileAlterationObserver.java:345)
at org.apache.commons.io.monitor.FileAlterationObserver.checkAndNotify(FileAlterationObserver.java:304)
at org.cytoscape.app.internal.manager.AppManager.checkForFileChanges(AppManager.java:781)
at org.cytoscape.app.internal.manager.AppManager.installApp(AppManager.java:870)
at org.cytoscape.app.internal.task.InstallAppsTask.run(InstallAppsTask.java:32)
at org.cytoscape.work.internal.task.JDialogTaskManager$TaskRunnable.innerRun(JDialogTaskManager.java:342)
at org.cytoscape.work.internal.task.JDialogTaskManager$TaskRunnable.run(JDialogTaskManager.java:355)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.cytoscape.app.internal.manager.SimpleApp.start(SimpleApp.java:175)
... 14 more
Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.apache.kafka.clients.producer.ProducerConfig
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:181)
at Action.Producer.<init>(Producer.java:30)
at App.MyControlPanel.<init>(MyControlPanel.java:130)
at App.CytoVisProject.<init>(CytoVisProject.java:29)
... 19 more
,输出应为
inputArray = [1, 1, 1]
。
我在Python 3中编写了这段代码:
arrayChange(inputArray) = 3
它通过了所有非隐藏测试但未通过第7次隐藏测试。有人可以帮我弄清楚我的代码有什么错误吗?非常感谢你!
答案 0 :(得分:1)
问题在于:
elif sorted(inputArray) == inputArray: return 0
以此数组为例:[1, 1, 2]
。它是相同的排序。但是,输出应为2:[1, 1 + 1, 2 + 1]
。您应该能够删除该条件以传递所有情况。
更简单的说明:
您的代码可以简化为:
def arrayChange(inputArray):
sum = 0
q = inputArray[0]
for i in inputArray[1:]:
if i <= q:
sum += q-i+1
q = q+1
else:
q = i
return sum
答案 1 :(得分:0)
您没有提到错误,我认为它失败了,因为执行超出了时间限制。看起来代码花费时间特别是在排序和枚举函数与大型数组一起使用时。记住测试用例检查所有边缘情况。一个包括3≤inputArray.length≤10^ 5和-10 ^5≤inputArray[i]≤10^ 5.
这是一个简单的解决方案,它通过了所有案例。
def arrayChange(inputArray):
cnt,temp = 0,0
while len(inputArray) > 1:
if inputArray[0] >= inputArray[1]:
temp = (inputArray[0]-inputArray[1])+1
inputArray[1]=temp+inputArray[1]
cnt=cnt+temp
inputArray.remove(inputArray[0])
return cnt
答案 2 :(得分:0)
perl -ne ' s/\.//g; s/ //; print ' names.txt
答案 3 :(得分:0)
Java 解决方案:
comp_point
答案 4 :(得分:0)
通过所有测试用例的 Python 3 更简单的解决方案
def arrayChange(a):
count, i = 0, 0
original = a.copy()
while i < len(a):
if i != len(a) - 1 and a[i] >= a[i+1]:
count += abs(a[i] - a[i+1]) + 1
a[i+1] += a[i] - a[i+1] + 1
i += 1
return count
答案 5 :(得分:-1)
简洁的Python解决方案
def arrayChange(inputArray):
a = 0
for i in range(1, len(inputArray)):
if inputArray[i] <= inputArray[i - 1]:
f = (inputArray[i - 1] - inputArray[i]) + 1
inputArray[i] = inputArray[i - 1] + 1
a += f
return a