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

Commit 634991b

Browse files
committed
Add solution #2375
1 parent 1c4b104 commit 634991b

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 2375. Construct Smallest Number From DI String
3+
* https://leetcode.com/problems/construct-smallest-number-from-di-string/
4+
* Difficulty: Medium
5+
*
6+
* You are given a 0-indexed string pattern of length n consisting of the characters
7+
* 'I' meaning increasing and 'D' meaning decreasing.
8+
*
9+
* A 0-indexed string num of length n + 1 is created using the following conditions:
10+
* - num consists of the digits '1' to '9', where each digit is used at most once.
11+
* - If pattern[i] == 'I', then num[i] < num[i + 1].
12+
* - If pattern[i] == 'D', then num[i] > num[i + 1].
13+
*
14+
* Return the lexicographically smallest possible string num that meets the conditions.
15+
*/
16+
17+
/**
18+
* @param {string} pattern
19+
* @return {string}
20+
*/
21+
var smallestNumber = function(pattern) {
22+
let result = '';
23+
24+
for (let i = 0, stack = []; i <= pattern.length; i++) {
25+
stack.push(i + 1);
26+
if (i === pattern.length || pattern[i] === 'I') {
27+
while (stack.length > 0) {
28+
result += stack.pop();
29+
}
30+
}
31+
}
32+
33+
return result;
34+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@
553553
2349|[Design a Number Container System](./2349-design-a-number-container-system.js)|Medium|
554554
2352|[Equal Row and Column Pairs](./2352-equal-row-and-column-pairs.js)|Medium|
555555
2364|[Count Number of Bad Pairs](./2364-count-number-of-bad-pairs.js)|Medium|
556+
2375|[Construct Smallest Number From DI String](./2375-construct-smallest-number-from-di-string.js)|Medium|
556557
2390|[Removing Stars From a String](./2390-removing-stars-from-a-string.js)|Medium|
557558
2396|[Strictly Palindromic Number](./2396-strictly-palindromic-number.js)|Medium|
558559
2413|[Smallest Even Multiple](./2413-smallest-even-multiple.js)|Easy|

0 commit comments

Comments
 (0)