Skip to content

Commit eee204f

Browse files
committed
Add solution #2606
1 parent 871601b commit eee204f

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,6 +1939,7 @@
19391939
2600|[K Items With the Maximum Sum](./solutions/2600-k-items-with-the-maximum-sum.js)|Easy|
19401940
2601|[Prime Subtraction Operation](./solutions/2601-prime-subtraction-operation.js)|Medium|
19411941
2605|[Form Smallest Number From Two Digit Arrays](./solutions/2605-form-smallest-number-from-two-digit-arrays.js)|Easy|
1942+
2606|[Find the Substring With Maximum Cost](./solutions/2606-find-the-substring-with-maximum-cost.js)|Medium|
19421943
2615|[Sum of Distances](./solutions/2615-sum-of-distances.js)|Medium|
19431944
2618|[Check if Object Instance of Class](./solutions/2618-check-if-object-instance-of-class.js)|Medium|
19441945
2619|[Array Prototype Last](./solutions/2619-array-prototype-last.js)|Easy|
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 2606. Find the Substring With Maximum Cost
3+
* https://leetcode.com/problems/find-the-substring-with-maximum-cost/
4+
* Difficulty: Medium
5+
*
6+
* You are given a string s, a string chars of distinct characters and an integer array vals of
7+
* the same length as chars.
8+
*
9+
* The cost of the substring is the sum of the values of each character in the substring. The
10+
* cost of an empty string is considered 0.
11+
*
12+
* The value of the character is defined in the following way:
13+
* - If the character is not in the string chars, then its value is its corresponding position
14+
* (1-indexed) in the alphabet.
15+
* - For example, the value of 'a' is 1, the value of 'b' is 2, and so on. The value of 'z' is 26.
16+
* - Otherwise, assuming i is the index where the character occurs in the string chars, then its
17+
* value is vals[i].
18+
*
19+
* Return the maximum cost among all substrings of the string s.
20+
*/
21+
22+
/**
23+
* @param {string} s
24+
* @param {string} chars
25+
* @param {number[]} vals
26+
* @return {number}
27+
*/
28+
var maximumCostSubstring = function(s, chars, vals) {
29+
const charValues = new Array(26).fill().map((_, i) => i + 1);
30+
for (let i = 0; i < chars.length; i++) {
31+
charValues[chars.charCodeAt(i) - 97] = vals[i];
32+
}
33+
34+
let result = 0;
35+
let currentCost = 0;
36+
for (const char of s) {
37+
currentCost = Math.max(0, currentCost + charValues[char.charCodeAt(0) - 97]);
38+
result = Math.max(result, currentCost);
39+
}
40+
41+
return result;
42+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy