Skip to content

Commit 4ac00a2

Browse files
committed
🚀 20-Sep-2020
1 parent eaacec0 commit 4ac00a2

File tree

4 files changed

+200
-0
lines changed

4 files changed

+200
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
- [Codeforces](competitive%20programming/codeforces)
2424

25+
- [CSES Problems](competitive%20programming/cses)
26+
2527
- [Leetcode](competitive%20programming/leetcode)
2628

2729
- [2020 June Challenge](competitive%20programming/leetcode/2020-June-Challenge)
@@ -30,6 +32,8 @@
3032

3133
- [2020 August Challenge](competitive%20programming/leetcode/2020-August-Challenge)
3234

35+
- [2020 September Challenge](competitive%20programming/leetcode/2020-September-Challenge)
36+
3337
* [Dynamic Programming](dynamic%20programming)
3438

3539
* [Graph](graph)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
An integer has sequential digits if and only if each digit in the number is one more than the previous digit.
2+
3+
Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.
4+
5+
6+
7+
Example 1:
8+
9+
Input: low = 100, high = 300
10+
Output: [123,234]
11+
Example 2:
12+
13+
Input: low = 1000, high = 13000
14+
Output: [1234,2345,3456,4567,5678,6789,12345]
15+
16+
17+
Constraints:
18+
19+
10 <= low <= high <= 10^9
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
class Solution {
31+
public:
32+
vector<int> sequentialDigits(int low, int high) {
33+
vector<int> res;
34+
queue<int> q;
35+
for(int i=1;i<=9;i++)
36+
q.push(i);
37+
38+
while(!q.empty()){
39+
int num=q.front();
40+
q.pop();
41+
if(num>=low && num<=high)
42+
res.push_back(num);
43+
if(num>high) break;
44+
int d=num%10;
45+
if(d<9){
46+
q.push((num*10)+d+1);
47+
}
48+
}
49+
return res;
50+
}
51+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
You are given a string text of words that are placed among some number of spaces. Each word consists of one or more lowercase English letters and are separated by at least one space. It's guaranteed that text contains at least one word.
2+
3+
Rearrange the spaces so that there is an equal number of spaces between every pair of adjacent words and that number is maximized. If you cannot redistribute all the spaces equally, place the extra spaces at the end, meaning the returned string should be the same length as text.
4+
5+
Return the string after rearranging the spaces.
6+
7+
8+
9+
Example 1:
10+
11+
Input: text = " this is a sentence "
12+
Output: "this is a sentence"
13+
Explanation: There are a total of 9 spaces and 4 words. We can evenly divide the 9 spaces between the words: 9 / (4-1) = 3 spaces.
14+
Example 2:
15+
16+
Input: text = " practice makes perfect"
17+
Output: "practice makes perfect "
18+
Explanation: There are a total of 7 spaces and 3 words. 7 / (3-1) = 3 spaces plus 1 extra space. We place this extra space at the end of the string.
19+
Example 3:
20+
21+
Input: text = "hello world"
22+
Output: "hello world"
23+
Example 4:
24+
25+
Input: text = " walks udp package into bar a"
26+
Output: "walks udp package into bar a "
27+
Example 5:
28+
29+
Input: text = "a"
30+
Output: "a"
31+
32+
33+
Constraints:
34+
35+
1 <= text.length <= 100
36+
text consists of lowercase English letters and ' '.
37+
text contains at least one word.
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
class Solution {
49+
public:
50+
string reorderSpaces(string text) {
51+
int n=text.length();
52+
53+
int sp=0, word=0;;
54+
55+
for(int i=0;i<n;i++){
56+
if(text[i]==' ')
57+
sp++;
58+
}
59+
60+
string res, tmp;
61+
stringstream ss(text);
62+
while(ss>>tmp){
63+
word++;
64+
}
65+
66+
if(word==1){
67+
if(sp==0) return text;
68+
res+=tmp;
69+
res.append(sp, ' ');
70+
return res;
71+
}
72+
73+
int spa=sp/(word-1);
74+
75+
stringstream s(text);
76+
77+
tmp="";
78+
res="";
79+
80+
while(s>>tmp){
81+
res+=tmp;
82+
res.append(spa, ' ');
83+
}
84+
85+
res=res.substr(0, res.length()-spa);
86+
87+
if(sp%(word-1)!=0){
88+
res.append(sp%(word-1), ' ');
89+
}
90+
91+
return res;
92+
}
93+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
An integer has sequential digits if and only if each digit in the number is one more than the previous digit.
3+
4+
Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.
5+
6+
7+
8+
Example 1:
9+
10+
Input: low = 100, high = 300
11+
Output: [123,234]
12+
Example 2:
13+
14+
Input: low = 1000, high = 13000
15+
Output: [1234,2345,3456,4567,5678,6789,12345]
16+
17+
18+
Constraints:
19+
20+
10 <= low <= high <= 10^9
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
class Solution {
32+
public:
33+
vector<int> sequentialDigits(int low, int high) {
34+
vector<int> res;
35+
queue<int> q;
36+
for(int i=1;i<=9;i++)
37+
q.push(i);
38+
39+
while(!q.empty()){
40+
int num=q.front();
41+
q.pop();
42+
if(num>=low && num<=high)
43+
res.push_back(num);
44+
if(num>high) break;
45+
int d=num%10;
46+
if(d<9){
47+
q.push((num*10)+d+1);
48+
}
49+
}
50+
return res;
51+
}
52+
};

0 commit comments

Comments
 (0)