Skip to content

Commit dc9d309

Browse files
solves #2496: Maximum Value of a String in an Array in java
1 parent fc73fe6 commit dc9d309

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@
786786
| 2485 | [Find the Pivot Integer](https://leetcode.com/problems/find-the-pivot-integer) | [![Java](assets/java.png)](src/FindThePivotInteger.java) | |
787787
| 2490 | [Circular Sentence](https://leetcode.com/problems/circular-sentence) | [![Java](assets/java.png)](src/CircularSentence.java) | |
788788
| 2492 | [Minimum Score of a Path Between Two Cities](https://leetcode.com/problems/minimum-score-of-a-path-between-two-cities) | [![Python](assets/python.png)](python/minimum_score_of_a_path_between_two_cities.py) | |
789-
| 2496 | [Maximum Value of a String in an Array](https://leetcode.com/problems/maximum-value-of-a-string-in-an-array) | | |
789+
| 2496 | [Maximum Value of a String in an Array](https://leetcode.com/problems/maximum-value-of-a-string-in-an-array) | [![Java](assets/java.png)](src/MaximumValueOfAStringInAnArray.java) | |
790790
| 2500 | [Delete Greatest Value in Each Row](https://leetcode.com/problems/delete-greatest-value-in-each-row) | | |
791791
| 2506 | [Count Pairs Of Similar Strings](https://leetcode.com/problems/count-pairs-of-similar-strings) | | |
792792
| 2511 | [Maximum Enemy Forts That Can Be Captured](https://leetcode.com/problems/maximum-enemy-forts-that-can-be-captured) | | |

Diff for: src/MaximumValueOfAStringInAnArray.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// https://leetcode.com/problems/maximum-value-of-a-string-in-an-array
2+
// T: O(|strings| * |strings[i]|)
3+
// S: O(1)
4+
5+
public class MaximumValueOfAStringInAnArray {
6+
public int maximumValue(String[] strings) {
7+
int max = -1;
8+
for (String string : strings) {
9+
max = Math.max(max, value(string));
10+
}
11+
return max;
12+
}
13+
14+
private int value(String string) {
15+
try {
16+
return Integer.parseInt(string);
17+
} catch (Exception e) {
18+
return string.length();
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)