Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commiteaf4941

Browse files
add 1781
1 parentc805826 commiteaf4941

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1781|[Sum of Beauty of All Substrings](https://leetcode.com/problems/sum-of-beauty-of-all-substrings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1781.java)||Medium|HashTable, String|
1112
|1780|[Check if Number is a Sum of Powers of Three](https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1780.java)||Medium|Math, Backtracking, Recursion|
1213
|1779|[Find Nearest Point That Has the Same X or Y Coordinate](https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1779.java)||Easy|Array|
1314
|1774|[Closest Dessert Cost](https://leetcode.com/problems/closest-dessert-cost/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1774.java)||Medium|Greedy|
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
packagecom.fishercoder.solutions;
2+
3+
publicclass_1781 {
4+
publicstaticclassSolution1 {
5+
/**
6+
* credit: https://leetcode.com/problems/sum-of-beauty-of-all-substrings/discuss/1096380/Java-or-T%3A-O(N2)-or-S%3A-O(1)-Get-the-beauty-of-all-substrings-and-sum-them
7+
*/
8+
publicintbeautySum(Strings) {
9+
intsum =0;
10+
for (inti =0;i <s.length();i++) {
11+
int[]charCount =newint[26];
12+
for (intj =i;j <s.length();j++) {
13+
charCount[s.charAt(j) -'a']++;
14+
//get beauty of s.substring(i, j)
15+
intbeauty =getMaxCount(charCount) -getMinCount(charCount);
16+
sum +=beauty;
17+
}
18+
}
19+
returnsum;
20+
}
21+
22+
privateintgetMinCount(int[]charCount) {
23+
intmin =Integer.MAX_VALUE;
24+
for (inti =0;i <26;i++) {
25+
if (charCount[i] !=0) {
26+
min =Math.min(min,charCount[i]);
27+
}
28+
}
29+
returnmin;
30+
}
31+
32+
privateintgetMaxCount(int[]charCount) {
33+
intmax =0;
34+
for (inti =0;i <26;i++) {
35+
if (charCount[i] !=0) {
36+
max =Math.max(max,charCount[i]);
37+
}
38+
}
39+
returnmax;
40+
}
41+
}
42+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1781;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1781Test {
10+
privatestatic_1781.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_1781.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(5,solution1.beautySum("aabcb"));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp