From eb51fec2ef56ab4843bac581e4eaf31c3408f93f Mon Sep 17 00:00:00 2001 From: ashwin-nair98 Date: Tue, 5 Mar 2024 21:25:58 +0530 Subject: [PATCH] feat: add elements of programming --- elements/4_1_Parity_of_word/solution.py | 42 +++++++++++++++++++ elements/4_7_x_pow_y/solution.py | 1 + .../100_Same Tree}/Solution.py | 0 .../Solution.py | 0 .../118_Pascals Triangle}/Solution.py | 0 .../Solution.py | 0 .../Solution.py | 0 .../198_House Robber}/Solution.py | 0 .../3010_Divide Array Min Cost 1}/Solution.py | 0 .../3011_Can Sort Array}/Solution.py | 0 .../Solution.py | 0 .../Solution.py | 0 .../31_Next Permutation}/Solution.py | 0 .../53_Maximum Subarray}/Solution.py | 0 .../576_Out of Boundary Paths}/Solution.py | 0 .../645_Set Mismatch}/Solution.py | 0 .../6468_CountServers}/Solution.java | 0 .../6892_Maximum String Pair}/Solution.java | 0 .../6895_Longest New String}/Solution.java | 0 .../Solution.java | 0 .../73_Set Matrix Zeroes}/Solution.py | 0 leetcode/README.md | 8 ++++ 22 files changed, 51 insertions(+) create mode 100644 elements/4_1_Parity_of_word/solution.py create mode 100644 elements/4_7_x_pow_y/solution.py rename {100_Same Tree => leetcode/100_Same Tree}/Solution.py (100%) rename {104_Maximum Depth Binary Tree => leetcode/104_Maximum Depth Binary Tree}/Solution.py (100%) rename {118_Pascals Triangle => leetcode/118_Pascals Triangle}/Solution.py (100%) rename {1239_Maximum Length Unique String => leetcode/1239_Maximum Length Unique String}/Solution.py (100%) rename {1457_Pseudo PAlindromic Path Binary Tree => leetcode/1457_Pseudo PAlindromic Path Binary Tree}/Solution.py (100%) rename {198_House Robber => leetcode/198_House Robber}/Solution.py (100%) rename {3010_Divide Array Min Cost 1 => leetcode/3010_Divide Array Min Cost 1}/Solution.py (100%) rename {3011_Can Sort Array => leetcode/3011_Can Sort Array}/Solution.py (100%) rename {3014_Minimum Number of Pushes 1 => leetcode/3014_Minimum Number of Pushes 1}/Solution.py (100%) rename {3016_Minimum Number of Pushes 2 => leetcode/3016_Minimum Number of Pushes 2}/Solution.py (100%) rename {31_Next Permutation => leetcode/31_Next Permutation}/Solution.py (100%) rename {53_Maximum Subarray => leetcode/53_Maximum Subarray}/Solution.py (100%) rename {576_Out of Boundary Paths => leetcode/576_Out of Boundary Paths}/Solution.py (100%) rename {645_Set Mismatch => leetcode/645_Set Mismatch}/Solution.py (100%) rename {6468_CountServers => leetcode/6468_CountServers}/Solution.java (100%) rename {6892_Maximum String Pair => leetcode/6892_Maximum String Pair}/Solution.java (100%) rename {6895_Longest New String => leetcode/6895_Longest New String}/Solution.java (100%) rename {6898_Decremental String Concat => leetcode/6898_Decremental String Concat}/Solution.java (100%) rename {73_Set Matrix Zeroes => leetcode/73_Set Matrix Zeroes}/Solution.py (100%) create mode 100644 leetcode/README.md diff --git a/elements/4_1_Parity_of_word/solution.py b/elements/4_1_Parity_of_word/solution.py new file mode 100644 index 0000000..6a545ca --- /dev/null +++ b/elements/4_1_Parity_of_word/solution.py @@ -0,0 +1,42 @@ +# The parity of a word is 1 if the number of 1 bits in the word is odd, else it's 0 +# +# Question: Find the parity of a word? + + +# Solution 1 +# Brute force to find all 1s +def parity(x): + result = 0 + while x: + result ^= x & 1 + x >>= 1 + return result + + +# Solution 2 +# Instead of checking for every bit, check for last set bit +# Use x & x - 1 to remove the last set bit +def parity2(x): + result = 0 + while x: + result ^= 1 + x &= x - 1 + return result + + +# Test +def expect(a, b): + result = a == b + print("Test for " + str(a) + (" passed!" if result else " failed!")) + + +print("Parity 1") +a = 0b1010 +expect(0, parity(a)) +a = 0b1110 +expect(1, parity(a)) +print("Parity 2") +a = 0b1010 +expect(0, parity2(a)) +a = 0b1110 +expect(1, parity2(a)) diff --git a/elements/4_7_x_pow_y/solution.py b/elements/4_7_x_pow_y/solution.py new file mode 100644 index 0000000..ae5e112 --- /dev/null +++ b/elements/4_7_x_pow_y/solution.py @@ -0,0 +1 @@ +# Question: Find x^y diff --git a/100_Same Tree/Solution.py b/leetcode/100_Same Tree/Solution.py similarity index 100% rename from 100_Same Tree/Solution.py rename to leetcode/100_Same Tree/Solution.py diff --git a/104_Maximum Depth Binary Tree/Solution.py b/leetcode/104_Maximum Depth Binary Tree/Solution.py similarity index 100% rename from 104_Maximum Depth Binary Tree/Solution.py rename to leetcode/104_Maximum Depth Binary Tree/Solution.py diff --git a/118_Pascals Triangle/Solution.py b/leetcode/118_Pascals Triangle/Solution.py similarity index 100% rename from 118_Pascals Triangle/Solution.py rename to leetcode/118_Pascals Triangle/Solution.py diff --git a/1239_Maximum Length Unique String/Solution.py b/leetcode/1239_Maximum Length Unique String/Solution.py similarity index 100% rename from 1239_Maximum Length Unique String/Solution.py rename to leetcode/1239_Maximum Length Unique String/Solution.py diff --git a/1457_Pseudo PAlindromic Path Binary Tree/Solution.py b/leetcode/1457_Pseudo PAlindromic Path Binary Tree/Solution.py similarity index 100% rename from 1457_Pseudo PAlindromic Path Binary Tree/Solution.py rename to leetcode/1457_Pseudo PAlindromic Path Binary Tree/Solution.py diff --git a/198_House Robber/Solution.py b/leetcode/198_House Robber/Solution.py similarity index 100% rename from 198_House Robber/Solution.py rename to leetcode/198_House Robber/Solution.py diff --git a/3010_Divide Array Min Cost 1/Solution.py b/leetcode/3010_Divide Array Min Cost 1/Solution.py similarity index 100% rename from 3010_Divide Array Min Cost 1/Solution.py rename to leetcode/3010_Divide Array Min Cost 1/Solution.py diff --git a/3011_Can Sort Array/Solution.py b/leetcode/3011_Can Sort Array/Solution.py similarity index 100% rename from 3011_Can Sort Array/Solution.py rename to leetcode/3011_Can Sort Array/Solution.py diff --git a/3014_Minimum Number of Pushes 1/Solution.py b/leetcode/3014_Minimum Number of Pushes 1/Solution.py similarity index 100% rename from 3014_Minimum Number of Pushes 1/Solution.py rename to leetcode/3014_Minimum Number of Pushes 1/Solution.py diff --git a/3016_Minimum Number of Pushes 2/Solution.py b/leetcode/3016_Minimum Number of Pushes 2/Solution.py similarity index 100% rename from 3016_Minimum Number of Pushes 2/Solution.py rename to leetcode/3016_Minimum Number of Pushes 2/Solution.py diff --git a/31_Next Permutation/Solution.py b/leetcode/31_Next Permutation/Solution.py similarity index 100% rename from 31_Next Permutation/Solution.py rename to leetcode/31_Next Permutation/Solution.py diff --git a/53_Maximum Subarray/Solution.py b/leetcode/53_Maximum Subarray/Solution.py similarity index 100% rename from 53_Maximum Subarray/Solution.py rename to leetcode/53_Maximum Subarray/Solution.py diff --git a/576_Out of Boundary Paths/Solution.py b/leetcode/576_Out of Boundary Paths/Solution.py similarity index 100% rename from 576_Out of Boundary Paths/Solution.py rename to leetcode/576_Out of Boundary Paths/Solution.py diff --git a/645_Set Mismatch/Solution.py b/leetcode/645_Set Mismatch/Solution.py similarity index 100% rename from 645_Set Mismatch/Solution.py rename to leetcode/645_Set Mismatch/Solution.py diff --git a/6468_CountServers/Solution.java b/leetcode/6468_CountServers/Solution.java similarity index 100% rename from 6468_CountServers/Solution.java rename to leetcode/6468_CountServers/Solution.java diff --git a/6892_Maximum String Pair/Solution.java b/leetcode/6892_Maximum String Pair/Solution.java similarity index 100% rename from 6892_Maximum String Pair/Solution.java rename to leetcode/6892_Maximum String Pair/Solution.java diff --git a/6895_Longest New String/Solution.java b/leetcode/6895_Longest New String/Solution.java similarity index 100% rename from 6895_Longest New String/Solution.java rename to leetcode/6895_Longest New String/Solution.java diff --git a/6898_Decremental String Concat/Solution.java b/leetcode/6898_Decremental String Concat/Solution.java similarity index 100% rename from 6898_Decremental String Concat/Solution.java rename to leetcode/6898_Decremental String Concat/Solution.java diff --git a/73_Set Matrix Zeroes/Solution.py b/leetcode/73_Set Matrix Zeroes/Solution.py similarity index 100% rename from 73_Set Matrix Zeroes/Solution.py rename to leetcode/73_Set Matrix Zeroes/Solution.py diff --git a/leetcode/README.md b/leetcode/README.md new file mode 100644 index 0000000..fb86e1b --- /dev/null +++ b/leetcode/README.md @@ -0,0 +1,8 @@ +# LeetCode Solutions + +This repository contains my solutions to various LeetCode problems. Each solution is implemented in Python, and the goal is to provide clear, concise, and efficient solutions. + +## Structure +Each problem has its own directory named after the problem's number and title. +`_/` +` solution.py`