├── README.md ├── 135. Candy ├── README.md └── 135. Candy.cpp ├── 100. Same Tree ├── README.md └── 100. Same Tree.cpp ├── 112. Path Sum ├── README.md └── 112. Path Sum.cpp ├── 458. Poor Pigs ├── README.md └── 458. Poor Pigs.cpp ├── 113. Path Sum II ├── README.md └── 113. Path Sum II.cpp ├── 133. Clone Graph ├── README.md └── 133. Clone Graph.cpp ├── 383. Ransom Note ├── README.md └── 326. Power of Three.cpp ├── 91. Decode Ways ├── README.md └── 91. Decode Ways.cpp ├── 16. 3Sum Closest ├── README.md └── 16. 3Sum Closest.cpp ├── 231. Power of Two ├── README.md └── 231. Power of Two.cpp ├── 38. Count and Say ├── README.md └── 38. Count and Say.cpp ├── 48. Rotate Image ├── README.md └── 48. Rotate Image.cpp ├── 62. Unique Paths ├── README.md └── 62. Unique Paths.cpp ├── 645. Set Mismatch ├── README.md └── 645. Set Mismatch.cpp ├── 71. Simplify Path ├── README.md └── 71. Simplify Path.cpp ├── 126. Word Ladder II ├── README.md └── 126. Word Ladder II.cpp ├── 136. Single Number ├── README.md └── 136. Single Number.cpp ├── 326. Power of Three ├── README.md └── 326. Power of Three.cpp ├── 342. Power of Four ├── README.md └── 342. Power of Four.cpp ├── 39. Combination Sum ├── README.md └── 39. Combination Sum.cpp ├── 49. Group Anagrams ├── README.md └── 49. Group Anagrams.cpp ├── 704. Binary Search ├── README.md └── 704. Binary Search.cpp ├── 729. My Calendar I ├── README.md └── 729. My Calendar I.cpp ├── 796. Rotate String ├── README.md └── 796. Rotate String.cpp ├── 835. Image Overlap ├── README.md └── 835. Image Overlap.cpp ├── 838. Push Dominoes ├── README.md └── 838. Push Dominoes.cpp ├── 948. Bag of Tokens ├── README.md └── 948. Bag of Tokens.cpp ├── 1035. Uncrossed Lines ├── README.md └── 1035. Uncrossed Lines.cpp ├── 12. Integer to Roman ├── README.md └── 12. Integer to Roman.cpp ├── 13. Roman to Integer ├── README.md └── 13. Roman to Integer.cpp ├── 336. Palindrome Pairs ├── README.md └── 336. Palindrome Pairs.cpp ├── 393. UTF-8 Validation ├── README.md └── 393. UTF-8 Validation.cpp ├── 43. Multiply Strings ├── README.md └── 43. Multiply Strings.cpp ├── 509. Fibonacci Number ├── README.md └── 509. Fibonacci Number.cpp ├── 732. My Calendar III ├── README.md └── 732. My Calendar III.cpp ├── 766. Toeplitz Matrix ├── README.md └── 766. Toeplitz Matrix.cpp ├── 20. Valid Parentheses ├── README.md └── 20. Valid Parentheses.cpp ├── 200. Number of Islands ├── README.md └── 200. Number of Islands.cpp ├── 242. Valid Anagram ├── README.md └── 242. Valid Anagram.cpp ├── 257. Binary Tree Paths ├── README.md └── 257. Binary Tree Paths.cpp ├── 858. Mirror Reflection ├── README.md └── 858. Mirror Reflection.cpp ├── 1020. Number of Enclaves ├── README.md └── 1020. Number of Enclaves.cpp ├── 1328. Break a Palindrome ├── README.md └── 1328. Break a Palindrome.cpp ├── 218. The Skyline Problem ├── README.md └── 218. The Skyline Problem.cpp ├── 29. Divide Two Integers ├── README.md └── 29. Divide Two Integers.cpp ├── 377. Combination Sum IV ├── README.md └── 377. Combination Sum IV.cpp ├── 42. Trapping Rain Water ├── README.md └── 42. Trapping Rain Water.cpp ├── 623. Add One Row to Tree ├── README.md └── 623. Add One Row to Tree.cpp ├── 814. Binary Tree Pruning ├── README.md └── 814. Binary Tree Pruning.cpp ├── 219. Contains Duplicate II ├── README.md └── 219. Contains Duplicate II.cpp ├── 450. Delete Node in a BST ├── README.md └── 450. Delete Node in a BST.cpp ├── 622. Design Circular Queue ├── README.md └── 622. Design Circular Queue.cpp ├── 692. Top K Frequent Words ├── README.md └── 692. Top K Frequent Words.cpp ├── 869. Reordered Power of 2 ├── README.md └── 869. Reordered Power of 2.cpp ├── 1531. String Compression II ├── README.md └── 1531. String Compression II.cpp ├── 234. Palindrome Linked List ├── README.md └── 234. Palindrome Linked List.cpp ├── 35. Search Insert Position ├── README.md └── 35. Search Insert Position.cpp ├── 1480. Running Sum of 1d Array ├── README.md └── 1480. Running Sum of 1d Array.cpp ├── 196. Delete Duplicate Emails ├── README.md └── 196. Delete Duplicate Emails.sql ├── 523. Continuous Subarray Sum ├── README.md └── 523. Continuous Subarray Sum.cpp ├── 658. Find K Closest Elements ├── README.md └── 658. Find K Closest Elements.cpp ├── 76. Minimum Window Substring ├── README.md └── 76. Minimum Window Substring.cpp ├── 804. Unique Morse Code Words ├── README.md └── 804. Unique Morse Code Words.cpp ├── 881. Boats to Save People ├── README.md └── 881. Boats to Save People.cpp ├── 890. Find and Replace Pattern ├── README.md └── 890. Find and Replace Pattern.cpp ├── 946. Validate Stack Sequences ├── README.md └── 946. Validate Stack Sequences.cpp ├── 1220. Count Vowels Permutation ├── README.md └── 1220. Count Vowels Permutation.cpp ├── 1254. Number of Closed Islands ├── README.md └── 1254. Number of Closed Islands.cpp ├── 1679. Max Number of K-Sum Pairs ├── README.md └── 1679. Max Number of K-Sum Pairs.cpp ├── 2439. Minimize Maximum of Array ├── README.md └── 2439. Minimize Maximum of Array.cpp ├── 349. Intersection of Two Arrays ├── README.md └── 349. Intersection of Two Arrays.cpp ├── 508. Most Frequent Subtree Sum ├── README.md └── 508. Most Frequent Subtree Sum.cpp ├── 53. Two Sum IV - Input is a BST ├── README.md └── 53. Two Sum IV - Input is a BST.cpp ├── 710. Random Pick with Blacklist ├── README.md └── 710. Random Pick with Blacklist.cpp ├── 823. Binary Trees With Factors ├── README.md └── 823. Binary Trees With Factors.cpp ├── 936. Stamping The Sequence ├── README.md └── 936. Stamping The Sequence.cpp ├── 976. Largest Perimeter Triangle ├── README.md └── 976. Largest Perimeter Triangle.cpp ├── 981. Time Based Key-Value Store ├── README.md └── 981. Time Based Key-Value Store.cpp ├── 1678. Goal Parser Interpretation ├── README.md └── 1678. Goal Parser Interpretation.cpp ├── 2405. Optimal Partition of String ├── README.md └── 2405. Optimal Partition of String.cpp ├── 417. Pacific Atlantic Water Flow ├── README.md └── 417. Pacific Atlantic Water Flow.cpp ├── 538. Convert BST to Greater Tree ├── README.md └── 538. Convert BST to Greater Tree.cpp ├── 98. Validate Binary Search Tree ├── README.md └── 98. Validate Binary Search Tree.cpp ├── 124. Binary Tree Maximum Path Sum ├── README.md └── 124. Binary Tree Maximum Path Sum.cpp ├── 237. Delete Node in a Linked List ├── README.md └── 237. Delete Node in a Linked List.cpp ├── 2390. Removing Stars From a String ├── README.md └── 2390. Removing Stars From a String.cpp ├── 557. Reverse Words in a String III ├── README.md └── 557. Reverse Words in a String III.cpp ├── 609. Find Duplicate File in System ├── README.md └── 609. Find Duplicate File in System.cpp ├── 94. Binary Tree Inorder Traversal ├── README.md └── 94. Binary Tree Inorder Traversal.cpp ├── 1329. Sort the Matrix Diagonally ├── README.md └── 1329. Sort the Matrix Diagonally.cpp ├── 1338. Reduce Array Size to The Half ├── README.md └── 1338. Reduce Array Size to The Half.cpp ├── 1383. Maximum Performance of a Team ├── README.md └── 1383. Maximum Performance of a Team.cpp ├── 1670. Design Front Middle Back Queue ├── README.md └── 1670. Design Front Middle Back Queue.cpp ├── 300. Longest Increasing Subsequence ├── README.md └── 300. Longest Increasing Subsequence.cpp ├── 334. Increasing Triplet Subsequence ├── README.md └── 334. Increasing Triplet Subsequence.cpp ├── 516. Longest Palindromic Subsequence ├── README.md └── 516. Longest Palindromic Subsequence.cpp ├── 1448. Count Good Nodes in Binary Tree ├── README.md └── 1448. Count Good Nodes in Binary Tree.cpp ├── 1832. Check if the Sentence Is Pangram ├── README.md └── 1832. Check if the Sentence Is Pangram.cpp ├── 19. Remove Nth Node From End of List ├── README.md └── 19. Remove Nth Node From End of List.cpp ├── 429. N-ary Tree Level Order Traversal ├── README.md └── 429. N-ary Tree Level Order Traversal.cpp ├── 637. Average of Levels in Binary Tree ├── README.md └── 637. Average of Levels in Binary Tree.cpp ├── 102. Binary Tree Level Order Traversal ├── README.md └── 102. Binary Tree Level Order Traversal.cpp ├── 188. Best Time to Buy and Sell Stock IV ├── README.md └── 188. Best Time to Buy and Sell Stock IV.cpp ├── 387. First Unique Character in a String ├── README.md └── 387. First Unique Character in a String.cpp ├── 871. Minimum Number of Refueling Stops ├── README.md └── 871. Minimum Number of Refueling Stops.cpp ├── 985. Sum of Even Numbers After Queries ├── README.md └── 985. Sum of Even Numbers After Queries.cpp ├── 1578. Minimum Time to Make Rope Colorful ├── README.md └── 1578. Minimum Time to Make Rope Colorful.cpp ├── 1636. Sort Array by Increasing Frequency ├── README.md └── 1636. Sort Array by Increasing Frequency.cpp ├── 2136. Earliest Possible Day of Full Bloom ├── README.md └── 2136. Earliest Possible Day of Full Bloom.cpp ├── 501. Find Mode in Binary Search Tree ├── README.md └── 501. Find Mode in Binary Search Tree.cpp ├── 718. Maximum Length of Repeated Subarray ├── README.md └── 718. Maximum Length of Repeated Subarray.cpp ├── 990. Satisfiability of Equality Equations ├── README.md └── 990. Satisfiability of Equality Equations.cpp ├── 1155. Number of Dice Rolls With Target Sum ├── README.md └── 1155. Number of Dice Rolls With Target Sum.cpp ├── 1335. Minimum Difficulty of a Job Schedule ├── README.md └── 1335. Minimum Difficulty of a Job Schedule.cpp ├── 363. Max Sum of Rectangle No Larger Than K ├── README.md └── 363. Max Sum of Rectangle No Larger Than K.cpp ├── 606. Construct String from Binary Tree ├── README.md └── 606. Construct String from Binary Tree.cpp ├── 2007. Find Original Array From Doubled Array ├── README.md └── 2007. Find Original Array From Doubled Array.cpp ├── 2300. Successful Pairs of Spells and Potions ├── README.md └── 2300. Successful Pairs of Spells and Potions.cpp ├── 378. Kth Smallest Element in a Sorted Matrix ├── README.md └── 378. Kth Smallest Element in a Sorted Matrix.cpp ├── 103. Binary Tree Zigzag Level Order Traversal ├── README.md └── 103. Binary Tree Zigzag Level Order Traversal.cpp ├── 2095. Delete the Middle Node of a Linked List ├── README.md └── 2095. Delete the Middle Node of a Linked List.cpp ├── 30. Substring with Concatenation of All Words ├── README.md └── 30. Substring with Concatenation of All Words.cpp ├── 659. Split Array into Consecutive Subsequences ├── README.md └── 659. Split Array into Consecutive Subsequences.cpp ├── 967. Numbers With Same Consecutive Differences ├── README.md └── 967. Numbers With Same Consecutive Differences.cpp ├── 987. Vertical Order Traversal of a Binary Tree ├── README.md └── 987. Vertical Order Traversal of a Binary Tree.cpp ├── 108. Convert Sorted Array to Binary Search Tree ├── README.md └── 108. Convert Sorted Array to Binary Search Tree.cpp ├── 1457. Pseudo-Palindromic Paths in a Binary Tree ├── README.md └── 1457. Pseudo-Palindromic Paths in a Binary Tree.cpp ├── 1662. Check If Two String Arrays are Equivalent ├── README.md └── 1662. Check If Two String Arrays are Equivalent.cpp ├── 1996. The Number of Weak Characters in the Game ├── README.md └── 1996. The Number of Weak Characters in the Game.cpp ├── 2114. Maximum Number of Words Found in Sentences ├── README.md └── 2114. Maximum Number of Words Found in Sentences.cpp ├── 1680. Concatenation of Consecutive Binary Numbers ├── README.md └── 1680. Concatenation of Consecutive Binary Numbers.cpp ├── 235. Lowest Common Ancestor of a Binary Search Tree ├── README.md └── 235. Lowest Common Ancestor of a Binary Search Tree.cpp ├── 1293. Shortest Path in a Grid with Obstacles Elimination ├── README.md └── 1293. Shortest Path in a Grid with Obstacles Elimination.cpp ├── 1770. Maximum Score from Performing Multiplication Operations ├── README.md └── 1770. Maximum Score from Performing Multiplication Operations.cpp ├── 105. Construct Binary Tree from Preorder and Inorder Traversal ├── README.md └── 105. Construct Binary Tree from Preorder and Inorder Traversal.cpp ├── 106. Construct Binary Tree from Inorder and Postorder Traversal ├── README.md └── 106. Construct Binary Tree from Inorder and Postorder Traversal.cpp ├── 1239. Maximum Length of a Concatenated String with Unique Characters ├── README.md └── 1239. Maximum Length of a Concatenated String with Unique Characters.cpp ├── LICENSE.md ├── Monotonic Renumeration.cpp ├── Water The Trees.cpp ├── AMONGUS2.cpp └── Balanced Ternary String.cpp /README.md: -------------------------------------------------------------------------------- 1 | ## Leetcode 2 | My leetcode struggle. 3 | -------------------------------------------------------------------------------- /135. Candy/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

135. Candy 3 | -------------------------------------------------------------------------------- /100. Same Tree/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

100. Same Tree 3 | -------------------------------------------------------------------------------- /112. Path Sum/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

112. Path Sum 3 | -------------------------------------------------------------------------------- /458. Poor Pigs/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

458. Poor Pigs 3 | -------------------------------------------------------------------------------- /113. Path Sum II/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

113. Path Sum II 3 | -------------------------------------------------------------------------------- /133. Clone Graph/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

133. Clone Graph 3 | -------------------------------------------------------------------------------- /383. Ransom Note/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

383. Ransom Note 3 | -------------------------------------------------------------------------------- /91. Decode Ways/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

91. Decode Ways 3 | -------------------------------------------------------------------------------- /16. 3Sum Closest/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

16. 3Sum Closest 3 | -------------------------------------------------------------------------------- /231. Power of Two/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

231. Power of Two 3 | -------------------------------------------------------------------------------- /38. Count and Say/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

38. Count and Say 3 | -------------------------------------------------------------------------------- /48. Rotate Image/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

48. Rotate Image 3 | -------------------------------------------------------------------------------- /62. Unique Paths/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

62. Unique Paths 3 | -------------------------------------------------------------------------------- /645. Set Mismatch/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

645. Set Mismatch 3 | -------------------------------------------------------------------------------- /71. Simplify Path/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

71. Simplify Path 3 | -------------------------------------------------------------------------------- /126. Word Ladder II/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

126. Word Ladder II 3 | -------------------------------------------------------------------------------- /136. Single Number/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

136. Single Number 3 | -------------------------------------------------------------------------------- /326. Power of Three/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

326. Power of Three 3 | -------------------------------------------------------------------------------- /342. Power of Four/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

342. Power of Four 3 | -------------------------------------------------------------------------------- /39. Combination Sum/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

39. Combination Sum 3 | -------------------------------------------------------------------------------- /49. Group Anagrams/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

