File tree 5 files changed +92
-0
lines changed
5 files changed +92
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ vector<int > nextGreaterElement (vector<int >& nums1, vector<int >& nums2) {
4
+ int n = nums1.size (), m = nums2.size ();
5
+ vector<int > res (n, -1 );
6
+ stack<int > st;
7
+ unordered_map<int , int > mp;
8
+ for (int i = 0 ; i < m; i++){
9
+ int val = nums2[i];
10
+ while (!st.empty () and val > st.top ()){
11
+ mp[st.top ()] = val;
12
+ st.pop ();
13
+ }
14
+ st.push (val);
15
+ }
16
+ for (int i = 0 ; i < n; i++){
17
+ int val = nums1[i];
18
+ if (mp.find (val) != mp.end ()){
19
+ int nge = mp[val];
20
+ res[i] = nge;
21
+ }
22
+ }
23
+ return res;
24
+ }
25
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ vector<int > nextGreaterElements (vector<int >& nums) {
4
+ int n = nums.size ();
5
+ vector<int > res (n, -1 );
6
+ stack<int > st;
7
+ for (int i = 2 *n-1 ; i > -1 ; i--){
8
+ while (!st.empty () and nums[i%n] >= st.top ()){
9
+ st.pop ();
10
+ }
11
+ if (i < n){
12
+ if (!st.empty ())
13
+ res[i] = st.top ();
14
+ }
15
+ st.push (nums[i%n]);
16
+ }
17
+ return res;
18
+ }
19
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int nextGreaterElement (int num) {
4
+ vector<int > v;
5
+ while (num){
6
+ v.push_back (num%10 );
7
+ num /= 10 ;
8
+ }
9
+ if (is_sorted (v.begin (), v.end ()))
10
+ return -1 ;
11
+ reverse (v.begin (), v.end ());
12
+ next_permutation (v.begin (), v.end ());
13
+ long long temp = 0 ;
14
+ for (int i = 0 ; i < v.size (); i++){
15
+ temp = temp*10 + v[i];
16
+ }
17
+ if (temp > INT_MAX) return -1 ;
18
+ return temp;
19
+ }
20
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ bool reorderedPowerOf2 (int n) {
4
+ map<string, int > mp;
5
+ mp[" 1" ]++;
6
+ int num = 1 ;
7
+ while (mp.size () != 30 ){
8
+ num *= 2 ;
9
+ string s = to_string (num);
10
+ sort (s.begin (), s.end ());
11
+ mp[s]++;
12
+ }
13
+ string s = to_string (n);
14
+ sort (s.begin (), s.end ());
15
+ return (mp.find (s) != mp.end ());
16
+ }
17
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int getXORSum (vector<int >& arr1, vector<int >& arr2) {
4
+ int a = 0 , b = 0 ;
5
+ for (auto i : arr1)
6
+ a = a ^ i;
7
+ for (auto j : arr2)
8
+ b = b ^ j;
9
+ return a & b;
10
+ }
11
+ };
You can’t perform that action at this time.
0 commit comments