#LeetCodeSolutions
Explore tagged Tumblr posts
code-recipe · 2 years ago
Text
Tumblr media
Google Interview Question Revealed!
Checkout our latest video where we have explained the popular Google, Microsoft, Facebook, Amazon interview question "Two Sum" in a simple, step by step and easy to understand way. Watch it now on YouTube : https://youtu.be/JMCTsP0Jxmc
2 notes · View notes
arifinfrds-blog · 2 years ago
Text
#9 Swift - TDD + Find Pivot Index
This time, let’s solve another Leetcode problem, Find Pivot Index, using Test Driven Development! In this video, I will show you how we can solve this problem starting from O(n^2) to more optimize O(n) time complexity, using Test Driven Development as our main development strategy. GitHub repository https://github.com/arifinfrds/ LeetCode:…
youtube
View On WordPress
1 note · View note
heartcentrictech · 5 years ago
Photo
Tumblr media
How do you keep your coding skills sharp and fresh?🤦‍♀️ Do you want to level up your coding skills?🙋‍♀️ I'd suggest LeetCode which is one of the best places to expand your knowledge and get prepared for your next interview👏 It's always good especially for backend developers👩🏻‍💻 to enhance their programming skills in their free time and master the programming language of your choice🙌 I'm currently on a monthly challenge where I focus on binary tree traversals😎 How does that sound? What data structure and algorithms you are big fan of? Let's code😇 #heartcentrictech #heartcentrictechmentoring #datastructures #datastructuresandalgorithms #datastructure #binarytree #binarytrees #csharp #csharpdotnet #csharpprogramming #csharpdeveloper #csharpcorner #csharpcoding #leetcode #leetcodesolution #leetcodeeasy #happycoding #happycoding😊 #letscode #girlcode #girlcoding #womenintech #womenintechnology #womenincoding https://www.instagram.com/p/CCxkT6ADaUc/?igshid=519qgg5f1clz
0 notes
tutorialcup · 4 years ago
Text
Power of Two Leetcode Solution
We are given an integer and the goal is to check whether the integer is a power of two, that is, it can be represented as some whole power of '2'.
Tumblr media
Example
16 Yes 13 No
Approach
A trivial solution can be: Check if all prime factors of the integer are all '2'. The time complexity of this method would be O(log2N). In order to do it in an optimal way, we can take help of Bit manipulations. "Any number which is a power of two can only have a single bit set in binary representation" How can it be checked that there is only a single bit set in the binary form? Consider any number, x. Now, if x is some power of two, then (x - 1) will turn off all the right bits to the set bit(set them as '0') and the set bit would be unset. x = 8, x - 1 = 7 So, using BITWISE-AND of x and (x - 1), we can say if a number is some power of two, then, x & (x - 1) = 0
Algorithm(Trivial)
- Keep dividing the number by '2' until it is not divisible by '2' anymore. - If the number is equal to '1': - The integer is a power of two - Else - The integer is not a power of two
Algorithm(Bit-Manipulation)
- Check if x & (x - 1) is equal to zero - If yes, the number is a power of 2 - The integer is not a power of 2, otherwise
Implementation
C++ Program of Power of Two Leetcode Solution Naive Method #include using namespace std; bool powerOfTwo(int n) { while(n % 2 == 0) n /= 2; return (n == 1); } int main() { int n = 16; if(powerOfTwo(n)) cout Read the full article
0 notes
tutorialcup · 4 years ago
Text
Rotate List Leetcode Solution
The problem Rotate List Leetcode Solution provides us a linked list and an integer. We are told to rotate the linked list to the right by k places. So if we rotate a linked list k places to the right, in each step we take the last element from the list and place it in from. We repeat this until we have done k number of these operations. Let's take a look at a few examples. head = , k = 2 Explanation: Let's break the operation into 2 simple rotation operations. So, in the first step or move, we simply take the element from the end of the list and place it in front. So, the list becomes . Now, again we repeat the same operation making the list, . And hence the answer. head = , k = 4 Explanation: Repeating the process 4 times result in the answer linked list. It can be better understood by looking at the image below.
Tumblr media
Approach for Rotate List Leetcode Solution
The problem Rotate List Leetcode Solution states that you are given a linked list with an integer for rotation. This means that we need to rotate the list k places to the right. The problem can be understood by a simple operation of taking the elements from the end of the list and placing them in front. Since there is no way to efficiently remove an element from the end and place it in front. We need to think of any other way to perform the operation. If we observe, we can see that after performing k operations, k elements from the end are removed and are placed in front. One thing to note here is, if the size of k is greater than the size of the linked list. We will take the modulo of k over the length of the linked list. Once done, we will traverse until the kth node from the end. Then we perform some of the operations, we assign the next of the last node to the head. Assign the kth node from the end as head of the linked list. But we should not forget assigning the next node of k-1 th node from the end as null. Now, after performing these 3 operations, we have rotated the list.
Code for Rotate List Leetcode Solution
C++ code #include using namespace std; struct ListNode{ int data; ListNode* next; }; ListNode* rotateRight(ListNode* head, int k) { if(head==NULL || head->next==NULL)return head; ListNode *tmp = head; int cnt = 0; while(tmp)tmp=tmp->next,cnt++; tmp=head; k%=cnt; if(k==0)return head; while(k--)tmp = tmp->next; ListNode *tmpHead = head; while(tmp->next!=NULL){ tmp = tmp->next; head = head->next; } ListNode* newHead = head->next; tmp->next = tmpHead; head->next = NULL; return newHead; } int main(){ ListNode *head = new ListNode(); head->data = 0; head->next = new ListNode(); head->next->data = 1; head->next->next = new ListNode(); head->next->next->data = 2; head = rotateRight(head, 4); while(head != NULL){ cout Read the full article
0 notes