小编Pra*_*wal的帖子

要执行的最大任务数

我陷入了一个问题。我知道dp可以在这里应用,但没有得到它。

0考虑从 开始并结束于 的正数轴的一部分10^9。您从 开始0,有 N 个任务可以执行。

ith任务已完成l[i]并且需要t[i]时间来执行。要执行ith任务,您必须到达该地点l[i]t[i]在该地点花费时间。

在路径上行进一个单位需要一秒钟,即从 1 到 3 需要 (3 - 1) = 2 秒。

你有 T 秒的时间,在这段时间里你必须执行尽可能多的任务并返回到起始位置。我需要找到可以在时间 T 内执行的最大值。

例子

考虑 M = 3、T = 10、l[] = [1, 2] 和 t[] = [3, 2]。

如果我们执行第一个任务,则消耗的总时间为 1(旅行)+ 3(完成任务)= 4。剩余时间为 10 - 4 = 6。

现在,如果我们连续执行第二个任务,则所需的总时间为 1(从 1 出发)+ 2(完成任务)= 3。剩余时间为 6 - 3 = 3。

现在如果我们从2返回到0。总共花费的时间是2。剩余时间是3 - 2 = …

java arrays algorithm dynamic-programming data-structures

5
推荐指数
1
解决办法
1万
查看次数

threadLocal 中的“withInitial”与“InitialValue”

我对threadLocal的initialValuewithInital方法有点困惑。

考虑一种情况,我在父线程中有数据,并且我正在使用InheritableThreadLocal.

public class Parent extends Thread {
public static ThreadLocal<String> data = new InheritableThreadLocal<String>() {
    @Override
    protected String initialValue() {
        return "temp";
    }
};

public static void main(String[] args) {
    new Parent().start();
}

public void run() {
    data.set("parent data");
    System.out.println("Parent Thread Value :" + data.get());
    new ChildThread().start();
}

class ChildThread extends Thread {
    public void run() {
        System.out.println("Child Thread Value :" + Parent.data.get());
    }
}
}
Run Code Online (Sandbox Code Playgroud)

输出:

Parent Thread Value : parent data …
Run Code Online (Sandbox Code Playgroud)

java multithreading thread-local java-8 inheritable-thread-local

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