From 7933941fd37289b37e288e53be5e1b63da1746c5 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Thu, 10 Feb 2022 08:00:32 +0000 Subject: - Added guest contributions by Eric Cheung. --- .../eric-cheung/excel-vba/Challenge_151.xlsm | Bin 0 -> 20786 bytes challenge-151/eric-cheung/excel-vba/ch-2.bas | 63 +++++++++++++++++++++ challenge-151/eric-cheung/python/ch-1.py | 43 ++++++++++++++ 3 files changed, 106 insertions(+) create mode 100755 challenge-151/eric-cheung/excel-vba/Challenge_151.xlsm create mode 100755 challenge-151/eric-cheung/excel-vba/ch-2.bas create mode 100755 challenge-151/eric-cheung/python/ch-1.py diff --git a/challenge-151/eric-cheung/excel-vba/Challenge_151.xlsm b/challenge-151/eric-cheung/excel-vba/Challenge_151.xlsm new file mode 100755 index 0000000000..0b21bdee00 Binary files /dev/null and b/challenge-151/eric-cheung/excel-vba/Challenge_151.xlsm differ diff --git a/challenge-151/eric-cheung/excel-vba/ch-2.bas b/challenge-151/eric-cheung/excel-vba/ch-2.bas new file mode 100755 index 0000000000..f438244726 --- /dev/null +++ b/challenge-151/eric-cheung/excel-vba/ch-2.bas @@ -0,0 +1,63 @@ +Attribute VB_Name = "ModTask_02" +Option Explicit +Option Base 1 + +Public Const strMyTitle As String = "Eric Cheung" + +Sub Task_02() + + Dim nRobValArr() As Variant + Dim nRobArrCnt As Integer + + Dim nElemArr() As Integer + Dim nElemArrCnt As Integer, nElemLoop As Integer, nElemSubLoop As Integer + + Dim dSumGain As Integer, dSumSubGain As Integer + + '' nRobValArr = Array(2, 4, 5) '' Example 1: + nRobValArr = Array(4, 2, 3, 6, 5, 3) '' Example 2: + + nRobArrCnt = UBound(nRobValArr) - LBound(nRobValArr) + 1 + + If nRobArrCnt Mod 2 = 0 Then + nElemArrCnt = nRobArrCnt / 2 + Else + nElemArrCnt = (nRobArrCnt + 1) / 2 + End If + + ReDim nElemArr(1 To nElemArrCnt) + + nElemLoop = 1 + nElemArr(1) = 1 + + dSumGain = 0 + + Do + Do While nElemArr(nElemLoop) <= nRobArrCnt + Do While nElemLoop < nElemArrCnt + nElemLoop = nElemLoop + 1 + nElemArr(nElemLoop) = nElemArr(nElemLoop - 1) + 2 + Loop + + dSumSubGain = 0 + For nElemSubLoop = 1 To nElemArrCnt + If nElemArr(nElemSubLoop) <= nRobArrCnt Then + dSumSubGain = dSumSubGain + nRobValArr(nElemArr(nElemSubLoop)) + End If + Next nElemSubLoop + + If dSumSubGain > dSumGain Then + dSumGain = dSumSubGain + End If + + nElemArr(nElemLoop) = nElemArr(nElemLoop) + 1 + Loop + + nElemLoop = nElemLoop - 1 + nElemArr(nElemLoop) = nElemArr(nElemLoop) + 1 + + Loop Until nElemArr(1) > nRobArrCnt + + MsgBox dSumGain, vbOKOnly, strMyTitle + +End Sub diff --git a/challenge-151/eric-cheung/python/ch-1.py b/challenge-151/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..cab9c88121 --- /dev/null +++ b/challenge-151/eric-cheung/python/ch-1.py @@ -0,0 +1,43 @@ +## Credit: +## https://www.educative.io/edpresso/finding-the-maximum-depth-of-a-binary-tree + +class Node: + def __init__(self , val): + self.value = val + self.left = None + self.right = None + +def maxDepth(root): + ## Null node has 0 depth. + if root == None: + return 0 + + ## Get the depth of the left and right subtree + ## using recursion. + leftDepth = maxDepth(root.left) + rightDepth = maxDepth(root.right) + + ## Choose the larger one and add the root to it. + if leftDepth > rightDepth: + return leftDepth + 1 + else: + return rightDepth + 1 + +## Driver Code + +## Example 1: +## root = Node(1) +## root.left = Node(2) +## root.right = Node(3) +## root.left.left = Node(4) +## root.left.right = Node(5) + +## Example 2: +root = Node(1) +root.left = Node(2) +root.right = Node(3) +root.left.left = Node(4) +root.right.right = Node(5) +root.left.left.right = Node(6) + +print("The max depth is:", maxDepth(root)) \ No newline at end of file -- cgit