Showing 24 posts in category "algorithms"
Binary Search ---- AGAIN!
Implementing classic binary search. Binary search is an $O(log N)$ algorithm to locate a target element within a sorted array. Algorithm Details We maintain two boundary markers: low and high. In each iteration, w...
1079. Letter Tile Possibilities
You have n tiles, where each tile has one letter tiles[i] printed on it. Return the number of possible non-empty sequences of letters you can make using the letters printed on thos...
Tuple with Same Product
Given an array nums of distinct positive integers, return the number of tuples (a, b, c, d) such that a * b = c * d where a, b, c, and d are elements...
2270. Number of Ways to Split Array
class Solution { public: int waysToSplitArray(vector<int>& nums) { // Keep track of sum of elements on left and right sides long long leftSum = 0, rightSum = 0; // Initially all eleme...
1030. Matrix Cells in Distance Order
You are given four integers row, cols, rCenter, and cCenter. There is a rows x cols matrix and you are on the cell with the coordinates (rCenter, cCenter). Return the coordinat...
90% of CS graduates can't figure this out
Someone important on X posted: https://x.com/vikhyatk/status/1873033432705712304 i have a very simple question i ask during phone screens: print each level of a tree on a separate line. 90% of CS grad candidates j...
Count Ways To Build Good Strings
Given the integers zero, one, low, and high, we can construct a string by starting with an empty string, and then at each step perform either of the following: Append the character '0' zero times...
Set Matrix Zeros
class Solution { public: void setMatrixZeroes(vector<vector<int>> &mat) { int n = mat.size(); int m = mat[0].size(); bool firstRowHasZero = false; bool firstColHa...
Zigzag string conversion solution in C++
The Zigzag Conversion problem (LeetCode 6) asks us to write a string in a zigzag pattern across a given number of rows, and then read it row by row. Visualizing Zigzag Movement For numRows = 3: P A H N A P...
Search Sorted Matrix Solution in C++
In this post, we explore a C++ solution to search for a value in a row-column sorted matrix. The matrix is sorted such that each row is sorted in ascending order from left to right, and the first element of any row is g...
LeetCode 543 Diameter of a Binary Tree in C++
Given the root of a binary tree, return the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a...
401. Binary Watch
A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right. For...
Letter Combinations of a phone number
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order. A mapping of digits to letters (just like o...
LeetCode 20 Valid Parentheses in Typescript
The mental challenge one might experience here is that JavaScript doesn't have abstract data structures like stacks and queues built into the language. Implementations of those structures use an array underneath. So so...
Max Chunks To Make Sorted
You are given an integer array arr of length n that represents a permutation of the integers in the range [0, n - 1]. We split arr into some number of chunks (i.e., partit...
Final Prices With a Special Discount in a Shop
You are given an integer array prices where prices[i] is the price of the ith item in a shop. There is a special discount for items in the shop. If you buy the ith item, then y...
C++ Solution for Generate Parentheses Leetcode Problem #22
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example 1: Input: n = 3 Output: ["((()))","(()())","(())()","()(())","()()()"] Example 2:...
Final Array State After K Multiplication Operations
You are given an integer array nums, an integer k, and an integer multiplier. You need to perform k operations on nums. In each operation: Find the minimum value x in nums. If there...
Find Minimum in Rotated Sorted Array
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the a...
Non Repeating Character
Difficulty: EasyAccuracy: 40.43%Submissions: 262K+Points: 2 Given a string s consisting of lowercase Latin Letters. Return the first non-repeating character in s. If there is no non-repeating...
Find the Peek Element in the Array
Peak element https://www.geeksforgeeks.org/problems/peak-elementhttps://leetcode.com/problems/find-peak-elementDifficulty: BasicAccuracy: 38.86%Submissions: 510K+Points: 1 Given an array arr[] where no t...
Insert New Interval and Merge Overlapping Intervals
Given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the ith event and intervals is sorted in ascendin...
Merge K Sorted Lists
https://leetcode.com/problems/merge-k-sorted-lists You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-...
WordPress Function Reference
I've spent some time on their site trying to understand how to retrieve the current category, and you know what, its not really working for me.You would think that the most popular CMS on the internet would have better d...