-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday19.java
More file actions
49 lines (45 loc) · 1.27 KB
/
day19.java
File metadata and controls
49 lines (45 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Q1: https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/
class Solution {
public int maxDepth(String s) {
int count=0;
int max=0;
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch == '(') {
count++;
max = Math.max(max, count);
} else if (ch == ')') {
count--;
}
}
return max;
}
}
TC-O(n)
SC-O(1)
class Solution {
public int romanToInt(String s) {
int n = s.length();
int ans = 0;
Map<Character, Integer> romanMap = new HashMap<>();
romanMap.put('I', 1);
romanMap.put('V', 5);
romanMap.put('X', 10);
romanMap.put('L', 50);
romanMap.put('C', 100);
romanMap.put('D', 500);
romanMap.put('M', 1000);
ans += romanMap.get(s.charAt(n - 1));
for (int i = n - 2; i >= 0; i--) {
if (romanMap.get(s.charAt(i)) < romanMap.get(s.charAt(i + 1))) {
ans -= romanMap.get(s.charAt(i));
} else {
ans += romanMap.get(s.charAt(i));
}
}
return ans;
}
}
TC-O(n)
SC-O(n)
// Q2: https://leetcode.com/problems/roman-to-integer/