\documentclass[10pt,a4paper]{article}

% Packages
\usepackage{fancyhdr}           % For header and footer
\usepackage{multicol}           % Allows multicols in tables
\usepackage{tabularx}           % Intelligent column widths
\usepackage{tabulary}           % Used in header and footer
\usepackage{hhline}             % Border under tables
\usepackage{graphicx}           % For images
\usepackage{xcolor}             % For hex colours
%\usepackage[utf8x]{inputenc}    % For unicode character support
\usepackage[T1]{fontenc}        % Without this we get weird character replacements
\usepackage{colortbl}           % For coloured tables
\usepackage{setspace}           % For line height
\usepackage{lastpage}           % Needed for total page number
\usepackage{seqsplit}           % Splits long words.
%\usepackage{opensans}          % Can't make this work so far. Shame. Would be lovely.
\usepackage[normalem]{ulem}     % For underlining links
% Most of the following are not required for the majority
% of cheat sheets but are needed for some symbol support.
\usepackage{amsmath}            % Symbols
\usepackage{MnSymbol}           % Symbols
\usepackage{wasysym}            % Symbols
%\usepackage[english,german,french,spanish,italian]{babel}              % Languages

% Document Info
\author{woshiamiaojiang}
\pdfinfo{
  /Title (labuladong.pdf)
  /Creator (Cheatography)
  /Author (woshiamiaojiang)
  /Subject (Labuladong Cheat Sheet)
}

% Lengths and widths
\addtolength{\textwidth}{6cm}
\addtolength{\textheight}{-1cm}
\addtolength{\hoffset}{-3cm}
\addtolength{\voffset}{-2cm}
\setlength{\tabcolsep}{0.2cm} % Space between columns
\setlength{\headsep}{-12pt} % Reduce space between header and content
\setlength{\headheight}{85pt} % If less, LaTeX automatically increases it
\renewcommand{\footrulewidth}{0pt} % Remove footer line
\renewcommand{\headrulewidth}{0pt} % Remove header line
\renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit
% This two commands together give roughly
% the right line height in the tables
\renewcommand{\arraystretch}{1.3}
\onehalfspacing

% Commands
\newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour
\newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols
\newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns
\newcommand{\tn}{\tabularnewline} % Required as custom column type in use

% Font and Colours
\definecolor{HeadBackground}{HTML}{333333}
\definecolor{FootBackground}{HTML}{666666}
\definecolor{TextColor}{HTML}{333333}
\definecolor{DarkBackground}{HTML}{A3A3A3}
\definecolor{LightBackground}{HTML}{F3F3F3}
\renewcommand{\familydefault}{\sfdefault}
\color{TextColor}

% Header and Footer
\pagestyle{fancy}
\fancyhead{} % Set header to blank
\fancyfoot{} % Set footer to blank
\fancyhead[L]{
\noindent
\begin{multicols}{3}
\begin{tabulary}{5.8cm}{C}
    \SetRowColor{DarkBackground}
    \vspace{-7pt}
    {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent
        \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}}
    }
\end{tabulary}
\columnbreak
\begin{tabulary}{11cm}{L}
    \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Labuladong Cheat Sheet}}}} \\
    \normalsize{by \textcolor{DarkBackground}{woshiamiaojiang} via \textcolor{DarkBackground}{\uline{cheatography.com/193748/cs/40865/}}}
\end{tabulary}
\end{multicols}}

\fancyfoot[L]{ \footnotesize
\noindent
\begin{multicols}{3}
\begin{tabulary}{5.8cm}{LL}
  \SetRowColor{FootBackground}
  \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}}  \\
  \vspace{-2pt}woshiamiaojiang \\
  \uline{cheatography.com/woshiamiaojiang} \\
  \end{tabulary}
\vfill
\columnbreak
\begin{tabulary}{5.8cm}{L}
  \SetRowColor{FootBackground}
  \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}}  \\
   \vspace{-2pt}Not Yet Published.\\
   Updated 17th October, 2023.\\
   Page {\thepage} of \pageref{LastPage}.
