-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.cpp
More file actions
166 lines (160 loc) · 3.5 KB
/
Copy patharray.cpp
File metadata and controls
166 lines (160 loc) · 3.5 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "stc++.h"
using namespace std;
/* 42. Trapping Rain Water */
int trap(vector<int> &height)
{
int lmax = 0, rmax = 0;
vector<int> l, r;
for (int i = 0; i < height.size(); ++i)
{
if (height[i] > lmax)
lmax = height[i];
l.push_back(lmax);
if (height[height.size() - i - 1] > rmax)
rmax = height[height.size() - i - 1];
r.push_back(rmax);
}
int res = 0;
for (int i = 0; i < height.size(); ++i)
{
res += min(l[i], r[height.size() - i - 1]) - height[i];
}
return res;
}
/* 567. Permutation in String */
bool checkInclusion(string s1, string s2)
{
bool res = false;
int count = 0;
int len1 = s1.length();
vector<int> ind(26, 0);
for (char s : s1)
ind[s - 'a'] += 1;
for (int x : ind)
if (x)
count += 1;
for (int i = 0; i < s2.length(); ++i)
{
if (i >= len1)
{
if (ind[s2[i - len1] - 'a'] == 0)
count++;
ind[s2[i - len1] - 'a']++;
if (ind[s2[i - len1] - 'a'] == 0)
count--;
}
if (ind[s2[i] - 'a'] == 0)
count++;
ind[s2[i] - 'a']--;
if (ind[s2[i] - 'a'] == 0)
count--;
if (count == 0)
return true;
}
return res;
}
/* 648. Replace Words */
string replaceWords(vector<string> &dictionary, string sentence)
{
map<string, bool> dicts;
for (string dict : dictionary)
{
dicts[dict] = true;
}
string res = "";
string root = "";
bool flag = true;
for (auto ch : sentence)
{
if (ch == ' ')
{
res += root;
res += " ";
flag = true;
root = "";
}
else
{
if (flag)
{
root += ch;
if (dicts.find(root) != dicts.end())
{
flag = false;
}
}
}
}
res += root;
return res;
}
/* 1004. Max Consecutive Ones III */
int longestOnes(vector<int> &A, int K)
{
int res = 0;
int tmp = 0;
int i = 0, j = 0;
while (j < A.size())
{
if (A[j] == 1)
tmp++;
else
{
if (K > 0)
{
K--;
tmp++;
}
else
{
while (A[i] != 0 && i <= j)
{
i++;
tmp--;
}
if (A[i] == 0)
i++;
}
}
j++;
res = max(res, tmp);
}
return res;
}
/* 57. Insert Interval */
vector<vector<int>> insert(vector<vector<int>> &intervals, vector<int> &newInterval)
{
vector<vector<int>> res;
bool flag = true;
for (auto interval : intervals)
{
if (interval[0] > newInterval[1])
{
if (flag)
{
res.push_back(newInterval);
flag = false;
}
res.push_back(interval);
}
if (interval[1] < newInterval[0])
res.push_back(interval);
else
{
if (interval[0] < newInterval[0])
{
newInterval[0] = interval[0];
}
if (interval[1] >= newInterval[1])
{
newInterval[1] = interval[1];
}
}
}
if (flag)
{
res.push_back(newInterval);
flag = false;
}
return res;
}