1035. Uncrossed Lines

Problem Setting

We write the integers of A and B (in the order they are given) on two separate horizontal lines.

Now, we may draw a straight line connecting two numbers A[i] and B[j] as long as A[i] == B[j], and the line we draw does not intersect any other connecting (non-horizontal) line.

Return the maximum number of connecting lines we can draw in this way.

Link for Problem: leetcode

Example 1:

image

Input: A = [1,4,2], B = [1,2,4]

Output: 2

Explanation: We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2.

Selecting Elements In A 1D Array

Goal

This post aims to select elements in a 1d array in python using following modules:

  • Numpy
  • Pandas

Referring to Chris Albon's blog, I only look at his title of the post but wrote my own contents without looking at the contents to deepen my understanding about the topic.

Numpy

Library

In [1]:
import numpy as np

1-d array

In [3]:
arr = np.array([1, 2, 3, 4, 5, 6])
arr
Out[3]:
array([1, 2, 3, 4, 5, 6])

1031. Maximum Sum of Two Non-Overlapping Subarrays

Problem Setting

Given an array A of non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengths L and M. (For clarification, the L-length subarray could occur before or after the M-length subarray.)

Formally, return the largest V for which V = (A[i] + A[i+1] + ... + A[i+L-1]) + (A[j] + A[j+1] + ... + A[j+M-1]) and either:

  • 0 <= i < i + L - 1 < j < j + M - 1 < A.length, or
  • 0 <= j < j + M - 1 < i < i + L - 1 < A.length.

Example 1:

Input: A = [0,6,5,2,2,5,1,9,4], L = 1, M = 2

Output: 20

Explanation: One choice of subarrays is [9] with length 1, and [6,5] with length 2.

image

Transpose A Matrix or Tensor

Goal

This post aims to transpose a matrix or tensor in python using following modules:

  • Numpy
  • Pandas
  • Tensorflow
  • Pytorch

Referring to Chris Albon's blog, I only look at his title and wrote my own contents to deepen my understanding about the topic.

Numpy

library

In [18]:
import numpy as np

Create an array

In [19]:
A = np.array([[1, 2, 3], 
             [4, 5, 6], 
             [7, 8, 9]])
A
Out[19]:
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

Transpose

In [20]:
A.transpose()
Out[20]:
array([[1, 4, 7],
       [2, 5, 8],
       [3, 6, 9]])
In [21]:
A.T
Out[21]:
array([[1, 4, 7],
       [2, 5, 8],
       [3, 6, 9]])

1026. Maximum Difference Between Node and Ancestor

Problem Setting

Given the root of a binary tree, find the maximum value V for which there exists different nodes A and B where V = |A.val - B.val| and A is an ancestor of B.

(A node A is an ancestor of B if either: any child of A is equal to B, or any child of A is an ancestor of B.)

Note:

  • The number of nodes in the tree is between 2 and 5000.
  • Each node will have value between 0 and 100000.

Example 1

Input: [8,3,10,1,6,null,14,null,null,4,7,13]

Example 1

Output: 7

Explanation:

We have various ancestor-node differences, some of which are given below : |8 - 3| = 5 |3 - 7| = 4 |8 - 1| = 7 |10 - 13| = 3 Among all possible differences, the maximum value of 7 is obtained by |8 - 1| = 7.

LeetCode Top 100 Problem Selection

This link was posted on Dec 30, 2018 in blind Curated List of Top 100 LeetCode Questions. I found it so useful and would like to cover these problem in the following post as well.

Array

Read more…

Split-Up: dtreeviz (Part 1)

Goal

This post aims to go through each function in dtreeviz module to fully understand what is implemented. After fully understanding this, I would like to contribute to this module and submit a pull request.

I really like this module and would like to see this works for other tree-based modules like XGBoost or Lightgbm. I found the exact same issue (issues 15) in github so I hope I could contribute to this issue.

You would just have to get ShadowDecisionTree wrappers for those trees.

Based on this comment, I need first understand the class object ShadowDecisionTree

image

Understand folder structure

In this post, we will deep dive into the core module dtreeviz image

This module comprises of 4 python files. image

__init__.py is empty so we can skip it.

Let's see one by one.

1025. Divisor Game (C++)

Problem Setting

Alice and Bob take turns playing a game, with Alice starting first.

Initially, there is a number N on the chalkboard. On each player's turn, that player makes a move consisting of:

Choosing any x with 0 < x < N and N % x == 0. Replacing the number N on the chalkboard with N - x. Also, if a player cannot make a move, they lose the game.

Return True if and only if Alice wins the game, assuming both players play optimally.

image