File tree Expand file tree Collapse file tree 11 files changed +310
-0
lines changed Expand file tree Collapse file tree 11 files changed +310
-0
lines changed Original file line number Diff line number Diff line change
1
+ class FindElements {
2
+ public:
3
+ FindElements (TreeNode* root) {
4
+ head = root;
5
+ if (root != nullptr )
6
+ {
7
+ root->val = 0 ;
8
+ deque<TreeNode*> dq;
9
+ dq.push_back (root);
10
+ elem.push_back (0 );
11
+ int len = 1 ;
12
+
13
+ while (len != 0 )
14
+ {
15
+ for (int i = 0 ; i < len; ++i)
16
+ {
17
+ TreeNode* node = dq[i];
18
+ if (node->left != nullptr )
19
+ {
20
+ node->left ->val = 2 *node->val + 1 ;
21
+ elem.push_back (node->left ->val );
22
+ dq.push_back (node->left );
23
+ }
24
+ if (node->right != nullptr )
25
+ {
26
+ node->right ->val = 2 *node->val + 2 ;
27
+ elem.push_back (node->right ->val );
28
+ dq.push_back (node->right );
29
+ }
30
+ }
31
+
32
+ dq.erase (dq.begin (), dq.begin ()+len);
33
+ len = dq.size ();
34
+ }
35
+
36
+ sort (elem.begin (), elem.end ());
37
+ }
38
+ }
39
+
40
+ bool find (int target) {
41
+ return binary_search (elem.begin (), elem.end (), target);
42
+ }
43
+ private:
44
+ TreeNode* head;
45
+ vector<int > elem;
46
+ };
Original file line number Diff line number Diff line change
1
+ vector<vector<int >> matrixBlockSum (vector<vector<int >>& mat, int K) {
2
+ int row = mat.size ();
3
+ int col = mat[0 ].size ();
4
+ vector<vector<int >> res (row, vector<int >(col, 0 ));
5
+
6
+ for (int i = 0 ; i < row; ++i)
7
+ {
8
+ for (int j = 0 ; j < col; ++j)
9
+ {
10
+ for (int m = i-K; m <= i+K; ++m)
11
+ {
12
+ for (int n = j-K; n <= j+K; ++n)
13
+ {
14
+ if (m >= 0 && m < row && n >= 0 && n < col)
15
+ res[i][j] += mat[m][n];
16
+ }
17
+ }
18
+ }
19
+ }
20
+
21
+ return res;
22
+ }
Original file line number Diff line number Diff line change
1
+ TreeNode* getTargetCopy (TreeNode* original, TreeNode* cloned, TreeNode* target) {
2
+ vector<int > paths;
3
+ search (original, target, paths);
4
+
5
+ int len = paths.size ();
6
+ TreeNode* node = cloned;
7
+ for (int i = 0 ; i < len; ++i)
8
+ {
9
+ if (paths[i] == 0 )
10
+ node = node->left ;
11
+ else
12
+ node = node->right ;
13
+ }
14
+
15
+ return node;
16
+ }
17
+
18
+ bool search (TreeNode* ori, TreeNode* target, vector<int >& paths)
19
+ {
20
+ if (ori != target)
21
+ {
22
+ if (ori->left == nullptr && ori->right == nullptr )
23
+ return false ;
24
+ bool flag = false ;
25
+ vector<int > tmp (paths);
26
+ if (ori->left != nullptr )
27
+ {
28
+ tmp.push_back (0 );
29
+ flag = search (ori->left , target, tmp);
30
+ }
31
+
32
+ if (!flag && ori->right != nullptr )
33
+ {
34
+ tmp.swap (paths);
35
+ tmp.push_back (1 );
36
+ flag = search (ori->right , target, tmp);
37
+ if (flag)
38
+ paths.swap (tmp);
39
+ return flag;
40
+ }
41
+ else
42
+ {
43
+ if (flag)
44
+ paths.swap (tmp);
45
+ return flag;
46
+ }
47
+ }
48
+ else
49
+ return true ;
50
+ }
Original file line number Diff line number Diff line change
1
+ class CustomStack {
2
+ public:
3
+ CustomStack (int maxSize) {
4
+ maxCap = maxSize;
5
+ curSz = 0 ;
6
+ dq.assign (maxCap, -1 );
7
+ }
8
+
9
+ void push (int x) {
10
+ if (curSz < maxCap)
11
+ {
12
+ dq[curSz++] = x;
13
+ }
14
+ }
15
+
16
+ int pop () {
17
+ if (curSz > 0 )
18
+ {
19
+ --curSz;
20
+ return dq[curSz];
21
+ }
22
+ else
23
+ return -1 ;
24
+ }
25
+
26
+ void increment (int k, int val) {
27
+ if (curSz < k)
28
+ k = curSz;
29
+ for (int i = 0 ; i < k; ++i)
30
+ dq[i] += val;
31
+ }
32
+ private:
33
+ deque<int > dq;
34
+ int maxCap;
35
+ int curSz;
36
+ };
Original file line number Diff line number Diff line change
1
+ int findTheDistanceValue (vector<int >& arr1, vector<int >& arr2, int d) {
2
+ int len1 = arr1.size (), len2 = arr2.size ();
3
+ int ct = 0 ;
4
+ for (int i = 0 ; i < len1; ++i)
5
+ {
6
+ int j = 0 ;
7
+ for (j = 0 ; j < len2; ++j)
8
+ if (abs (arr1[i]-arr2[j]) <= d)
9
+ break ;
10
+ if (j == len2)
11
+ ++ct;
12
+ }
13
+
14
+ return ct;
15
+ }
Original file line number Diff line number Diff line change
1
+ int getKth (int lo, int hi, int k) {
2
+ struct Elem
3
+ {
4
+ int val;
5
+ int ct;
6
+ Elem (int v = 0 , int n = 0 ):val(v),ct(n){}
7
+ };
8
+
9
+ vector<Elem> res (hi-lo+1 );
10
+ for (int i = lo; i <= hi; ++i)
11
+ {
12
+ int t = i;
13
+ int ct = 0 ;
14
+ while (t != 1 )
15
+ {
16
+ if (t%2 == 0 )
17
+ t /= 2 ;
18
+ else
19
+ t = t*3 +1 ;
20
+ ++ct;
21
+ }
22
+
23
+ res[i-lo].val = i;
24
+ res[i-lo].ct = ct;
25
+ }
26
+
27
+ auto cmp = [](const Elem& a, const Elem& b) {
28
+ return a.ct < b.ct || (a.ct == b.ct && a.val < b.val );
29
+ };
30
+
31
+ sort (res.begin (), res.end (), cmp);
32
+
33
+ return res[k-1 ].val ;
34
+ }
Original file line number Diff line number Diff line change
1
+ vector<int > createTargetArray (vector<int >& nums, vector<int >& index) {
2
+ int len = nums.size ();
3
+ vector<int > res (len, -1 );
4
+ for (int i = 0 ; i < len; ++i)
5
+ {
6
+ if (res[index[i]] == -1 )
7
+ res[index[i]] = nums[i];
8
+ else
9
+ res.insert (res.begin ()+index[i], 1 , nums[i]);
10
+ }
11
+
12
+ res.resize (len);
13
+ return res;
14
+ }
Original file line number Diff line number Diff line change
1
+ int findLucky (vector<int >& arr) {
2
+ int len = arr.size ();
3
+ if (len == 1 )
4
+ return arr[0 ] == 1 ? 1 : -1 ;
5
+ sort (arr.begin (), arr.end ());
6
+ int i = len-1 ;
7
+ while (i >= 0 )
8
+ {
9
+ int tmp = arr[i];
10
+ if (i-tmp+1 >= 0 && arr[i-tmp+1 ] == tmp)
11
+ {
12
+ if (i-tmp < 0 || arr[i-tmp] != tmp)
13
+ return tmp;
14
+ else
15
+ i = i-tmp;
16
+ }
17
+
18
+ for (; i >= 0 ; --i)
19
+ if (arr[i] != tmp)
20
+ break ;
21
+ }
22
+
23
+ return -1 ;
24
+ }
Original file line number Diff line number Diff line change
1
+ int numTeams (vector<int >& rating) {
2
+ int len = rating.size ();
3
+ if (len < 3 )
4
+ return 0 ;
5
+
6
+ int ct = 0 ;
7
+ for (int i = 0 ; i < len-2 ; ++i)
8
+ {
9
+ for (int j = i+1 ; j < len-1 ; ++j)
10
+ {
11
+ bool flag = rating[i] < rating[j] ? true : false ;
12
+ for (int k = j+1 ; k < len; ++k)
13
+ {
14
+ if (flag && rating[j] < rating[k])
15
+ ++ct;
16
+ else if (!flag && rating[j] > rating[k])
17
+ ++ct;
18
+ }
19
+ }
20
+ }
21
+
22
+ return ct;
23
+ }
Original file line number Diff line number Diff line change
1
+ vector<int > minSubsequence (vector<int >& nums) {
2
+ int len = nums.size ();
3
+ if (len == 1 )
4
+ return move (nums);
5
+
6
+ int sum = 0 ;
7
+ for (int i = 0 ; i < len; ++i)
8
+ sum += nums[i];
9
+
10
+ sort (nums.begin (), nums.end ());
11
+ vector<int > res;
12
+ int subsum = 0 ;
13
+ int i = len-1 ;
14
+ for (i = len-1 ; i >= 0 ; --i)
15
+ {
16
+ subsum += nums[i];
17
+ if (subsum > sum-subsum)
18
+ break ;
19
+ }
20
+
21
+ res.assign (nums.rbegin (), nums.rbegin ()+len-i);
22
+
23
+ return res;
24
+ }
Original file line number Diff line number Diff line change
1
+ vector<string> stringMatching (vector<string>& words) {
2
+ int len = words.size ();
3
+ auto cmp = [](const string& a, const string& b) {
4
+ return a.size () < b.size ();
5
+ };
6
+
7
+ sort (words.begin (), words.end (), cmp);
8
+ vector<string> res;
9
+ for (int i = 0 ; i < len-1 ; ++i)
10
+ {
11
+ for (int j = i+1 ; j < len; ++j)
12
+ {
13
+ if (words[j].find (words[i]) != string::npos)
14
+ {
15
+ res.push_back (words[i]);
16
+ break ;
17
+ }
18
+ }
19
+ }
20
+
21
+ return res;
22
+ }
You can’t perform that action at this time.
0 commit comments