Posts

MiniMaxSum

  miniMaxSum Question: Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers. Example: Sample input: 1 2 3 4 5 Expected output: minSum: 1+2+3+4 maxSum: 2+3+4+5 Reference: https://www.hackerrank.com/challenges/mini-max-sum/problem?isFullScreen=true&h_r=next-challenge&h_v=zen&h_r=next-challenge&h_v=zen Idea: Min sum: 1. get sum of array first and 2. minus the max value of arrary Max sum : 1. get sum of array first and 2. minus the min value of arrary print ( sum ( arr ) - max ( arr ), sum ( arr ) - min ( arr ))

Print a triangle

  Print a triangle Question: you need to print a triangle like this:      #     ##    ###   ####  ##### ###### Ideas: Think of this like a square -----# ----## ---### --#### -##### ###### we need to print the left triangle first for i in range ( 0 , n ):   for j in range ( 0 , n - i - 1 ):     print ( "-" , end = " " )   print ( "" ) now we can print the right triangle easier for i in range ( 0 , n ):   for j in range ( 0 , n - i - 1 ):     print ( "-" , end = "" )   for j in range ( 0 , i + 1 ):     print ( "*" , end = "" )   print ( "" ) change "-" to "" def staircase ( n ):     # Write your code here     for i in range ( 0 , n ):         for j in range ( 0 , n - i - 1 ):             print ( "-" , end = "" )             # print("",end = " ")  ...

Binary search

  Binary search Ideas: Array must be sorted While low <= high, keep searching Set a mid position If target value > mid value, shift right (low = mid + 1) If target value < mid value, shift left (high = mid - 1) In python, we need to use "//" to get mid value Time complexity n , \frac{n}{2} , \frac{n}{4} , \frac{n}{8} , we can get \frac{n}{2^k} , and set \frac{n}{2^k} = 1 , we can get O(h) = O(log2n) def search ( self , nums : List [ int ], target : int ) -> int :         mid , low , high = 0 , 0 , len ( nums ) - 1         while ( low <= high ):             mid = ( high - low ) // 2 + low #round down             if nums [ mid ] == target : return mid             elif nums [ mid ] > target : high = mid - 1             elif nums [ mid ] < target : low = mid + 1     ...

Given a square matrix, calculate the absolute difference between the sums of its diagonals

Given a square matrix, calculate the absolute difference between the sums of its diagonals. Question: Return the absolute difference between the sums of the matrix's two diagonals as a single integer. Method: We can consider left diagonal: when i = j -> sum (e.g., if we have a 3*3 matrix, we can know the coordinate of left diagonal is (0,0) (1,1) (2,2)) Right diagonal can be consider as when i+j = len(arr)-1 -> sum (e.g., take 3*3 matrix as an example as well, the coordinate of right diagnal is (0,2) (1,1) (2,0), from the example we can get that the sum of x,y is eaqul to len(arr) -1) def diagonalDifference ( arr ):     # Write your code here     sumLeft = 0     sumRight = 0     lenArr = len ( arr ) - 1 ​     for i in range ( 0 , len ( arr )):         for j in range ( 0 , len ( arr )):             # calculate the left diagonals         ...