The problems today are Largest Magic Square1, Spiral Matrix II2, Find Kth Bit in Nth Binary String3, Reachable Nodes In Subdivided Graph4 and Subarrays with K Different Integers5. I am motivated today and decided to solve two more problems: Maximum Side Length of a Square with Sum Less than or Equal to Threshold6 and Jump Game V7.
The solution scripts are available: A, B, C, D, E, F and G.
A. Largest Magic Square⌗
Timeline⌗
- [2021.06.18 22:04] Start
- [2021.06.18 22:17] First attempt (AC)
B. Spiral Matrix II⌗
Timeline⌗
- [2021.06.18 22:17] Start
- [2021.06.18 22:24] First attempt (AC)
C. Find Kth Bit in Nth Binary String⌗
Commentary⌗
There is a naive solution that computes all the bits of $S_n$ and take the $k$-th. I decided to use recursion that solves the problem with $\mathcal{O}(\text{log}\ k)$. Although it is an overkill, I practised.
The recursion is simple. Suppose that $B_k$ is the $k$-th bit of the sequence (one-indexed):
- [Base Case] $B_k = 0$ if $k = 1$.
- [Base Case] $B_k = 1$ if $k = 2^b$ for some integer $b > 0$.
- [Transition] $B_k = 1 - B_{2^b - r}$ if $k = 2^b + r$ for some integers $b > 0$ and $0 < r < 2^b$.
Timeline⌗
- [2021.06.18 22:24] Start
- [2021.06.18 22:36] First attempt (RE: 4/63)
- Reason: Caused a stack overflow because I mishandled the second base case.
- [2021.06.18 22:38] Second attempt (AC)
D. Reachable Nodes In Subdivided Graph⌗
Timeline⌗
- [2021.06.18 22:36] Start
- [2021.06.18 22:42] Paused
- [2021.06.18 23:22] Resumed
- [2021.06.18 23:43] First attempt (WA: 28/47)
- Reason: Heaps are sorted incorrectly.
- [2021.06.18 23:50] Second attempt (AC)
E. Subarrays with K Different Integers⌗
Timeline⌗
- [2021.06.18 22:42] Start
- [2021.06.18 23:22] First attempt (AC)
F. Maximum Side Length of a Square with Sum Less than or Equal to Threshold⌗
Timeline⌗
- [2021.06.18 23:52] Start
- [2021.06.19 00:03] First attempt (AC)
G. Jump Game V⌗
Timeline⌗
- [2021.06.19 00:06] Start
- [2021.06.19 00:22] First attempt (AC)
-
LeetCode "Largest Magic Square"
https://leetcode.com/problems/largest-magic-square/ ↩︎ -
LeetCode "Spiral Matrix I"
https://leetcode.com/problems/spiral-matrix-ii/ ↩︎ -
LeetCode "Find Kth Bit in Nth Binary String"
https://leetcode.com/problems/find-kth-bit-in-nth-binary-string/ ↩︎ -
LeetCode "Reachable Nodes In Subdivided Graph"
https://leetcode.com/problems/reachable-nodes-in-subdivided-graph/ ↩︎ -
LeetCode "Subarrays with K Different Integers"
https://leetcode.com/problems/subarrays-with-k-different-integers/ ↩︎ -
LeetCode "Maximum Side Length of a Square with Sum Less than or Equal to Threshold"
https://leetcode.com/problems/maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold/ ↩︎ -
LeetCode "Jump Game V"
https://leetcode.com/problems/jump-game-v/ ↩︎