\end{tabulary}
\vfill
\columnbreak
\begin{tabulary}{5.8cm}{L}
  \SetRowColor{FootBackground}
  \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}}  \\
  \SetRowColor{white}
  \vspace{-5pt}
  %\includegraphics[width=48px,height=48px]{dave.jpeg}
  Measure your website readability!\\
  www.readability-score.com
\end{tabulary}
\end{multicols}}




\begin{document}
\raggedright
\raggedcolumns

% Set font size to small. Switch to any value
% from this page to resize cheat sheet text:
% www.emerson.emory.edu/services/latex/latex_169.html
\footnotesize % Small font.


\begin{tabularx}{17.67cm}{X}
\SetRowColor{DarkBackground}
\mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{124. \seqsplit{二叉树中的最大路径和}}}  \tn
\SetRowColor{LightBackground}
\mymulticolumn{1}{x{17.67cm}}{class Solution \{ \newline  \newline     int res = Integer.MIN\_VALUE; \newline  \newline     public int maxPathSum(TreeNode root) \{ \newline         getOneSideMax(root); \newline         return res; \newline     \} \newline  \newline     public int getOneSideMax(TreeNode root) \{ \newline         if (root == null) \{ \newline             return 0; \newline         \} \newline         int left = Math.max(0, \seqsplit{getOneSideMax(root.left));} \newline         int right = Math.max(0, \seqsplit{getOneSideMax(root.right));} \newline         res = Math.max(left + right + root.val, res); \newline         return Math.max(left, right) + root.val; \newline     \} \newline \}} \tn 
\hhline{>{\arrayrulecolor{DarkBackground}}-}
\end{tabularx}
\par\addvspace{1.3em}

\begin{tabularx}{17.67cm}{X}
\SetRowColor{DarkBackground}
\mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{230. \seqsplit{二叉搜索树中第K小的元素}}}  \tn
\SetRowColor{LightBackground}
\mymulticolumn{1}{x{17.67cm}}{class Solution \{ \newline  \newline     int k = 0; \newline     int res = 0; \newline  \newline     public int kthSmallest(TreeNode root, int k) \{ \newline         this.k = k; \newline         traverse(root); \newline         return res; \newline     \} \newline  \newline     public void traverse(TreeNode root) \{ \newline         if (root == null) \{ \newline             return; \newline         \} \newline         traverse(root.left); \newline         k-{}-; \newline         if (k == 0) \{ \newline             res = root.val; \newline             return; \newline         \} \newline         traverse(root.right); \newline     \} \newline \}} \tn 
\hhline{>{\arrayrulecolor{DarkBackground}}-}
\end{tabularx}
\par\addvspace{1.3em}

\begin{tabularx}{17.67cm}{X}
\SetRowColor{DarkBackground}
\mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{322. 零钱兑换}}  \tn
\SetRowColor{LightBackground}
\mymulticolumn{1}{x{17.67cm}}{/{\emph{ \newline     开辟一个动态数组,基底dp{[}0{]}=0。 \newline     dp{[}i{]} = num。 \newline     \seqsplit{i代表目标值,num代表硬币个数。} \newline     \seqsplit{num初值为amt+1。代表不可能。} \newline     每个dp{[}i{]}都与dp{[}i-coin{]}的结果相关联。 \newline }}/ \newline class Solution \{ \newline     public int coinChange(int{[}{]} coins, int amount) \{ \newline 		 \newline         int{[}{]} dp = new int{[}amount + 1{]}; \newline         Arrays.fill(dp, amount + 1); \newline         dp{[}0{]} = 0; \newline         for (int i = 1; i \textless{} dp.length; i++) \{ \newline             for (int coin : coins) \{ \newline                 if (i - coin \textgreater{}= 0) \{ \newline                     dp{[}i{]} = Math.min(dp{[}i - coin{]} + 1, dp{[}i{]}); \newline                 \} \newline             \} \newline         \} \newline         return dp{[}dp.length - 1{]} == amount + 1? -1 : dp{[}dp.length - 1{]}; \newline     \} \newline \}} \tn 
\hhline{>{\arrayrulecolor{DarkBackground}}-}
\end{tabularx}
\par\addvspace{1.3em}

