我正在做一个我无法解决的练习.我需要通过买卖比特币来获得最大的累积利润.我有一个函数(A,Y),它接收一个A =不同价格的数组,在时间和Y =费用限制:
注意:如果比特币在0买入并在1卖出,我们将失去A [1] - A [0] = 7050 -7200 - Y = -200.所以,没有做出这种运动.
注意2:您当时只能拥有1个比特币.要卖,你必须先买.要购买,您需要先购买或出售.
注3:运动需要时间结果.你不能在A [5]购买并在A [4]出售
注4:如果无法获利,则应返回0
复杂度是O(N)
A = [7200,7050,7300,7500,7440,7200,7300,7280,7400] //expected result 550
Y = 50
A[3] - A[1] - Y = 7500 - 7050 - 50 = 400
A[8] - A[5] - Y = 7400 - 7200 - 50 = 150
result = 550 //maximum accumulated profit
Run Code Online (Sandbox Code Playgroud)
这就是我所拥有的
function solution(A, Y) {
if(A.length < 2) {
return 0;
}
var minIndex = (A[0] > A[1]) …Run Code Online (Sandbox Code Playgroud) 我正在做一些编码练习,但我无法解决这个问题.
求出给定整数的所有除数之和.对于n = 12,输入应为sumOfDivisors(n)= 28.
例如:1 + 2 + 3 + 4 + 6 + 12 = 28.
约束条件:1≤n≤15.
我怎么能解决这个练习?我无法做到.
function(n){
var arr = [],
finalSum;
if(n <= 1 || n => 16){
return false ;
}
for(var i = 0; i < n; i++){
var tmp= n/2;
arr.push(tmp)
// i need to keep on dividing n but i can't get the way of how to
}
return finalSum;
}
Run Code Online (Sandbox Code Playgroud) 我收到了一个由按钮 type="submit" 提交的表单,当我单击它时,它会启动前端验证,但我也有一些与 ajax 相关的服务器验证。例如,另一个人没有使用它的名字。
我的问题是,如果我更改为 button type="button" 前端验证不会执行。只有 ajax 验证才能做到。我能做什么?P/D:我使用类似于 jquery 的 tiny.js 来处理 js 事件。
html
<form>
<input name="name" class="name">
<button type="submit" class="name__valid" value="save">save</button>
<form>
Run Code Online (Sandbox Code Playgroud)
js
tiny.ajax("/update",
{
method: "PUT",
data: {
name: document.querySelector('name__valid').value;
},
dataType: "json",
success: function (data) {
alert('available name');
window.location = "/home?name-update=success"
},
error: function (error) {
//example: used name error
var errorJSON = JSON.parse(error.response);
console.log('Messages ' + errorJSON.messages);
}
}
);
//this validation only it's executed by de button (type submit)
var name …Run Code Online (Sandbox Code Playgroud)