49. Group Anagrams 3 | -------------------------------------------------------------------------------- /704. Binary Search/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

704. Binary Search 3 | -------------------------------------------------------------------------------- /729. My Calendar I/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

729. My Calendar I 3 | -------------------------------------------------------------------------------- /796. Rotate String/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

796. Rotate String 3 | -------------------------------------------------------------------------------- /835. Image Overlap/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

835. Image Overlap 3 | -------------------------------------------------------------------------------- /838. Push Dominoes/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

838. Push Dominoes 3 | -------------------------------------------------------------------------------- /948. Bag of Tokens/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

948. Bag of Tokens 3 | -------------------------------------------------------------------------------- /1035. Uncrossed Lines/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1035. Uncrossed Lines 3 | -------------------------------------------------------------------------------- /12. Integer to Roman/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

12. Integer to Roman 3 | -------------------------------------------------------------------------------- /13. Roman to Integer/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

13. Roman to Integer 3 | -------------------------------------------------------------------------------- /336. Palindrome Pairs/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

336. Palindrome Pairs 3 | -------------------------------------------------------------------------------- /393. UTF-8 Validation/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

393. UTF-8 Validation 3 | -------------------------------------------------------------------------------- /43. Multiply Strings/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

43. Multiply Strings 3 | -------------------------------------------------------------------------------- /509. Fibonacci Number/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

509. Fibonacci Number 3 | -------------------------------------------------------------------------------- /732. My Calendar III/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

732. My Calendar III 3 | -------------------------------------------------------------------------------- /766. Toeplitz Matrix/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

766. Toeplitz Matrix 3 | -------------------------------------------------------------------------------- /20. Valid Parentheses/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

20. Valid Parentheses 3 | -------------------------------------------------------------------------------- /200. Number of Islands/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

200. Number of Islands 3 | -------------------------------------------------------------------------------- /242. Valid Anagram/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

242. Valid Anagram 3 | -------------------------------------------------------------------------------- /257. Binary Tree Paths/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

257. Binary Tree Paths 3 | -------------------------------------------------------------------------------- /858. Mirror Reflection/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

858. Mirror Reflection 3 | -------------------------------------------------------------------------------- /1020. Number of Enclaves/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1020. Number of Enclaves 3 | -------------------------------------------------------------------------------- /1328. Break a Palindrome/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1328. Break a Palindrome 3 | -------------------------------------------------------------------------------- /218. The Skyline Problem/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

218. The Skyline Problem 3 | -------------------------------------------------------------------------------- /29. Divide Two Integers/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

29. Divide Two Integers 3 | -------------------------------------------------------------------------------- /377. Combination Sum IV/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

377. Combination Sum IV 3 | -------------------------------------------------------------------------------- /42. Trapping Rain Water/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

42. Trapping Rain Water 3 | -------------------------------------------------------------------------------- /623. Add One Row to Tree/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

623. Add One Row to Tree 3 | -------------------------------------------------------------------------------- /814. Binary Tree Pruning/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

814. Binary Tree Pruning 3 | -------------------------------------------------------------------------------- /219. Contains Duplicate II/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

219. Contains Duplicate II 3 | -------------------------------------------------------------------------------- /450. Delete Node in a BST/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

450. Delete Node in a BST 3 | -------------------------------------------------------------------------------- /622. Design Circular Queue/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

622. Design Circular Queue 3 | -------------------------------------------------------------------------------- /692. Top K Frequent Words/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

692. Top K Frequent Words 3 | -------------------------------------------------------------------------------- /869. Reordered Power of 2/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

869. Reordered Power of 2 3 | -------------------------------------------------------------------------------- /1531. String Compression II/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1531. String Compression II 3 | -------------------------------------------------------------------------------- /234. Palindrome Linked List/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

234. Palindrome Linked List 3 | -------------------------------------------------------------------------------- /35. Search Insert Position/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

35. Search Insert Position 3 | -------------------------------------------------------------------------------- /1480. Running Sum of 1d Array/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1480. Running Sum of 1d Array 3 | -------------------------------------------------------------------------------- /196. Delete Duplicate Emails/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

196. Delete Duplicate Emails 3 | -------------------------------------------------------------------------------- /523. Continuous Subarray Sum/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

523. Continuous Subarray Sum 3 | -------------------------------------------------------------------------------- /658. Find K Closest Elements/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

658. Find K Closest Elements 3 | -------------------------------------------------------------------------------- /76. Minimum Window Substring/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

76. Minimum Window Substring 3 | -------------------------------------------------------------------------------- /804. Unique Morse Code Words/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

804. Unique Morse Code Words 3 | -------------------------------------------------------------------------------- /881. Boats to Save People/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

881. Boats to Save People 3 | -------------------------------------------------------------------------------- /890. Find and Replace Pattern/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

890. Find and Replace Pattern 3 | -------------------------------------------------------------------------------- /946. Validate Stack Sequences/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

946. Validate Stack Sequences 3 | -------------------------------------------------------------------------------- /1220. Count Vowels Permutation/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1220. Count Vowels Permutation 3 | -------------------------------------------------------------------------------- /1254. Number of Closed Islands/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1254. Number of Closed Islands 3 | -------------------------------------------------------------------------------- /1679. Max Number of K-Sum Pairs/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1679. Max Number of K-Sum Pairs 3 | -------------------------------------------------------------------------------- /2439. Minimize Maximum of Array/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

2439. Minimize Maximum of Array 3 | -------------------------------------------------------------------------------- /349. Intersection of Two Arrays/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

349. Intersection of Two Arrays 3 | -------------------------------------------------------------------------------- /508. Most Frequent Subtree Sum/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

508. Most Frequent Subtree Sum 3 | -------------------------------------------------------------------------------- /53. Two Sum IV - Input is a BST/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

53. Two Sum IV - Input is a BST 3 | -------------------------------------------------------------------------------- /710. Random Pick with Blacklist/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

710. Random Pick with Blacklist 3 | -------------------------------------------------------------------------------- /823. Binary Trees With Factors/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

823. Binary Trees With Factors 3 | -------------------------------------------------------------------------------- /936. Stamping The Sequence/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

936. Stamping The Sequence 3 | -------------------------------------------------------------------------------- /976. Largest Perimeter Triangle/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

976. Largest Perimeter Triangle 3 | -------------------------------------------------------------------------------- /981. Time Based Key-Value Store/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

981. Time Based Key-Value Store 3 | -------------------------------------------------------------------------------- /1678. Goal Parser Interpretation/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1678. Goal Parser Interpretation 3 | -------------------------------------------------------------------------------- /2405. Optimal Partition of String/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

2405. Optimal Partition of String 3 | -------------------------------------------------------------------------------- /417. Pacific Atlantic Water Flow/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

417. Pacific Atlantic Water Flow 3 | -------------------------------------------------------------------------------- /538. Convert BST to Greater Tree/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

538. Convert BST to Greater Tree 3 | -------------------------------------------------------------------------------- /98. Validate Binary Search Tree/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

98. Validate Binary Search Tree 3 | -------------------------------------------------------------------------------- /124. Binary Tree Maximum Path Sum/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

124. Binary Tree Maximum Path Sum 3 | -------------------------------------------------------------------------------- /237. Delete Node in a Linked List/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

237. Delete Node in a Linked List 3 | -------------------------------------------------------------------------------- /2390. Removing Stars From a String/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

2390. Removing Stars From a String 3 | -------------------------------------------------------------------------------- /557. Reverse Words in a String III/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

557. Reverse Words in a String III 3 | -------------------------------------------------------------------------------- /609. Find Duplicate File in System/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

609. Find Duplicate File in System 3 | -------------------------------------------------------------------------------- /94. Binary Tree Inorder Traversal/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

94. Binary Tree Inorder Traversal 3 | -------------------------------------------------------------------------------- /1329. Sort the Matrix Diagonally/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1329. Sort the Matrix Diagonally 3 | -------------------------------------------------------------------------------- /1338. Reduce Array Size to The Half/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1338. Reduce Array Size to The Half 3 | -------------------------------------------------------------------------------- /1383. Maximum Performance of a Team/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1383. Maximum Performance of a Team 3 | -------------------------------------------------------------------------------- /1670. Design Front Middle Back Queue/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1670. Design Front Middle Back Queue 3 | -------------------------------------------------------------------------------- /300. Longest Increasing Subsequence/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

300. Longest Increasing Subsequence 3 | -------------------------------------------------------------------------------- /334. Increasing Triplet Subsequence/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

334. Increasing Triplet Subsequence 3 | -------------------------------------------------------------------------------- /516. Longest Palindromic Subsequence/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

516. Longest Palindromic Subsequence 3 | -------------------------------------------------------------------------------- /1448. Count Good Nodes in Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1448. Count Good Nodes in Binary Tree 3 | -------------------------------------------------------------------------------- /1832. Check if the Sentence Is Pangram/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1832. Check if the Sentence Is Pangram 3 | -------------------------------------------------------------------------------- /19. Remove Nth Node From End of List/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

19. Remove Nth Node From End of List 3 | -------------------------------------------------------------------------------- /429. N-ary Tree Level Order Traversal/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

429. N-ary Tree Level Order Traversal 3 | -------------------------------------------------------------------------------- /637. Average of Levels in Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

637. Average of Levels in Binary Tree 3 | -------------------------------------------------------------------------------- /102. Binary Tree Level Order Traversal/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

102. Binary Tree Level Order Traversal 3 | -------------------------------------------------------------------------------- /188. Best Time to Buy and Sell Stock IV/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

188. Best Time to Buy and Sell Stock IV 3 | -------------------------------------------------------------------------------- /387. First Unique Character in a String/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

387. First Unique Character in a String 3 | -------------------------------------------------------------------------------- /871. Minimum Number of Refueling Stops/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

871. Minimum Number of Refueling Stops 3 | -------------------------------------------------------------------------------- /985. Sum of Even Numbers After Queries/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

985. Sum of Even Numbers After Queries 3 | -------------------------------------------------------------------------------- /1578. Minimum Time to Make Rope Colorful/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1578. Minimum Time to Make Rope Colorful 3 | -------------------------------------------------------------------------------- /1636. Sort Array by Increasing Frequency/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1636. Sort Array by Increasing Frequency 3 | -------------------------------------------------------------------------------- /2136. Earliest Possible Day of Full Bloom/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

2136. Earliest Possible Day of Full Bloom 3 | -------------------------------------------------------------------------------- /501. Find Mode in Binary Search Tree/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

501. Find Mode in Binary Search Tree 3 | -------------------------------------------------------------------------------- /718. Maximum Length of Repeated Subarray/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

718. Maximum Length of Repeated Subarray 3 | -------------------------------------------------------------------------------- /990. Satisfiability of Equality Equations/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

990. Satisfiability of Equality Equations 3 | -------------------------------------------------------------------------------- /1155. Number of Dice Rolls With Target Sum/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1155. Number of Dice Rolls With Target Sum 3 | -------------------------------------------------------------------------------- /1335. Minimum Difficulty of a Job Schedule/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1335. Minimum Difficulty of a Job Schedule 3 | -------------------------------------------------------------------------------- /196. Delete Duplicate Emails/196. Delete Duplicate Emails.sql: -------------------------------------------------------------------------------- 1 | #Author: Devendra Uraon 2 | 3 | delete org from Person as org, Person as dup where org.email=dup.email and org.id>dup.id; 4 | 5 | 6 | -------------------------------------------------------------------------------- /363. Max Sum of Rectangle No Larger Than K/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

363. Max Sum of Rectangle No Larger Than K 3 | -------------------------------------------------------------------------------- /606. Construct String from Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

606. Construct String from Binary Tree 3 | -------------------------------------------------------------------------------- /2007. Find Original Array From Doubled Array/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

2007. Find Original Array From Doubled Array 3 | -------------------------------------------------------------------------------- /2300. Successful Pairs of Spells and Potions/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

2300. Successful Pairs of Spells and Potions 3 | -------------------------------------------------------------------------------- /378. Kth Smallest Element in a Sorted Matrix/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

378. Kth Smallest Element in a Sorted Matrix 3 | -------------------------------------------------------------------------------- /103. Binary Tree Zigzag Level Order Traversal/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

103. Binary Tree Zigzag Level Order Traversal 3 | -------------------------------------------------------------------------------- /2095. Delete the Middle Node of a Linked List/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

2095. Delete the Middle Node of a Linked List 3 | -------------------------------------------------------------------------------- /30. Substring with Concatenation of All Words/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

30. Substring with Concatenation of All Words 3 | -------------------------------------------------------------------------------- /659. Split Array into Consecutive Subsequences/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

659. Split Array into Consecutive Subsequences 3 | -------------------------------------------------------------------------------- /967. Numbers With Same Consecutive Differences/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

967. Numbers With Same Consecutive Differences 3 | -------------------------------------------------------------------------------- /987. Vertical Order Traversal of a Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

987. Vertical Order Traversal of a Binary Tree 3 | -------------------------------------------------------------------------------- /108. Convert Sorted Array to Binary Search Tree/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

108. Convert Sorted Array to Binary Search Tree 3 | -------------------------------------------------------------------------------- /1457. Pseudo-Palindromic Paths in a Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1457. Pseudo-Palindromic Paths in a Binary Tree 3 | -------------------------------------------------------------------------------- /1662. Check If Two String Arrays are Equivalent/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1662. Check If Two String Arrays are Equivalent 3 | -------------------------------------------------------------------------------- /1996. The Number of Weak Characters in the Game/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1996. The Number of Weak Characters in the Game 3 | -------------------------------------------------------------------------------- /2114. Maximum Number of Words Found in Sentences/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

2114. Maximum Number of Words Found in Sentences 3 | -------------------------------------------------------------------------------- /1680. Concatenation of Consecutive Binary Numbers/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1680. Concatenation of Consecutive Binary Numbers 3 | -------------------------------------------------------------------------------- /235. Lowest Common Ancestor of a Binary Search Tree/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

235. Lowest Common Ancestor of a Binary Search Tree 3 | -------------------------------------------------------------------------------- /1293. Shortest Path in a Grid with Obstacles Elimination/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1293. Shortest Path in a Grid with Obstacles Elimination 3 | -------------------------------------------------------------------------------- /1770. Maximum Score from Performing Multiplication Operations/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1770. Maximum Score from Performing Multiplication Operations 3 | -------------------------------------------------------------------------------- /105. Construct Binary Tree from Preorder and Inorder Traversal/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

105. Construct Binary Tree from Preorder and Inorder Traversal 3 | -------------------------------------------------------------------------------- /106. Construct Binary Tree from Inorder and Postorder Traversal/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

106. Construct Binary Tree from Inorder and Postorder Traversal 3 | -------------------------------------------------------------------------------- /1239. Maximum Length of a Concatenated String with Unique Characters/README.md: -------------------------------------------------------------------------------- 1 | ## Question : 2 |