\begin{tabularx}{17.67cm}{X}
\SetRowColor{DarkBackground}
\mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{46. 全排列}}  \tn
\SetRowColor{LightBackground}
\mymulticolumn{1}{x{17.67cm}}{class Solution \{ \newline  \newline     boolean{[}{]} used; \newline     List\textless{}List\textless{}Integer\textgreater{}\textgreater{} res; \newline  \newline     public List\textless{}List\textless{}Integer\textgreater{}\textgreater{} permute(int{[}{]} nums) \{ \newline         res = new LinkedList\textless{}\textgreater{}(); \newline         used = new boolean{[}nums.length{]}; \newline         backtrack(nums, new LinkedList\textless{}\textgreater{}()); \newline         return res; \newline     \} \newline  \newline     public void backtrack(int{[}{]} nums, LinkedList\textless{}Integer\textgreater{} track) \{ \newline 				// \seqsplit{临时track大小满的时候} \newline         if (track.size() == nums.length) \{ \newline             res.add(new LinkedList(track)); \newline             return; \newline         \} \newline         for (int i = 0; i \textless{} nums.length; i++) \{ \newline             if (used{[}i{]}) continue; \newline 						// \seqsplit{做选择,添加,下探结果} \newline             used{[}i{]} = true; \newline             track.add(nums{[}i{]}); \newline             backtrack(nums, track); \newline             used{[}i{]} = false; \newline             track.removeLast(); \newline         \} \newline     \} \newline \}} \tn 
\hhline{>{\arrayrulecolor{DarkBackground}}-}
\end{tabularx}
\par\addvspace{1.3em}

\begin{tabularx}{17.67cm}{X}
\SetRowColor{DarkBackground}
\mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{105. \seqsplit{从前序与中序遍历序列构造二叉树}}}  \tn
\SetRowColor{LightBackground}
\mymulticolumn{1}{x{17.67cm}}{class Solution \{ \newline  \newline  \newline     HashMap\textless{}Integer, Integer\textgreater{} inorderValMapIdx = new HashMap\textless{}\textgreater{}(); \newline  \newline     public TreeNode buildTree(int{[}{]} preorder, int{[}{]} inorder) \{ \newline         for (int i = 0; i \textless{} inorder.length; i++) \{ \newline             inorderValMapIdx.put(inorder{[}i{]}, i); \newline         \} \newline         return buildTree(preorder, inorder, 0, preorder.length - 1, 0, inorder.length - 1); \newline     \} \newline  \newline     // pre: 3 9 20 15 7 \newline     //      T L R \newline     // in : 9 3 15 20 7 \newline     //      L T R \newline     public TreeNode buildTree(int{[}{]} preorder, int{[}{]} inorder, int preStart, int preEnd, int inStart, int inEnd) \{ \newline         if (preStart \textgreater{} preEnd) \{ \newline             return null; \newline         \} \newline         int rootVal = preorder{[}preStart{]}; \newline         int inorderRootIdx = \seqsplit{inorderValMapIdx.get(rootVal);} \newline         TreeNode root = new TreeNode(rootVal); \newline         root.left = buildTree(preorder, inorder, preStart + 1, preStart + inorderRootIdx - inStart, inStart, inorderRootIdx - 1); \newline         root.right = buildTree(preorder, inorder, preStart + inorderRootIdx - inStart + 1, preEnd, inorderRootIdx + 1, inEnd); \newline         return root; \newline     \} \newline \}} \tn 
\hhline{>{\arrayrulecolor{DarkBackground}}-}
\end{tabularx}
\par\addvspace{1.3em}



\end{document}