Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 029a8f6

Browse files
committed
Add solution #1663
1 parent 8f1ba03 commit 029a8f6

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,458 LeetCode solutions in JavaScript
1+
# 1,459 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1281,6 +1281,7 @@
12811281
1658|[Minimum Operations to Reduce X to Zero](./solutions/1658-minimum-operations-to-reduce-x-to-zero.js)|Medium|
12821282
1659|[Maximize Grid Happiness](./solutions/1659-maximize-grid-happiness.js)|Hard|
12831283
1662|[Check If Two String Arrays are Equivalent](./solutions/1662-check-if-two-string-arrays-are-equivalent.js)|Easy|
1284+
1663|[Smallest String With A Given Numeric Value](./solutions/1663-smallest-string-with-a-given-numeric-value.js)|Medium|
12841285
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12851286
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
12861287
1672|[Richest Customer Wealth](./solutions/1672-richest-customer-wealth.js)|Easy|
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 1663. Smallest String With A Given Numeric Value
3+
* https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/
4+
* Difficulty: Medium
5+
*
6+
* The numeric value of a lowercase character is defined as its position (1-indexed) in the
7+
* alphabet, so the numeric value of a is 1, the numeric value of b is 2, the numeric value
8+
* of c is 3, and so on.
9+
*
10+
* The numeric value of a string consisting of lowercase characters is defined as the sum of
11+
* its characters' numeric values. For example, the numeric value of the string "abe" is equal
12+
* to 1 + 2 + 5 = 8.
13+
*
14+
* You are given two integers n and k. Return the lexicographically smallest string with length
15+
* equal to n and numeric value equal to k.
16+
*
17+
* Note that a string x is lexicographically smaller than string y if x comes before y in
18+
* dictionary order, that is, either x is a prefix of y, or if i is the first position
19+
* such that x[i] != y[i], then x[i] comes before y[i] in alphabetic order.
20+
*/
21+
22+
/**
23+
* @param {number} n
24+
* @param {number} k
25+
* @return {string}
26+
*/
27+
var getSmallestString = function(n, k) {
28+
const result = new Array(n).fill('a');
29+
let remainingValue = k - n;
30+
31+
for (let i = n - 1; i >= 0 && remainingValue > 0; i--) {
32+
const addValue = Math.min(25, remainingValue);
33+
result[i] = String.fromCharCode(97 + addValue);
34+
remainingValue -= addValue;
35+
}
36+
37+
return result.join('');
38+
};

0 commit comments

Comments
 (0)