Skip to content

Commit daf3336

Browse files
Update
1 parent f446b35 commit daf3336

File tree

189 files changed

+64
-156
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

189 files changed

+64
-156
lines changed

1962-remove-stones-to-minimize-the-total/1962-remove-stones-to-minimize-the-total.cpp

Lines changed: 0 additions & 14 deletions
This file was deleted.

2129-capitalize-the-title/2129-capitalize-the-title.cpp

Lines changed: 0 additions & 36 deletions
This file was deleted.

2515-shortest-distance-to-target-string-in-a-circular-array/NOTES.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

2520-count-the-digits-that-divide-a-number/NOTES.md

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
int main() {
3+
int leftmost = -1;
4+
int left = 0, right = nums.length - 1;
5+
while (left <= right) {
6+
mid = (left + right) / 2;
7+
if (nums[mid] < target) {
8+
left = mid + 1;
9+
} else if (nums[mid] > target) {
10+
right = mid - 1;
11+
} else {
12+
// binary search for leftmost index
13+
leftmost = mid;
14+
right = mid - 1;
15+
}
16+
}
17+
18+
left = 0; right = nums.length - 1;
19+
int rightmost = -1;
20+
while (left <= right) {
21+
mid = (left + right) / 2;
22+
if (nums[mid] < target) {
23+
left = mid + 1;
24+
} else if (nums[mid] > target) {
25+
right = mid - 1;
26+
} else {
27+
// binary search for rightmost index
28+
rightmost = mid;
29+
left = mid + 1;
30+
}
31+
}
32+
}

code/algorithms/union-find.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ class UnionFind {
1212
int rootY = find(y);
1313
if (rootX != rootY)
1414
for (int i = 0; i < root.size(); i++)
15-
if (root[i] == rootY) root[i] = rootX;
15+
if (root[i] == rootY)
16+
root[i] = rootX;
1617
}
1718

1819
bool connected(int x, int y) {

leetcode/1962-remove-stones-to-minimize-the-total/1962-remove-stones-to-minimize-the-total.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class Solution {
33
int minStoneSum(vector<int>& piles, int k) {
44
long long int sum = accumulate(piles.begin(), piles.end(), 0);
55
priority_queue<int> pq(piles.begin(), piles.end());
6-
while(k--) {
6+
while (k--) {
77
auto top = pq.top(); pq.pop();
88
auto substract = top >> 1;
99
sum -= substract;

leetcode/1962-remove-stones-to-minimize-the-total/README.md

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
1+
// class Solution {
2+
// public:
3+
// string capitalizeTitle(string title) {
4+
// stringstream ss(title);
5+
// string word;
6+
// string res = "";
7+
// while(ss >> word) {
8+
// bool first = word.size() >= 3 ? true: false;
9+
// for (auto &c: word) {
10+
// if (first) {
11+
// c = toupper(c);
12+
// first = false;
13+
// } else c = tolower(c);
14+
// }
15+
// res+= word;
16+
// res+= ' ';
17+
// }
18+
// res.pop_back();
19+
// return res;
20+
// }
21+
// };
22+
123
class Solution {
224
public:
325
string capitalizeTitle(string title) {
4-
stringstream ss(title);
5-
string word;
6-
string res = "";
7-
while(ss >> word) {
8-
bool first = word.size() >= 3 ? true: false;
9-
for (auto &c: word) {
10-
if (first) {
11-
c = toupper(c);
12-
first = false;
13-
} else c = tolower(c);
14-
}
15-
res+= word;
16-
res+= ' ';
26+
stringstream ss(title); string res = "";
27+
auto shouldCap = [](string& s) { return s.size() > 2; };
28+
for (string word; ss >> word;) {
29+
transform(word.begin(), word.end(), word.begin(), ::tolower);
30+
if (shouldCap(word)) word[0] = toupper(word[0]);
31+
res += word; res += " ";
1732
}
18-
res.pop_back();
33+
if (res.size()) res.pop_back();
1934
return res;
2035
}
2136
};

leetcode/2129-capitalize-the-title/README.md

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)