Skip to main content

Posts

Showing posts from October, 2022

LeetCode Solutions | 334. Increasing Triplet Subsequence | LeetCode C++ Solution

  LeetCode Question:   334. Increasing Triplet Subsequence  ( Level :  Medium ) Note :- Scroll horizontally to see the full line of code. class Solution { public : bool increasingTriplet ( vector < int > & nums ) { int n = nums . size (); vector <int> minimum ( n ); vector <int> maximum ( n ); int minElem = INT_MAX , maxElem = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { int frontSideElem = nums [ i ], backSideElem = nums [ n - i - 1 ]; if ( frontSideElem < minElem ) { minElem = frontSideElem ; } minimum [ i ] = minElem ; if ( backSideElem > maxElem ) { maxElem = backSideElem ; } maximum [ n - i - 1 ] = maxElem ; } for ( int i = 1 ; i < n - 1 ; i ++ ) { int p...

LeetCode Solutions | 1328. Break a Palindrome | LeetCode C++ Solution

   LeetCode Question:  1328. Break a Palindrome ( Level :  Medium ) Note :- Scroll horizontally to see the full line of code. class Solution { public : string breakPalindrome ( string palindrome ) { int n = palindrome . length (); if ( n == 1 ) { return "" ; } if ( n & 1 ) { int start = 0 , end = n - 1 ; while ( start != end ) { if ( palindrome [ start ] == ' a ' && palindrome [ end ] == ' a ' ) { start ++ ; end -- ; } else { break ; } } if ( start == end ) { palindrome [ n - 1 ] = ' b ' ; return palindrome ; } } for ( int i = 0 ; i < n ; i ++ ) ...

LeetCode Solutions | 653. Two Sum IV - Input is a BST | LeetCode C++ Solution

 LeetCode Question:  653. Two Sum IV - Input is a BST   ( Level :  Easy ) Problem Statement: Given the root of a Binary Search Tree and a target number k, return true if there exist two elements in the BST such that their sum is equal to the given target. Note :- Scroll horizontally to see the full line of code. /** *Definition for a binary tree node. *struct TreeNode { * int val; * TreeNode * left; * TreeNode * right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} *}; */ class Solution { vector <int> elements ; public : void preorder ( TreeNode * root ) { if ( root -> left ) { preorder ( root -> left ); } if ( root -> right ) { preorder ( root -> right ); } elemen...

LeetCode Solutions | 2166. Design Bitset | LeetCode C++ Solution

  LeetCode Question:  2166. Design Bitset   ( Level :  Medium ) Note :- Scroll horizontally to see the full line of code. class Bitset { int * bits ; int * flippedBits ; int size ; int countOne = 0 ; public : Bitset ( int size ) { this -> size = size ; bits = new int [ size ]; flippedBits = new int [ size ]; for ( int i = 0 ; i < size ; i ++ ) { bits [ i ] = 0 ; flippedBits [ i ] = 1 ; } } void fix ( int idx ) { if ( bits [ idx ]) { return ; } else { bits [ idx ] = 1 ; flippedBits [ idx ] = 0 ; countOne ++ ; } } void unfix ( int idx ) { if ( bits [ idx ]) { bits [ idx ] = 0 ; flippedBits [ idx ] = 1 ; countOne -- ; } else { ret...

LeetCode Solutions | 2279. Maximum Bags With Full Capacity of Rocks | LeetCode C++ Solution

 LeetCode Question:  2279. Maximum Bags With Full Capacity of Rocks ( Level :  Medium ) Note :- Scroll horizontally to see the full line of code. class Solution { public : int maximumBags ( vector < int > & capacity , vector < int > & rocks , int additionalRocks ) { vector <int> availability ; int n = capacity . size (); for ( int i = 0 ; i < n ; i ++ ) { int availableSpace = capacity [ i ] - rocks [ i ]; availability . push_back ( availableSpace ); } sort ( availability . begin (), availability . end ()); int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( availability [ i ] != 0 ) { if ( availability [ i ] <= additionalRocks ) { additionalRocks -= availability [ i ]; ans ++ ; } ...

LeetCode Solutions | 987. Vertical Order Traversal of a Binary Tree | LeetCode C++ Solution

  LeetCode Question:  987. Vertical Order Traversal of a Binary Tree  ( Level : Hard ) Note :- Scroll horizontally to see the full line of code. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public : map <int , map <int , vector <int>>> m ; void addToMap ( TreeNode * root , int y , int x ) { if ( m . find ( y ) == m . end ()) { map <int , vector <int>> temp ; temp . insert ( pair < int , vector < int >>( x , { root -> val } )); m . insert ( pair < int , map < int , vector < int >>>( y , temp )); } ...

LeetCode Solutions | 981. Time Based Key-Value Store | LeetCode C++ Solution

 LeetCode Question:  981. Time Based Key-Value Store   ( Level :  Medium ) Note :- Scroll horizontally to see the full line of code. class TimeMap { map < string , map <int , string >> Dictionary ; public : TimeMap () { } void set ( string key , string value , int timestamp ) { Dictionary [ key ][ timestamp ] = value ; } string get ( string key , int timestamp ) { if ( Dictionary . find ( key ) == Dictionary . end ()) { return "" ; } else { if ( Dictionary [ key ]. find ( timestamp ) == Dictionary [ key ]. end ()) { for ( auto i = Dictionary [ key ]. rbegin (); i != Dictionary [ key ]. rend (); ++ i ) { if ( i -> first < timestamp ) { return i -> second ; } } ...

LeetCode Solutions | 623. Add One Row to Tree | LeetCode C++ Solution

  LeetCode Question:  623. Add One Row to Tree  ( Level :  Medium ) Note :- Scroll horizontally to see the full line of code. /** *Definition for a binary tree node. *struct TreeNode { * int val; * TreeNode * left; * TreeNode * right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} *}; */ class Solution { int val ; int depth ; public : void addNewNodes ( TreeNode *& root , int currentDepth ) { if ( currentDepth == depth - 1 ) { if ( root -> left ) { TreeNode * nextChild = root -> left ; root -> left = new TreeNode ( val ); root -> left -> left = nextChild ; } else { root -> left = new Tree...

LeetCode Solutions | 1578. Minimum Time to Make Rope Colorful | LeetCode C++ Solution

  LeetCode Question: 1578. Minimum Time to Make Rope Colorful  ( Level :  Medium ) Note :- Scroll horizontally to see the full line of code. class Solution { public : int minCost ( string colors , vector < int > & neededTime ) { int n = colors . length (); int m = neededTime . size (); int i = 1 ; vector <int> sameColorsTime ; sameColorsTime . push_back ( neededTime [ 0 ]); int ans = 0 ; while ( i < n ) { int previousIndex = i - 1 ; if ( colors [ previousIndex ] == colors [ i ]) { sameColorsTime . push_back ( neededTime [ i ]); i ++ ; } else { ans += ( accumulate ( sameColorsTime . begin (), sameColorsTime . end (), 0 ) - * max_element ( sameColorsTime . begin (), sameColorsTime . end ())); sameColorsTime . clear (); ...

LeetCode Solutions | 112. Path Sum | LeetCode C++ Solution

 LeetCode Question:  112. Path Sum   ( Level :  Easy ) Problem Statement : Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.  A leaf is a node with no children. Note :- Scroll horizontally to see the full line of code. /** *Definition for a binary tree node. *struct TreeNode { * int val; * TreeNode * left; * TreeNode * right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} *}; */ class Solution { bool ans = false ; int targetSum ; public : void preorder ( TreeNode * root , int sum ) { if ( ans ) { return ; } sum += root -> val ; if ( sum == targetSum && ! root ...