File tree Expand file tree Collapse file tree 7 files changed +160
-0
lines changed Expand file tree Collapse file tree 7 files changed +160
-0
lines changed Original file line number Diff line number Diff line change
1
+ int maximumElementAfterDecrementingAndRearranging (vector<int >& arr) {
2
+ int len = arr.size ();
3
+ sort (arr.begin (), arr.end ());
4
+
5
+ arr[0 ] = 1 ;
6
+ for (int i = 1 ; i < len; ++i)
7
+ {
8
+ if (arr[i] > arr[i-1 ]+1 )
9
+ {
10
+ arr[i] = arr[i-1 ]+1 ;
11
+ }
12
+ }
13
+
14
+ return arr[len-1 ];
15
+ }
Original file line number Diff line number Diff line change
1
+ int numOfPairs (vector<string>& nums, string target) {
2
+ int len = nums.size ();
3
+ int targetlen = target.size ();
4
+ vector<int > sz_vec (len, 0 );
5
+ for (int i = 0 ; i < len; ++i)
6
+ sz_vec[i] = nums[i].size ();
7
+
8
+ int ct = 0 ;
9
+ for (int i = 0 ; i < len-1 ; ++i)
10
+ {
11
+ for (int j = i+1 ; j < len; ++j)
12
+ {
13
+ if (sz_vec[i] + sz_vec[j] == targetlen)
14
+ {
15
+ if (nums[i] + nums[j] == target)
16
+ ct += 1 ;
17
+ if (nums[j] + nums[i] == target)
18
+ ct += 1 ;
19
+ }
20
+ }
21
+ }
22
+
23
+ return ct;
24
+ }
Original file line number Diff line number Diff line change
1
+ vector<int > nodesBetweenCriticalPoints (ListNode* head) {
2
+ ListNode* pre = head, *cur = head->next , *post = head->next ->next ;
3
+ int lastindex = -1 , nextindex = -1 , firstindex = -1 ;
4
+ int n = 1 ;
5
+ int mindist = 1e5 ;
6
+ while (post != nullptr )
7
+ {
8
+ if ((cur->val < pre->val && cur->val < post->val )
9
+ || (cur->val > pre->val && cur->val > post->val ))
10
+ {
11
+ lastindex = nextindex;
12
+ nextindex = n;
13
+ if (lastindex == -1 )
14
+ {
15
+ firstindex = n;
16
+ }
17
+ else
18
+ {
19
+ int dist = nextindex - lastindex;
20
+ if (dist < mindist)
21
+ mindist = dist;
22
+ }
23
+ }
24
+ pre = cur;
25
+ cur = post;
26
+ post = post->next ;
27
+ ++n;
28
+ }
29
+
30
+ vector<int > res (2 , -1 );
31
+ if (lastindex != -1 && nextindex != -1 )
32
+ {
33
+ res[0 ] = mindist;
34
+ res[1 ] = nextindex - firstindex;
35
+ }
36
+
37
+ return res;
38
+ }
Original file line number Diff line number Diff line change
1
+ string addSpaces (string s, vector<int >& spaces) {
2
+ int sz = s.size ();
3
+ int len = spaces.size ();
4
+ int n = sz + len;
5
+ int i = 0 , j = 0 , k = 0 ;
6
+
7
+ string res (n, ' ' );
8
+ while (i < n && j < len)
9
+ {
10
+ if (i < spaces[j]+j)
11
+ {
12
+ res[i++] = s[k++];
13
+ }
14
+ else
15
+ {
16
+ ++i;
17
+ ++j;
18
+ }
19
+ }
20
+
21
+ if (i < n)
22
+ copy (s.begin ()+k, s.end (), res.begin ()+i);
23
+
24
+ return res;
25
+ }
Original file line number Diff line number Diff line change
1
+ int prefixCount (vector<string>& words, string pref) {
2
+ int len = words.size ();
3
+ int n = pref.size ();
4
+ int ct = 0 ;
5
+ for (int i = 0 ; i < len; ++i)
6
+ {
7
+ int j = 0 ;
8
+ for (j = 0 ; j < n; ++j)
9
+ {
10
+ if (words[i][j] != pref[j])
11
+ break ;
12
+ }
13
+ if (j == n)
14
+ ++ct;
15
+ }
16
+
17
+ return ct;
18
+ }
Original file line number Diff line number Diff line change
1
+ int mostFrequent (vector<int >& nums, int key) {
2
+ int len = nums.size ();
3
+ int target = 0 , maxnum = 0 ;
4
+ for (int i = 0 ; i < len; ++i)
5
+ {
6
+ int tmp = nums[i], ct = 0 ;
7
+ for (int j = 0 ; j < len-1 ; ++j)
8
+ {
9
+ if (nums[j] == key && nums[j+1 ] == tmp)
10
+ ++ct;
11
+ }
12
+
13
+ if (ct > maxnum)
14
+ {
15
+ target = tmp;
16
+ maxnum = ct;
17
+ }
18
+ }
19
+
20
+ return target;
21
+ }
Original file line number Diff line number Diff line change
1
+ vector<string> cellsInRange (string s) {
2
+ int m = s[4 ]-s[1 ]+1 ;
3
+ int n = s[3 ]-s[0 ]+1 ;
4
+ int sum = m*n;
5
+
6
+ vector<string> res (sum, " " );
7
+ int k = 0 ;
8
+ for (int i = 0 ; i < n; ++i)
9
+ {
10
+ for (int j = 0 ; j < m; ++j)
11
+ {
12
+ res[k][0 ] = s[0 ]+i;
13
+ res[k][1 ] = s[1 ]+j;
14
+ ++k;
15
+ }
16
+ }
17
+
18
+ return res;
19
+ }
You can’t perform that action at this time.
0 commit comments