Skip to main content

Posts

Showing posts from August, 2022

Programming Skills LeetCode Day 2, Solutions:

  1. Number of 1 bits Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). Solution Hamming Weight (roughly saying) is number of 1's string, or the digits sum of binary representation.  Example: 111 0 1 has hamming weight of 4                 678 0 1234 0 567 has hamming weight of 10.                    0000 has hamming weight of 0.         Code to solve this problem is:                     class Solution:               def hammingWeight(self, n: int) -> int:                 return ((bin(n).count('1')))      2. Subtract the Product and Sum of Digits of an Integer The problem goes like this: Given digits 123 is a integer. We have to first find the product of individual digits (i.e. 1 * 2 * 3) and then subtract to the sum of individual digits (i.e. 1 + 2 + 3). Which is 6 - 6 equals to zero.  Another example: 234 -> 2 * 3 * 4 = 24               2 + 3 + 4 = 9             24 - 9 = 15 Code to

Programming Skills LeetCode Day 1: Solutions

  Day 1 of LeetCode are 1. Count odd numbers in an interval range:     If we do not apply trick to this question, then we are going to fail. If we try testing each number one by one, it will take up a lot of memory and runtime. And henceforth, we fail. The trick is simple. i. Check to find if first number or last number is odd. If that's the case, we can do like this:               if (low%2 == 1 or high %2 == 1):                 return (high-low)//2 + 1 If you get confused, just take pen and paper and try it yourself. ii. If the above case is not satisfied:              else:                    return (high-low)//2       Complete Solution:             class Solution:              def countOdds(self,low:int, high:int)->int:                   if (low%2 == 1 or high%2 == 1):                        return (high-low)//2 + 1                    else:                   return (high-low)//2 2. Average Salary Excluding the Minimum and Maximum Salary     This is even more easy than prev