1239. Maximum Length of a Concatenated String with Unique Characters 3 | -------------------------------------------------------------------------------- /237. Delete Node in a Linked List/237. Delete Node in a Linked List.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | void deleteNode(ListNode* node) { 5 | node -> val = node -> next -> val; 6 | node -> next = node -> next -> next; 7 | } 8 | }; 9 | -------------------------------------------------------------------------------- /509. Fibonacci Number/509. Fibonacci Number.cpp: -------------------------------------------------------------------------------- 1 | //Devendra Uraon 2 | class Solution { 3 | public: 4 | int fib(int N) { 5 | int f[N+2], i; 6 | f[0] = 0, f[1] = 1; 7 | 8 | for (i = 2; i <= N; i++){ 9 | f[i] = f[i-1] + f[i-2]; 10 | } 11 | return f[N]; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /136. Single Number/136. Single Number.cpp: -------------------------------------------------------------------------------- 1 | //Devendra Uraon 2 | class Solution { 3 | public: 4 | int singleNumber(vector& nums) { 5 | int sol = 0, T = nums.size(), i = 0; 6 | while(T--){ 7 | sol = nums[i]^sol; 8 | ++i; 9 | } 10 | return sol; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /458. Poor Pigs/458. Poor Pigs.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int poorPigs(int buckets, int minutesToDie, int minutesToTest) { 5 | int t = (minutesToTest - 1) / minutesToDie + 1; 6 | if (buckets == 1) return 0; 7 | return log(buckets - 1) / log(t + 1) + 1; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /231. Power of Two/231. Power of Two.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | bool isPowerOfTwo(int n) { 5 | if(n == 0) 6 | return 0; 7 | while(n != 1){ 8 | if(n % 2 != 0) 9 | return 0; 10 | n = n / 2; 11 | } 12 | return 1; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /342. Power of Four/342. Power of Four.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | bool isPowerOfFour(int n) { 5 | if(n == 0) 6 | return 0; 7 | while(n != 1){ 8 | if(n % 4 != 0) 9 | return 0; 10 | n = n / 4; 11 | } 12 | return 1; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /387. First Unique Character in a String/387. First Unique Character in a String.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int firstUniqChar(string s) { 5 | vector v(26,0); 6 | for(char c : s) v[c - 'a']++; 7 | for(int i = 0; i < s.size(); i++) if(v[s[i] - 'a'] == 1) return i; 8 | return -1; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /112. Path Sum/112. Path Sum.cpp: -------------------------------------------------------------------------------- 1 | //Devendra Uraon 2 | class Solution { 3 | public: 4 | bool hasPathSum(TreeNode* root, int sum) { 5 | if(root == NULL) return false; 6 | sum -= root->val; 7 | return sum == 0 && root->left == NULL && root->right == NULL ? true : hasPathSum(root->left, sum) | hasPathSum(root->right, sum); 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /1832. Check if the Sentence Is Pangram/1832. Check if the Sentence Is Pangram.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | bool checkIfPangram(string sentence) { 5 | unordered_map mp; 6 | for( int i = 0; i < sentence.size(); i++) mp[sentence[i]]++; 7 | if(mp.size() == 26) return true; 8 | return false; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /326. Power of Three/326. Power of Three.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | bool isPowerOfThree(int n) { 5 | if(n == 0) 6 | return 0; 7 | while(n != 1){ 8 | if(n % 3 != 0) 9 | return 0; 10 | n = n / 3; 11 | } 12 | return 1; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /100. Same Tree/100. Same Tree.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | bool isSameTree(TreeNode* p, TreeNode* q) { 5 | if(!p && !q) return true; 6 | if(!p || !q) return false; 7 | if(p -> val != q -> val) return false; 8 | return isSameTree(p -> left, q -> left) && isSameTree(p -> right, q -> right); 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /1662. Check If Two String Arrays are Equivalent/1662. Check If Two String Arrays are Equivalent.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | bool arrayStringsAreEqual(vector& word1, vector& word2) { 5 | string s1, s2; 6 | for(auto &i: word1) s1+=i; 7 | for(auto &i: word2) s2+=i; 8 | return s1 == s2; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /1480. Running Sum of 1d Array/1480. Running Sum of 1d Array.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | vector runningSum(vector& nums) { 5 | vector solve; 6 | int sum = 0; 7 | for(int i = 0; i < nums.size(); i++){ 8 | sum += nums[i]; 9 | solve.push_back(sum); 10 | } 11 | return solve; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /814. Binary Tree Pruning/814. Binary Tree Pruning.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | TreeNode* pruneTree(TreeNode* root) { 5 | if(!root) return NULL; 6 | root->left = pruneTree(root -> left); 7 | root->right = pruneTree(root -> right); 8 | if(root->val == 0 && !root -> left && !root -> right) return NULL; 9 | return root; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /35. Search Insert Position/35. Search Insert Position.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int searchInsert(vector& nums, int target) { 5 | int low = 0, high = nums.size()-1; 6 | while (low <= high) { 7 | int mid = low + (high-low)/2; 8 | if ((nums[mid] < target) ? low = mid+1 : high = mid-1); 9 | } 10 | return low; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /946. Validate Stack Sequences/946. Validate Stack Sequences.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | bool validateStackSequences(vector &push, vector &pop) { 5 | stack s; 6 | for (int i = 0, j = 0; i < push.size(); ++i) { 7 | s.push(push[i]); 8 | while (!s.empty() && s.top() == pop[j]) s.pop(), ++j; 9 | } 10 | return s.empty(); 11 | } 12 | }; 13 | 14 | 15 | -------------------------------------------------------------------------------- /13. Roman to Integer/13. Roman to Integer.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int romanToInt(string s) { 5 | unordered_map mp = {{'M', 1000}, {'D', 500}, {'C', 100}, {'L', 50}, {'X', 10}, {'V', 5}, {'I', 1}}; 6 | int sum = mp[s.back()]; 7 | for(int i = 0; i < s.size() - 1; i++) { 8 | if(mp[s[i]] < mp[s[i + 1]]) sum-= mp[s[i]]; 9 | else sum += mp[s[i]]; 10 | } 11 | return sum; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /1680. Concatenation of Consecutive Binary Numbers/1680. Concatenation of Consecutive Binary Numbers.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int concatenatedBinary(int n) { 5 | long long int val = 0; 6 | int i = 1; 7 | while(i <= n) 8 | { 9 | val = ((val << (1 + int(log2(i)))) % 1000000007 +i) % 1000000007; 10 | i++ ; 11 | } 12 | return val; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /1254. Number of Closed Islands/1254. Number of Closed Islands.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int minimizeArrayValue(vector& nums) { 5 | int n = nums.size(); 6 | long long ans = nums[0]; 7 | long long sum = nums[0]; 8 | for(int i = 1; i < n; i++){ 9 | sum += nums[i]; 10 | ans = max(ans, (sum + i) / (i + 1)); 11 | } 12 | return ans; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /2439. Minimize Maximum of Array/2439. Minimize Maximum of Array.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int minimizeArrayValue(vector& nums) { 5 | int n = nums.size(); 6 | long long ans = nums[0]; 7 | long long sum = nums[0]; 8 | for(int i = 1; i < n; i++){ 9 | sum += nums[i]; 10 | ans = max(ans, (sum + i) / (i + 1)); 11 | } 12 | return ans; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /976. Largest Perimeter Triangle/976. Largest Perimeter Triangle.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int largestPerimeter(vector& nums) { 5 | sort(nums.begin(), nums.end()); 6 | int mx = 0; 7 | for(int i = nums.size()-1; i >= 2; i--){ 8 | if(nums[i-2]+nums[i-1] > nums[i]){ 9 | return nums[i] + nums[i-1] + nums[i-2]; 10 | } 11 | } 12 | return 0; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /704. Binary Search/704. Binary Search.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int search(vector& nums, int target) { 5 | int s = 0; 6 | int e = nums.size() - 1; 7 | while(s <= e){ 8 | int mid = s + (e - s) / 2; 9 | if(nums[mid] == target) return mid; 10 | else if(nums[mid] > target) e = mid - 1; 11 | else s = mid + 1; 12 | } 13 | return -1; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /94. Binary Tree Inorder Traversal/94. Binary Tree Inorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | vectorv; 5 | void inorder(TreeNode* root){ 6 | if(root == NULL) 7 | return; 8 | 9 | inorder(root -> left); 10 | v.push_back(root -> val); 11 | inorder(root -> right); 12 | } 13 | vector inorderTraversal(TreeNode* root) { 14 | inorder(root); 15 | return v; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /2405. Optimal Partition of String/2405. Optimal Partition of String.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int partitionString(string s) { 5 | vector mp(26, 0); 6 | int ans = 1; 7 | for(char c: s) 8 | { 9 | if(mp[c - 'a'] != 0) 10 | { 11 | mp = vector(26, 0); 12 | ans++; 13 | } 14 | mp[c - 'a']++; 15 | } 16 | return ans; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /1328. Break a Palindrome/1328. Break a Palindrome.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | string breakPalindrome(string palindrome) { 5 | int n = palindrome.size(); 6 | if(n <= 1) return ""; 7 | for(int i = 0; i < n/2; i++){ 8 | if(palindrome[i] != 'a'){ 9 | palindrome[i] = 'a'; 10 | return palindrome; 11 | } 12 | } 13 | palindrome[n-1] = 'b'; 14 | return palindrome; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /378. Kth Smallest Element in a Sorted Matrix/378. Kth Smallest Element in a Sorted Matrix.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int kthSmallest(vector>& matrix, int k) 5 | { 6 | priority_queue pq; 7 | for(int i=0;ik) pq.pop(); 11 | } 12 | } 13 | return pq.top(); 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /124. Binary Tree Maximum Path Sum/124. Binary Tree Maximum Path Sum.cpp: -------------------------------------------------------------------------------- 1 | //Devendra Uraon 2 | class Solution { 3 | public: 4 | int helper(TreeNode* x, int &line) { 5 | if (x == NULL) return INT_MIN; 6 | int ll = 0, rl = 0, lm = helper(x->left, ll), rm = helper(x->right, rl); 7 | line = max(0, max(ll, rl) + x->val); 8 | return max(ll + rl + x->val, max(lm, rm)); 9 | } 10 | int maxPathSum(TreeNode* root) { 11 | int line = 0; 12 | return helper(root, line); 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /219. Contains Duplicate II/219. Contains Duplicate II.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | bool containsNearbyDuplicate(vector& nums, int k) { 5 | size_t size = nums.size(); 6 | int i; 7 | unordered_map m; 8 | for (i = 0; i < size; ++i) { 9 | if (m.find(nums[i]) == m.end()) m[nums[i]] = i; 10 | else if (i-m[nums[i]] <= k) return true; 11 | else m[nums[i]] = i; 12 | } 13 | return false; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /235. Lowest Common Ancestor of a Binary Search Tree/235. Lowest Common Ancestor of a Binary Search Tree.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 5 | if( root->val > p->val && root->val > q->val) 6 | return lowestCommonAncestor( root->left, p, q); 7 | if( root->val < p->val && root->val < q->val) 8 | return lowestCommonAncestor( root->right, p, q); 9 | return root; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /858. Mirror Reflection/858. Mirror Reflection.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int mirrorReflection(int p, int q) { 5 | while((p%2==0) && (q%2==0)){ //executing till getting odd either p or q 6 | p/=2; q/=2; 7 | } 8 | if((p%2) == 0 && (q%2)!=0) return 2; //p = even, q = odd 9 | if((p%2) != 0 && (q%2)!=0) return 1; //p = odd, q = odd 10 | return 0; //p = odd, q = even 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /48. Rotate Image/48. Rotate Image.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | void transpose(vector> &mat){ 5 | int m = mat.size(); 6 | 7 | for(int i = 0; i < m; i++){ 8 | for(int j = i; j < m; j++){ 9 | swap(mat[i][j], mat[j][i]); 10 | } 11 | } 12 | } 13 | void rotate(vector>& matrix) { 14 | transpose(matrix); 15 | 16 | for(auto &i:matrix) reverse(i.begin(), i.end()); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /2095. Delete the Middle Node of a Linked List/2095. Delete the Middle Node of a Linked List.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | ListNode* deleteMiddle(ListNode* head) { 5 | if(head -> next == NULL) return head -> next; 6 | ListNode* slow = head, *fast = head, *prev = NULL; 7 | while(fast && fast -> next) { 8 | prev = slow; 9 | slow = slow -> next; 10 | fast = fast -> next->next; 11 | } 12 | prev -> next = slow -> next; 13 | return head; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /869. Reordered Power of 2/869. Reordered Power of 2.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | unordered_setmp; 5 | Solution(){ 6 | 7 | int n=1; 8 | while(n<=1000000000){ 9 | string s=to_string(n); 10 | sort(s.begin(),s.end()); 11 | mp.insert(s); 12 | n<<=1; 13 | } 14 | } 15 | bool reorderedPowerOf2(int n) { 16 | string v=to_string(n); 17 | sort(v.begin(),v.end()); 18 | return mp.count(v); 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /12. Integer to Roman/12. Integer to Roman.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | string intToRoman(int num) { 5 | int n[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; 6 | string roman[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; 7 | 8 | int i = 0; 9 | string ans; 10 | while (num != 0){ 11 | while (n[i] > num) ++i; 12 | num = num - n[i]; 13 | ans += roman[i]; 14 | } 15 | return ans; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /2114. Maximum Number of Words Found in Sentences/2114. Maximum Number of Words Found in Sentences.cpp: -------------------------------------------------------------------------------- 1 | //Devendra Uraon 2 | class Solution { 3 | public: 4 | int mostWordsFound(vector& sentences) { 5 | 6 | int maax=INT_MIN; 7 | 8 | for(int i=0;i& nums) { 5 | if(nums.size() < 3) return false; 6 | int first = INT_MAX, second = INT_MAX; 7 | for(int i = 0; i < nums.size(); i++){ 8 | if(nums[i] > second) return true; 9 | else if(nums[i]>first && nums[i]val >= currentMax || currentMax == INT_MIN) sum++; 7 | doRec(root->left, max(currentMax, root->val)); 8 | doRec(root->right, max(currentMax, root->val)); 9 | } 10 | } 11 | public: 12 | int goodNodes(TreeNode* root) { 13 | doRec(root, INT_MIN); 14 | return sum; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /658. Find K Closest Elements/658. Find K Closest Elements.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | vector findClosestElements(vector& arr, int k, int x) { 5 | priority_queue, vector>> pq; 6 | for(auto n: arr){ 7 | pq.push({abs(n - x), n}); 8 | if(pq.size() > k) pq.pop(); 9 | } 10 | vector ans; 11 | while(!pq.empty()) ans.push_back(pq.top().second), pq.pop(); 12 | sort(ans.begin(), ans.end()); 13 | return ans; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /300. Longest Increasing Subsequence/300. Longest Increasing Subsequence.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int lengthOfLIS(vector& nums) { 5 | set s; 6 | int n = nums.size(); 7 | for(int i = 0; i < n; i++) { 8 | auto it = s.lower_bound(nums[i]); 9 | if(it == s.end()) { 10 | s.insert(nums[i]); 11 | continue; 12 | } 13 | s.erase(it); 14 | s.insert(nums[i]); 15 | } 16 | return (int)s.size(); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /49. Group Anagrams/49. Group Anagrams.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | vector> groupAnagrams(vector& strs) { 5 | 6 | unordered_map> m; 7 | for(auto it:strs){ 8 | string temp = it; 9 | sort(temp.begin(), temp.end()); 10 | m[temp].push_back(it); 11 | } 12 | 13 | vector> ans; 14 | for(auto [temp,vec]:m) 15 | ans.push_back(vec); 16 | 17 | return ans; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /766. Toeplitz Matrix/766. Toeplitz Matrix.cpp: -------------------------------------------------------------------------------- 1 | //Devendra Uraon 2 | class Solution { 3 | public: 4 | bool isToeplitzMatrix(const vector>& m) { 5 | if (m.empty()) { 6 | return true; 7 | } 8 | const auto n = m[0].size() - 1; 9 | for (auto i = 1u; i < m.size(); ++i) { 10 | const auto b1 = m[i - 1].begin(); 11 | const auto b2 = m[i].begin(); 12 | if (!equal(b1, b1 + n, b2 + 1)) { 13 | return false; 14 | } 15 | } 16 | return true; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /729. My Calendar I/729. My Calendar I.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class MyCalendar { 3 | vector> vec; 4 | public: 5 | MyCalendar() { 6 | 7 | } 8 | 9 | bool book(int start, int end) { 10 | for (auto date: vec) 11 | if ((start >= date.first && start < date.second) or 12 | (end > date.first && end < date.second) or 13 | (start <= date.first && end > date.first)) 14 | return false; 15 | 16 | vec.push_back({start, end}); 17 | return true; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /1679. Max Number of K-Sum Pairs/1679. Max Number of K-Sum Pairs.cpp: -------------------------------------------------------------------------------- 1 | //Devendra Uraon 2 | class Solution { 3 | public: 4 | int maxOperations(vector& nums, int k) { 5 | int ans=0; 6 | unordered_map mp; 7 | mp[nums[0]]++; 8 | for(int i=1;i0){ 10 | mp[k-nums[i]]--; // Decreasing the count of other number in the map 11 | ans++; 12 | } 13 | else mp[nums[i]]++; 14 | } 15 | return ans; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /242. Valid Anagram/242. Valid Anagram.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | bool isAnagram(string s, string t) { 5 | int alphabet[26]={0}; 6 | for(int i = 0; i < s.size(); i++){ 7 | alphabet[s[i]-'a']++; 8 | } 9 | int alphabet1[26]={0}; 10 | for(int i = 0; i < t.size(); i++){ 11 | alphabet1[t[i]-'a']++; 12 | } 13 | bool flag = 1; 14 | for(int i = 0; i<26; i++){ 15 | if(alphabet[i] != alphabet1[i]){ 16 | flag = 0; 17 | break; 18 | } 19 | } 20 | return flag; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /645. Set Mismatch/645. Set Mismatch.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | vector findErrorNums(vector& nums) { 5 | unordered_map mp; 6 | vector ans; 7 | for(int i=1; i<=nums.size(); i++) mp[i] = 1; 8 | for(auto n : nums){ 9 | mp[n]--; 10 | if(mp[n] < 0) ans.push_back(n); 11 | } 12 | for(auto m : mp){ 13 | if(m.second == 1){ 14 | ans.push_back(m.first); 15 | break; 16 | } 17 | } 18 | return ans; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /538. Convert BST to Greater Tree/538. Convert BST to Greater Tree.cpp: -------------------------------------------------------------------------------- 1 | //Devendra Uraon 2 | class Solution { 3 | private: 4 | vector num; 5 | void treeToVector(TreeNode *root) { 6 | if (!root) return; 7 | treeToVector(root->left); 8 | num.push_back(root); 9 | treeToVector(root->right); 10 | } 11 | public: 12 | TreeNode* convertBST(TreeNode* root) { 13 | treeToVector(root); 14 | if (num.size() <= 1) return root; 15 | for (int i = num.size()-2; i >= 0; i--) { num[i]->val += num[i+1]->val; } 16 | return root; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /62. Unique Paths/62. Unique Paths.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int uniquePaths(int m, int n) { 5 | double num = 1, div = 1; 6 | int max, min; 7 | 8 | if (m > n){ 9 | max = m - 1; 10 | min = n - 1; 11 | } 12 | else{ 13 | max = n - 1; 14 | min = m - 1; 15 | } 16 | 17 | for(int i = max + min; i > max; i--) 18 | num *= i; 19 | for (int i = 1; i <= min; i++) 20 | div *= i; 21 | 22 | return num / div; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /349. Intersection of Two Arrays/349. Intersection of Two Arrays.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | vector intersection(vector& a, vector& b) { 5 | 6 | unordered_set s; 7 | for(int i=0; i res; 11 | 12 | for(int j=0; j &N) 6 | { 7 | int mid=(e+b)/2; 8 | 9 | TreeNode *R=new TreeNode(N[mid]); 10 | 11 | if(mid>b) R->left= Make(b,mid-1,N); 12 | if(midright=Make(mid+1,e,N); 13 | 14 | return(R); 15 | } 16 | 17 | 18 | TreeNode* sortedArrayToBST(vector& N) { 19 | 20 | int n=N.size(); 21 | return(Make(0,n-1,N)); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /19. Remove Nth Node From End of List/19. Remove Nth Node From End of List.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | ListNode* removeNthFromEnd(ListNode* head, int n) { 5 | ListNode* ptr1 = head; 6 | while(n-- && ptr1) 7 | ptr1 = ptr1 -> next; 8 | ListNode* ptr2 = head; 9 | if(ptr1 != NULL){ 10 | while(ptr1 -> next){ 11 | ptr1 = ptr1 -> next; 12 | ptr2 = ptr2 -> next; 13 | } 14 | ptr2 -> next = ptr2 -> next -> next; 15 | } 16 | else head = head -> next; 17 | return head; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /257. Binary Tree Paths/257. Binary Tree Paths.cpp: -------------------------------------------------------------------------------- 1 | //Devendra Uraon 2 | class Solution { 3 | public: 4 | vector ans; 5 | void fun(TreeNode* root,string s) 6 | { 7 | if(!root->left && !root->right) 8 | ans.push_back(s); 9 | if(root->left) 10 | fun(root->left,s+"->"+to_string(root->left->val)); 11 | if(root->right) 12 | fun(root->right,s+"->"+to_string(root->right->val)); 13 | } 14 | 15 | vector binaryTreePaths(TreeNode* root) { 16 | string s=""; 17 | s+=to_string(root->val); 18 | fun(root,s); 19 | return ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /377. Combination Sum IV/377. Combination Sum IV.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int solve(vector &nums, int target,vector &dp){ 5 | if(target < 0) return 0; 6 | if(target == 0) return 1; 7 | if(dp[target] != -1) return dp[target]; 8 | int ans = 0; 9 | for(int i=0; i& nums, int target) { 15 | vector dp(target+1, -1); 16 | return solve(nums, target, dp); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /516. Longest Palindromic Subsequence/516. Longest Palindromic Subsequence.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int longestPalindromeSubseq(string s) { 5 | string s2 = s; 6 | reverse(s2.begin(), s2.end()); 7 | vector pre(s.size()+1, 0), cur(s.size()+1, 0); 8 | for(int i = 1; i <= s.size(); i++){ 9 | for(int j = 1; j <= s.size(); j++){ 10 | if(s[i-1] == s2[j-1]) cur[j] = 1 + pre[j-1]; 11 | else cur[j] = max(pre[j], cur[j-1]); 12 | } 13 | pre = cur; 14 | } 15 | return pre[s.size()]; 16 | } 17 | }; 18 | 19 | 20 | -------------------------------------------------------------------------------- /1678. Goal Parser Interpretation/1678. Goal Parser Interpretation.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | string interpret(string command) { 5 | ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 6 | string ans; 7 | for( int i = 0 ; i <= command.size()-1 ; i++){ 8 | if(command[i] == 'G'){ 9 | ans += 'G'; 10 | } 11 | if(command[i] == '('){ 12 | if(command[i+1] == ')') 13 | ans += 'o'; 14 | else 15 | ans += "al"; 16 | } 17 | } 18 | return ans; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /606. Construct String from Binary Tree/606. Construct String from Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | private: 4 | void bi(TreeNode*root, string &s){ 5 | if(root==NULL) 6 | return; 7 | s += "("; 8 | s += to_string(root->val); 9 | if(root -> left) bi(root -> left, s); 10 | else 11 | if(root -> right) 12 | s += "()"; 13 | bi(root->right, s); 14 | s += ")"; 15 | return; 16 | } 17 | public: 18 | string tree2str(TreeNode* root) { 19 | string s; 20 | bi(root, s); 21 | return s.substr(1, s.size()-2); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /523. Continuous Subarray Sum/523. Continuous Subarray Sum.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | bool checkSubarraySum(vector& nums, int k) { 5 | int n = nums.size(); 6 | if(n == 1) return false; 7 | unordered_map m; 8 | int sum = nums[0]; 9 | m[sum%k] = 0; 10 | for(int i = 1; i < n; i++){ 11 | sum += nums[i]; 12 | if(sum%k == 0) return true; 13 | if(m.find(sum%k) != m.end() && i - m[sum%k] >= 2) return true; 14 | if( (nums[i-1] + nums[i]) %k == 0) return true; 15 | m[sum%k] = i; 16 | } 17 | return false; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /53. Two Sum IV - Input is a BST/53. Two Sum IV - Input is a BST.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | vector v; 5 | void inorder(TreeNode* root){ 6 | if(!root) return ; 7 | inorder(root -> left); 8 | v.push_back(root -> val); 9 | inorder(root -> right); 10 | } 11 | bool findTarget(TreeNode* root, int k){ 12 | if(!root) return false; 13 | inorder(root); 14 | int l = 0, r = v.size()-1; 15 | while(l < r){ 16 | if(v[l]+v[r] == k) return true; 17 | else if(v[l]+v[r]>& stations) { 5 | vector dp(stations.size() + 1, startFuel); 6 | for(int i = 0; i < stations.size(); i++) { 7 | for(int t = i; t >= 0; t--) { 8 | if(dp[t] < stations[i][0]) break; 9 | dp[t + 1] = max(dp[t + 1], stations[i][1] + dp[t]); 10 | } 11 | } 12 | for(int i = 0; i <= stations.size(); i++){ 13 | if(dp[i]>=target)return i; 14 | } 15 | return -1; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /985. Sum of Even Numbers After Queries/985. Sum of Even Numbers After Queries.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | vector sumEvenAfterQueries(vector& n, vector>& q) { 5 | int sum = 0, val, index; 6 | vector vec; 7 | for(int i = 0; i < n.size(); i++) if(n[i]%2 == 0) sum += n[i]; 8 | for(int i = 0; i < q.size(); i++){ 9 | val = q[i][0]; 10 | index = q[i][1]; 11 | if(n[index]%2 == 0) sum -= n[index]; 12 | n[index] += val; 13 | if(n[index]%2 == 0) sum += n[index]; 14 | vec.push_back(sum); 15 | } 16 | return vec; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /796. Rotate String/796. Rotate String.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | bool rotateString(string s, string goal) { 5 | queue q; 6 | int n=s.size(); 7 | 8 | for(char c:goal) 9 | q.push(c); 10 | 11 | while(n--){ 12 | if(q.front() == s[0]){ 13 | queue tq = q; 14 | string str; 15 | 16 | while(tq.size()){ 17 | str += tq.front(); 18 | tq.pop(); 19 | } 20 | if(str == s) return true; 21 | } 22 | char y=q.front(); 23 | q.push(y); 24 | q.pop(); 25 | } 26 | return false; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /393. UTF-8 Validation/393. UTF-8 Validation.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | bool validUtf8(vector& data) { 5 | int m[6] = {128, 192, 224, 240, 248, 10000000}; 6 | int nbits,remaningbits = 0; 7 | for(auto d: data){ 8 | nbits = 0; 9 | while(d >= m[nbits]) nbits++; 10 | if (nbits >= 5) return false; 11 | if (remaningbits>0){ 12 | if (nbits!=1) return false; 13 | remaningbits--; 14 | } 15 | else if (nbits >= 2) remaningbits = nbits-1; 16 | else if (nbits == 1) return false; 17 | } 18 | return remaningbits == 0; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /804. Unique Morse Code Words/804. Unique Morse Code Words.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int uniqueMorseRepresentations(vector& words) { 5 | vector a = {".-","-...","-.-.","-..",".","..-.","--.","....", 6 | "..",".---","-.-",".-..","--","-.","---",".--.", 7 | "--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."}; 8 | 9 | unordered_map um; 10 | 11 | for (int i=0; i< words.size(); i++) { 12 | string st; 13 | for (int j=0; j> ans; 4 | vector path; 5 | int sum = 0, target; 6 | 7 | void dfs(TreeNode* x) { 8 | if (!x) return; 9 | sum += x->val; 10 | path.push_back(x -> val); 11 | if (sum == target && !x -> left && !x -> right) 12 | ans.push_back(path); 13 | dfs(x -> left); 14 | dfs(x -> right); 15 | sum -= x -> val; 16 | path.pop_back(); 17 | } 18 | 19 | public: 20 | vector> pathSum(TreeNode* root, int targetSum) { 21 | target = targetSum; 22 | dfs(root); 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /29. Divide Two Integers/29. Divide Two Integers.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int divide(int dividend, int divisor) { 5 | //bitwise 6 | long long n = dividend; 7 | long long m = divisor; 8 | int res = n<0 ^ m<0 ? -1 : 1; 9 | n = abs(n); 10 | m = abs(m); 11 | long long q=0, t=0; 12 | for(long long i = 31; i>=0; i--){ 13 | if(t + (m<= INT_MAX) return INT_MAX; 21 | return q; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /1996. The Number of Weak Characters in the Game/1996. The Number of Weak Characters in the Game.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | static bool cmp(vector& v1,vector& v2){ 5 | return v1[0] == v2[0] ? v1[1]>v2[1] : v1[0]>& nums){ 8 | int n = nums.size(), count = 0; 9 | sort(nums.begin(), nums.end(), cmp); 10 | stack s; 11 | for(int i = 0; i < n; i++){ 12 | while(s.size() && s.top() successfulPairs(vector& v1, vector& v2, long long success) { 5 | sort(v2.begin(), v2.end()); 6 | int n = v1.size(), m = v2.size(); 7 | vector ans; 8 | for (int i = 0; i < n; i++) { 9 | long long req = (success + v1[i] - 1) / v1[i]; 10 | if (req > v2[m - 1]) { 11 | ans.push_back(0); 12 | continue; 13 | } 14 | auto it = lower_bound(v2.begin(), v2.end(), req); 15 | ans.push_back(v2.end() - it); 16 | } 17 | return ans; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /71. Simplify Path/71. Simplify Path.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | string simplifyPath(string path) { 5 | string token = ""; 6 | 7 | stringstream ss(path); 8 | 9 | stack stk; 10 | 11 | while(getline(ss, token, '/')){ 12 | if(token == "" || token == ".") continue; 13 | 14 | if(token != "..") stk.push(token); 15 | else if(!stk.empty()) stk.pop(); 16 | } 17 | 18 | if(stk.empty()) 19 | return "/"; 20 | 21 | string res = ""; 22 | 23 | while(!stk.empty()){ 24 | res = "/" + stk.top() + res; 25 | stk.pop(); 26 | } 27 | return res; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /38. Count and Say/38. Count and Say.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | string countAndSay(int n) { 5 | if(n == 1) 6 | return "1"; 7 | 8 | string s = "1"; 9 | for(int i=2; i<=n; i++){ 10 | string t = ""; 11 | s = s + '/'; 12 | int count = 1; 13 | for(int j=1; j averageOfLevels(TreeNode* root) { 5 | vector ans; 6 | queue q; 7 | q.push(root); 8 | while(!q.empty()) { 9 | long temp = 0; 10 | int n = q.size(); 11 | for(int i = 0; i < n; i++) { 12 | TreeNode* t = q.front(); 13 | q.pop(); 14 | if(t->left) q.push(t->left); 15 | if(t->right) q.push(t->right); 16 | temp += t->val; 17 | } 18 | ans.push_back((double)temp/n); 19 | } 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /383. Ransom Note/326. Power of Three.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | bool canConstruct(string ransomNote, string magazine) { 5 | bool a = 0; 6 | int alphabet[26]={0}; 7 | int alphabet1[26]={0}; 8 | for(int i = 0; i < ransomNote.size(); i++){ 9 | alphabet[ransomNote[i]-'a']++; 10 | } 11 | for(int i = 0; i < magazine.size(); i++){ 12 | alphabet1[magazine[i]-'a']++; 13 | } 14 | for(int i = 0; i < ransomNote.size(); i++){ 15 | if(alphabet[ransomNote[i]-'a'] <= alphabet1[ransomNote[i]-'a']){ 16 | a = 1; 17 | } 18 | else{ 19 | a = 0; 20 | break; 21 | } 22 | } 23 | return a; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /732. My Calendar III/732. My Calendar III.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class MyCalendarThree { 3 | public: 4 | vector s; 5 | vector e; 6 | int maxi = 0; 7 | 8 | void check(){ 9 | sort(s.begin(),s.end()); 10 | sort(e.begin(),e.end()); 11 | int i = 0, j = 0, ans = 0; 12 | while(i& tokens, int power) { 5 | int c = 0; 6 | sort(tokens.begin(),tokens.end()); 7 | int l = 0, r = tokens.size()-1; 8 | int ans = 0; 9 | while(l <= r){ 10 | if(power >= tokens[l]){ 11 | c++; 12 | ans = max(ans,c); 13 | power -= tokens[l]; 14 | l++; 15 | } 16 | else if(power < tokens[l] && c>=1){ 17 | power += tokens[r]; 18 | c--; r--; 19 | } 20 | else if(c==0) 21 | break; 22 | } 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /659. Split Array into Consecutive Subsequences/659. Split Array into Consecutive Subsequences.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | bool isPossible(vector& nums){ 5 | unordered_map left,end; 6 | for(int i:nums){ 7 | left[i]++; 8 | } 9 | for(int i:nums){ 10 | if(left[i]!=0){ 11 | left[i]--; 12 | if(end[i-1]>0){ 13 | end[i-1]--; 14 | end[i]++; 15 | } 16 | else if(left[i+1]>0 && left[i+2]>0){ 17 | left[i+1]--; 18 | left[i+2]--; 19 | end[i+2]++; 20 | } 21 | else return false; 22 | } 23 | } 24 | return true; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /823. Binary Trees With Factors/823. Binary Trees With Factors.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int numFactoredBinaryTrees(vector& A) { 5 | sort(A.begin(), A.end()); 6 | const long Mod = 1e9+7; 7 | unordered_map dp; 8 | for(int i = 0; i < A.size(); i++){ 9 | dp[A[i]] = 1; 10 | for(int j = 0; j < i; j++){ // A[i] = A[j] * A[i]/A[j] 11 | if(A[i] % A[j] ==0 && dp.count(A[i] / A[j])){ 12 | dp[A[i]] += (dp[A[j]] * dp[A[i] / A[j]]) % Mod; 13 | } 14 | } 15 | } 16 | long ans = 0; 17 | for(auto& i: dp) 18 | ans = (ans + i.second) % Mod; 19 | return ans % Mod; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /91. Decode Ways/91. Decode Ways.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int numDecodings(string s){ 5 | vector hg(s.size()+1, 0); 6 | int ans = 0; 7 | ans = helper(s, 0, hg); 8 | return ans; 9 | } 10 | int helper(string s, int i, vector&hs) { 11 | if(s.size() == i) return 1; 12 | if(s[i] == '0') return 0; 13 | if(hs[i] > 0) return hs[i]; 14 | if(i == s.size()-1) return 1; 15 | if(i < s.size()-1){ 16 | int num = (s[i] - '0') * 10 + (s[i+1] - '0'); 17 | if(num>0 && num<=26){ 18 | return hs[i] = helper(s, i+1, hs) + helper(s, i+2, hs); 19 | } 20 | } 21 | return hs[i]=helper(s, i+1, hs); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /98. Validate Binary Search Tree/98. Validate Binary Search Tree.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | 5 | bool isBST(TreeNode *root, long long int min, long long int max){ 6 | 7 | bool l=true,r=true; 8 | if(!root->left && !root->right){ 9 | if(!(root->val >min && root->val < max)) 10 | return false; 11 | else 12 | return true; 13 | } 14 | if(!(root->val >min && root->val < max)) 15 | return false; 16 | 17 | if(root->left) 18 | l = isBST(root->left, min, root->val); 19 | if(root->right) 20 | r = isBST(root->right, root->val, max); 21 | 22 | return l && r; 23 | } 24 | 25 | bool isValidBST(TreeNode* root) { 26 | long long int min = (long long int)INT_MIN-1, max = (long long int)INT_MAX+1; 27 | return isBST(root,min,max); 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /42. Trapping Rain Water/42. Trapping Rain Water.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int trap(vector& height) { 5 | 6 | if (height.empty()) return 0; 7 | int trapWater = 0; 8 | int size = height.size(); 9 | vector left_max(size), right_max(size); 10 | left_max[0] = height[0]; 11 | for (int i = 1; i < size; i++) { 12 | left_max[i] = max(height[i], left_max[i - 1]); 13 | } 14 | right_max[size - 1] = height[size - 1]; 15 | for (int i = size - 2; i >= 0; i--) { 16 | right_max[i] = max(height[i], right_max[i + 1]); 17 | } 18 | for (int i = 1; i < size - 1; i++) { 19 | trapWater += min(left_max[i], right_max[i]) - height[i]; 20 | } 21 | return trapWater; 22 | 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /1338. Reduce Array Size to The Half/1338. Reduce Array Size to The Half.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int minSetSize(vector& arr) { 5 | 6 | mapmp; 7 | for(auto i:arr){ 8 | mp[i]++; 9 | } 10 | 11 | vector>use; 12 | for(auto i:mp){ 13 | use.push_back({i.second,i.first}); 14 | } 15 | sort(use.begin(),use.end(),greater>()); 16 | int n=arr.size(); 17 | 18 | int sum=0,count=0; 19 | for(int i=0;i p1, pair p2){ 6 | return p1.second>> m; 10 | TimeMap() { 11 | 12 | } 13 | 14 | void set(string key, string value, int timestamp) { 15 | m[key].push_back({value, timestamp}); 16 | } 17 | 18 | string get(string key, int timestamp) { 19 | auto itr = upper_bound(m[key].begin(), m[key].end(), make_pair("", timestamp), compare); 20 | if(itr == m[key].begin()){ 21 | return ""; 22 | } 23 | 24 | return prev(itr) -> first; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /43. Multiply Strings/43. Multiply Strings.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | string multiply(string num1, string num2) { 5 | if(num1[0] == '0' || num2[0] == '0') return "0"; 6 | vector v(num1.size()+num2.size(),0); 7 | for(int i = num2.size()-1; i >= 0; i--){ 8 | for(int j = num1.size()-1; j >= 0; j--){ 9 | v[i+j+1] += (num2[i]-'0')*(num1[j]-'0'); 10 | v[i+j] += v[i+j+1]/10; 11 | v[i+j+1] %= 10; 12 | } 13 | } 14 | string str; 15 | int c = 0; 16 | for(int i = 0; i remap; 5 | public: 6 | Solution(int n, vector& blacklist) 7 | { 8 | sort(blacklist.begin(), blacklist.end()); 9 | last = n - 1; 10 | 11 | for(auto p = blacklist.rbegin(); p != blacklist.rend(); ++p, last--){ 12 | if(*p != last){ 13 | if(remap.count(last)){ 14 | remap[*p] = remap[last]; 15 | }else{ 16 | remap[*p] = last; 17 | } 18 | } 19 | } 20 | } 21 | 22 | int pick() 23 | { 24 | const int m = rand() % (last + 1); 25 | return remap.count(m) ? remap[m] : m; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /39. Combination Sum/39. Combination Sum.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | vector> combinationSum(vector& candidates, int target) { 5 | vector> ans; 6 | vector temp; 7 | ans_fun(0, temp, ans, candidates, target, 0); 8 | return ans; 9 | } 10 | 11 | void ans_fun(int sums, vector&temp, vector>&ans, vector &cand, int targ, int pos){ 12 | if(sums == targ){ 13 | ans.push_back(temp); 14 | } 15 | if(sums < targ && pos < cand.size()){ 16 | temp.push_back(cand[pos]); 17 | ans_fun(sums + cand[pos], temp, ans, cand, targ, pos); 18 | temp.pop_back(); 19 | ans_fun(sums, temp, ans, cand, targ, pos+1); 20 | } 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /450. Delete Node in a BST/450. Delete Node in a BST.cpp: -------------------------------------------------------------------------------- 1 | //Devendra Uraon 2 | class Solution { 3 | public: 4 | TreeNode* deleteNode(TreeNode* root, int key) { 5 | TreeNode* head = new TreeNode(0); 6 | head->left = root; 7 | dfs(head, root, key); 8 | return head->left; 9 | } 10 | 11 | void dfs(TreeNode* pre, TreeNode* cur, int key){ 12 | if(!cur) return; 13 | cur->val > key ? dfs(cur, cur->left, key) : cur->val < key ? dfs(cur, cur->right, key) : helper(pre, cur); 14 | } 15 | 16 | void helper(TreeNode* pre, TreeNode* cur){ 17 | TreeNode* p = cur->left; 18 | while(p && p->right) p = p->right; 19 | p ? p->right = cur->right : cur->left = cur->right; 20 | cur == pre->left ? pre->left = cur->left : pre->right = cur->left; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /2136. Earliest Possible Day of Full Bloom/2136. Earliest Possible Day of Full Bloom.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int earliestFullBloom(vector& plantTime, vector& growTime) { 5 | vector> vec; 6 | int n = plantTime.size(); 7 | for (int i = 0; i < n; i++) { 8 | vec.push_back({plantTime[i], growTime[i]}); 9 | } 10 | 11 | sort(begin(vec), end(vec), [](auto &p1, auto &p2){ 12 | return p1.second == p2.second ? p1.first < p2.first : p1.second > p2.second; 13 | }); 14 | int farthest; 15 | int prev = 0; 16 | for (int i = 0; i < n; i++) { 17 | prev += vec[i].first; 18 | farthest = max(farthest, prev + vec[i].second); 19 | } 20 | return farthest; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /1155. Number of Dice Rolls With Target Sum/1155. Number of Dice Rolls With Target Sum.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | #define ll long long 3 | class Solution { 4 | public: 5 | ll mod = 1e9+7; 6 | ll ans = 0; 7 | int cal(int n, int k, int tar, vector>& dp ){ 8 | if(tar<0) return 0; 9 | if(n == 0){ 10 | if(tar == 0) return 1; 11 | return 0; 12 | } 13 | if(dp[n][tar] != -1) return dp[n][tar]; 14 | ll ans = 0; 15 | for(int i = 1 ; i <= k; i++){ 16 | ans += cal(n-1 , k , tar-i ,dp ) % mod; 17 | } 18 | return dp[n][tar] = ans % mod; 19 | } 20 | 21 | int numRollsToTarget(int n, int k, int target) { 22 | vector>dp(n+1 , vector(target+1 , -1 )); 23 | return cal(n , k, target, dp ) ; 24 | 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /188. Best Time to Buy and Sell Stock IV/188. Best Time to Buy and Sell Stock IV.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int fun(vector& prices,bool buy, int k, int day, vector>> &dp){ 5 | if(day >= prices.size()) return 0; 6 | if(k <= 0) return 0; 7 | if(dp[day][k][buy] != -1) return dp[day][k][buy]; 8 | int a = 0, b = 0; 9 | if(buy) a = fun(prices, false, k, day+1, dp) - prices[day]; 10 | else a = fun(prices, true, k-1, day+1, dp) + prices[day]; 11 | b = fun(prices, buy, k, day+1, dp); 12 | return dp[day][k][buy] = max(a, b); 13 | } 14 | int maxProfit(int k, vector& prices) { 15 | vector>> dp(prices.size(), vector> (k+1, vector (2,-1))); 16 | return fun(prices, true, k, 0, dp); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /1383. Maximum Performance of a Team/1383. Maximum Performance of a Team.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int maxPerformance(int n, std::vector &speed, vector &efficiency, int k){ 5 | vector> vec; 6 | long sum = 0, result = 0; 7 | for (int i = 0; i < n; i++) vec.push_back({efficiency[i], speed[i]}); 8 | 9 | sort(vec.rbegin(), vec.rend()); 10 | 11 | priority_queue, greater> pq; 12 | for (int i = 0; i < n; i++) { 13 | if (i >= k) { 14 | sum -= pq.top(); 15 | pq.pop(); 16 | } 17 | sum += vec[i].second; 18 | pq.push(vec[i].second); 19 | result = max(result, sum * vec[i].first); 20 | } 21 | return result % static_cast(1e9 + 7); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /1329. Sort the Matrix Diagonally/1329. Sort the Matrix Diagonally.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | vector> diagonalSort(vector>& mat) { 5 | int i, j, k, l, p, q, n = mat.size(), m = mat[0].size(); 6 | for(i = 0; i < n; i++){ 7 | vector a; 8 | for(j = i, k = 0; j < n && k < m; j++, k++) 9 | a.push_back(mat[j][k]); 10 | sort(a.begin(), a.end()); 11 | for(j = i, k = 0; j < n && k < m; j++, k++) 12 | mat[j][k] = a[k]; 13 | } 14 | for(i = 0; i < m; i++){ 15 | vector a; 16 | for(j = i, k = 0; j < m && k < n; j++, k++) 17 | a.push_back(mat[k][j]); 18 | sort(a.begin(), a.end()); 19 | for(j = i, k = 0; j < m && k < n; j++, k++) 20 | mat[k][j] = a[k]; 21 | } 22 | return mat; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /16. 3Sum Closest/16. 3Sum Closest.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int threeSumClosest(vector& nums, int target) { 5 | sort(nums.begin(), nums.end()); 6 | int n = nums.size(), sum = nums[0] + nums[1] + nums[2]; 7 | int closest = abs(target - sum); 8 | for(int first = 0; first < n-2; first++){ 9 | int second = first + 1, third = n -1; 10 | while(second < third){ 11 | int sum2 = nums[first] + nums[second]+ nums[third]; 12 | int checking = abs(target - sum2); 13 | if(closest > checking){ 14 | sum = sum2; 15 | closest = checking; 16 | } 17 | if(sum2 > target) third--; 18 | else second++; 19 | } 20 | } 21 | return sum; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /1770. Maximum Score from Performing Multiplication Operations/1770. Maximum Score from Performing Multiplication Operations.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int solve(vector& nums, vector& multipliers,int s,int e,int i,vector>&dp){ 5 | if(i == multipliers.size()) return 0; 6 | if(dp[i][s] != INT_MIN) return dp[i][s]; 7 | 8 | int left = multipliers[i] * nums[s] + solve(nums, multipliers, s+1, e, i+1, dp); 9 | int right = multipliers[i] * nums[e] + solve(nums, multipliers, s, e-1, i+1, dp); 10 | 11 | return dp[i][s] = max(left, right); 12 | } 13 | int maximumScore(vector& nums, vector& multipliers) { 14 | int n = nums.size() , m = multipliers.size(); 15 | vector>dp(1001, vector(1001, INT_MIN)); 16 | return solve(nums, multipliers, 0, n-1, 0, dp); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /718. Maximum Length of Repeated Subarray/718. Maximum Length of Repeated Subarray.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int findLength(vector& nums1, vector& nums2) { 5 | int n = nums1.size(), m = nums2.size(); 6 | vector> dp(n+1, vector(m+1)); 7 | for(int i = 0; i < n+1; i++){ 8 | for(int j = 0; j < m+1; j++){ 9 | if(i==0 || j==0) 10 | dp[i][j] = 0; 11 | } 12 | } 13 | 14 | int ans = 0; 15 | for(int i = 1; i < n+1; i++){ 16 | for(int j = 1; j < m+1; j++){ 17 | if(nums1[i-1] == nums2[j-1]){ 18 | dp[i][j] = 1 + dp[i-1][j-1]; 19 | ans = max(ans, dp[i][j]); 20 | } 21 | else 22 | dp[i][j] = 0; 23 | } 24 | } 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /1457. Pseudo-Palindromic Paths in a Binary Tree/1457. Pseudo-Palindromic Paths in a Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | void solve(TreeNode* root, vector v,int &count){ 5 | if (!root->left && !root->right){ // at a leaf node 6 | v[root->val-1] = v[root->val -1] +1; 7 | int odd = 0; 8 | for(int i:v){ 9 | if (i%2 == 1) odd++; 10 | } 11 | if (odd <=1 ) count++; 12 | return; 13 | } 14 | v[root -> val-1] = v[root -> val -1] +1; 15 | if (root -> left) solve(root -> left, v,count); 16 | if (root -> right) solve(root -> right, v,count); 17 | } 18 | int pseudoPalindromicPaths (TreeNode* root) { 19 | vector v(9, 0); 20 | int count = 0; 21 | solve(root, v,count); 22 | return count; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /1578. Minimum Time to Make Rope Colorful/1578. Minimum Time to Make Rope Colorful.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int minCost(string s, vector& v) { 5 | vector> v1; 6 | for(int i = 0; i < v.size(); i++){ 7 | int j = i + 1; 8 | vector x; 9 | x.push_back(v[i]); 10 | while(j<(int)v.size() && s[i]==s[j]){ 11 | x.push_back(v[j]); 12 | j++; 13 | } 14 | v1.push_back(x); 15 | i = j - 1; 16 | } 17 | 18 | int ans = 0; 19 | for(int i = 0; i < v1.size(); i++){ 20 | if(v1[i].size() == 1) continue; 21 | sort(v1[i].begin(), v1[i].end()); 22 | for(int j = 0; j < v1[i].size()-1; j++) 23 | ans += v1[i][j]; 24 | } 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /890. Find and Replace Pattern/890. Find and Replace Pattern.cpp: -------------------------------------------------------------------------------- 1 | //Devendra Uraon 2 | class Solution { 3 | public: 4 | bool compare(string a, string b){ 5 | if(a.size() != b.size()) return false; 6 | for(int i =0 ; i < a.size(); ++i){ 7 | for(int j = 0; j <= a.size(); ++j){ 8 | if(b[i] == b[j] && a[i] != a[j]){ 9 | return false; 10 | } else if(b[i] != b[j] && a[i] == a[j]){ 11 | return false; 12 | } 13 | } 14 | } 15 | return true; 16 | } 17 | vector findAndReplacePattern(vector& words, string pattern) { 18 | vector v; 19 | for(int i = 0; i < words.size(); ++i){ 20 | if(compare(words[i], pattern) == true){ 21 | v.push_back(words[i]); 22 | } 23 | } 24 | return v; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /1035. Uncrossed Lines/1035. Uncrossed Lines.cpp: -------------------------------------------------------------------------------- 1 | //Devendra Uraon 2 | class Solution { 3 | public: 4 | int maxUncrossedLines(vector& nums1, vector& nums2) { 5 | unordered_map um; 6 | int n = nums1.size(); 7 | int m = nums2.size(); 8 | 9 | int dp[2][m+1]; 10 | dp[0][0] = 0; 11 | dp[1][0] = 0; 12 | for(int j=1;j<=m;++j){ 13 | dp[0][j] = 0; 14 | if(nums1[0] == nums2[j-1]) dp[0][j] = 1; 15 | dp[0][j] = max(dp[0][j],dp[0][j-1]); 16 | } 17 | for(int i=1;i& preorder, vector& inorder) { 5 | return buildTree(preorder, inorder, 0, preorder.size()-1, 0, inorder.size()-1); 6 | } 7 | 8 | private: 9 | TreeNode* buildTree(vector& preorder, vector& inorder, int prel, int prer, int inl, int inr) { 10 | if (prel > prer) return NULL; 11 | TreeNode* root = new TreeNode(preorder[prel]); 12 | int pos; 13 | for (pos=inl; pos<=inr; pos++) 14 | if (root->val == inorder[pos]) 15 | break; 16 | root->left = buildTree(preorder, inorder, prel+1, prel+pos-inl, inl, pos-1); 17 | root->right = buildTree(preorder, inorder, prel+pos-inl+1, prer, pos+1, inr); 18 | return root; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /987. Vertical Order Traversal of a Binary Tree/987. Vertical Order Traversal of a Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | void helper(TreeNode* root, map>> &mp, int row, int col){ 5 | if(root==NULL){ 6 | return; 7 | } 8 | mp[col].insert({row, root->val}); 9 | helper(root->right, mp, row+1, col+1); 10 | helper(root->left, mp, row+1, col-1); 11 | return; 12 | } 13 | vector> verticalTraversal(TreeNode* root) { 14 | map>> mp; 15 | helper(root, mp, 0, 0); 16 | vector> ans; 17 | for(auto it:mp){ 18 | vector temp; 19 | for(auto itr:it.second){ 20 | temp.push_back(itr.second); 21 | } 22 | ans.push_back(temp); 23 | } 24 | return ans; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /967. Numbers With Same Consecutive Differences/967. Numbers With Same Consecutive Differences.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | vector numsSameConsecDiff(int n, int k) { 5 | vectort; 6 | for(int i=1;i<=9;i++){ 7 | if(i-k>=0){ 8 | t.push_back(11*i-k); 9 | } 10 | if(i+k<=9 && k!=0){ 11 | t.push_back(11*i+k); 12 | } 13 | } 14 | n = n-2; 15 | vectortm; 16 | while(n--){ 17 | for(int i:t){ 18 | int m = i%10; 19 | if(m-k>=0){ 20 | tm.push_back(10*i+m-k); 21 | } 22 | if(m+k<=9 && k!=0){ 23 | tm.push_back(10*i+m+k); 24 | } 25 | } 26 | t = tm; 27 | tm.clear(); 28 | } 29 | return t; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /218. The Skyline Problem/218. The Skyline Problem.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | vector> getSkyline(vector>& buildings) { 5 | vector> heights; 6 | vector> res; 7 | for(auto x: buildings){ 8 | heights.push_back(pair(x[0],-x[2])); 9 | heights.push_back(pair(x[1],x[2])); 10 | } 11 | sort(heights.begin(), heights.end()); 12 | int prev=0,cur; 13 | multiset st; 14 | st.insert(0); 15 | for(auto h: heights){ 16 | if(h.second<0){st.insert(-h.second);} 17 | else st.erase(st.find(h.second)); 18 | cur = *st.rbegin(); 19 | if(cur!=prev){ 20 | vector v{h.first, cur}; 21 | res.push_back(v); 22 | prev = cur; 23 | } 24 | } 25 | return res; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /508. Most Frequent Subtree Sum/508. Most Frequent Subtree Sum.cpp: -------------------------------------------------------------------------------- 1 | //Devendra Uraon 2 | class Solution { 3 | public: 4 | vector findFrequentTreeSum(TreeNode* root) { 5 | std::vector results; 6 | int max = 0; 7 | 8 | ConvertTree(root); 9 | 10 | for(std::pair pr : mp) { 11 | if(max < pr.second) 12 | max = pr.second; 13 | } 14 | 15 | for(std::pair pr : mp) { 16 | if(pr.second == max) 17 | results.push_back(pr.first); 18 | } 19 | 20 | return results; 21 | } 22 | private: 23 | int ConvertTree(TreeNode* root) { 24 | if(!root) 25 | return 0; 26 | int l = ConvertTree(root->left), r = ConvertTree(root->right); 27 | root->val = root->val + l + r; 28 | mp[root->val]++; 29 | return root->val; 30 | } 31 | 32 | std::unordered_map mp; 33 | }; 34 | -------------------------------------------------------------------------------- /1636. Sort Array by Increasing Frequency/1636. Sort Array by Increasing Frequency.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | bool static comp(pair a,pair b) 5 | { 6 | if(a.second==b.second) 7 | return a>b; 8 | else 9 | return a.second frequencySort(vector& nums) { 12 | unordered_map mp; 13 | for(int i=0;i > vec; 18 | for(auto pr:mp) 19 | { 20 | vec.push_back(pr); 21 | } 22 | sort(vec.begin(),vec.end(), comp); 23 | 24 | vector ans; 25 | for(int i=0;i0) 28 | { 29 | ans.push_back(vec[i].first); 30 | vec[i].second--; 31 | } 32 | } 33 | return ans; 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /881. Boats to Save People/881. Boats to Save People.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | bool isPossible(vector& people, int no_boats, int& limit) { 5 | int left = 0, right = people.size() - 1; 6 | while(left < right and no_boats) { 7 | if(people[left] + people[right] <= limit) { 8 | left++, right--; 9 | } 10 | else right--; 11 | no_boats--; 12 | } 13 | if(left == right) return no_boats; //no boats 14 | return left > right; 15 | } 16 | int numRescueBoats(vector& people, int limit) { 17 | sort(people.begin(), people.end()); 18 | int minval = 1, maxval= people.size(), medval; 19 | while(minval <= maxval) { 20 | medval = minval + ((maxval - minval)>>1); 21 | if(isPossible(people, medval, limit)) maxval = medval - 1; 22 | else minval = medval + 1; 23 | } 24 | return minval; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /102. Binary Tree Level Order Traversal/102. Binary Tree Level Order Traversal.cpp: -------------------------------------------------------------------------------- 1 | //Devendra Uraon 2 | class Solution { 3 | public: 4 | vector> levelOrder(TreeNode* root) 5 | { 6 | vector list; 7 | 8 | if (root != NULL) 9 | list.push_back(root); 10 | 11 | vector> res; 12 | while (list.size() > 0) 13 | { 14 | vector op = list; 15 | 16 | list.clear(); 17 | vector v; 18 | for (TreeNode* &node : op) 19 | { 20 | v.push_back(node->val); 21 | 22 | if (node->left != NULL) 23 | list.push_back(node->left); 24 | if (node->right != NULL) 25 | list.push_back(node->right); 26 | } 27 | 28 | res.push_back(v); 29 | } 30 | 31 | return res; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /234. Palindrome Linked List/234. Palindrome Linked List.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | ListNode* reverse(ListNode* slow){ 5 | ListNode* dummy = NULL; 6 | while(slow!=NULL){ 7 | ListNode* next = slow->next; 8 | slow->next = dummy; 9 | dummy = slow; 10 | slow = next; 11 | } 12 | return dummy; 13 | } 14 | 15 | bool isPalindrome(ListNode* head) { 16 | ListNode* slow = head, *fast=head; 17 | while(fast->next && fast->next->next){ 18 | slow = slow->next; 19 | fast = fast->next->next; 20 | } 21 | ListNode* temp = slow; 22 | slow->next = reverse(slow->next); 23 | slow = slow->next; 24 | while(slow!=NULL){ 25 | if(head->val != slow->val) 26 | return false; 27 | head = head->next; 28 | slow = slow->next; 29 | } 30 | return true; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /429. N-ary Tree Level Order Traversal/429. N-ary Tree Level Order Traversal.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | vector> levelOrder(Node* root) { 5 | vector> ans; 6 | queue> q; 7 | vector temp; 8 | 9 | if(!root) return ans; 10 | 11 | temp.push_back(root -> val); 12 | q.push(root -> children); 13 | ans.push_back(temp); 14 | 15 | while(!q.empty()){ 16 | int size = q.size(); 17 | vector temp; 18 | while(size--){ 19 | auto cur = q.front(); 20 | q.pop(); 21 | 22 | for(auto it:cur){ 23 | q.push(it -> children); 24 | temp.push_back(it -> val); 25 | } 26 | } 27 | if(temp.size() > 0) 28 | ans.push_back(temp); 29 | } 30 | return ans; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /135. Candy/135. Candy.cpp: -------------------------------------------------------------------------------- 1 | //Devendra Uraon 2 | class Solution { 3 | public: 4 | int candy(vector& ratings) { 5 | int sum=1, height=0, heightVal=INT_MAX, current=1; 6 | for(int i=1;iratings[i-1]){ 8 | current++; 9 | heightVal=current; 10 | height=i; 11 | sum+=current; 12 | } 13 | else if(ratings[i] left = root; 8 | return temp; 9 | } 10 | insertNode(root, val, depth, 1); 11 | return root; 12 | } 13 | 14 | void insertNode(TreeNode* root, int val, int depth, int current){ 15 | if(root == NULL) return; 16 | if(current != depth-1){ 17 | insertNode(root -> left, val, depth, current+1); 18 | insertNode(root -> right, val, depth, current+1); 19 | } 20 | else{ 21 | TreeNode *temp = root -> left; 22 | root -> left = new TreeNode (val); 23 | root -> left -> left = temp; 24 | temp = root -> right; 25 | root -> right = new TreeNode (val); 26 | root -> right -> right = temp; 27 | } 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /1531. String Compression II/1531. String Compression II.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | vector> dp; 5 | int find(int n){ 6 | if(n==1) return 1; 7 | else if(n>1 && n<10) return 2; 8 | else if(n>=10 && n<100) return 3; 9 | else return 4; 10 | } 11 | int dfs(string &s, int in, int k){ 12 | if(k<0) return INT_MAX; 13 | if(in>=s.size() || (s.size()-in)<=k) return 0; 14 | if(dp[in][k]!=-1) return dp[in][k]; 15 | 16 | int n = 0, t = 0, i = in; 17 | char ch = s[in]; 18 | int re = dfs(s, in+1, k-1); 19 | 20 | for(; i>(s.size(), vector(k+1, -1)); 29 | return dfs(s, 0, k); 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /692. Top K Frequent Words/692. Top K Frequent Words.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | struct myComp { 4 | bool operator () (const pair& p1, const pair& p2) { 5 | if(p1.second!=p2.second) 6 | return p1.second topKFrequent(vector& words, int k) { 14 | unordered_map m; 15 | 16 | for(const string& word: words) { 17 | m[word]++; 18 | } 19 | 20 | priority_queue, deque>, myComp> pq(m.begin(), m.end()); 21 | 22 | vector ans; 23 | ans.reserve(k); 24 | 25 | for(int i=0; i st; 21 | for(int i=0;i&arr, vector&taken, int pos, int &ans){ 5 | if(pos == arr.size()){ 6 | ans = max(ans, accumulate(taken.begin(), taken.end(), 0)); 7 | return ; 8 | } 9 | 10 | solve(arr, taken, pos+1, ans); 11 | 12 | int i = 0; 13 | for(; i < arr[pos].size(); ++i){ 14 | if(taken[arr[pos][i]-'a']) break; 15 | ++taken[arr[pos][i]-'a']; 16 | } 17 | 18 | if(i < arr[pos].size()){ 19 | for(--i; i>=0; --i) --taken[arr[pos][i]-'a']; return; 20 | } 21 | 22 | solve(arr,taken,pos+1,ans); 23 | 24 | for(auto a : arr[pos]) 25 | --taken[a-'a']; 26 | } 27 | int maxLength(vector& arr) { 28 | int ans = 0; 29 | vector taken(26, 0); 30 | solve(arr, taken, 0, ans); 31 | return ans; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /1220. Count Vowels Permutation/1220. Count Vowels Permutation.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int mod = 1e9 + 7; 5 | int countVowelPermutation(int n) { 6 | int p = 0; 7 | vector>dp(n ,vector(5 , 0)); 8 | dp[0][0] = 1; dp[0][1] = 2; dp[0][2] = 4; dp[0][3] = 2; dp[0][4] = 1; 9 | if(n == 1) return 5 ; 10 | if(n > 2){ 11 | for(int i = 1; i <= n-2; i++){ 12 | for(int j = 0; j < 5;j++){ 13 | if(j == 0) dp[i][j] +=(dp[i-1][1])%mod; 14 | if(j == 1) dp[i][j] +=(dp[i-1][0] + dp[i-1][2])%mod; 15 | if(j == 2) dp[i][j] +=(dp[i-1][0] + dp[i-1][1] + dp[i-1][3] + dp[i-1][4])%mod; 16 | if(j == 3) dp[i][j] +=(dp[i-1][2] + dp[i-1][4])%mod; 17 | if(j == 4) dp[i][j] +=(dp[i-1][0])%mod; 18 | } 19 | p = i; 20 | } 21 | } 22 | long long int ans = 0 ; 23 | for(int j = 0; j < 5; j++) 24 | ans += dp[p][j]%mod; 25 | return ans% mod; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /2007. Find Original Array From Doubled Array/2007. Find Original Array From Doubled Array.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int arr[100001]; 5 | vector findOriginalArray(vector& changed) { 6 | 7 | vectorres; 8 | 9 | for (int i = 0; i < changed.size(); i++){ 10 | arr[changed[i]]++; 11 | } 12 | 13 | if (arr[0]){ 14 | if (arr[0] % 2 == 0){ 15 | while(arr[0] > 0){ 16 | arr[0] -= 2; 17 | res.push_back(0); 18 | } 19 | }else{ 20 | return res; 21 | } 22 | } 23 | 24 | for (int i = 1; i <= 50001; i++){ 25 | if (arr[i]){ 26 | if (arr[i] <= arr[i * 2]){ 27 | while(arr[i]-- && arr[i * 2]--) 28 | res.push_back(i); 29 | }else{ 30 | return vector(); 31 | } 32 | } 33 | } 34 | return res; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /76. Minimum Window Substring/76. Minimum Window Substring.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | string minWindow(string s, string t) { 5 | int i=0; 6 | int j=0; 7 | int mini=INT_MAX; 8 | unordered_mapm; 9 | for(int i=0;ij-i+1){ 24 | mini=min(mini,j-i+1); 25 | ans=s.substr(i,j-i+1); 26 | } 27 | if(m.find(s[i])!=m.end()){ 28 | m[s[i]]++; 29 | if(m[s[i]]==1){ 30 | count++; 31 | } 32 | } 33 | i++; 34 | } 35 | j++; 36 | } 37 | return ans; 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /106. Construct Binary Tree from Inorder and Postorder Traversal/106. Construct Binary Tree from Inorder and Postorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | //Devendra Uraon 2 | class Solution { 3 | public: 4 | TreeNode* buildTree(vector& inorder, vector& postorder) { 5 | unordered_map map; 6 | for (int i = 0; i < inorder.size(); ++i) 7 | { 8 | map[inorder[i]] = i; 9 | } 10 | 11 | return helper(0, postorder.size() - 1, 0, inorder.size() - 1, postorder, map); 12 | } 13 | 14 | TreeNode* helper(int poStart, int poEnd, int inStart, int inEnd, vector& postorder, unordered_map& map) 15 | { 16 | if (poStart > poEnd || inStart > inEnd) 17 | { 18 | return NULL; 19 | } 20 | 21 | int mid = postorder[poEnd]; 22 | TreeNode* root = new TreeNode(mid); 23 | int inIndex = map[mid]; 24 | 25 | root->left = helper(poStart, poStart + inIndex - inStart - 1, inStart, inIndex - 1, postorder, map); 26 | root->right = helper(poStart + inIndex - inStart, poEnd - 1, inIndex + 1, inEnd, postorder, map); 27 | 28 | return root; 29 | } 30 | 31 | }; 32 | -------------------------------------------------------------------------------- /501. Find Mode in Binary Search Tree/501. Find Mode in Binary Search Tree.cpp: -------------------------------------------------------------------------------- 1 | //Devendra Uraon 2 | class Solution { 3 | vector result; 4 | int maxreps = 0; 5 | void solve(TreeNode* cur, int & curval, int & reps) 6 | { 7 | if (cur == nullptr) 8 | return; 9 | solve(cur->left, curval, reps); 10 | if (cur->val == curval) 11 | reps ++; 12 | else{ 13 | if (reps > maxreps) 14 | { 15 | result.clear(); 16 | result.push_back(curval); 17 | maxreps = reps; 18 | }else if (reps > 0 && reps == maxreps) 19 | result.push_back(curval); 20 | curval = cur->val, reps = 1; 21 | } 22 | solve(cur->right, curval, reps); 23 | } 24 | public: 25 | vector findMode(TreeNode* root) { 26 | int curval = 0, reps = 0; 27 | solve(root, curval, reps); 28 | if (reps > maxreps) 29 | { 30 | result.clear(); 31 | result.push_back(curval); 32 | maxreps = reps; 33 | }else if (reps > 0 && reps == maxreps) 34 | result.push_back(curval); 35 | return result; 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /363. Max Sum of Rectangle No Larger Than K/363. Max Sum of Rectangle No Larger Than K.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int getSum(vector& arr,int k){ 5 | int maxi = INT_MIN; 6 | int n = arr.size(); 7 | int sum=0; 8 | set s; 9 | s.insert(0); 10 | for(int i = 0; i < n; i++){ 11 | sum += arr[i]; 12 | 13 | auto it = s.lower_bound(sum-k); 14 | if(it != s.end()){ 15 | maxi = max(maxi,sum -* it); 16 | } 17 | 18 | s.insert(sum); 19 | } 20 | return maxi; 21 | } 22 | 23 | int maxSumSubmatrix(vector>& matrix, int k) { 24 | int n = matrix.size(); 25 | int m = matrix[0].size(); 26 | int maxi = INT_MIN; 27 | for(int i = 0; i <= n; i++){ 28 | vector arr(m, 0); 29 | for(int j = i; j < n; j++){ 30 | for(int k = 0; k < m; k++){ 31 | arr[k] += matrix[j][k]; 32 | } 33 | int maxSum = getSum(arr,k); 34 | maxi = max(maxi,maxSum); 35 | } 36 | } 37 | return maxi; 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /1335. Minimum Difficulty of a Job Schedule/1335. Minimum Difficulty of a Job Schedule.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | int dp[300][11]; 4 | public: 5 | int minDifficulty(vector& jobDifficulty, int d) { 6 | memset(dp, -1, sizeof(dp)); 7 | int result = sol(jobDifficulty, 0, jobDifficulty.size()-1, d); 8 | 9 | return result == INT_MAX ? -1 : result; 10 | } 11 | 12 | int sol(vector& jobDifficulty, int s, int e, int d) 13 | { 14 | int val; 15 | 16 | if (dp[s][d] != -1) 17 | return dp[s][d]; 18 | 19 | if (d == 1) 20 | { 21 | val = jobDifficulty[s]; 22 | for (int i = s; i <=e; i++) 23 | { 24 | val = max(val, jobDifficulty[i]); 25 | } 26 | dp[s][d] = val; 27 | return val; 28 | } 29 | 30 | val = INT_MAX; 31 | int maxVal = jobDifficulty[s]; 32 | for (int i = s; e-i+1 >= d; i++) 33 | { 34 | maxVal = max(maxVal, jobDifficulty[i]); 35 | val = min(val, maxVal + sol(jobDifficulty, i+1, e, d-1)); 36 | } 37 | dp[s][d] = val; 38 | return val; 39 | } 40 | }; 41 | -------------------------------------------------------------------------------- /200. Number of Islands/200. Number of Islands.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | vector> pr = {{1, 0}, {-1, 0}, {0, 1}, {0,-1}}; 5 | void dfs(int i, int j, vector> &grid, vector> &vis){ 6 | int m = grid.size(); 7 | int n = grid[0].size(); 8 | 9 | for(auto it : pr){ 10 | int x = it.first + i; 11 | int y = it.second + j; 12 | 13 | if(x<0 || y<0 || x>=m || y>=n || vis[x][y] || grid[x][y] =='0') continue; 14 | vis[x][y] = 1; 15 | dfs(x, y, grid, vis); 16 | } 17 | } 18 | int numIslands(vector> &grid) { 19 | 20 | int m = grid.size(); 21 | int n = grid[0].size(); 22 | 23 | int numberOfIsland = 0; 24 | 25 | vector> vis(m,vector(n,0)); 26 | 27 | for(int i=0; i>&grid,vector>&directions, 5 | vector>&visited){ 6 | if(row<0 || col<0 || row==grid.size() || col==grid[0].size())return -1; 7 | if(grid[row][col]==0 || visited[row][col])return 0; 8 | visited[row][col]=true; 9 | int prev=0; 10 | for(auto [new_row,new_col]:directions){ 11 | int ans=dfs(row+new_row,col+new_col,grid,directions,visited); 12 | (prev==-1 || ans==-1)?prev=-1:prev+=ans; 13 | } 14 | return prev==-1?prev:grid[row][col]+prev; 15 | } 16 | int numEnclaves(vector>& grid) { 17 | int n=grid.size(); 18 | vector>directions{{0,-1},{0,1},{1,0},{-1,0}}; 19 | vector>visited(n,vector(grid[0].size(),false)); 20 | int res=0; 21 | for(int row=0;row findSubstring(string s, vector& words) { 5 | int n = s.length(); 6 | int wordSize = words[0].length(); 7 | vector ans; 8 | 9 | if(n hash; 12 | 13 | for(int i = 0; i <= n-wordSize*words.size(); i++){ 14 | for(int k = 0; k v; 5 | int front, size, cap, rear; 6 | 7 | MyCircularQueue(int k) { 8 | for(int i = 0; i < k; i++) v.push_back(-1); 9 | front = 0; 10 | rear = -1; 11 | size = k; 12 | cap = 0; 13 | } 14 | 15 | bool enQueue(int value) { 16 | if(!isFull()){ 17 | rear++; 18 | rear = rear % size; 19 | v[rear] = value; 20 | cap++; 21 | return true; 22 | } 23 | else return false; 24 | 25 | } 26 | 27 | bool deQueue() { 28 | if(!isEmpty()){ 29 | v[front] = -1; 30 | front++; 31 | front = front % size; 32 | cap--; 33 | return true; 34 | } 35 | else return false; 36 | 37 | 38 | } 39 | 40 | int Front() { 41 | return v[front]; 42 | } 43 | 44 | int Rear() { 45 | if(isEmpty()) return -1; 46 | else return v[rear]; 47 | } 48 | 49 | bool isEmpty() { 50 | if(cap == 0) return true; 51 | else return false; 52 | 53 | 54 | } 55 | 56 | bool isFull() { 57 | if(cap == size) return true; 58 | else return false; 59 | } 60 | }; 61 | -------------------------------------------------------------------------------- /838. Push Dominoes/838. Push Dominoes.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | 5 | bool dfs (vector >& v, int a, int b, vector & vis) { 6 | 7 | if (a == b) return true; 8 | vis [a] = true; 9 | 10 | for (int x : v [a]) { 11 | 12 | if (vis [x] == true) continue; 13 | if (dfs (v, x, b, vis)) return true; 14 | } 15 | 16 | return false; 17 | } 18 | 19 | bool equationsPossible(vector& arr) { 20 | 21 | vector > v (26); 22 | for (int i = 0; i < arr.size (); i++) { 23 | 24 | string s = arr [i]; 25 | 26 | if (s [1] == '=') { 27 | 28 | v [s [0] - 'a'].push_back (s [3] - 'a'); 29 | v [s [3] - 'a'].push_back (s [0] - 'a'); 30 | } 31 | } 32 | 33 | for (int i = 0; i < arr.size (); i++) { 34 | 35 | string s = arr [i]; 36 | vector vis (26); 37 | 38 | if (s [1] == '!') { 39 | 40 | if (dfs (v, s [0] - 'a', s [3] - 'a', vis)) return false; 41 | } 42 | } 43 | 44 | return true; 45 | } 46 | }; 47 | -------------------------------------------------------------------------------- /990. Satisfiability of Equality Equations/990. Satisfiability of Equality Equations.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | 5 | bool dfs (vector >& v, int a, int b, vector & vis) { 6 | 7 | if (a == b) return true; 8 | vis [a] = true; 9 | 10 | for (int x : v [a]) { 11 | 12 | if (vis [x] == true) continue; 13 | if (dfs (v, x, b, vis)) return true; 14 | } 15 | 16 | return false; 17 | } 18 | 19 | bool equationsPossible(vector& arr) { 20 | 21 | vector > v (26); 22 | for (int i = 0; i < arr.size (); i++) { 23 | 24 | string s = arr [i]; 25 | 26 | if (s [1] == '=') { 27 | 28 | v [s [0] - 'a'].push_back (s [3] - 'a'); 29 | v [s [3] - 'a'].push_back (s [0] - 'a'); 30 | } 31 | } 32 | 33 | for (int i = 0; i < arr.size (); i++) { 34 | 35 | string s = arr [i]; 36 | vector vis (26); 37 | 38 | if (s [1] == '!') { 39 | 40 | if (dfs (v, s [0] - 'a', s [3] - 'a', vis)) return false; 41 | } 42 | } 43 | 44 | return true; 45 | } 46 | }; 47 | -------------------------------------------------------------------------------- /1293. Shortest Path in a Grid with Obstacles Elimination/1293. Shortest Path in a Grid with Obstacles Elimination.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int shortestPath(std::vector>& grid, int k) { 5 | const std::vector dirs{0, 1, 0, -1, 0}; 6 | const int m = grid.size(), n = grid[0].size(); 7 | std::vector> seen(m, vector(n, INT_MAX)); 8 | std::queue> queue_{{{0, 0, 0}}}; 9 | int steps = 0; 10 | seen[0][0] = 0; 11 | while (!queue_.empty()) { 12 | int size = queue_.size(); 13 | while (size-- > 0) { 14 | int cx = 0, cy = 0, co = 0; 15 | std::tie(cx, cy, co) = queue_.front(); queue_.pop(); 16 | if (cx == n - 1 && cy == m - 1) return steps; 17 | for (int i = 0; i < 4; i++) { 18 | const int x = cx + dirs[i]; 19 | const int y = cy + dirs[i + 1]; 20 | if (x < 0 || y < 0 || x >= n || y >= m) continue; 21 | const int o = co + grid[y][x]; 22 | if (o >= seen[y][x] || o > k) continue; 23 | seen[y][x] = o; 24 | queue_.emplace(x, y, o); 25 | } 26 | } 27 | steps++; 28 | } 29 | return -1; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /103. Binary Tree Zigzag Level Order Traversal/103. Binary Tree Zigzag Level Order Traversal.cpp: -------------------------------------------------------------------------------- 1 | //Devendra Uraon 2 | class Solution { 3 | public: 4 | vector> zigzagLevelOrder(TreeNode* root) { 5 | vector> ans; 6 | if (!root) 7 | return ans; 8 | 9 | stack left, right; 10 | left.push(root); 11 | while(!left.empty() || !right.empty()) { 12 | vector level; 13 | if (!left.empty()) { 14 | // left is NOT empty, pop left push right 15 | while(!left.empty()) { 16 | TreeNode *node = left.top(); left.pop(); 17 | level.push_back(node->val); 18 | if (node->left) 19 | right.push(node->left); 20 | if (node->right) 21 | right.push(node->right); 22 | } 23 | } else { 24 | // Right is NOT empty, pop right push left 25 | while(!right.empty()) { 26 | TreeNode *node = right.top(); right.pop(); 27 | level.push_back(node->val); 28 | if (node->right) 29 | left.push(node->right); 30 | if (node->left) 31 | left.push(node->left); 32 | } 33 | } 34 | ans.push_back(level); 35 | } 36 | return ans; 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /133. Clone Graph/133. Clone Graph.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | /* 3 | // Definition for a Node. 4 | class Node { 5 | public: 6 | int val; 7 | vector neighbors; 8 | Node() { 9 | val = 0; 10 | neighbors = vector(); 11 | } 12 | Node(int _val) { 13 | val = _val; 14 | neighbors = vector(); 15 | } 16 | Node(int _val, vector _neighbors) { 17 | val = _val; 18 | neighbors = _neighbors; 19 | } 20 | }; 21 | */ 22 | 23 | class Solution { 24 | public: 25 | Node* cloneGraph(Node* node) { 26 | if(node == nullptr){ 27 | return nullptr; 28 | } 29 | vector v1; 30 | for(int i = 0; i < 101; i++){ 31 | Node *temp = new Node(); 32 | v1.push_back(temp); 33 | } 34 | vector visited(101, 0); 35 | queue q1; 36 | q1.push(node); 37 | visited[node -> val] = 1; 38 | v1[node -> val] -> val = node -> val; 39 | while(q1.size() > 0){ 40 | Node * temp = q1.front(); 41 | q1.pop(); 42 | for(auto x : temp -> neighbors){ 43 | if(!visited[x -> val]){ 44 | q1.push(x); 45 | visited[x -> val] = 1; 46 | } 47 | v1[x -> val] -> val = x -> val; 48 | v1[temp -> val]->neighbors.push_back(v1[x -> val]); 49 | } 50 | } 51 | return v1[node -> val]; 52 | } 53 | }; 54 | -------------------------------------------------------------------------------- /417. Pacific Atlantic Water Flow/417. Pacific Atlantic Water Flow.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | vector> pacificAtlantic(vector>& heights) { 5 | int row =heights.size(); int col = heights[0].size(); 6 | vector> pacific(row, vector(col, false)); 7 | vector> atlantic(row, vector(col, false)); 8 | 9 | for(int i = 0; i <= col; i++){ 10 | dfs(0,i,pacific,INT_MIN,heights); 11 | dfs(row-1,i,atlantic,INT_MIN,heights); 12 | } 13 | for(int j = 0 ; j <= row ; j++){ 14 | dfs(j,0,pacific,INT_MIN,heights); 15 | dfs(j,col-1,atlantic,INT_MIN,heights); 16 | } 17 | vector> ans; 18 | 19 | for(int r=0; r < row; r++){ 20 | for(int c = 0; c < col; c++){ 21 | if(pacific[r][c] && atlantic[r][c]){ 22 | ans.push_back({r, c}); 23 | } 24 | } 25 | } 26 | return ans; 27 | } 28 | 29 | void dfs(int r, int c, vector> &visit, int prev, vector> &heights){ 30 | 31 | if( r<0 || r>=heights.size() || c>=heights[0].size() || c<0 ) 32 | return; 33 | if(visit[r][c]|| heights[r][c] < prev ) return; 34 | visit[r][c] = true; 35 | dfs(r+1,c,visit,heights[r][c],heights); 36 | dfs(r-1,c,visit,heights[r][c],heights); 37 | dfs(r,c+1,visit,heights[r][c],heights); 38 | dfs(r,c-1,visit,heights[r][c],heights); 39 | } 40 | }; 41 | -------------------------------------------------------------------------------- /609. Find Duplicate File in System/609. Find Duplicate File in System.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | vector> findDuplicate(vector& paths) { 5 | unordered_map>mp; 6 | 7 | for(int i = 0; i < paths.size(); i++){ 8 | 9 | string directory=paths[i]; 10 | string s, directoryName; 11 | 12 | bool firstWord = true; 13 | stringstream ss(directory); 14 | 15 | while(getline(ss, s, ' ')){ 16 | 17 | if(firstWord){ 18 | directoryName = s; 19 | firstWord = false; 20 | } 21 | else{ 22 | int j = 0; 23 | int fileLength = 0; 24 | while(j < s.size() and s[j++]!='(') fileLength++; 25 | string fileName = s.substr(0, fileLength); 26 | 27 | j = fileLength+1; 28 | 29 | int contentLength = 0; 30 | while(j < s.size() && s[j++] != ')') contentLength++; 31 | string content = s.substr(fileLength+1, contentLength); 32 | 33 | mp[content].push_back(directoryName + "/" + fileName); 34 | } 35 | } 36 | } 37 | vector>ans; 38 | 39 | for(auto &[content, directory]:mp){ 40 | if(directory.size() == 1) continue; 41 | ans.push_back(directory); 42 | } 43 | 44 | return ans; 45 | } 46 | }; 47 | -------------------------------------------------------------------------------- /835. Image Overlap/835. Image Overlap.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | int largestOverlap(vector>&a, vector>&b) { 5 | int n = a.size(); 6 | int ans = 0; 7 | int cur = 0; 8 | 9 | for(int z=0;z<4;z++){ 10 | for(int i=n-1;i>=0;i--){ 11 | for(int j=0;jac; 15 | pairbc; 16 | ac.first = i;ac.second = 0; 17 | bc.first = 0;bc.second = n-1-j; 18 | 19 | while(tot--){ 20 | 21 | if(a[ac.first][ac.second]==1 && b[bc.first][bc.second]==1) 22 | cur++; 23 | ac.second++; 24 | bc.second++; 25 | if(ac.second==(j+1)){ 26 | ac.first++; 27 | ac.second = 0; 28 | bc.first++; 29 | bc.second = n-1-j; 30 | } 31 | } 32 | 33 | ans < cur ? ans = cur : ans = ans; 34 | } 35 | } 36 | if(z%2==0){ 37 | for(int x=0;x l; 4 | deque r; 5 | 6 | void resize() { 7 | if (l.size() > r.size() + 1) { 8 | r.push_front(l.back()); 9 | l.pop_back(); 10 | } else if (r.size() > l.size()) { 11 | l.push_back(r.front()); 12 | r.pop_front(); 13 | } 14 | } 15 | public: 16 | FrontMiddleBackQueue() { 17 | 18 | } 19 | 20 | void pushFront(int val) { 21 | l.push_front(val); 22 | resize(); 23 | } 24 | 25 | // if they aren't the same size, transfer last element in left queue to right queue before adding to middle 26 | void pushMiddle(int val) { 27 | if (l.size() > r.size()) { 28 | r.push_front(l.back()); 29 | l.pop_back(); 30 | } 31 | l.push_back(val); 32 | } 33 | 34 | void pushBack(int val) { 35 | r.push_back(val); 36 | resize(); 37 | } 38 | 39 | int popFront() { 40 | if (l.empty()) return -1; 41 | 42 | int val = l.front(); 43 | l.pop_front(); 44 | resize(); 45 | return val; 46 | } 47 | 48 | int popMiddle() { 49 | if (l.empty()) return -1; 50 | 51 | int val = l.back(); 52 | l.pop_back(); 53 | resize(); 54 | return val; 55 | } 56 | 57 | // pop back from right deque if not empty, else pop from left deque 58 | int popBack() { 59 | if (l.empty() && r.empty()) return -1; 60 | 61 | deque& curr = r.empty() ? l : r; 62 | int val = curr.back(); 63 | curr.pop_back(); 64 | resize(); 65 | return val; 66 | } 67 | }; 68 | -------------------------------------------------------------------------------- /Monotonic Renumeration.cpp: -------------------------------------------------------------------------------- 1 | //Question: https://codeforces.com/contest/1102/problem/E 2 | //Author: Devendra Uraon 3 | #include 4 | #define vi vector 5 | #define vl vector 6 | #define vc vector 7 | #define pi pair 8 | #define vp vector 9 | #define ll long long 10 | #define forloop for(int i = 0; i < n; ++i) 11 | #define input cin >> 12 | #define print cout << 13 | #define MAX 2147000000 14 | #define MOD 998244353LL 15 | using namespace std; 16 | 17 | ll modpow(ll a, ll b){ 18 | ll ans = 1; 19 | while(b > 0){ 20 | if(b % 2){ 21 | ans *= a; 22 | ans %= MOD; 23 | } 24 | a *= a; 25 | b /= 2; 26 | a %= MOD; 27 | } 28 | return ans; 29 | } 30 | 31 | int main(){ 32 | ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 33 | int n; input n; 34 | vi vec(n); 35 | map m; 36 | forloop{ 37 | input vec[i]; 38 | m[vec[i]].push_back(i); 39 | } 40 | vp v; 41 | for(auto& i : m){ 42 | if(i.second.size() == 1) continue; 43 | auto& s = i.second; 44 | sort(s.begin(), s.end()); 45 | v.push_back({s.front(), s.back()}); 46 | } 47 | int sum = 0; 48 | sort(v.begin(), v.end()); 49 | int l = -1, r = -1, cnt = 1; 50 | if(v.size()){ 51 | l = v[0].first; 52 | r = v[0].second; 53 | } 54 | for(int i = 0; i < (int)v.size(); ++i){ 55 | if(v[i].first > r){ 56 | sum += r - l + 1; 57 | l = v[i].first; 58 | r = v[i].second; 59 | cnt++; 60 | } 61 | else{ 62 | //l = min(l, v[i].first); 63 | r = max(r, v[i].second); 64 | } 65 | } 66 | sum += r - l + 1; 67 | print modpow(2, n - 1 - sum + cnt); 68 | } 69 | -------------------------------------------------------------------------------- /936. Stamping The Sequence/936. Stamping The Sequence.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | private: 4 | // >0: need to stamp 5 | // 0: no need to stamp 6 | // -1: cannot stamp 7 | int needStamp(int index, const string& stamp, string& target) { 8 | int count = 0; 9 | for (int i = 0; i < stamp.length() && i+index < target.length(); i++) { 10 | if (target[i+index] == '?') { 11 | continue; 12 | } else if (target[i+index] == stamp[i]) { 13 | count ++; 14 | } else { 15 | count = -1; 16 | break; 17 | } 18 | } 19 | if (count > 0) { 20 | for(int i = 0; i < stamp.length() && i+index < target.length(); i++) { 21 | target[i+index] = '?'; 22 | } 23 | } 24 | return count; 25 | } 26 | public: 27 | vector movesToStamp(string stamp, string target) { 28 | queue q; //record every index has not be stamped 29 | vector order; 30 | for (int i = 0; i + stamp.length() <= target.length();i++) { 31 | q.push(i); 32 | } 33 | while (true) { 34 | int length = q.size(); 35 | for (int i = 0; i < length; i++) { 36 | int index = q.front(); 37 | q.pop(); 38 | int status = needStamp(index, stamp, target); 39 | if (0 < status) { // need stamp 40 | order.push_back(index); 41 | } 42 | else if (0 > status) { // cannot be stamped 43 | q.push(index); 44 | } 45 | } 46 | if (length == q.size()) { //which means no index has been stamped 47 | break; 48 | } 49 | } 50 | 51 | if (q.size() > 0) { // is any index cannot be stamped, return empty array 52 | return vector(); 53 | } 54 | std::reverse(order.begin(),order.end()); 55 | return order; 56 | } 57 | }; 58 | -------------------------------------------------------------------------------- /336. Palindrome Pairs/336. Palindrome Pairs.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon 2 | class Solution { 3 | public: 4 | bool check_palindrome(string &s) { 5 | int left = 0; 6 | int right = s.size() - 1; 7 | 8 | while (left < right) { 9 | if (s[left] != s[right]) { 10 | return false; 11 | } 12 | left++; right--; 13 | } 14 | 15 | return true; 16 | } 17 | 18 | vector> palindromePairs(vector& words) { 19 | unordered_map rev; 20 | string temp; 21 | 22 | int n = words.size(); 23 | 24 | for (int i = 0; i < n; i++) { 25 | temp = words[i]; 26 | reverse(temp.begin(), temp.end()); 27 | 28 | rev[temp] = i; 29 | } 30 | 31 | vector> ans; 32 | 33 | if (rev.find("") != rev.end()) { 34 | for (int i = 0; i < n; i++) { 35 | //for (int i = 0; i < 1e9; i++) {} 36 | if (rev[""] == i) { 37 | continue; 38 | } 39 | 40 | if (check_palindrome(words[i])) { 41 | ans.push_back({i, rev[""]}); 42 | } 43 | } 44 | } 45 | 46 | string left, right, word; 47 | 48 | for (int i = 0; i < n; i++) { 49 | word = words[i]; 50 | 51 | left = ""; 52 | right = word; 53 | 54 | int sz = word.size(); 55 | 56 | for (int j = 0; j < sz; j++) { 57 | left.push_back(word[j]); 58 | right.erase(0, 1); 59 | 60 | if (rev.find(left) != rev.end() && rev[left] != i && check_palindrome(right)) { 61 | ans.push_back({i, rev[left]}); 62 | } 63 | 64 | if (rev.find(right) != rev.end() && rev[right] != i && check_palindrome(left)) { 65 | ans.push_back({rev[right], i}); 66 | } 67 | } 68 | } 69 | 70 | return ans; 71 | } 72 | }; 73 | -------------------------------------------------------------------------------- /Water The Trees.cpp: -------------------------------------------------------------------------------- 1 | Practical : https://drive.google.com/drive/folders/129jl153chFlt7oeZfKb513P7_i5EjYsM?usp=share_link 2 | 3 | //Question: https://codeforces.com/contest/1661/problem/C 4 | 5 | //Author: Devendra Uraon 6 | #include 7 | #define vi vector 8 | #define vl vector 9 | #define vc vector 10 | #define pi pair 11 | #define vp vector 12 | #define ll long long 13 | #define forloop for(ll i = 0; i < n; ++i) 14 | #define input cin >> 15 | #define print cout << 16 | #define MAX 2147000000 17 | #define MOD 998244353LL 18 | using namespace std; 19 | 20 | int main(){ 21 | ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 22 | ll T; cin >> T; 23 | while(T--){ 24 | ll n; input n; 25 | vl vec(n); 26 | ll mx = 0; 27 | forloop{ 28 | input vec[i]; 29 | mx = max(mx, vec[i]); 30 | } 31 | ll l = 0, r = LLONG_MAX; 32 | ll ans = LLONG_MAX; 33 | while(l <= r){ 34 | ll m = (l + r) / 2; 35 | ll one = (m + 1) / 2; 36 | ll two = m / 2; 37 | vl v; 38 | forloop{ 39 | v.push_back(mx - vec[i]); 40 | } 41 | forloop{ 42 | ll k = v[i] / 2; 43 | v[i] -= min(k, two) * 2; 44 | two -= min(k, two); 45 | } 46 | if(one >= accumulate(v.begin(), v.end(), 0LL)){ 47 | r = m - 1; 48 | ans = min(ans, m); 49 | } 50 | else{ 51 | l = m + 1; 52 | } 53 | } 54 | l = 0, r = LLONG_MAX; 55 | while(l <= r){ 56 | ll m = (l + r) / 2; 57 | ll one = (m + 1) / 2; 58 | ll two = m / 2; 59 | vl v; 60 | forloop{ 61 | v.push_back(mx + 1 - vec[i]); 62 | } 63 | forloop{ 64 | ll k = v[i] / 2; 65 | v[i] -= min(k, two) * 2; 66 | two -= min(k, two); 67 | } 68 | if(one >= accumulate(v.begin(), v.end(), 0LL)){ 69 | r = m - 1; 70 | ans = min(ans, m); 71 | } 72 | else{ 73 | l = m + 1; 74 | } 75 | } 76 | print ans << "\n"; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /126. Word Ladder II/126. Word Ladder II.cpp: -------------------------------------------------------------------------------- 1 | //Author: Devendra Uraon - TLE 2 | //Helper: Tania Rebello - TLE.Eliminator 3 | class Solution { 4 | public: 5 | map> parent; 6 | queue q; 7 | vector path; 8 | vector> ans; 9 | unordered_map distance; 10 | void location(string str,vector &path){ 11 | if(str=="0"){ 12 | ans.push_back(path); 13 | return; 14 | } 15 | for(string node : parent[str]){ 16 | path.push_back(str); 17 | location(node,path); 18 | path.pop_back(); 19 | } 20 | } 21 | vector> findLadders(string first, string last, vector &list){ 22 | for(string node : list) distance[node] = INT_MAX , parent[node] = {}; 23 | parent[first] = {"0"}; 24 | distance[first] = 1; 25 | q.push(first); 26 | while(!q.empty()){ 27 | string par = q.front(); 28 | q.pop(); 29 | if(par==last) break; 30 | for(int i=0; i 1 + distance[par]){ 41 | distance[child] = 1 + distance[par]; 42 | parent[child].clear(); 43 | parent[child].push_back(par); 44 | q.push(child); 45 | } 46 | else if(distance[child] == 1 + distance[par]){ 47 | parent[child].push_back(par); 48 | } 49 | } 50 | } 51 | } 52 | } 53 | } 54 | location(last, path); 55 | for(int i=0; i 4 | using namespace std; 5 | 6 | int main() { 7 | ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 8 | int T, n, q, a, b, c, h, p, count, flag = 1; 9 | cin >> T; 10 | while(T--){ 11 | a = b = c = h = p = count = 0; 12 | cin >> n >> q; 13 | vector v(n+10); 14 | for(int i = 0; i <= n; i++){ 15 | v[i] = 0; 16 | } 17 | vector>> m(n+10); 18 | for(int i = 0; i> a >> b >> c; 20 | m[b].push_back({a,c}); 21 | m[c].push_back({a,b}); 22 | } 23 | a = b = c = p = h = 0; 24 | for(int i = 1; i <= n; i++){ 25 | if(v[i] == 0){ 26 | v[i] = -1; 27 | count++; 28 | p++; 29 | queue q; 30 | q.push(i); 31 | while(q.empty() == false){ 32 | a=q.front(); 33 | q.pop(); 34 | if(v[a] == -1){ 35 | for(int j = 0; j < m[a].size(); j++){ 36 | if(m[a][j].first == 1){ 37 | if(v[m[a][j].second] == -1){ 38 | flag = 0; 39 | break; 40 | } 41 | else if(v[m[a][j].second] == 0){ 42 | q.push(m[a][j].second); 43 | count++; 44 | v[m[a][j].second] = 1; 45 | h++; 46 | } 47 | } 48 | else{ 49 | if(v[m[a][j].second] == 1){ 50 | flag=0; 51 | break; 52 | } 53 | else if(v[m[a][j].second] == 0){ 54 | q.push(m[a][j].second); 55 | count++; 56 | v[m[a][j].second] = -1; 57 | p++; 58 | } 59 | } 60 | } 61 | } 62 | else if(v[a] == 1){ 63 | for(int j=0; j < m[a].size(); j++){ 64 | if(m[a][j].first == 1){ 65 | if(v[m[a][j].second] == 1){ 66 | flag = 0; 67 | break; 68 | } 69 | else if(v[m[a][j].second] == 0){ 70 | q.push(m[a][j].second); 71 | count++; 72 | v[m[a][j].second] = -1; 73 | p++; 74 | } 75 | } 76 | else{ 77 | if(v[m[a][j].second] == -1){ 78 | flag = 0; 79 | break; 80 | } 81 | else if(v[m[a][j].second] == 0){ 82 | q.push(m[a][j].second); 83 | count++; 84 | v[m[a][j].second] = 1; 85 | h++; 86 | } 87 | } 88 | } 89 | } 90 | if(flag == 0){ 91 | break; 92 | } 93 | } 94 | if(flag == 0){ 95 | break; 96 | } 97 | else{ 98 | if(p > h){ 99 | b += p; 100 | } 101 | else{ 102 | b += h; 103 | } 104 | } 105 | } 106 | p = h = a = c = 0; 107 | } 108 | if(flag == 0){ 109 | flag = 1; 110 | cout << -1 << '\n'; 111 | } 112 | else{ 113 | cout << b << '\n'; 114 | } 115 | } 116 | return 0; 117 | } 118 | -------------------------------------------------------------------------------- /Balanced Ternary String.cpp: -------------------------------------------------------------------------------- 1 | //Question: https://codeforces.com/contest/1102/problem/D 2 | //Author: Devendra Uraon 3 | #include 4 | #define vi vector 5 | #define vl vector 6 | #define vc vector 7 | #define pi pair 8 | #define vp vector 9 | #define ll long long 10 | #define forloop for(int i = 0; i < n; ++i) 11 | #define input cin >> 12 | #define print cout << 13 | #define MAX 2147000000 14 | #define MOD 998244353LL 15 | using namespace std; 16 | 17 | int main(){ 18 | ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 19 | int n; input n; 20 | string str; input str; 21 | vp vec(3); 22 | vec[0].second = 0; 23 | vec[1].second = 1; 24 | vec[2].second = 2; 25 | forloop vec[str[i] - '0'].first++; 26 | int k = n / 3; 27 | sort(vec.begin(), vec.end()); 28 | if(vec[1].first > k){ 29 | int a = vec[1].first - k; 30 | if(vec[0].second > vec[1].second){ 31 | for(int i{n - 1}, j = 0; i >= 0 && j < a; --i){ 32 | if(str[i] == char('0' + vec[1].second)){ 33 | str[i] = char('0' + vec[0].second); 34 | j++; 35 | } 36 | } 37 | } 38 | else{ 39 | for(int i = 0, j = 0; i < n && j < a; ++i){ 40 | if(str[i] == char('0' + vec[1].second)){ 41 | str[i] = char('0' + vec[0].second); 42 | j++; 43 | } 44 | } 45 | } 46 | a = vec[2].first - k; 47 | if(vec[0].second > vec[2].second){ 48 | for(int i = n - 1, j = 0; i >= 0 && j < a; --i){ 49 | if(str[i] == char('0' + vec[2].second)){ 50 | str[i] = char('0' + vec[0].second); 51 | j++; 52 | } 53 | } 54 | } 55 | else{ 56 | for(int i = 0, j = 0; i < n && j < a; ++i){ 57 | if(str[i] == char('0' + vec[2].second)){ 58 | str[i] = char('0' + vec[0].second); 59 | j++; 60 | } 61 | } 62 | } 63 | } 64 | else{ 65 | if(vec[2].second == 2){ 66 | if(vec[0].second == 1) swap(vec[0], vec[1]); 67 | int a = k - vec[0].first; 68 | for(int i = 0, j = 0; i < n && j < a; ++i){ 69 | if(str[i] == char('0' + vec[2].second)){ 70 | str[i] = char('0' + vec[0].second); 71 | j++; 72 | } 73 | } 74 | a = k - vec[1].first; 75 | for(int i = 0, j = 0; i < n && j < a; ++i){ 76 | if(str[i] == char('0' + vec[2].second)){ 77 | str[i] = char('0' + vec[1].second); 78 | j++; 79 | } 80 | } 81 | } 82 | else if(vec[2].second == 1){ 83 | if(vec[0].second == 2) swap(vec[0], vec[1]); 84 | int a = k - vec[0].first; 85 | for(int i = 0, j = 0; i < n && j < a; ++i){ 86 | if(str[i] == char('0' + vec[2].second)){ 87 | str[i] = char('0' + vec[0].second); 88 | j++; 89 | } 90 | } 91 | a = k - vec[1].first; 92 | for(int i = n - 1, j = 0; i >= 0 && j < a; --i){ 93 | if(str[i] == char('0' + vec[2].second)){ 94 | str[i] = char('0' + vec[1].second); 95 | j++; 96 | } 97 | } 98 | } 99 | else if(vec[2].second == 0){ 100 | if(vec[0].second == 1) swap(vec[0], vec[1]); 101 | int a = k - vec[0].first; 102 | for(int i = n - 1, j = 0; i >= 0 && j < a; --i){ 103 | if(str[i] == char('0' + vec[2].second)){ 104 | str[i] = char('0' + vec[0].second); 105 | j++; 106 | } 107 | } 108 | a = k - vec[1].first; 109 | for(int i = n - 1, j = 0; i >= 0 && j < a; --i){ 110 | if(str[i] == char('0' + vec[2].second)){ 111 | str[i] = char('0' + vec[1].second); 112 | j++; 113 | } 114 | } 115 | } 116 | } 117 | print str; 118 | } 119 | --------------------------------------------------------------------------------