-
Notifications
You must be signed in to change notification settings - Fork 5
/
LC_1419_MinNumOfFrogsCroaking.cpp
91 lines (79 loc) · 2 KB
/
LC_1419_MinNumOfFrogsCroaking.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
https://leetcode.com/problems/minimum-number-of-frogs-croaking/
1419. Minimum Number of Frogs Croaking
*/
class Solution {
public:
/*
int minNumberOfFrogs(string croakOfFrogs) {
unordered_map<char,int> f;
f['c']=0;
f['r']=0;
f['o']=0;
f['a']=0;
f['k']=0;
unordered_map<char,char> prevChar = {
{'c', 'k'},
{'r', 'c'},
{'o', 'r'},
{'a', 'o'},
{'k', 'a'}
};
char prevch;
for(char ch: croakOfFrogs)
{
prevch = prevChar[ch];
if(prevch == 'k' || f[prevch]>0) //first char 'c' OR prevchar is positive value;
{
if(prevch == 'k' && f[prevch]==0)
f['c']++;
else
{
f[prevch]--;
f[ch]++;
}
}
else
return -1;
}
for(auto x: f)
if(x.first!='k' && x.second>0)
return -1;
return f['k'];
}
*/
int minNumberOfFrogs(string croakOfFrogs) {
unordered_map<char,int> f;
unordered_map<char,char> prevChar = {
{'c', 'k'},
{'r', 'c'},
{'o', 'r'},
{'a', 'o'},
{'k', 'a'}
};
char prevch;
for(char ch: croakOfFrogs)
{
prevch = prevChar[ch];
if(ch == 'c')
{
if(f[prevch]==0) f[ch]++;
else{
f[prevch]--;
f[ch]++;
}
}
else if(f[prevch]>0)
{
f[prevch]--;
f[ch]++;
}
else
return -1;
}
for(auto x: f)
if(x.first!='k' && x.second>0)
return -1;
return f['k'];
}
};