Skip to content

Commit df93db3

Browse files
committed
add 3573 & 1859
1 parent 729401d commit df93db3

3 files changed

Lines changed: 67 additions & 37 deletions

File tree

javascript/LeetCode/Array/3573.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 3573. Best Time to Buy and Sell Stock V
3+
*
4+
* 正常交易:i天買,j天賣(i < j),利潤:proces[j] - prices[i]
5+
* 炒短線:i天賣,j天買回來(i < j),利潤:prices[i] - prices[j]
6+
* 不能同一天買賣單張股票
7+
* 回傳在交易k次後最多能賺多少
8+
*
9+
* @param {number[]} prices 第i天的股價
10+
* @param {number} k 最多只能交易k次
11+
* @return {number}
12+
*/
13+
var maximumProfit = function(prices, k) {
14+
const firstPrice = prices[0];
15+
const dp = Array(k + 1).fill(null).map(() => ({
16+
maxProfit: 0,
17+
buyHold: -firstPrice,
18+
sellHold: firstPrice
19+
}));
20+
for(let i = 0;i < prices.length;++i) {
21+
const current = prices[i];
22+
for(let j = k;j > 0;--j) {
23+
const prevProfit = dp[j - 1].maxProfit;
24+
dp[j].maxProfit = Math.max(dp[j].maxProfit, dp[j].buyHold + current, dp[j].sellHold - current);
25+
dp[j].buyHold = Math.max(dp[j].buyHold, prevProfit - current);
26+
dp[j].sellHold = Math.max(dp[j].sellHold, prevProfit + current);
27+
}
28+
}
29+
return dp[k].maxProfit;
30+
};
31+
let prices = [1,7,9,8,2], k = 2;
32+
// 14
33+
// can make $14 of profit through 2 transactions:
34+
// A normal transaction: buy the stock on day 0 for $1 then sell it on day 2 for $9.
35+
// A short selling transaction: sell the stock on day 3 for $8 then buy back on day 4 for $2.
36+
// 9 - 1 = 8 ; 8 - 2 = 6; 8 + 6 = 14
37+
// console.log(maximumProfit(prices,k))

javascript/LeetCode/String/1859.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 1859. Sorting the Sentence
3+
*
4+
* 字串句子中每個單字最後面都會有個數字,利用該數字重新將句子做排序,但結果每個單字不能有數字。
5+
*
6+
* @param {string} s
7+
* @return {string}
8+
*/
9+
var sortSentence = function(s) {
10+
let res = "";
11+
let splitS = s.split(" ");
12+
let map = new Map();
13+
for(let i = 0;i < splitS.length;++i){
14+
// 取得每個字的最後一個數字,並把它放入new map => key = int,value = str
15+
const lastInt = parseInt(splitS[i].substring(splitS[i].length - 1));
16+
map.set(lastInt,splitS[i].substring(0,splitS[i].length - 1));
17+
}
18+
// sort map
19+
var mapAsc = new Map([...map.entries()].sort());
20+
for(const [key,value] of mapAsc) {
21+
res += " "+value;
22+
}
23+
return res.trim()
24+
};
25+
let s = "is2 sentence4 This1 a3";
26+
// Output: "This is a sentence"
27+
// Explanation: Sort the words in s to their original positions "This1 is2 a3 sentence4", then remove the numbers.
28+
console.log(sortSentence(s));

javascript/index.js

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,40 +1176,5 @@ let nums = [8,4,2,8,4];
11761176
// console.log(specialTriplets(nums));
11771177

11781178

1179-
/**
1180-
* 3573. Best Time to Buy and Sell Stock V
1181-
*
1182-
* 正常交易:i天買,j天賣(i < j),利潤:proces[j] - prices[i]
1183-
* 炒短線:i天賣,j天買回來(i < j),利潤:prices[i] - prices[j]
1184-
* 不能同一天買賣單張股票
1185-
* 回傳在交易k次後最多能賺多少
1186-
*
1187-
* @param {number[]} prices 第i天的股價
1188-
* @param {number} k 最多只能交易k次
1189-
* @return {number}
1190-
*/
1191-
var maximumProfit = function(prices, k) {
1192-
const firstPrice = prices[0];
1193-
const dp = Array(k + 1).fill(null).map(() => ({
1194-
maxProfit: 0,
1195-
buyHold: -firstPrice,
1196-
sellHold: firstPrice
1197-
}));
1198-
for(let i = 0;i < prices.length;++i) {
1199-
const current = prices[i];
1200-
for(let j = k;j > 0;--j) {
1201-
const prevProfit = dp[j - 1].maxProfit;
1202-
dp[j].maxProfit = Math.max(dp[j].maxProfit, dp[j].buyHold + current, dp[j].sellHold - current);
1203-
dp[j].buyHold = Math.max(dp[j].buyHold, prevProfit - current);
1204-
dp[j].sellHold = Math.max(dp[j].sellHold, prevProfit + current);
1205-
}
1206-
}
1207-
return dp[k].maxProfit;
1208-
};
1209-
let prices = [1,7,9,8,2], k = 2;
1210-
// 14
1211-
// can make $14 of profit through 2 transactions:
1212-
// A normal transaction: buy the stock on day 0 for $1 then sell it on day 2 for $9.
1213-
// A short selling transaction: sell the stock on day 3 for $8 then buy back on day 4 for $2.
1214-
// 9 - 1 = 8 ; 8 - 2 = 6; 8 + 6 = 14
1215-
console.log(maximumProfit(prices,k))
1179+
1180+

0 commit comments

Comments
 (0)