Press "Enter" to skip to content

Huahua's Tech Road

花花酱 LeetCode Ultimate DP Study Plan Day 9

139. Word Break

Python3

Optimization

Python3

Adding Dark Mode to WordPress

Supporting dark mode now becomes an industry standard, I also received several requests from my viewers/readers recently thus I decided to add this feature. First thing in my mind is a plugin, I found WP Dark Mode, it’s feature rich and kind of worked until someone told me it didn’t work with cached pages. Then I found Darkmode.js, there’re several plugin versions but I decided to use the plain js version. It’s simple but makes the job done. There are only a few options you can play with, like the position, size, color and emoji of the icon, that’s it. Here’s mine.

Put the following code in header.php

Basically, it adds an overlay layer that applies a revert color filter (white -> black, blue -> yellow, etc) to ALL the content including images and iframes as well as syntax-highlighted code which I decided to ignore later on.

To ignore a few types of content, add the following code to header.php

The next thing is to apply a different color theme for code under darkmode. I plan to use classic for day-mode and sublime-text for darkmode. Although the author says the persistent darkmode setting is in cookies, but turns out it’s in localstorage. There are two scenarios we need to handle. 1) page loads with darkmode on, 2) user click button to toggle darkmode. We need to change the class for code blocks accordingly.

Put the following code in the footer.php, since we need to let the syntax-highlighter do the work first.

Or don’t forget to preload the darkmode theme since only the default theme will be loaded to reduce requests and bandwidth.

Put the following code in the header.php <head> section, replace with your domain and plugin version.

Finally it looks like below, not perfect but pretty good and easy to config. I hope wordpress could officially add dark mode in the future release such that all the themes and plugins could implement that (more work though).

花花酱 LeetCode Ultimate DP Study Plan Day 8

309. Best Time to Buy and Sell Stock with Cooldown

Python3

714 Best Time to Buy and Sell Stock with Transaction Fee

Python3

花花酱 LeetCode 231. Power of Two

Given an integer n, return true if it is a power of two. Otherwise, return false.

An integer n is a power of two, if there exists an integer x such that n == 2x.

Example 1:

Input: n = 1
Output: true
Explanation: 20 = 1

Example 2:

Input: n = 16
Output: true
Explanation: 24 = 16

Example 3:

Input: n = 3
Output: false

Example 4:

Input: n = 4
Output: true

Example 5:

Input: n = 5
Output: false

Constraints:

  • -231 <= n <= 231 - 1

Solution: 1 bit set

Any power of two has only 1 bit set in the binary format. e.g. 1=0b01, 2=0b10, 4=0b100, 8=0b1000

  1. use popcount.

C++

2. Use (n) & (n – 1) to remove the last set bit, so it should be zero.

C++

花花酱 LeetCode 222. Count Complete Tree Nodes

Given the root of a complete binary tree, return the number of the nodes in the tree.

According to Wikipedia, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.

Design an algorithm that runs in less than O(n) time complexity.

Example 1:

Input: root = [1,2,3,4,5,6]
Output: 6

Example 2:

Input: root = []
Output: 0

Example 3:

Input: root = [1]
Output: 1

Constraints:

  • The number of nodes in the tree is in the range [0, 5 * 104].
  • 0 <= Node.val <= 5 * 104
  • The tree is guaranteed to be complete.

Solution: Recursion


For each node, count the height of it’s left and right subtree by going left only.

Let L = height(left) R = height(root), if L == R, which means the left subtree is perfect.
It has (2^L – 1) nodes, +1 root, we only need to count nodes of right subtree recursively.
If L != R, L must be R + 1 since the tree is complete, which means the right subtree is perfect.
It has (2^(L-1) – 1) nodes, +1 root, we only need to count nodes of left subtree recursively.

Time complexity: T(n) = T(n/2) + O(logn) = O(logn*logn)

Space complexity: O(logn)

C++