├── .DS_Store ├── Algorithm&DataStructure ├── .DS_Store ├── Advanced │ ├── .DS_Store │ └── FibonacciHeap │ │ ├── FibNode.java │ │ └── FibonacciHeap.java ├── BST │ └── KVpairBST.java ├── Graph │ ├── AcyclicSP.java │ ├── BFSPath.java │ ├── Bipartite.java │ ├── Bipartite.txt │ ├── CC.java │ ├── Cycle.java │ ├── DFSPath.java │ ├── DepthFirstOrder.java │ ├── Digraph.java │ ├── DigraphCycle.java │ ├── DigraphDFS.java │ ├── DirectedEdge.java │ ├── Edge.java │ ├── EdgeWeightedDigraph.java │ ├── EdgeWeightedDirectedCycle.java │ ├── EdgeWeightedGraph.java │ ├── Graph.java │ ├── IndexMinPQ.java │ ├── KosarajuSCC.java │ ├── KruskalMST.java │ ├── PrimMST.java │ ├── PrimMSTLazy.java │ ├── ShortestPath_BellmanFord.java │ ├── ShortestPath_Dijkstra.java │ ├── SymbolGraph.java │ ├── TEST_DAG.java │ ├── TEST_Digraph.java │ ├── TEST_EdgeWeightedDigraph.java │ ├── TEST_EdgeWeightedGraph.java │ ├── TEST_UndirectedGraph.java │ ├── TarjanSCC.java │ ├── TinyEWD.txt │ ├── Topological.java │ ├── UF.java │ ├── movies.txt │ ├── routes.txt │ ├── tinyDAG.txt │ ├── tinyDG.txt │ ├── tinyEWDAG.txt │ ├── tinyEWDn.txt │ ├── tinyEWDnc.txt │ ├── tinyEWG.txt │ └── tinyG.txt ├── Sort │ ├── CountingSort.java │ ├── HeapSort.java │ ├── InsertionSort.java │ ├── Main.java │ ├── MaxPQ.java │ ├── MergeSortBottomUp.java │ ├── MergeSortInPlace.java │ ├── MergeSortTopDown.java │ ├── QuickSort.java │ ├── SelectionSort.java │ └── ShellSort.java ├── String │ ├── KMP.java │ └── LSD.java └── UnionFind │ ├── QuickFindUF.java │ ├── QuickUnionPathCompressionUF.java │ ├── QuickUnionUF.java │ └── WeightedQuickUnionUF.java ├── Distributed System └── Cloud Computing - UIUC │ ├── Part1 │ ├── Week1 - Introduction and MapReduce.pdf │ ├── Week1_Slides │ │ ├── Introduction │ │ │ ├── C3_IntroClouds_A_CSRAfinal.pdf │ │ │ ├── C3_IntroClouds_B_CSRAfinal.pdf │ │ │ ├── C3_IntroClouds_C_CSRAfinal.pdf │ │ │ ├── C3_IntroClouds_D_CSRAfinal.pdf │ │ │ ├── C3_IntroClouds_E_CSRAfinal.pdf │ │ │ └── C3_IntroClouds_F_CSRAfinal.pdf │ │ └── MapReduce │ │ │ ├── C3_Mapreduce_A_CSRAfinal.pdf │ │ │ ├── C3_Mapreduce_B_CSRAfinal.pdf │ │ │ ├── C3_Mapreduce_C_CSRAfinal.pdf │ │ │ └── C3_Mapreduce_D_CSRAfinal.pdf │ ├── Week2_Slides │ │ ├── Gossip │ │ │ ├── C3_Gossip_A_CSRAfinal.pdf │ │ │ ├── C3_Gossip_B_CSRAfinal.pdf │ │ │ ├── C3_Gossip_C_CSRAfinal.pdf │ │ │ └── C3_Gossip_D_CSRAfinal.pdf │ │ ├── Grids │ │ │ ├── C3_Grids_A_CSRAfinal.pdf │ │ │ └── C3_Grids_B_CSRAfinal.pdf │ │ ├── L_member.sp11.ppt │ │ └── Membership │ │ │ ├── C3_Member_A_CSRAfinal.pdf │ │ │ ├── C3_Member_B_CSRAfinal.pdf │ │ │ ├── C3_Member_C_CSRAfinal.pdf │ │ │ ├── C3_Member_D_CSRAfinal.pdf │ │ │ ├── C3_Member_E_CSRAfinal.pdf │ │ │ └── C3_Member_F_CSRAfinal.pdf │ ├── Week3 │ │ ├── C3_p2p_A_CSRAfinal.pdf │ │ ├── C3_p2p_B_CSRAfinal.pdf │ │ ├── C3_p2p_C_CSRAfinal.pdf │ │ ├── C3_p2p_D_CSRAfinal.pdf │ │ ├── C3_p2p_E_CSRAfinal.pdf │ │ ├── C3_p2p_F_CSRAfinal.pdf │ │ ├── C3_p2p_G_CSRAfinal.pdf │ │ └── C3_p2p_H_CSRAfinal.pdf │ ├── Week4 │ │ ├── C3_Key-valueStores_A_CSRAfinal.pdf │ │ ├── C3_Key-valueStores_B_CSRAfinal.pdf │ │ ├── C3_Key-valueStores_C_CSRAfinal.pdf │ │ ├── C3_Key-valueStores_D_CSRAfinal.pdf │ │ ├── C3_Key-valueStores_E_CSRAfinal.pdf │ │ ├── C3_TimeAndOrdering_A_CSRAfinal.pdf │ │ ├── C3_TimeAndOrdering_B_CSRAfinal.pdf │ │ ├── C3_TimeAndOrdering_C_CSRAfinal.pdf │ │ ├── C3_TimeAndOrdering_D_CSRAfinal.pdf │ │ └── C3_TimeAndOrdering_E_CSRAfinal.pdf │ └── Week5 │ │ ├── C3_Multicast_A_CSRAfinal.pdf │ │ ├── C3_Multicast_B_CSRAfinal.pdf │ │ ├── C3_Multicast_C_CSRAfinal.pdf │ │ ├── C3_Multicast_D_CSRAfinal.pdf │ │ ├── C3_Multicast_E_CSRAfinal.pdf │ │ ├── C3_Part1Outro_CSRAfinal.pdf │ │ ├── C3_Paxos_A_CSRAfinal.pdf │ │ ├── C3_Paxos_B_CSRAfinal.pdf │ │ ├── C3_Paxos_C_CSRAfinal.pdf │ │ ├── C3_Paxos_D_CSRAfinal.pdf │ │ ├── C3_Snapshots_A_CSRAfinal.pdf │ │ ├── C3_Snapshots_B_CSRAfinal.pdf │ │ ├── C3_Snapshots_C_CSRAfinal.pdf │ │ └── C3_Snapshots_D_CSRAfinal.pdf │ └── Part2 │ ├── week1 │ ├── C3_Election_A_CSRAfinal.pdf │ ├── C3_Election_B_CSRAfinal.pdf │ ├── C3_Election_C_CSRAfinal.pdf │ ├── C3_Election_D_CSRAfinal.pdf │ ├── C3_MutualExclusion_A_CSRAfinal.pdf │ ├── C3_MutualExclusion_B_CSRAfinal.pdf │ ├── C3_MutualExclusion_C_CSRAfinal.pdf │ └── C3_MutualExclusion_D_CSRAfinal.pdf │ ├── week2 │ ├── C3_Concurrency_A_CSRAfinal.pdf │ ├── C3_Concurrency_B_CSRAfinal.pdf │ ├── C3_Concurrency_C_CSRAfinal.pdf │ ├── C3_Concurrency_D_CSRAfinal.pdf │ ├── C3_Concurrency_E_CSRAfinal.pdf │ ├── C3_Replication_A_CSRAfinal.pdf │ └── C3_Replication_B_CSRAfinal.pdf │ └── week3 │ ├── C3_GraphProcessing_A_CSRAfinal.pdf │ ├── C3_Scheduling_A_CSRAfinal.pdf │ ├── C3_Scheduling_B_CSRAfinal.pdf │ ├── C3_Scheduling_C_CSRAfinal.pdf │ ├── C3_StreamProcessing_A_CSRAfinal.pdf │ └── C3_StructureofNetworks_A_CSRAfinal.pdf ├── Hackerrank ├── String │ ├── Bigger_Is_Greater.java │ ├── Funny_String.java │ ├── Sherlock.java │ └── Two_Strings.java └── WarmUp │ ├── Q01_Solve_Me_First.java │ ├── Q02_Solve_Me_Second.java │ ├── Q03_Simple_Array_Sum.java │ ├── Q04_A_Very_Big_Sum.java │ ├── Q05_Diagonal_Difference.java │ ├── Q06_Plus_Minus.java │ ├── Q07_Staircase.java │ ├── Q08_Time_Conversion.java │ ├── Q09_Library_Fine.java │ └── Q10_Extra_Long_Factorials.java ├── Leetcode ├── .DS_Store ├── All │ ├── .DS_Store │ ├── 001_Two_Sum.java │ ├── 002_Add_Two_Numbers.java │ ├── 003_Longest_Substring_Without_Repeating_Characters.java │ ├── 004_Median_of_Two_Sorted_Arrays.java │ ├── 005_Longest_Palindromic_Substring.java │ ├── 006_ZigZag_Conversion.java │ ├── 007_Reverse_Integer.java │ ├── 008_String_to_Integer.java │ ├── 009_Palindrome_Number.java │ ├── 010_Regular_Expression_Matching.java │ ├── 011_Container_With_Most_Water.java │ ├── 012_Integer_to_Roman.java │ ├── 013_Roman_to_Integer.java │ ├── 014_Longest_Common_Prefix.java │ ├── 015_3Sum.java │ ├── 016_3Sum_Closest.java │ ├── 017_Letter_Combinations_of_a_Phone_Number.java │ ├── 018_4Sum.java │ ├── 019_Remove_Nth_Node_From_End_of_List.java │ ├── 020_Valid_Parentheses.java │ ├── 021_Merge_Two_Sorted_Lists.java │ ├── 022_Generate_Parentheses.java │ ├── 023_Merge_k_Sorted_Lists.java │ ├── 024_Swap_Nodes_in_Pairs.java │ ├── 025_Reverse_Nodes_in_k-Group.java │ ├── 026_Remove_Duplicates_from_Sorted_Array.java │ ├── 027_Remove_Element.java │ ├── 028_Implement_strStr().java │ ├── 029_Divide_Two_Integers.java │ ├── 030_Substring_with_Concatenation_of_All_Words.java │ ├── 031_Next_Permutation.java │ ├── 032_Longest_Valid_Parentheses.java │ ├── 033_Search_in_Rotated_Sorted_Array.java │ ├── 034_Search_for_a_Range.java │ ├── 035_Search_Insert_Position.java │ ├── 036_Valid_Sudoku.java │ ├── 037_Sudoku_Solver.java │ ├── 038_Count_and_Say.java │ ├── 039_Combination_Sum.java │ ├── 040_Combination_Sum_II.java │ ├── 041_First_Missing_Positive.java │ ├── 042_Trapping_Rain_Water.java │ ├── 043_Multiply_Strings.java │ ├── 044_Wildcard_Matching.java │ ├── 045_Jump_Game_II.java │ ├── 046_Permutations.java │ ├── 047_Permutations_II.java │ ├── 048_Rotate_Image.java │ ├── 049_Group_Anagrams.java │ ├── 050_Pow(x,n).java │ ├── 051_N-Queens.java │ ├── 052_N-Queens_II.java │ ├── 053_Maximum_Subarray.java │ ├── 054_Spiral_Matrix.java │ ├── 055_Jump_Game.java │ ├── 056_Merge_Intervals.java │ ├── 057_Insert_Intervals.java │ ├── 057_Insert_Intervals_FollowUp.java │ ├── 057_Insert_Intervals_FollowUp2.java │ ├── 058_Length_of_Last_Word.java │ ├── 059_Spiral_Matrix_II.java │ ├── 060_Permutation_Sequence.java │ ├── 061_Rotate_List.java │ ├── 062_Unique_Path.java │ ├── 063_Unique_Path_II.java │ ├── 064_Minimum_Path_Sum.java │ ├── 065_Valid_Number.java │ ├── 066_Plus_One.java │ ├── 067_Add_Binary.java │ ├── 068_Text_Justification.java │ ├── 069_Sqrt(x).java │ ├── 070_Climbing_Stairs.java │ ├── 071_Simplify_Path.java │ ├── 072_Edit_Distance.java │ ├── 073_Set_Matrix_Zeroes.java │ ├── 074_Search_a_2D_Matrix.java │ ├── 075_Sort_Colors.java │ ├── 076_Minimum_Window_Substring.java │ ├── 077_Combinations.java │ ├── 078_Subsets.java │ ├── 079_Word_Search.java │ ├── 080_Remove_Duplicates_from_Sorted_Array_II.java │ ├── 081_Search_in_Rotated_Sorted_Array_II.java │ ├── 082_Remove_Duplicates_from_Sorted_List_II.java │ ├── 083_Remove_Duplicates_from_Sorted_List.java │ ├── 084_Largest_Rectangle_in_Histogram.java │ ├── 085_Maximal_Rectangle.java │ ├── 086_Partition_List.java │ ├── 088_Merge_Sorted_Array.java │ ├── 089_Gray_Code.java │ ├── 090_Subsets_II.java │ ├── 091_Decode Ways.java │ ├── 092_Reverse_Linked_List_II.java │ ├── 093_Restore_IP_Addresses.java │ ├── 094_Binary_Tree_Inorder_Traversal.java │ ├── 095_Unique_Binary_Search_Trees_II.java │ ├── 096_Unique_Binary_Search_Trees.java │ ├── 097_Interleaving_String.java │ ├── 098_Validate_Binary_Search_Tree.java │ ├── 099_Recover_Binary_Search_Tree.java │ ├── 100_Same_Tree.java │ ├── 101_Symmetric_Tree.java │ ├── 102_Binary_Tree_Level_Order_Traversal.java │ ├── 103_Binary_Tree_Zigzag_Level_Order_Traversal.java │ ├── 104_Maximum_Depth_of_Binary_Tree.java │ ├── 105_Construct_Binary_Tree_from_Preorder and Inorder Traversal.java │ ├── 106_Construct_Binary_Tree_from_Inorder and Postorder Traversal.java │ ├── 107_Binary_Tree_Level_Order_Traversal II.java │ ├── 108_Convert_Sorted_Array_to_Binary_Search_Tree.java │ ├── 109_Convert_Sorted_List_to_Binary_Search_Tree.java │ ├── 110_Balanced_Binary_Tree.java │ ├── 111_Minimum_Depth_of_Binary_Tree.java │ ├── 112_Path_Sum.java │ ├── 113_Path_Sum_II.java │ ├── 114_Flatten_Binary_Tree_to_Linked_List.java │ ├── 115_Distinct_Subsequences.java │ ├── 116_Populating_Next_Right_Pointers_in_Each_Node.java │ ├── 117_Populating_Next_Right_Pointers_in_Each_Node_II.java │ ├── 118_Pascal's_Triangle.java │ ├── 119_Pascal's_Triangle_II.java │ ├── 120_Triangle.java │ ├── 121_Best_Time_to_Buy_and_Sell_Stock.java │ ├── 122_Best_Time_to_Buy_and_Sell_Stock _II.java │ ├── 123_Best_Time_to_Buy_and_Sell_Stock_III.java │ ├── 124_Binary_Tree_Maximum_Path_Sum.java │ ├── 125_Valid_Palindrome.java │ ├── 127_Word_Ladder.java │ ├── 128_Longest_Consecutive_Sequence.java │ ├── 129_Sum_Root_to_Leaf_Numbers.java │ ├── 130_Surrounded_Regions.java │ ├── 131_Palindrome_Partitioning.java │ ├── 132_Palindrome_Partitioning_II.java │ ├── 133_Clone_Graph.java │ ├── 134_Gas_Station.java │ ├── 135_Candy.java │ ├── 136_Single_Number.java │ ├── 137_Single_Number_II.java │ ├── 138_Copy_List_with_Random_Pointer.java │ ├── 139_Word_Break.java │ ├── 140_Word_Break_II.java │ ├── 141_Linked_List_Cycle.java │ ├── 142_Linked_List_Cycle_II.java │ ├── 143_Reorder_List.java │ ├── 144_Binary_Tree_Preorder_Traversal.java │ ├── 145_Binary_Tree_Postorder_Traversal.java │ ├── 146_LRU_Cache.java │ ├── 147_Insertion_Sort_List.java │ ├── 148_Sort_List.java │ ├── 149_Max_Points_on_a_Line.java │ ├── 150_Evaluate_Reverse_Polish_Notation.java │ ├── 151_Reverse_Words_in_a_String.java │ ├── 152_Maximum_Product_Subarray.java │ ├── 153_Find Minimum_in_Rotated_Sorted_Array.java │ ├── 154_Find Minimum_in_Rotated_Sorted_Array_II.java │ ├── 155_FollowUp_Max_stack.java │ ├── 155_Min_Stack.java │ ├── 156_Binary_Tree_Upside_Down.java │ ├── 157_Read_N_Characters_Given_Read4.java │ ├── 158_Read_N_Characters_Given_Read4_II.java │ ├── 159_Follow_Up_Longest_Substring_with_At_Most_K_Distinct_Characters.java │ ├── 159_Longest_Substring_with_At_Most_Two_Distinct_Characters.java │ ├── 160_Intersection_of_Two_Linked_Lists.java │ ├── 161_One_Edit_Distance.java │ ├── 162_Find_Peak_Element.java │ ├── 163_Missing_Ranges.java │ ├── 164_Maximum_Gap.java │ ├── 165_Compare_Version_Numbers.java │ ├── 166_Fraction_to_Recurring_Decimal.java │ ├── 167_Two_Sum_II(input_array_is_sorted).java │ ├── 168_Excel_Sheet_Column_Title.java │ ├── 169_Majority_Element.java │ ├── 170_Two_Sum_III-Data_Structure_Design.java │ ├── 171_Excel_Sheet_Column_Number.java │ ├── 172_Factorial_Trailing_Zeroes.java │ ├── 173_Binary_Search_Tree_Iterator.java │ ├── 174_Dungeon_Game.java │ ├── 179_Largest_Number.java │ ├── 186_Follow_Up_Reverse_Words_in_K_Groups.java │ ├── 186_Reverse_Words_in_a_String_II.java │ ├── 187_Repeated_DNA_Sequences.java │ ├── 188_Best_Time_to_Buy_and_Sell_Stock _IV.java │ ├── 189_Rotate_Array.java │ ├── 190_Reverse_Bits.java │ ├── 191_Number_of_1_Bits.java │ ├── 198_House_Robber.java │ ├── 199_Binary_Tree_Right_Side_View.java │ ├── 200_Number_of_Islands.java │ ├── 201_Bitwise_AND_of_Numbers_Range.java │ ├── 202_Happy_Number.java │ ├── 203_Remove_Linked_List_Elements.java │ ├── 204_Count_Primes.java │ ├── 205_Isomorphic_Strings.java │ ├── 206_Reverse_Linked_List.java │ ├── 207_Course_Schedule.java │ ├── 208_Implement_Trie_(Prefix_Tree).java │ ├── 209_Minimum_Size_Subarray_Sum.java │ ├── 210_Course_Schedule_II.java │ ├── 211_Add_and_Search_Word-Data_structure_design.java │ ├── 212_Word_Search_II.java │ ├── 213_House_Robber_II.java │ ├── 214_Shortest_Palindrome.java │ ├── 215_Kth_Largest_Element_in_an_Array.java │ ├── 216_Combination_Sum_III.java │ ├── 217_Contains_Duplicate.java │ ├── 219_Contains_Duplicate_II.java │ ├── 220_Contains_Duplicate_III.java │ ├── 221_Maximal_Square.java │ ├── 222_Count_Complete_Tree_Nodes.java │ ├── 223_Rectangle_Area.java │ ├── 224_Basic_Calculator.java │ ├── 225_Implement_Stack_Using_Queues.java │ ├── 226_Invert_Binary_Tree.java │ ├── 227_Basic_Calculator_II.java │ ├── 228_Summary_Ranges.java │ ├── 229_Majority_Element_II.java │ ├── 230_Kth_Smallest_Element_in_a_BST.java │ ├── 231_Power_of_Two.java │ ├── 232_Implement_Queue_using_Stacks.java │ ├── 233_Number_of_Digit_One.java │ ├── 234_Palindrome_Linked_List.java │ ├── 235_Lowest_Common_Ancestor_of_a_Binary_ Search_Tree.java │ ├── 236_Lowest_Common_Ancestor_of_a_Binary_Tree.java │ ├── 237_Delete_Node_in_a_Linked_List.java │ ├── 238_Product_of_Array_Except_Self.java │ ├── 239_Sliding_Window_Maximum.java │ ├── 240_Search_a_2D_Matrix_II.java │ ├── 241_Different_Ways_to_Add_Parentheses.java │ ├── 243_Shortest_Word_Distance.java │ ├── 244_Shortest_Word_Distance_II.java │ ├── 245_Shortest_Word_Distance_III.java │ ├── 246_Strobogrammatic_Number.java │ ├── 249_Group_Shifted_Strings.java │ ├── 250_Count_Univalue_Subtrees.java │ ├── 251_Flatten_2D_Vector.java │ ├── 252_Meeting_Rooms.java │ ├── 253_Meeting_Rooms_II.java │ ├── 254_Factor_Combinations.java │ ├── 256_Paint_House.java │ ├── 257_Binary_Tree_Paths.java │ ├── 258_Add_Digits.java │ ├── 259_3Sum_Smaller.java │ ├── 260_Single_Number_III.java │ ├── 261_Graph_Valid_Tree.java │ ├── 263_Ugly_Number.java │ ├── 264_Ugly_Number_II.java │ ├── 265_Paint_House_II.java │ ├── 266_Palindrome_Permutation.java │ ├── 267_Palindrome_Permutation_II.java │ ├── 268_Missing_Number.java │ ├── 269_Alien_Dictionary.java │ ├── 270_Closest_Binary_Search_Tree_Value.java │ ├── 271_Encode_and_Decode_Strings.java │ ├── 272_Closest_Binary_Search_Tree_Value_II.java │ ├── 275_H-Index_II.java │ ├── 276_Paint_Fence.java │ ├── 277_Find_the_Celebrity.java │ ├── 280_Wiggle_Sort.java │ ├── 282_Expression_Add_Operators.java │ ├── 286_Walls_and_Gates.java │ ├── 287_Find_the_Duplicate_Number.java │ ├── 288_Unique_Word_Abbreviation.java │ ├── 289_Game_of_Life.java │ ├── 291_Word_Pattern_II.java │ ├── 292_Nim_Game.java │ ├── 293_Flip_Game.java │ ├── 294_Flip_Game_II.java │ ├── 295_Find_Median_from_Data_Stream.java │ ├── 296_Best_Meeting_Point.java │ ├── 297_Serialize_and_Deserialize_Binary_Tree.java │ ├── 298_Binary_Tree_Longest_Consecutive_Sequence.java │ ├── 300_Longest_Increasing_Subsequence.java │ ├── 302_Smallest_Rectangle_Enclosing_Black_Pixels.java │ ├── 303_Range_Sum_Query.java │ ├── 304_Range_Sum_Query_2D.java │ ├── 305_Number_of_Islands II.java │ ├── 306_Additive_Number.java │ ├── 307_Range_Sum_Query.java │ ├── 310_Minimum_Height_Trees.java │ ├── 311_Sparse_Matrix_Multiplication.java │ ├── 315_Count_of_Smaller_Numbers_After_Self.java │ ├── 322_Coin_Change.java │ ├── 326_Power_of_Three.java │ ├── 328_Odd_Even_Linked_List.java │ ├── 333_Largest_BST_Subtree.java │ ├── 337_House_Robber_III.java │ ├── 338_Counting_Bits.java │ ├── 340_Longest_Substring_with_At_Most_K_Distinct_Characters.java │ ├── 342_Power_of_Four.java │ ├── 344_Reverse_String.java │ ├── 345_Reverse_Vowels_of_a_String.java │ ├── 346_Moving_Average_from_Data_Stream.java │ ├── 347_Top_K_Frequent_Elements.java │ ├── 349_Intersection_of_Two_Arrays.java │ ├── 350_Intersection_of_Two_Arrays_II.java │ ├── 357_Count_Numbers_with_Unique_Digits.java │ ├── 359_Logger_Rate_Limiter.java │ ├── 366_Find_Leaves_of_Binary_Tree.java │ ├── 367_Valid_Perfect_Square.java │ ├── 369_Plus_One_Linked_List.java │ ├── 373_Find_K_Pairs_with_Smallest_Sums.java │ ├── 374_Guess_Number_Higher_or_Lower.java │ ├── 383_Ransom_Note.java │ ├── 388_Longest_Absolute_File_Path.java │ ├── 389_Find_the_Difference.java │ ├── 392_Is_subsequence.java │ ├── 404_Sum_of_Left_Leaves.java │ ├── 406_Queue_Reconstruction_by_Height.java │ ├── 408_Valid_Word_Abbreviation.java │ ├── 409_Longest_Palindrome.java │ ├── 412_Fizz_Buzz.java │ ├── 415_Add_Strings.java │ ├── 434_Number_of_Segments_in_a_String.java │ ├── 437_Path_Sum_III.java │ ├── 442_Find_All_Duplicates_in_an_Array.java │ ├── 445_Add_Two_Numbers_II.java │ ├── 447_Number_of_Boomerangs.java │ ├── 448_Find_All_Numbers_Disappeared_in_an_Array.java │ ├── 451_Sort_Characters_By_Frequency.java │ ├── 459_Repeated_Substring_Pattern.java │ ├── 461_Hamming_Distance.java │ ├── 463_Island_Perimeter.java │ ├── 468_Validate_IP_Address.java │ ├── 476_Number_Complement.java │ ├── 482_License_Key_Formatting.java │ ├── 485_Max_Consecutive_Ones.java │ ├── 493_Reverse_Pairs.java │ ├── 496_Next_Greater_Element_I.java │ ├── 500_Keyboard_Row.java │ ├── 501_Find_Mode_in_Binary_Search_Tree.java │ ├── 503_Next_Greater_Element_II.java │ ├── 508_Most_Frequent_Subtree_Sum.java │ ├── 513_Find_Bottom_Left_Tree_Value.java │ ├── 515_Find_Largest_Value_in_Each_Tree_Row.java │ ├── 520_Detect_Capital.java │ ├── 521_Longest_Uncommon_Subsequence_I.java │ ├── 522_Longest_Uncommon_Subsequence_II.java │ ├── 523_Continuous_Subarray_Sum.java │ ├── 526_Beautiful_Arrangement.java │ ├── 530_Minimum_Absolute_Difference_in_BST.java │ ├── 538_Convert_BST_to_Greater_Tree.java │ ├── 539_Minimum_Time_Difference.java │ ├── 541_Reverse_String_II.java │ ├── 544_Output_Contest_Matches.java │ ├── 551_Student_Attendance_Record_I.java │ ├── 553_Optimal_Division.java │ ├── 556_Next_Greater_Element_III.java │ ├── 557_Reverse_Words_in_a_String_III.java │ ├── 560_Subarray_Sum_Equals_K.java │ ├── 561_Array_Partition_I.java │ ├── 563_Binary_Tree_Tilt.java │ ├── 566_Reshape_the_Matrix.java │ ├── 572_Subtree_of_Another_Tree.java │ ├── 575_Distribute_Candies.java │ ├── 581_Shortest_Unsorted_Continuous_Subarray.java │ ├── 599_Minimum_Index_Sum_of_Two_Lists.java │ └── 617_Merge_Two_Binary_Trees.java ├── Array │ ├── .DS_Store │ ├── 001_Two_Sum.cpp │ ├── 001_Two_Sum.java │ ├── 011_Container_With_Most_Water.cpp │ ├── 011_Container_With_Most_Water.java │ ├── 015_3Sum.cpp │ ├── 015_3Sum.java │ ├── 016_3Sum_Closest.cpp │ ├── 016_3Sum_Closest.java │ ├── 018_4Sum.java │ ├── 026_Remove_Duplicates_from_Sorted_Array.java │ ├── 027_Remove_Element.java │ ├── 031_Next_Permutation.java │ ├── 036_Valid_Sudoku.java │ ├── 041_First_Missing_Positive.java │ ├── 042_Trapping_Rain_Water.java │ ├── 048_Rotate_Image.java │ ├── 054_Spiral_Matrix.java │ ├── 056_Merge_Intervals.java │ ├── 057_Insert_Intervals.java │ ├── 057_Insert_Intervals_FollowUp.java │ ├── 057_Insert_Intervals_FollowUp2.java │ ├── 059_Spiral_Matrix_II.java │ ├── 066_Plus_One.java │ ├── 073_Set_Matrix_Zeroes.java │ ├── 075_Sort_Colors.java │ ├── 080_Remove_Duplicates_from_Sorted_Array_II.java │ ├── 084_Largest_Rectangle_in_Histogram.java │ ├── 085_Maximal_Rectangle.java │ ├── 088_Merge_Sorted_Array.java │ ├── 118_Pascal's_Triangle.java │ ├── 119_Pascal's_Triangle_II.java │ ├── 127_Word_Ladder.java │ ├── 128_Longest_Consecutive_Sequence.java │ ├── 130_Surrounded_Regions.java │ ├── 150_Evaluate_Reverse_Polish_Notation.java │ ├── 163_Missing_Ranges.java │ ├── 164_Maximum_Gap.java │ ├── 167_Two_Sum_II(input_array_is_sorted).java │ ├── 169_Majority_Element.java │ ├── 179_Largest_Number.java │ ├── 189_Rotate_Array.java │ ├── 209_Minimum_Size_Subarray_Sum.java │ ├── 215_Kth_Largest_Element_in_an_Array.java │ ├── 217_Contains_Duplicate.java │ ├── 219_Contains_Duplicate_II.java │ ├── 220_Contains_Duplicate_III.java │ ├── 228_Summary_Ranges.java │ ├── 229_Majority_Element_II.java │ ├── 238_Product_of_Array_Except_Self.cpp │ ├── 238_Product_of_Array_Except_Self.java │ ├── 239_Sliding_Window_Maximum.java │ ├── 243_Shortest_Word_Distance.java │ ├── 244_Shortest_Word_Distance_II.java │ ├── 245_Shortest_Word_Distance_III.java │ ├── 252_Meeting_Rooms.java │ ├── 253_Meeting_Rooms_II.java │ ├── 259_3Sum_Smaller.java │ ├── 275_H-Index_II.java │ ├── 277_Find_the_Celebrity.java │ ├── 280_Wiggle_Sort.java │ ├── 283_Move_Zeroes.java │ ├── 287_Find_the_Duplicate_Number.java │ ├── 289_Game_of_Life.java │ ├── 303_Range_Sum_Query.java │ ├── 304_Range_Sum_Query_2D.java │ ├── 305_Number_of_Islands II.java │ ├── 307_Range_Sum_Query.java │ ├── 311_Sparse_Matrix_Multiplication.java │ ├── 315_Count_of_Smaller_Numbers_After_Self.java │ ├── 325_Maximum_Size_Subarray_Sum_Equals_k.java │ ├── 341_Flatten_Nested_List_Iterator.java │ ├── 347_Top_K_Frequent_Elements.java │ ├── 360_Sort_Transformed_Array.java │ ├── 373_Find_K_Pairs_with_Smallest_Sums.java │ ├── 379_Desgin_Phone_Directory.java │ ├── 384_Shuffle_an_Array.java │ ├── 398_Random_Pick_Index.java │ ├── 406_Queue_Reconstruction_by_Height.java │ ├── 407_Trapping_Rain_Water_II.java │ ├── 435_Non-overlapping_Intervals.java │ ├── 442_Find_All_Duplicates_in_an_Array.java │ ├── 447_Number_of_Boomerangs.java │ ├── 448_Find_All_Numbers_Disappeared_in_an_Array.java │ ├── 452_Minimum_Number_of_Arrows_to_Burst Balloons.java │ ├── 463_Island_Perimeter.java │ ├── 485_Max_Consecutive_Ones.java │ ├── 493_Reverse_Pairs.java │ ├── 495_Teemo_Attacking.java │ ├── 496_Next_Greater_Element_I.java │ ├── 498_Diagonal_Traverse.java │ ├── 503_Next_Greater_Element_II.java │ ├── 528_Random_Pick_with_Weight.java │ ├── 531_Lonely_Pixel_I.java │ ├── 560_Subarray_Sum_Equals_K.java │ ├── 561_Array_Partition_I.java │ ├── 562_Longest_Line_of_Consecutive_One_in_Matrix.java │ ├── 566_Reshape_the_Matrix.java │ ├── 575_Distribute_Candies.java │ ├── 581_Shortest_Unsorted_Continuous_Subarray.java │ ├── 670_Maximum_Swap.java │ ├── 674_Longest_Continuous_Increasing_Subsequence.java │ ├── 683_K_Empty_Slots.java │ ├── 697_Degree_of_an_Array.java │ ├── 703_Kth_Largest_Element_in_a_Stream.java │ ├── 717_1-bit_and_2-bit_Characters.java │ ├── 721_Accounts_Merge.java │ ├── 724_Find_Pivot_Index.java │ ├── 731_My_Calendar II.java │ ├── 734_Sentence_Similarity.java │ ├── 737_Sentence_Similarity_II.java │ ├── 759_Employee_Free_Time.java │ ├── 760_Find_Anagram_Mappings.java │ ├── 765_Couples_Holding_Hands.java │ ├── 766_Toeplitz_Matrix.java │ ├── 778_Swim_in_Rising_Water.java │ ├── 803_Bricks_Falling_When_Hit.java │ ├── 807_Max_Increase_to_Keep_City_Skyline.java │ ├── 825_Friends_Of_Appropriate_Ages.java │ ├── 836_Rectangle_Overlap.java │ ├── 839_Similar_String_Groups.java │ ├── 846_Hand_of_Straights.java │ ├── 849_Maximize_Distance_to_Closest_Person.java │ ├── 857_Minimum_Cost_to_Hire_K_Workers.java │ ├── 896_Monotonic_Array.java │ ├── 904_Fruit_Into_Baskets.java │ ├── 915_Partition_Array_into_Disjoint_Intervals.java │ ├── 930_Binary_Subarrays_With_Sum.java │ └── 932_Beautiful_Array.java ├── BFSDFS │ ├── 126_Word_Ladder_II.java │ ├── 317_Shortest_Distance_from_All_Buildings.java │ ├── 394_Decode_String.java │ ├── 490_The_Maze.java │ ├── 499_The_Maze_III.java │ ├── 505_The_Maze_II.java │ ├── 690_Employee_Importance.java │ ├── 694_Number_of_Distinct_Islands.java │ ├── 733_Flood_Fill.java │ └── 886_Possible_Bipartition.java ├── BackTracking │ ├── 017_Letter_Combinations_of_a_Phone_Number.java │ ├── 022_Generate_Parentheses.java │ ├── 037_Sudoku_Solver.java │ ├── 039_Combination_Sum.java │ ├── 040_Combination_Sum_II.java │ ├── 046_Permutations.java │ ├── 047_Permutations_II.java │ ├── 051_N-Queens.cpp │ ├── 051_N-Queens.java │ ├── 052_N-Queens_II.java │ ├── 060_Permutation_Sequence.java │ ├── 077_Combinations.java │ ├── 078_Subsets.java │ ├── 079_Word_Search.java │ ├── 089_Gray_Code.java │ ├── 090_Subsets_II.java │ ├── 093_Restore_IP_Addresses.java │ ├── 131_Palindrome_Partitioning.java │ ├── 140_Word_Break_II.java │ ├── 212_Word_Search_II.java │ ├── 216_Combination_Sum_III.java │ ├── 254_Factor_Combinations.java │ ├── 291_Word_Pattern_II.java │ ├── 301_Remove_Invalid_Parentheses.java │ ├── 306_Additive_Number.java │ ├── 339_Nested_List_Weight_Sum.java │ ├── 364_Nested_List_Weight_Sum_II.java │ ├── 491_Increasing_Subsequences.java │ ├── 526_Beautiful_Arrangement.java │ ├── 638_Shopping_Offers.java │ ├── 753_Cracking_the_Safe.java │ └── 842_Split_Array_into_Fibonacci_Sequence.java ├── BinarySearch │ ├── 004_Median_of_Two_Sorted_Arrays.java │ ├── 033_Search_in_Rotated_Sorted_Array.java │ ├── 034_Search_for_a_Range.cpp │ ├── 034_Search_for_a_Range.java │ ├── 035_Search_Insert_Position.cpp │ ├── 035_Search_Insert_Position.java │ ├── 039_Combination_Sum.java │ ├── 074_Search_a_2D_Matrix.java │ ├── 081_Search_in_Rotated_Sorted_Array_II.cpp │ ├── 081_Search_in_Rotated_Sorted_Array_II.java │ ├── 153_Find Minimum_in_Rotated_Sorted_Array.java │ ├── 154_Find Minimum_in_Rotated_Sorted_Array_II.cpp │ ├── 154_Find Minimum_in_Rotated_Sorted_Array_II.java │ ├── 162_Find_Peak_Element.cpp │ ├── 162_Find_Peak_Element.java │ ├── 240_Search_a_2D_Matrix_II.java │ ├── 349_Intersection_of_Two_Arrays.java │ ├── 350_Intersection_of_Two_Arrays_II.java │ ├── 367_Valid_Perfect_Square.java │ ├── 374_Guess_Number_Higher_or_Lower.java │ ├── 704_Binary_Search.java │ ├── 852_Peak_Index_in_a_Mountain_Array.java │ └── 875_Koko_Eating_Bananas.java ├── BitManipulation │ ├── .DS_Store │ ├── 136_Single_Number.java │ ├── 137_Single_Number_II.java │ ├── 187_Repeated_DNA_Sequences.java │ ├── 190_Reverse_Bits.java │ ├── 191_Number_of_1_Bits.java │ ├── 201_Bitwise_AND_of_Numbers_Range.java │ ├── 231_Power_of_Two.java │ ├── 260_Single_Number_III.java │ ├── 268_Missing_Number.java │ ├── 371_Sum_of_Two_Integers.java │ ├── 461_Hamming_Distance.java │ └── 476_Number_Complement.java ├── DP │ ├── 032_Longest_Valid_Parentheses.java │ ├── 045_Jump_Game_II.java │ ├── 053_Maximum_Subarray.java │ ├── 055_Jump_Game.java │ ├── 062_Unique_Path.java │ ├── 063_Unique_Path_II.java │ ├── 064_Minimum_Path_Sum.java │ ├── 070_Climbing_Stairs.java │ ├── 072_Edit_Distance.java │ ├── 091_Decode Ways.java │ ├── 095_Unique_Binary_Search_Trees_II.java │ ├── 096_Unique_Binary_Search_Trees.java │ ├── 097_Interleaving_String.java │ ├── 115_Distinct_Subsequences.java │ ├── 120_Triangle.java │ ├── 121_Best_Time_to_Buy_and_Sell_Stock.java │ ├── 122_Best_Time_to_Buy_and_Sell_Stock _II.java │ ├── 123_Best_Time_to_Buy_and_Sell_Stock_III.java │ ├── 132_Palindrome_Partitioning_II.java │ ├── 139_Word_Break.java │ ├── 140_Word_Break_II.java │ ├── 152_Maximum_Product_Subarray.java │ ├── 174_Dungeon_Game.java │ ├── 188_Best_Time_to_Buy_and_Sell_Stock _IV.java │ ├── 198_House_Robber.java │ ├── 213_House_Robber_II.java │ ├── 221_Maximal_Square.java │ ├── 256_Paint_House.java │ ├── 265_Paint_House_II.java │ ├── 276_Paint_Fence.java │ ├── 279_Perfect_Squares.java │ ├── 300_Longest_Increasing_Subsequence.java │ ├── 312_Burst_Balloons.java │ ├── 322_Coin_Change.java │ ├── 338_Counting_Bits.java │ ├── 343_Integer_Break.java │ ├── 354_Russian_Doll_Envelopes.java │ ├── 357_Count_Numbers_with_Unique_Digits.java │ ├── 413_Arithmetic_Slices.java │ ├── 486_Predict_the_Winner.java │ ├── 516_Longest_Palindromic_Subsequence.java │ ├── 518_Coin_Change_2.java │ ├── 523_Continuous_Subarray_Sum.java │ ├── 560_Subarray_Sum_Equals_K.java │ └── 931_Minimum_Falling_Path_Sum.java ├── DataStructureDesign │ ├── 146_LRU_Cache.java │ ├── 155_FollowUp_Max_stack.java │ ├── 155_Min_Stack.java │ ├── 170_Two_Sum_III-Data_Structure_Design.java │ ├── 208_Implement_Trie_(Prefix_Tree).java │ ├── 211_Add_and_Search_Word-Data_structure_design.java │ ├── 225_Implement_Stack_Using_Queues.java │ ├── 232_Implement_Queue_using_Stacks.java │ ├── 251_Flatten_2D_Vector.java │ ├── 288_Unique_Word_Abbreviation.java │ ├── 295_Find_Median_from_Data_Stream.java │ ├── 346_Moving_Average_from_Data_Stream.java │ ├── 348_Design_Tic_Tac_Toe.java │ ├── 359_Logger_Rate_Limiter.java │ ├── 380_Insert_Delete_GetRandom_O(1).java │ └── 389_Find_the_Difference.java ├── Geometry │ ├── 218_The_Skyline_Problem.java │ └── 296_Best_Meeting_Point.java ├── Graph │ ├── 133_Clone_Graph.java │ ├── 200_Number_of_Islands.java │ ├── 207_Course_Schedule.java │ ├── 210_Course_Schedule_II.java │ ├── 261_Graph_Valid_Tree.java │ ├── 269_Alien_Dictionary.java │ ├── 286_Walls_and_Gates.java │ ├── 302_Smallest_Rectangle_Enclosing_Black_Pixels.java │ ├── 329_Longest_Increasing_Path_in_a_Matrix.java │ ├── 332_Reconstruct_Itinerary.java │ ├── 399_Evaluate_Division.java │ ├── 444_Sequence_Reconstruction.java │ ├── 547_Friend_Circles.java │ ├── 684_Redundant_Connection.java │ ├── 695_Max_Area_of_Island.java │ └── 785_Is_Graph_Bipartite.java ├── Greedy │ ├── 122_Best_Time_to_Buy_and_Sell_Stock _II.java │ ├── 134_Gas_Station.java │ └── 135_Candy.java ├── LeetShit │ ├── 004_Median_of_Two_Sorted_Arrays.java │ ├── Familiar but complicated │ │ └── 174_Dungeon_Game.java │ ├── Not familiar │ │ ├── 041_First_Missing_Positive.java │ │ ├── 089_Gray_Code.java │ │ └── 173_Binary_Search_Tree_Iterator.java │ └── wholly Shit │ │ ├── 004_Median_of_Two_Sorted_Arrays.java │ │ ├── 005_Longest_Palindromic_Substring.java │ │ ├── 010_Regular_Expression_Matching.java │ │ ├── 032_Longest_Valid_Parentheses.java │ │ ├── 060_Permutation_Sequence.java │ │ ├── 210_Course_Schedule_II.java │ │ └── 212_Word_Search_II.java ├── Leetcode_todo.pages │ ├── Data │ │ └── Hardcover_bullet_black-13.png │ ├── Index.zip │ ├── Metadata │ │ ├── BuildVersionHistory.plist │ │ ├── DocumentIdentifier │ │ └── Properties.plist │ ├── preview-micro.jpg │ ├── preview-web.jpg │ └── preview.jpg ├── LinkedList │ ├── 002_Add_Two_Numbers.java │ ├── 019_Remove_Nth_Node_From_End_of_List.java │ ├── 021_Merge_Two_Sorted_Lists.java │ ├── 023_Merge_k_Sorted_Lists.java │ ├── 024_Swap_Nodes_in_Pairs.java │ ├── 025_Reverse_Nodes_in_k-Group.java │ ├── 061_Rotate_List.java │ ├── 082_Remove_Duplicates_from_Sorted_List_II.java │ ├── 083_Remove_Duplicates_from_Sorted_List.java │ ├── 086_Partition_List.java │ ├── 092_Reverse_Linked_List_II.java │ ├── 138_Copy_List_with_Random_Pointer.java │ ├── 141_Linked_List_Cycle.java │ ├── 142_Linked_List_Cycle_II.java │ ├── 143_Reorder_List.java │ ├── 147_Insertion_Sort_List.java │ ├── 148_Sort_List.java │ ├── 160_Intersection_of_Two_Linked_Lists.java │ ├── 203_Remove_Linked_List_Elements.java │ ├── 206_Reverse_Linked_List.java │ ├── 234_Palindrome_Linked_List.java │ ├── 237_Delete_Node_in_a_Linked_List.java │ ├── 328_Odd_Even_Linked_List.java │ ├── 369_Plus_One_Linked_List.java │ ├── 430_Flatten_a_Multilevel_Doubly_Linked_List.java │ ├── 445_Add_Two_Numbers_II.java │ ├── 707_Design_Linked_List.java │ ├── 708_Insert_into_a_Cyclic_Sorted_List.java │ ├── 725_Split_Linked_List_in_Parts.java │ ├── 817_Linked_List_Components.java │ └── 876_Middle_of_the_Linked_List.java ├── Math │ ├── 007_Reverse_Integer.java │ ├── 009_Palindrome_Number.java │ ├── 029_Divide_Two_Integers.java │ ├── 050_Pow(x,n).java │ ├── 069_Sqrt(x).java │ ├── 149_Max_Points_on_a_Line.java │ ├── 166_Fraction_to_Recurring_Decimal.java │ ├── 168_Excel_Sheet_Column_Title.java │ ├── 171_Excel_Sheet_Column_Number.java │ ├── 172_Factorial_Trailing_Zeroes.java │ ├── 202_Happy_Number.java │ ├── 204_Count_Primes.java │ ├── 223_Rectangle_Area.java │ ├── 224_Basic_Calculator.java │ ├── 227_Basic_Calculator_II.java │ ├── 233_Number_of_Digit_One.java │ ├── 246_Strobogrammatic_Number.java │ ├── 258_Add_Digits.java │ ├── 263_Ugly_Number.java │ ├── 264_Ugly_Number_II.java │ ├── 292_Nim_Game.java │ ├── 326_Power_of_Three.java │ ├── 342_Power_of_Four.java │ └── 412_Fizz_Buzz.java ├── String │ ├── 003_Longest_Substring_Without_Repeating_Characters.java │ ├── 005_Longest_Palindromic_Substring.cpp │ ├── 005_Longest_Palindromic_Substring.java │ ├── 006_ZigZag_Conversion.java │ ├── 008_String_to_Integer.java │ ├── 010_Regular_Expression_Matching.java │ ├── 012_Integer_to_Roman.java │ ├── 013_Roman_to_Integer.java │ ├── 014_Longest_Common_Prefix.java │ ├── 020_Valid_Parentheses.java │ ├── 028_Implement_strStr().java │ ├── 030_Substring_with_Concatenation_of_All_Words.java │ ├── 038_Count_and_Say.java │ ├── 043_Multiply_Strings.java │ ├── 044_Wildcard_Matching.java │ ├── 049_Group_Anagrams.java │ ├── 058_Length_of_Last_Word.java │ ├── 065_Valid_Number.java │ ├── 067_Add_Binary.java │ ├── 068_Text_Justification.java │ ├── 071_Simplify_Path.java │ ├── 076_Minimum_Window_Substring.java │ ├── 125_Valid_Palindrome.java │ ├── 151_Reverse_Words_in_a_String.java │ ├── 157_Read_N_Characters_Given_Read4.java │ ├── 158_Read_N_Characters_Given_Read4_II.java │ ├── 159_Follow_Up_Longest_Substring_with_At_Most_K_Distinct_Characters.java │ ├── 159_Longest_Substring_with_At_Most_Two_Distinct_Characters.java │ ├── 161_One_Edit_Distance.java │ ├── 165_Compare_Version_Numbers.java │ ├── 186_Follow_Up_Reverse_Words_in_K_Groups.java │ ├── 186_Reverse_Words_in_a_String_II.java │ ├── 205_Isomorphic_Strings.java │ ├── 214_Shortest_Palindrome.java │ ├── 227_Basic_Calculator_II.java │ ├── 241_Different_Ways_to_Add_Parentheses.java │ ├── 249_Group_Shifted_Strings.java │ ├── 266_Palindrome_Permutation.java │ ├── 267_Palindrome_Permutation_II.java │ ├── 271_Encode_and_Decode_Strings.java │ ├── 282_Expression_Add_Operators.java │ ├── 293_Flip_Game.java │ ├── 294_Flip_Game_II.java │ ├── 299_Bulls_and_Cows.java │ ├── 306_Additive_Number.java │ ├── 336_Palindrome_Pairs.java │ ├── 340_Longest_Substring_with_At_Most_K_Distinct_Characters.java │ ├── 344_Reverse_String.java │ ├── 345_Reverse_Vowels_of_a_String.java │ ├── 358_Rearrange_String_k_Distance_Apart.java │ ├── 383_Ransom_Note.java │ ├── 387_First_Unique_Character_in_a_String.java │ ├── 388_Longest_Absolute_File_Path.java │ ├── 392_Is_subsequence.java │ ├── 408_Valid_Word_Abbreviation.java │ ├── 409_Longest_Palindrome.java │ ├── 415_Add_Strings.java │ ├── 418_Sentence_Screen_Fitting.java │ ├── 422_Valid_Word_Square.java │ ├── 424_Longest_Repeating_Character_Replacement.java │ ├── 425_Word_Squares.java │ ├── 434_Number_of_Segments_in_a_String.java │ ├── 438_Find_All_Anagrams_in_a_String.java │ ├── 443_String_Compression.java │ ├── 451_Sort_Characters_By_Frequency.java │ ├── 459_Repeated_Substring_Pattern.java │ ├── 468_Validate_IP_Address.java │ ├── 482_License_Key_Formatting.java │ ├── 500_Keyboard_Row.java │ ├── 520_Detect_Capital.java │ ├── 521_Longest_Uncommon_Subsequence_I.java │ ├── 522_Longest_Uncommon_Subsequence_II.java │ ├── 539_Minimum_Time_Difference.java │ ├── 541_Reverse_String_II.java │ ├── 544_Output_Contest_Matches.java │ ├── 551_Student_Attendance_Record_I.java │ ├── 553_Optimal_Division.java │ ├── 556_Next_Greater_Element_III.java │ ├── 557_Reverse_Words_in_a_String_III.java │ ├── 567_Permutation_in_String.java │ ├── 599_Minimum_Index_Sum_of_Two_Lists.java │ ├── 621_Task_Scheduler.java │ ├── 636_Exclusive_Time_of_Functions.java │ ├── 647_Palindromic_Substrings.java │ ├── 678_Valid_Parenthesis_String.java │ ├── 680_Valid_Palindrome_II.java │ ├── 681_Next_Closest_Time.java │ ├── 686_Repeated_String_Match.java │ ├── 767_Reorganize_String.java │ ├── 791_Custom_Sort_String.java │ ├── 804_Unique_Morse_Code_Words.java │ ├── 824_Goat_Latin.java │ ├── 833_Find_And_Replace_in_String.java │ ├── 844_Backspace_String_Compare.java │ ├── 890_Find_and_Replace_Pattern.java │ ├── 893_Groups_of_Special_Equivalent_Strings.java │ ├── 929_Unique_Email_Addresses.java │ └── public class Solution { └── Tree │ ├── .DS_Store │ ├── 094_Binary_Tree_Inorder_Traversal.java │ ├── 098_Validate_Binary_Search_Tree.java │ ├── 099_Recover_Binary_Search_Tree.java │ ├── 100_Same_Tree.java │ ├── 101_Symmetric_Tree.java │ ├── 102_Binary_Tree_Level_Order_Traversal.java │ ├── 103_Binary_Tree_Zigzag_Level_Order_Traversal.java │ ├── 104_Maximum_Depth_of_Binary_Tree.java │ ├── 105_Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.java │ ├── 106_Construct_Binary_Tree_from_Inorder and Postorder Traversal.java │ ├── 107_Binary_Tree_Level_Order_Traversal II.java │ ├── 108_Convert_Sorted_Array_to_Binary_Search_Tree.java │ ├── 109_Convert_Sorted_List_to_Binary_Search_Tree.java │ ├── 110_Balanced_Binary_Tree.java │ ├── 111_Minimum_Depth_of_Binary_Tree.java │ ├── 112_Path_Sum.java │ ├── 113_Path_Sum_II.java │ ├── 114_Flatten_Binary_Tree_to_Linked_List.java │ ├── 116_Populating_Next_Right_Pointers_in_Each_Node.java │ ├── 117_Populating_Next_Right_Pointers_in_Each_Node_II.java │ ├── 124_Binary_Tree_Maximum_Path_Sum.java │ ├── 129_Sum_Root_to_Leaf_Numbers.java │ ├── 144_Binary_Tree_Preorder_Traversal.java │ ├── 145_Binary_Tree_Postorder_Traversal.java │ ├── 156_Binary_Tree_Upside_Down.java │ ├── 173_Binary_Search_Tree_Iterator.java │ ├── 199_Binary_Tree_Right_Side_View.cpp │ ├── 199_Binary_Tree_Right_Side_View.java │ ├── 222_Count_Complete_Tree_Nodes.java │ ├── 226_Invert_Binary_Tree.java │ ├── 230_Kth_Smallest_Element_in_a_BST.java │ ├── 235_Lowest_Common_Ancestor_of_a_Binary_ Search_Tree.java │ ├── 236_Lowest_Common_Ancestor_of_a_Binary_Tree.java │ ├── 250_Count_Univalue_Subtrees.java │ ├── 255_Verify_Preorder_Sequence_in_Binary_Search_Tree.java │ ├── 257_Binary_Tree_Paths.java │ ├── 270_Closest_Binary_Search_Tree_Value.java │ ├── 272_Closest_Binary_Search_Tree_Value_II.java │ ├── 285_Inorder_Successor_in_BST.java │ ├── 297_Serialize_and_Deserialize_Binary_Tree.java │ ├── 298_Binary_Tree_Longest_Consecutive_Sequence.java │ ├── 310_Minimum_Height_Trees.java │ ├── 333_Largest_BST_Subtree.java │ ├── 337_House_Robber_III.java │ ├── 366_Find_Leaves_of_Binary_Tree.java │ ├── 404_Sum_of_Left_Leaves.java │ ├── 428_Serialize_and_Deserialize_N-ary_Tree.java │ ├── 437_Path_Sum_III.java │ ├── 450_Delete_Node_in_a_BST.java │ ├── 501_Find_Mode_in_Binary_Search_Tree.java │ ├── 508_Most_Frequent_Subtree_Sum.java │ ├── 513_Find_Bottom_Left_Tree_Value.java │ ├── 515_Find_Largest_Value_in_Each_Tree_Row.java │ ├── 530_Minimum_Absolute_Difference_in_BST.java │ ├── 538_Convert_BST_to_Greater_Tree.java │ ├── 543_Diameter_of_Binary_Tree.java │ ├── 545_Boundary_of_Binary_Tree.java │ ├── 563_Binary_Tree_Tilt.java │ ├── 572_Subtree_of_Another_Tree.java │ ├── 582_Kill_Process.java │ ├── 606_Construct_String_from_Binary_Tree.java │ ├── 617_Merge_Two_Binary_Trees.java │ ├── 623_Add_One_Row_to_Tree.java │ ├── 652_Find_Duplicate_Subtrees.java │ ├── 653_Two_Sum_IV_Input_is_a_BST.java │ ├── 654_Maximum_Binary_Tree.java │ ├── 662_Maximum_Width_of_Binary_Tree.java │ ├── 663_Equal_Tree_Partition.java │ ├── 669_Trim_a_Binary_Search_Tree.java │ ├── 687_Longest_Univalue_Path.java │ ├── 700_Search_in_a_Binary_Search_Tree.java │ ├── 701_Insert_into_a_Binary_Search_Tree.java │ ├── 814_Binary_Tree_Pruning.java │ ├── 865_Smallest_Subtree_with_all_the_Deepest_Nodes.java │ ├── 872_Leaf-Similar_Trees.java │ └── 889_Construct_Binary_Tree_from_Preorder_and_Postorder_Traversal.java ├── Lintcode ├── Fibonacci.java ├── String │ ├── Binary_Representation.java │ ├── Compare_Strings.java │ └── Two_Strings_Are Anagrams.java └── kSum.java ├── README.txt ├── Storage ├── Storage-EXT_2.pdf ├── Storage-SSD.pdf └── Storage.pages │ ├── Data │ └── Hardcover_bullet_black-13.png │ ├── Index.zip │ ├── Metadata │ ├── BuildVersionHistory.plist │ ├── DocumentIdentifier │ └── Properties.plist │ ├── preview-micro.jpg │ ├── preview-web.jpg │ └── preview.jpg └── interview.numbers /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/.DS_Store -------------------------------------------------------------------------------- /Algorithm&DataStructure/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Algorithm&DataStructure/.DS_Store -------------------------------------------------------------------------------- /Algorithm&DataStructure/Advanced/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Algorithm&DataStructure/Advanced/.DS_Store -------------------------------------------------------------------------------- /Algorithm&DataStructure/Graph/Bipartite.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 9 3 | 0 8 4 | 1 8 5 | 2 10 6 | 2 13 7 | 3 11 8 | 4 9 9 | 4 15 10 | 6 11 11 | 7 14 12 | -------------------------------------------------------------------------------- /Algorithm&DataStructure/Graph/TinyEWD.txt: -------------------------------------------------------------------------------- 1 | 8 2 | 15 3 | 4 5 0.35 4 | 5 4 0.35 5 | 4 7 0.37 6 | 5 7 0.28 7 | 7 5 0.28 8 | 5 1 0.32 9 | 0 4 0.38 10 | 0 2 0.26 11 | 7 3 0.39 12 | 1 3 0.29 13 | 2 7 0.34 14 | 6 2 0.40 15 | 3 6 0.52 16 | 6 0 0.58 17 | 6 4 0.93 -------------------------------------------------------------------------------- /Algorithm&DataStructure/Graph/routes.txt: -------------------------------------------------------------------------------- 1 | JFK MCO 2 | ORD DEN 3 | ORD HOU 4 | DFW PHX 5 | JFK ATL 6 | ORD DFW 7 | ORD PHX 8 | ATL HOU 9 | DEN PHX 10 | PHX LAX 11 | JFK ORD 12 | DEN LAS 13 | DFW HOU 14 | ORD ATL 15 | LAS LAX 16 | ATL MCO 17 | HOU MCO 18 | LAS PHX 19 | -------------------------------------------------------------------------------- /Algorithm&DataStructure/Graph/tinyDAG.txt: -------------------------------------------------------------------------------- 1 | 13 2 | 15 3 | 2 3 4 | 0 6 5 | 0 1 6 | 2 0 7 | 11 12 8 | 9 12 9 | 9 10 10 | 9 11 11 | 3 5 12 | 8 7 13 | 5 4 14 | 0 5 15 | 6 4 16 | 6 9 17 | 7 6 -------------------------------------------------------------------------------- /Algorithm&DataStructure/Graph/tinyDG.txt: -------------------------------------------------------------------------------- 1 | 13 2 | 22 3 | 4 2 4 | 2 3 5 | 3 2 6 | 6 0 7 | 0 1 8 | 2 0 9 | 11 12 10 | 12 9 11 | 9 10 12 | 9 11 13 | 7 9 14 | 10 12 15 | 11 4 16 | 4 3 17 | 3 5 18 | 6 8 19 | 8 6 20 | 5 4 21 | 0 5 22 | 6 4 23 | 6 9 24 | 7 6 -------------------------------------------------------------------------------- /Algorithm&DataStructure/Graph/tinyEWDAG.txt: -------------------------------------------------------------------------------- 1 | 8 2 | 13 3 | 5 4 0.35 4 | 4 7 0.37 5 | 5 7 0.28 6 | 5 1 0.32 7 | 4 0 0.38 8 | 0 2 0.26 9 | 3 7 0.39 10 | 1 3 0.29 11 | 7 2 0.34 12 | 6 2 0.40 13 | 3 6 0.52 14 | 6 0 0.58 15 | 6 4 0.93 -------------------------------------------------------------------------------- /Algorithm&DataStructure/Graph/tinyEWDn.txt: -------------------------------------------------------------------------------- 1 | 8 2 | 15 3 | 4 5 0.35 4 | 5 4 0.35 5 | 4 7 0.37 6 | 5 7 0.28 7 | 7 5 0.28 8 | 5 1 0.32 9 | 0 4 0.38 10 | 0 2 0.26 11 | 7 3 0.39 12 | 1 3 0.29 13 | 2 7 0.34 14 | 6 2 -1.20 15 | 3 6 0.52 16 | 6 0 -1.40 17 | 6 4 -1.25 -------------------------------------------------------------------------------- /Algorithm&DataStructure/Graph/tinyEWDnc.txt: -------------------------------------------------------------------------------- 1 | 8 2 | 15 3 | 4 5 0.35 4 | 5 4 -0.66 5 | 4 7 0.37 6 | 5 7 0.28 7 | 7 5 0.28 8 | 5 1 0.32 9 | 0 4 0.38 10 | 0 2 0.26 11 | 7 3 0.39 12 | 1 3 0.29 13 | 2 7 0.34 14 | 6 2 0.40 15 | 3 6 0.52 16 | 6 0 0.58 17 | 6 4 0.93 -------------------------------------------------------------------------------- /Algorithm&DataStructure/Graph/tinyEWG.txt: -------------------------------------------------------------------------------- 1 | 8 2 | 16 3 | 4 5 0.35 4 | 4 7 0.37 5 | 5 7 0.28 6 | 0 7 0.16 7 | 1 5 0.32 8 | 0 4 0.38 9 | 2 3 0.17 10 | 1 7 0.19 11 | 0 2 0.26 12 | 1 2 0.36 13 | 1 3 0.29 14 | 2 7 0.34 15 | 6 2 0.40 16 | 3 6 0.52 17 | 6 0 0.58 18 | 6 4 0.93 -------------------------------------------------------------------------------- /Algorithm&DataStructure/Graph/tinyG.txt: -------------------------------------------------------------------------------- 1 | 13 2 | 13 3 | 0 5 4 | 4 3 5 | 0 1 6 | 9 12 7 | 6 4 8 | 5 4 9 | 0 2 10 | 11 12 11 | 9 10 12 | 0 6 13 | 7 8 14 | 9 11 15 | 5 3 16 | -------------------------------------------------------------------------------- /Algorithm&DataStructure/Sort/InsertionSort.java: -------------------------------------------------------------------------------- 1 | public class InsertionSort { 2 | public static void sort(int[] arr){ 3 | int len = arr.length; 4 | for(int i = 1; i < len; i++){ 5 | // 从后往前遍历,如果一直小,就交换 6 | for(int j = i; j > 0 && arr[j] < arr[j-1]; j--) 7 | swap(arr, j, j-1); 8 | } 9 | } 10 | 11 | public static void swap(int[] arr, int n, int m){ 12 | int temp = arr[n]; 13 | arr[n] = arr[m]; 14 | arr[m] = temp; 15 | } 16 | // advantage: 对于接近sorted的数组,运行时间短,但很稳定 17 | // disadvantage: 逆序数组时间长 18 | } 19 | -------------------------------------------------------------------------------- /Algorithm&DataStructure/Sort/SelectionSort.java: -------------------------------------------------------------------------------- 1 | public class SelectionSort { 2 | public static void sort(int[] arr){ 3 | int len = arr.length; 4 | for(int i = 0; i < len; i++){ 5 | int min = i; 6 | for(int j = i+1; j < len; j++){ 7 | if(arr[j] < arr[min]) 8 | min = j; 9 | } 10 | swap(arr, i, min); 11 | } 12 | } 13 | 14 | public static void swap(int[] arr, int n, int m){ 15 | int temp = arr[n]; 16 | arr[n] = arr[m]; 17 | arr[m] = temp; 18 | } 19 | // advantage: 数据移动次数在所以排序算法中最少,为N 20 | // disadvantage: 无论数组是否有序,时间一样,因为有序数组就是自己和自己换 21 | } 22 | -------------------------------------------------------------------------------- /Algorithm&DataStructure/Sort/ShellSort.java: -------------------------------------------------------------------------------- 1 | public class ShellSort { 2 | public static void sort(int[] arr){ 3 | int len = arr.length; 4 | int h = 1; 5 | while(h < len/3){ 6 | h = 3*h+1; 7 | } 8 | System.out.println("len: "+ len + " h:" + h); 9 | while(h >= 1){ 10 | for(int i = h; i < len; i++){ 11 | for(int j = i; j >= h && arr[j] < arr[j-h]; j -= h){ 12 | swap(arr, j, j-h); 13 | } 14 | } 15 | h /= 3; 16 | } 17 | } 18 | 19 | public static void swap(int[] arr, int n, int m){ 20 | int temp = arr[n]; 21 | arr[n] = arr[m]; 22 | arr[m] = temp; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week1 - Introduction and MapReduce.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week1 - Introduction and MapReduce.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week1_Slides/Introduction/C3_IntroClouds_A_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week1_Slides/Introduction/C3_IntroClouds_A_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week1_Slides/Introduction/C3_IntroClouds_B_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week1_Slides/Introduction/C3_IntroClouds_B_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week1_Slides/Introduction/C3_IntroClouds_C_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week1_Slides/Introduction/C3_IntroClouds_C_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week1_Slides/Introduction/C3_IntroClouds_D_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week1_Slides/Introduction/C3_IntroClouds_D_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week1_Slides/Introduction/C3_IntroClouds_E_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week1_Slides/Introduction/C3_IntroClouds_E_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week1_Slides/Introduction/C3_IntroClouds_F_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week1_Slides/Introduction/C3_IntroClouds_F_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week1_Slides/MapReduce/C3_Mapreduce_A_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week1_Slides/MapReduce/C3_Mapreduce_A_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week1_Slides/MapReduce/C3_Mapreduce_B_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week1_Slides/MapReduce/C3_Mapreduce_B_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week1_Slides/MapReduce/C3_Mapreduce_C_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week1_Slides/MapReduce/C3_Mapreduce_C_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week1_Slides/MapReduce/C3_Mapreduce_D_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week1_Slides/MapReduce/C3_Mapreduce_D_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Gossip/C3_Gossip_A_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Gossip/C3_Gossip_A_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Gossip/C3_Gossip_B_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Gossip/C3_Gossip_B_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Gossip/C3_Gossip_C_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Gossip/C3_Gossip_C_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Gossip/C3_Gossip_D_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Gossip/C3_Gossip_D_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Grids/C3_Grids_A_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Grids/C3_Grids_A_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Grids/C3_Grids_B_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Grids/C3_Grids_B_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/L_member.sp11.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/L_member.sp11.ppt -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Membership/C3_Member_A_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Membership/C3_Member_A_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Membership/C3_Member_B_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Membership/C3_Member_B_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Membership/C3_Member_C_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Membership/C3_Member_C_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Membership/C3_Member_D_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Membership/C3_Member_D_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Membership/C3_Member_E_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Membership/C3_Member_E_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Membership/C3_Member_F_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week2_Slides/Membership/C3_Member_F_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week3/C3_p2p_A_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week3/C3_p2p_A_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week3/C3_p2p_B_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week3/C3_p2p_B_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week3/C3_p2p_C_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week3/C3_p2p_C_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week3/C3_p2p_D_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week3/C3_p2p_D_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week3/C3_p2p_E_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week3/C3_p2p_E_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week3/C3_p2p_F_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week3/C3_p2p_F_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week3/C3_p2p_G_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week3/C3_p2p_G_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week3/C3_p2p_H_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week3/C3_p2p_H_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week4/C3_Key-valueStores_A_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week4/C3_Key-valueStores_A_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week4/C3_Key-valueStores_B_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week4/C3_Key-valueStores_B_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week4/C3_Key-valueStores_C_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week4/C3_Key-valueStores_C_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week4/C3_Key-valueStores_D_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week4/C3_Key-valueStores_D_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week4/C3_Key-valueStores_E_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week4/C3_Key-valueStores_E_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week4/C3_TimeAndOrdering_A_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week4/C3_TimeAndOrdering_A_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week4/C3_TimeAndOrdering_B_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week4/C3_TimeAndOrdering_B_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week4/C3_TimeAndOrdering_C_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week4/C3_TimeAndOrdering_C_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week4/C3_TimeAndOrdering_D_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week4/C3_TimeAndOrdering_D_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week4/C3_TimeAndOrdering_E_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week4/C3_TimeAndOrdering_E_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Multicast_A_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Multicast_A_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Multicast_B_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Multicast_B_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Multicast_C_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Multicast_C_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Multicast_D_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Multicast_D_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Multicast_E_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Multicast_E_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Part1Outro_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Part1Outro_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Paxos_A_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Paxos_A_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Paxos_B_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Paxos_B_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Paxos_C_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Paxos_C_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Paxos_D_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Paxos_D_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Snapshots_A_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Snapshots_A_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Snapshots_B_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Snapshots_B_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Snapshots_C_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Snapshots_C_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Snapshots_D_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part1/Week5/C3_Snapshots_D_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part2/week1/C3_Election_A_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part2/week1/C3_Election_A_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part2/week1/C3_Election_B_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part2/week1/C3_Election_B_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part2/week1/C3_Election_C_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part2/week1/C3_Election_C_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part2/week1/C3_Election_D_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part2/week1/C3_Election_D_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part2/week1/C3_MutualExclusion_A_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part2/week1/C3_MutualExclusion_A_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part2/week1/C3_MutualExclusion_B_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part2/week1/C3_MutualExclusion_B_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part2/week1/C3_MutualExclusion_C_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part2/week1/C3_MutualExclusion_C_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part2/week1/C3_MutualExclusion_D_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part2/week1/C3_MutualExclusion_D_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part2/week2/C3_Concurrency_A_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part2/week2/C3_Concurrency_A_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part2/week2/C3_Concurrency_B_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part2/week2/C3_Concurrency_B_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part2/week2/C3_Concurrency_C_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part2/week2/C3_Concurrency_C_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part2/week2/C3_Concurrency_D_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part2/week2/C3_Concurrency_D_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part2/week2/C3_Concurrency_E_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part2/week2/C3_Concurrency_E_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part2/week2/C3_Replication_A_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part2/week2/C3_Replication_A_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part2/week2/C3_Replication_B_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part2/week2/C3_Replication_B_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part2/week3/C3_GraphProcessing_A_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part2/week3/C3_GraphProcessing_A_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part2/week3/C3_Scheduling_A_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part2/week3/C3_Scheduling_A_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part2/week3/C3_Scheduling_B_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part2/week3/C3_Scheduling_B_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part2/week3/C3_Scheduling_C_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part2/week3/C3_Scheduling_C_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part2/week3/C3_StreamProcessing_A_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part2/week3/C3_StreamProcessing_A_CSRAfinal.pdf -------------------------------------------------------------------------------- /Distributed System/Cloud Computing - UIUC/Part2/week3/C3_StructureofNetworks_A_CSRAfinal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Distributed System/Cloud Computing - UIUC/Part2/week3/C3_StructureofNetworks_A_CSRAfinal.pdf -------------------------------------------------------------------------------- /Hackerrank/WarmUp/Q01_Solve_Me_First.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | static int solveMeFirst(int a, int b) { 10 | return a + b; 11 | } 12 | 13 | 14 | public static void main(String[] args) { 15 | Scanner in = new Scanner(System.in); 16 | int a; 17 | a = in.nextInt(); 18 | int b; 19 | b = in.nextInt(); 20 | int sum; 21 | sum = solveMeFirst(a, b); 22 | System.out.println(sum); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Hackerrank/WarmUp/Q02_Solve_Me_Second.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class Solution { 5 | 6 | static int solveMeSecond(int a, int b) { 7 | return a+b; 8 | } 9 | 10 | public static void main(String[] args) { 11 | Scanner in = new Scanner(System.in); 12 | int t; 13 | int sum; 14 | int a,b; 15 | t = in.nextInt(); 16 | for (int i=0;i 1){ 15 | BigInteger cur = new BigInteger(String.valueOf(n)); 16 | ret = ret.multiply(cur); 17 | n--; 18 | } 19 | System.out.print(ret); 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Leetcode/.DS_Store -------------------------------------------------------------------------------- /Leetcode/All/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Leetcode/All/.DS_Store -------------------------------------------------------------------------------- /Leetcode/All/001_Two_Sum.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(n) space 3 | public int[] twoSum(int[] nums, int target) { 4 | int[] ret = new int[2]; 5 | if(nums == null || nums.length == 0) 6 | return ret; 7 | 8 | HashMap map = new HashMap(); 9 | for(int i = 0; i < nums.length; i++){ 10 | if(map.containsKey(target - nums[i])){ 11 | ret[0] = map.get(target - nums[i]) + 1; 12 | ret[1] = i + 1; 13 | } 14 | map.put(nums[i], i); 15 | } 16 | return ret; 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/All/003_Longest_Substring_Without_Repeating_Characters.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n) time 3 | public int lengthOfLongestSubstring(String s) { 4 | if(s == null || s.length() < 1) 5 | return 0; 6 | int start = 0; 7 | int cur = 0; 8 | int max = 0; 9 | int[] table = new int[256]; 10 | Arrays.fill(table, -1); 11 | while(cur < s.length()){ 12 | int ch = s.charAt(cur); 13 | if(table[ch] >= start) 14 | start = table[ch]+1; 15 | max = Math.max(max, cur-start+1); 16 | table[ch] = cur; 17 | cur++; 18 | } 19 | return max; 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/All/006_ZigZag_Conversion.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public String convert(String s, int numRows) { 3 | if(s == null || s.length() == 0 || numRows <= 0) 4 | return ""; 5 | if(numRows == 1) return s; 6 | 7 | String ret = ""; 8 | int size = 2 * numRows - 2; //k + (k -2) 9 | for(int i = 0; i < numRows; i++){ 10 | for(int j = i; j < s.length(); j += size){ 11 | ret += s.charAt(j); 12 | if(i != 0 && i != numRows - 1){ 13 | int temp = j + size - 2 * i; 14 | if(temp < s.length()) 15 | ret += s.charAt(temp); 16 | } 17 | } 18 | } 19 | return ret; 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/All/007_Reverse_Integer.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(1) time (integer length/size is constant) 3 | public int reverse(int x) { 4 | int abs = Math.abs(x); 5 | int ret = 0; 6 | while(abs > 0){ 7 | if(ret > (Integer.MAX_VALUE - abs % 10)/10){ 8 | return 0; 9 | } 10 | ret = ret*10 + abs%10; 11 | abs /= 10; 12 | } 13 | return x < 0 ? -ret : ret; 14 | } 15 | } -------------------------------------------------------------------------------- /Leetcode/All/009_Palindrome_Number.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(1) time (integer length/size is constant) 3 | public boolean isPalindrome(int x) { 4 | if(x < 0) return false; 5 | int probe = 1; 6 | while(probe <= x/10){ 7 | probe *= 10; 8 | } 9 | while(x > 0){ 10 | int lowest = x % 10; 11 | int highest = x / probe; 12 | if(lowest != highest) return false; 13 | x = (x - highest * probe)/10; 14 | probe /= 100; 15 | } 16 | return true; 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/All/011_Container_With_Most_Water.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) space 3 | public int maxArea(int[] height) { 4 | if(height == null || height.length == 0) 5 | return 0; 6 | int l = 0, r = height.length - 1; 7 | int area = Integer.MIN_VALUE; 8 | while(l < r){ 9 | int h = Math.min(height[l], height[r]); 10 | area = Math.max(area, (r - l) * h); 11 | if(height[l] < height[r]) l++; 12 | else r--; 13 | } 14 | return area; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/All/012_Integer_to_Roman.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n) time 3 | public String intToRoman(int num) { 4 | String[] table = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; 5 | int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; 6 | StringBuilder ret = new StringBuilder(); 7 | for(int i = 0; i < values.length; i++){ 8 | while(num >= values[i]){ 9 | ret.append(table[i]); 10 | num -= values[i]; 11 | } 12 | } 13 | return ret.toString(); 14 | } 15 | } -------------------------------------------------------------------------------- /Leetcode/All/014_Longest_Common_Prefix.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // 3 | public String longestCommonPrefix(String[] strs) { 4 | int num = strs.length; 5 | String ret = ""; 6 | if(num == 0 || strs.length == 0) 7 | return ret; 8 | for(int i = 0; i < strs[0].length(); i++){ 9 | for(int j = 0; j < num; j++){ 10 | if(strs[j].length() <= i || strs[j].charAt(i) != strs[0].charAt(i)) 11 | return strs[0].substring(0,i); 12 | } 13 | } 14 | return strs[0]; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/All/019_Remove_Nth_Node_From_End_of_List.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // Two pointer: 3 | // O(1) space, O(n) time 4 | public ListNode removeNthFromEnd(ListNode head, int n) { 5 | ListNode dummy = new ListNode(-1); 6 | dummy.next = head; 7 | ListNode slow = dummy; 8 | ListNode fast = dummy; 9 | for(int i = 0; i <= n; i++){ 10 | fast = fast.next; 11 | } 12 | while(fast != null){ 13 | fast = fast.next; 14 | slow = slow.next; 15 | } 16 | slow.next = slow.next.next; 17 | return dummy.next; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/All/026_Remove_Duplicates_from_Sorted_Array.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) space 3 | public int removeDuplicates(int[] nums) { 4 | if(nums == null || nums.length == 0) 5 | return 0; 6 | int j = 1; 7 | for(int i = 1; i < nums.length; i++){ 8 | if(nums[i] != nums[i-1]){ 9 | nums[j++] = nums[i]; 10 | } 11 | } 12 | return j; 13 | } 14 | } -------------------------------------------------------------------------------- /Leetcode/All/027_Remove_Element.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) space 3 | public int removeElement(int[] nums, int val) { 4 | if(nums == null || nums.length == 0) 5 | return 0; 6 | int j = 0; 7 | for(int i = 0; i < nums.length; i++){ 8 | if(nums[i] != val) 9 | nums[j++] = nums[i]; 10 | } 11 | return j; 12 | } 13 | } -------------------------------------------------------------------------------- /Leetcode/All/058_Length_of_Last_Word.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n) time 3 | public int lengthOfLastWord(String s) { 4 | if(s == null || s.length() == 0) 5 | return 0; 6 | 7 | int len = 0; 8 | for(int i = s.length() - 1; i >= 0; i--){ 9 | if(s.charAt(i) != ' ') len++; 10 | if(s.charAt(i) == ' ' && len != 0) 11 | return len; 12 | } 13 | return len; 14 | } 15 | } -------------------------------------------------------------------------------- /Leetcode/All/066_Plus_One.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) space 3 | public int[] plusOne(int[] digits) { 4 | for(int i = digits.length - 1; i >= 0; i--){ 5 | digits[i] = (digits[i] + 1) % 10; 6 | if(digits[i] == 0){ 7 | if(i == 0){ 8 | int[] ret = new int[digits.length + 1]; 9 | ret[0] = 1; 10 | return ret; 11 | } 12 | }else{ 13 | return digits; 14 | } 15 | } 16 | return digits; 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/All/067_Add_Binary.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n) time 3 | public String addBinary(String a, String b) { 4 | int alen = a.length(); 5 | int blen = b.length(); 6 | int len = Math.max(alen, blen); 7 | int carry = 0; 8 | 9 | String ret = ""; 10 | for(int i = 0; i < len; i++){ 11 | int v1 = i < alen ? a.charAt(alen -1 - i) - '0' : 0; 12 | int v2 = i < blen ? b.charAt(blen -1 - i) - '0' : 0; 13 | int digit = (carry + v1 + v2) % 2; 14 | carry = (carry + v1 + v2)/ 2; 15 | ret = digit + ret; 16 | } 17 | if(carry == 1) 18 | ret = 1 + ret; 19 | return ret; 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/All/070_Climbing_Stairs.java: -------------------------------------------------------------------------------- 1 | public class ClimbingStairs { 2 | // 一维DP: 3 | // O(n) space, O(n) time 4 | public int climbStairs(int n) { 5 | if(n <= 2) return n; 6 | int[] dp = new int[n+1]; 7 | dp[0] = 0; 8 | dp[1] = 1; 9 | dp[2] = 2; 10 | 11 | for(int i = 3; i < n+1; i++){ 12 | dp[i] = dp[i-1]+dp[i-2]; 13 | } 14 | return dp[n]; 15 | } 16 | // 状压dp: 17 | public int climbStairs(int n) { 18 | if(n <= 2) return n; 19 | int dp1 = 1; 20 | int dp2 = 2; 21 | int dp = 0; 22 | for(int i = 3; i <= n; i++){ 23 | dp = dp1 + dp2; 24 | dp1 = dp2; 25 | dp2 = dp; 26 | } 27 | return dp; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Leetcode/All/080_Remove_Duplicates_from_Sorted_Array_II.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) space 3 | public int removeDuplicates(int[] nums) { 4 | if(nums == null || nums.length == 0) 5 | return 0; 6 | int j = 1; 7 | int cnt = 0; 8 | for(int i = 1; i < nums.length; i++){ 9 | if(nums[i] == nums[i-1]){ 10 | cnt++; 11 | if(cnt == 1){ 12 | nums[j++] = nums[i]; 13 | } 14 | }else{ 15 | cnt = 0; 16 | nums[j++] = nums[i]; 17 | } 18 | } 19 | return j; 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/All/083_Remove_Duplicates_from_Sorted_List.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n) time 3 | public ListNode deleteDuplicates(ListNode head) { 4 | if(head == null || head.next == null) 5 | return head; 6 | ListNode dummy = new ListNode(-1); 7 | dummy.next = head; 8 | ListNode cur = head; 9 | ListNode next = cur.next; 10 | 11 | while(next != null){ 12 | if(next.val == cur.val){ 13 | next = next.next; 14 | }else{ 15 | cur.next = next; 16 | cur = cur.next; 17 | next = next.next; 18 | } 19 | } 20 | cur.next = next; 21 | return dummy.next; 22 | } 23 | } -------------------------------------------------------------------------------- /Leetcode/All/088_Merge_Sorted_Array.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(m+n) space, O(m+n) time 3 | public void merge(int[] nums1, int m, int[] nums2, int n) { 4 | int end = m + n - 1; 5 | int a = m - 1; 6 | int b = n - 1; 7 | while(a >= 0 && b >= 0){ 8 | if(nums1[a] > nums2[b]) 9 | nums1[end--] = nums1[a--]; 10 | else 11 | nums1[end--] = nums2[b--]; 12 | } 13 | while(b >= 0) 14 | nums1[end--] = nums2[b--]; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/All/096_Unique_Binary_Search_Trees.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // 一维DP: 3 | // O(n) space, O(n^2) time 4 | public int numTrees(int n) { 5 | if(n < 0) return 0; 6 | int[] dp = new int[n+1]; 7 | dp[0] = dp[1] = 1; 8 | // outer loop i means total node in the tree 9 | for(int i = 2; i <= n; i++){ 10 | // inner loop j means total nodes of left subtree 11 | // then i-j-1 means total nodes of right subtree 12 | // -1 since we need to reserve one node as root 13 | for(int j = 0; j < i; j++){ 14 | dp[i] += dp[j] * dp[i-j-1]; 15 | } 16 | } 17 | return dp[n]; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/All/108_Convert_Sorted_Array_to_Binary_Search_Tree.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space OR O(logn) space if consider recursion stack 3 | // O(n) time 4 | public TreeNode sortedArrayToBST(int[] nums) { 5 | if(nums == null || nums.length == 0) 6 | return null; 7 | return BuildTree(nums, 0, nums.length - 1); 8 | } 9 | 10 | private TreeNode BuildTree(int[] nums, int start, int end){ 11 | if(start > end) 12 | return null; 13 | int mid = start + (end - start)/2; 14 | TreeNode root = new TreeNode(nums[mid]); 15 | root.left = BuildTree(nums, start, mid - 1); 16 | root.right = BuildTree(nums, mid + 1, end); 17 | return root; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/All/119_Pascal's_Triangle_II.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) Space, O(n^2) time 3 | public List getRow(int rowIndex) { 4 | List ret = new LinkedList(); 5 | if(rowIndex < 0) return ret; 6 | ret.add(1); 7 | for(int i = 1; i <= rowIndex; i++){ 8 | for(int j = ret.size() - 2; j >= 0; j--){ 9 | ret.set(j+1, ret.get(j+1)+ret.get(j)); 10 | } 11 | ret.add(1); 12 | } 13 | return ret; 14 | } 15 | } -------------------------------------------------------------------------------- /Leetcode/All/122_Best_Time_to_Buy_and_Sell_Stock _II.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(1) time 3 | public int maxProfit(int[] prices) { 4 | if(prices == null || prices.length <= 1) 5 | return 0; 6 | int max = 0; 7 | for(int i = 1; i < prices.length; i++){ 8 | if(prices[i] > prices[i-1]) 9 | max += prices[i] - prices[i-1]; 10 | } 11 | return max; 12 | } 13 | } -------------------------------------------------------------------------------- /Leetcode/All/129_Sum_Root_to_Leaf_Numbers.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space OR O(logn) space if consider recursion stack 3 | // O(n) time 4 | public int sumNumbers(TreeNode root) { 5 | if(root == null) return 0; 6 | return sum(root, 0); 7 | } 8 | 9 | private int sum(TreeNode root, int sum){ 10 | if(root == null) return 0; 11 | int cur = sum * 10 + root.val; 12 | if(root.left == null && root.right == null){ 13 | return cur; 14 | } 15 | return sum(root.left, cur) + sum(root.right, cur); 16 | } 17 | } -------------------------------------------------------------------------------- /Leetcode/All/134_Gas_Station.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) space 3 | public int canCompleteCircuit(int[] gas, int[] cost) { 4 | int diff = 0; 5 | int sum = 0; 6 | int start = 0; 7 | int total = 0; 8 | for(int i = 0; i < gas.length; i++){ 9 | diff = gas[i] - cost[i]; 10 | sum += diff; 11 | total += diff; 12 | if(sum < 0){ 13 | sum = 0; 14 | start = i+1; 15 | } 16 | } 17 | if(total < 0) 18 | return -1; 19 | return start; 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/All/136_Single_Number.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n) time 3 | public int singleNumber(int[] A) { 4 | if(A == null || A.length == 0) return 0; 5 | int ret = A[0]; 6 | for(int i = 1; i < A.length; i++) 7 | ret = A[i] ^ ret; 8 | return ret; 9 | } 10 | } -------------------------------------------------------------------------------- /Leetcode/All/137_Single_Number_II.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int singleNumber(int[] nums) { 3 | // O(1) space, O(32n) => O(n) time 4 | int ret = 0; 5 | for(int i = 0; i < 32; i++){ 6 | int cnt = 0, temp = 0; 7 | int mask = 1 << i; 8 | for(int j = 0; j < nums.length; j++){ 9 | temp = nums[j] & mask; 10 | if(temp != 0){ 11 | cnt++; 12 | } 13 | } 14 | if(cnt % 3 == 1){ 15 | ret |= mask; 16 | } 17 | } 18 | return ret; 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/All/139_Word_Break.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // 一维DP: O(n) space, O(n^2) time 3 | public boolean wordBreak(String s, Set dict) { 4 | int len = s.length(); 5 | boolean[] possible = new boolean[len+1]; 6 | possible[0] = true; 7 | for(int i = 1; i <= len; i++){ 8 | for(int j = 0; j < i; j++){ 9 | if(possible[j] && dict.contains(s.substring(j, i))) { 10 | possible[i] = true; 11 | break; 12 | } 13 | } 14 | } 15 | return possible[len]; 16 | } 17 | } -------------------------------------------------------------------------------- /Leetcode/All/141_Linked_List_Cycle.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n) time 3 | public boolean hasCycle(ListNode head) { 4 | if(head == null || head.next == null) 5 | return false; 6 | ListNode slow = head; 7 | ListNode fast = head; 8 | while(fast != null){ 9 | slow = slow.next; 10 | fast = fast.next == null ? null : fast.next.next; 11 | if(slow.equals(fast)) 12 | return true; 13 | } 14 | return false; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/All/147_Insertion_Sort_List.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n^2) time 3 | public ListNode insertionSortList(ListNode head) { 4 | if(head == null || head.next == null) 5 | return head; 6 | ListNode dummy = new ListNode(-1); 7 | ListNode pre = dummy; 8 | ListNode cur = head; 9 | 10 | while(cur != null){ 11 | ListNode next = cur.next; 12 | pre = dummy; 13 | while(pre.next != null && pre.next.val <= cur.val) 14 | pre = pre.next; 15 | cur.next = pre.next; 16 | pre.next = cur; 17 | cur = next; 18 | } 19 | return dummy.next; 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/All/152_Maximum_Product_Subarray.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // DP, 只需要俩常数, O(1) space, O(n) time 3 | public int maxProduct(int[] A) { 4 | int dpMax = A[0]; 5 | int dpMin = A[0]; 6 | int max = A[0]; 7 | for(int i = 1; i < A.length; i++){ 8 | int tempMax = dpMax; // dpmax在dpmin更新前已经更新了, 要记录下 9 | dpMax = Math.max(Math.max(A[i] * dpMax, A[i]), A[i] * dpMin); 10 | dpMin = Math.min(Math.min(A[i] * dpMin, A[i]), A[i] * tempMax); 11 | max = Math.max(dpMax, max); 12 | } 13 | return max; 14 | } 15 | } -------------------------------------------------------------------------------- /Leetcode/All/162_Find_Peak_Element.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(logn) time 3 | public int findPeakElement(int[] nums) { 4 | if(nums == null) return -1; 5 | if(nums.length == 1) return 0; 6 | int lo = 0, hi = nums.length-1; 7 | while(lo <= hi){ 8 | if(lo == hi) return lo; 9 | int mid = lo + (hi - lo)/2; 10 | if(nums[mid] < nums[mid+1]){ 11 | lo = mid+1; 12 | }else{ 13 | hi = mid; 14 | } 15 | } 16 | return -1; 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/All/167_Two_Sum_II(input_array_is_sorted).java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n) time 3 | public int[] twoSum(int[] numbers, int target) { 4 | int[] ret = new int[2]; 5 | int l = 0, r = numbers.length - 1; 6 | while(l < r){ 7 | int sum = numbers[l] + numbers[r]; 8 | if(sum == target){ 9 | ret[0] = l + 1; 10 | ret[1] = r + 1; 11 | return ret; 12 | } 13 | if(sum > target) 14 | r--; 15 | else 16 | l++; 17 | } 18 | return ret; 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/All/168_Excel_Sheet_Column_Title.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) space, O(log26n) time(26为底,n的对数) 3 | public String convertToTitle(int n) { 4 | StringBuilder ret = new StringBuilder(); 5 | if(n < 1) return ret.toString(); 6 | while(n > 0){ 7 | n--; 8 | char c = (char)((n % 26) + 'A'); 9 | ret.insert(0, c); 10 | n /= 26; 11 | } 12 | return ret.toString(); 13 | } 14 | } -------------------------------------------------------------------------------- /Leetcode/All/170_Two_Sum_III-Data_Structure_Design.java: -------------------------------------------------------------------------------- 1 | public class TwoSum { 2 | // O(n) space, O(n) time 3 | HashMap map = new HashMap(); 4 | public void add(int number) { 5 | if(map.containsKey(number)) 6 | map.put(number, map.get(number)+1); 7 | else 8 | map.put(number, 1); 9 | } 10 | 11 | public boolean find(int value) { 12 | for(int num: map.keySet()){ 13 | if(value - num == num){ 14 | if(map.get(num) >= 2) 15 | return true; 16 | }else{ 17 | if(map.containsKey(value-num)) 18 | return true; 19 | } 20 | } 21 | return false; 22 | } 23 | } -------------------------------------------------------------------------------- /Leetcode/All/171_Excel_Sheet_Column_Number.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n) time 3 | public int titleToNumber(String s) { 4 | int ret = 0; 5 | for(int i = 0; i < s.length(); i++){ 6 | char ch = s.charAt(i); 7 | ret = ret*26 + (ch - 'A' + 1); 8 | } 9 | return ret; 10 | } 11 | } -------------------------------------------------------------------------------- /Leetcode/All/172_Factorial_Trailing_Zeroes.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(log5n) time 3 | public int trailingZeroes(int n) { 4 | int ret = 0; 5 | while(n > 0){ 6 | n = n / 5; 7 | ret += n; 8 | } 9 | return ret; 10 | } 11 | } -------------------------------------------------------------------------------- /Leetcode/All/189_Rotate_Array.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) sapce 3 | public void rotate(int[] nums, int k) { 4 | if(nums == null || nums.length <= 1 || k == 0) 5 | return; 6 | k = k % nums.length; 7 | reverse(nums, 0, nums.length-1); 8 | reverse(nums, 0, k-1); 9 | reverse(nums, k, nums.length-1); 10 | } 11 | 12 | private void reverse(int[] nums, int left, int right){ 13 | while(left < right){ 14 | int temp = nums[left]; 15 | nums[left] = nums[right]; 16 | nums[right] = temp; 17 | left++; 18 | right--; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/All/190_Reverse_Bits.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // you need treat n as an unsigned value 3 | // O(1) space, O(1) time 4 | public int reverseBits(int n) { 5 | int ret = 0; 6 | for(int i = 0; i < 32; i++){ 7 | if((n & (1 << i)) != 0) 8 | ret |= (1 << (31-i)); 9 | } 10 | return ret; 11 | } 12 | } -------------------------------------------------------------------------------- /Leetcode/All/191_Number_of_1_Bits.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // you need to treat n as an unsigned value 3 | // Shift 32 times: 4 | // O(1) space, O(1) time 5 | public int hammingWeight(int n) { 6 | int ret = 0; 7 | for(int i = 0; i < 32; i++){ 8 | if((n & (1 << i)) != 0) 9 | ret++; 10 | } 11 | return ret; 12 | } 13 | 14 | // Eliminate the rightmost '1': 15 | // O(1) space, O(1) time 16 | public int hammingWeight2(int n) { 17 | int ret = 0; 18 | while(n != 0){ 19 | ret++; 20 | n = n & (n - 1); 21 | } 22 | return ret; 23 | } 24 | } -------------------------------------------------------------------------------- /Leetcode/All/201_Bitwise_AND_of_Numbers_Range.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(1) time 3 | public int rangeBitwiseAnd(int m, int n) { 4 | int c = 0; 5 | while(m != n){ 6 | m >>= 1; 7 | n >>= 1; 8 | c++; 9 | } 10 | return m << c; 11 | } 12 | } -------------------------------------------------------------------------------- /Leetcode/All/203_Remove_Linked_List_Elements.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n) time 3 | public ListNode removeElements(ListNode head, int val) { 4 | if(head == null) return head; 5 | ListNode dummy = new ListNode(-1); 6 | ListNode cur = head; 7 | ListNode prev = dummy; 8 | while(cur != null){ 9 | if(cur.val == val){ 10 | cur = cur.next; 11 | }else{ 12 | prev.next = cur; 13 | cur = cur.next; 14 | prev = prev.next; 15 | } 16 | } 17 | prev.next = cur; 18 | return dummy.next; 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/All/217_Contains_Duplicate.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) space, O(1) time 3 | 4 | public boolean containsDuplicate(int[] nums) { 5 | if(nums == null || nums.length == 0) 6 | return false; 7 | HashSet set = new HashSet(); 8 | for(int i = 0; i < nums.length; i++){ 9 | if(set.contains(nums[i])) 10 | return true; 11 | else 12 | set.add(nums[i]); 13 | } 14 | return false; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/All/220_Contains_Duplicate_III.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // window + TreeSet(RB tree) 3 | // O(k) space, O(nlogk) time 4 | public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) { 5 | if(k < 1 || t < 0) 6 | return false; 7 | TreeSet set = new TreeSet(); 8 | for(int i = 0; i < nums.length; i++){ 9 | int c = nums[i]; 10 | if((set.floor(c) != null && c <= set.floor(c) + t) 11 | ||(set.ceiling(c) != null && c >= set.ceiling(c) - t)) 12 | return true; 13 | set.add(c); 14 | if(i >= k) 15 | set.remove(nums[i-k]); 16 | } 17 | return false; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/All/223_Rectangle_Area.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(1) time 3 | public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { 4 | int area = (C - A) * (D - B) + (G - E) * (H - F); 5 | // no overlap: 6 | // (abcd在efgh左边|| abcd在efgh右边) || (abcd在efgh上边||abcd在efgh下边) 7 | if((C < E || G < A) || (B > H || F > D)) 8 | return area; 9 | int left = Math.max(E, A); 10 | int right = Math.min(C, G); 11 | int top = Math.min(D, H); 12 | int bottom = Math.max(B, F); 13 | return area - (right - left) * (top - bottom); 14 | } 15 | } -------------------------------------------------------------------------------- /Leetcode/All/226_Invert_Binary_Tree.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(height) space, O(nlogn) time 3 | public TreeNode invertTree(TreeNode root) { 4 | if(root == null) 5 | return root; 6 | TreeNode temp = root.left; 7 | root.left = invertTree(root.right); 8 | root.right = invertTree(temp); 9 | return root; 10 | } 11 | } -------------------------------------------------------------------------------- /Leetcode/All/231_Power_of_Two.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(1) time 3 | public boolean isPowerOfTwo(int n) { 4 | if(n <= 0) return false; 5 | return (n & (n - 1)) == 0; 6 | } 7 | } -------------------------------------------------------------------------------- /Leetcode/All/233_Number_of_Digit_One.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int countDigitOne(int n) { 3 | int ones = 0; 4 | long left, right, pos = 1; 5 | if(n < 1) return 0; 6 | while(n >= pos){ 7 | left = n / pos; 8 | right = n % pos; 9 | long remain = left % 10; 10 | if(remain == 1) 11 | ones += (left/10) * pos + right + 1; 12 | else if(remain > 1) 13 | ones += (left/10 + 1) * pos; 14 | else 15 | ones += (left/10) * pos; 16 | pos *= 10; 17 | } 18 | return ones; 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/All/236_Lowest_Common_Ancestor_of_a_Binary_Tree.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // Bottom-Up Solution: 3 | // O(1) space OR O(logn) space if consider recursion stack 4 | // O(n) time 5 | public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { 6 | if(root == null || root == p || root == q) 7 | return root; 8 | TreeNode left = lowestCommonAncestor(root.left, p, q); 9 | TreeNode right = lowestCommonAncestor(root.right, p, q); 10 | if(left != null && right != null) 11 | return root; 12 | return left != null ? left : right; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Leetcode/All/237_Delete_Node_in_a_Linked_List.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(1) time 3 | public void deleteNode(ListNode node) { 4 | if(node == null || node.next == null) 5 | return; 6 | node.val = node.next.val; 7 | node.next = node.next.next; 8 | } 9 | } -------------------------------------------------------------------------------- /Leetcode/All/238_Product_of_Array_Except_Self.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n) time 3 | public int[] productExceptSelf(int[] nums) { 4 | if(nums == null || nums.length == 0) 5 | return nums; 6 | int[] ret = new int[nums.length]; 7 | int temp = 1; 8 | for(int i = 0; i < nums.length; i++){ 9 | ret[i] = temp; 10 | temp *= nums[i]; 11 | } 12 | 13 | temp = 1; 14 | for(int i = nums.length - 1; i >= 0; i--){ 15 | ret[i] *= temp; 16 | temp *= nums[i]; 17 | } 18 | return ret; 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/All/239_Sliding_Window_Maximum.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[] maxSlidingWindow(int[] nums, int k) { 3 | if(nums == null || nums.length == 0) 4 | return new int[0]; 5 | int n = nums.length; 6 | int[] ret = new int[n-k+1]; 7 | Deque dq = new LinkedList(); 8 | for(int i = 0; i < nums.length; i++){ 9 | while(!dq.isEmpty() && nums[dq.peekLast()] <= nums[i]) 10 | dq.pollLast(); 11 | while (!dq.isEmpty() && dq.peekFirst() <= i - k) 12 | dq.pollFirst(); 13 | dq.addLast(i); 14 | if(i >= k-1) 15 | ret[i-k+1] = nums[dq.peekFirst()]; 16 | 17 | } 18 | return ret; 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/All/240_Search_a_2D_Matrix_II.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(m + n) time 3 | public boolean searchMatrix(int[][] matrix, int target) { 4 | if(matrix == null || matrix.length == 0) 5 | return false; 6 | int row = matrix.length; 7 | int col = matrix[0].length; 8 | int i = 0, j = col - 1; 9 | while(i < row && j >= 0){ 10 | if(target == matrix[i][j]) 11 | return true; 12 | if(target > matrix[i][j]) 13 | i++; 14 | else 15 | j--; 16 | } 17 | return false; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/All/257_Binary_Tree_Paths.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public List binaryTreePaths(TreeNode root) { 3 | List ret = new LinkedList(); 4 | rec(root, "", ret); 5 | return ret; 6 | } 7 | 8 | private void rec(TreeNode root, String path, List ret){ 9 | if(root == null) return; 10 | if(root.left == null && root.right == null){ 11 | path += String.valueOf(root.val); 12 | ret.add(path); 13 | return; 14 | } 15 | path += String.valueOf(root.val) + "->"; 16 | rec(root.left, path, ret); 17 | rec(root.right, path, ret); 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/All/258_Add_Digits.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // while loop solution 3 | // O(1) space O(log num) time 4 | // 10 5 | public int addDigits(int num) { 6 | while(num > 9) 7 | num = num/10 + num % 10; 8 | return num; 9 | } 10 | 11 | //Recursion solution 12 | public int addDigits(int num) { 13 | if(num <= 9) return num; 14 | return addDigits(num/10 + num%10); 15 | } 16 | 17 | // One line solution 18 | public int addDigits(int num) { 19 | return num - ((num-1)/9)*9; 20 | } 21 | 22 | public int addDigits(int num) { 23 | return (num-1)%9 + 1; 24 | } 25 | } -------------------------------------------------------------------------------- /Leetcode/All/259_3Sum_Smaller.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n^2) time 3 | public int threeSumSmaller(int[] nums, int target) { 4 | if(nums == null || nums.length <= 2) 5 | return 0; 6 | Arrays.sort(nums); 7 | int cnt = 0; 8 | // 不要手贱去去重 9 | for(int i = 0; i < nums.length - 2; i++){ 10 | int l = i + 1, r = nums.length - 1; 11 | while(l < r){ 12 | int sum = nums[i] + nums[l] + nums[r]; 13 | if(sum < target){ 14 | cnt += r - l; 15 | l++; 16 | }else 17 | r--; 18 | } 19 | } 20 | return cnt; 21 | } 22 | } -------------------------------------------------------------------------------- /Leetcode/All/260_Single_Number_III.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // 一路异或得到的是两个single的异或 a^b = diff 3 | // diff&=-diff得到最低位异或为1的mask,即这个1只出现在a或b的一个数字 4 | // 然后原数组继续分两组异或,分别得到这两个数 5 | public int[] singleNumber(int[] nums) { 6 | int diff = 0; 7 | for(int num : nums) 8 | diff ^= num; 9 | diff &= -diff; 10 | int[] ret = {0, 0}; 11 | for(int num : nums){ 12 | if((num & diff) == 0) 13 | ret[0] ^= num; 14 | else 15 | ret[1] ^= num; 16 | } 17 | return ret; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/All/266_Palindrome_Permutation.java: -------------------------------------------------------------------------------- 1 | /* 题意: 2 | * 给一个String s, 其permutation能否构成一个Palindrome 3 | * E.g.: 4 | * "code" -> False, "aab" -> True, "carerac" -> True. 5 | * 6 | * solution: 7 | * 最多一个字符出现奇数次 8 | */ 9 | 10 | public class Solution { 11 | public boolean canPermutePalindrome(String s) { 12 | int[] table = new int[256]; 13 | for(int i = 0; i < s.length(); i++){ 14 | char ch = s.charAt(i); 15 | table[ch] = table[ch] > 0 ? --table[ch] : ++table[ch]; 16 | } 17 | int cnt = 0; 18 | for(int i = 0; i < 256; i++){ 19 | if(table[i] != 0) cnt++; 20 | } 21 | return cnt <= 1; 22 | } 23 | } -------------------------------------------------------------------------------- /Leetcode/All/268_Missing_Number.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // 数学解法,0要特殊对待 3 | public int missingNumber(int[] nums) { 4 | boolean hasZero = false; 5 | int len = nums.length; 6 | int sum = 0; 7 | for(int i = 0; i < nums.length; i++){ 8 | sum += nums[i]; 9 | if(nums[i] == 0) 10 | hasZero = true; 11 | } 12 | int idea = (0 + len) * (len + 1) / 2; 13 | return hasZero ? idea - sum : 0; 14 | } 15 | 16 | // 位操作 17 | public int missingNumber(int[] nums) { 18 | int ret = 0, i = 0; 19 | for(; i < nums.length; i++){ 20 | ret = ret ^ i ^ nums[i]; 21 | } 22 | return ret ^ i; 23 | } 24 | } -------------------------------------------------------------------------------- /Leetcode/All/275_H-Index_II.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int hIndex(int[] citations) { 3 | if(citations == null || citations.length == 0) 4 | return 0; 5 | int len = citations.length; 6 | int idx = 0; 7 | int lo = 0, hi = citations.length - 1; 8 | while(lo <= hi){ 9 | int mid = lo + (hi - lo)/2; 10 | if(citations[mid] + mid < len){ // search in right part 11 | lo = mid + 1; 12 | }else{ 13 | hi = mid - 1; 14 | idx = len - mid; 15 | } 16 | } 17 | return idx; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/All/276_Paint_Fence.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int numWays(int n, int k) { 3 | if(n == 0) return 0; 4 | if(n == 1) return k; 5 | int same = k; 6 | int diff = k * (k - 1); 7 | for(int i = 2; i < n; i++){ 8 | int temp = diff; 9 | diff = (diff + same) * (k - 1); 10 | same = temp; 11 | } 12 | return same + diff; 13 | } 14 | } -------------------------------------------------------------------------------- /Leetcode/All/277_Find_the_Celebrity.java: -------------------------------------------------------------------------------- 1 | public class Solution extends Relation { 2 | // O(nk) time, k is running time of knows() 3 | // O(1) space 4 | public int findCelebrity(int n) { 5 | int c = 0; 6 | for(int i = 1; i < n; i++){ 7 | if(knows(c, i)) 8 | c = i; 9 | } 10 | for(int i = 0; i < n; i++){ 11 | if(i != c && (knows(c, i) || !knows(i, c))) 12 | return -1; 13 | } 14 | return c; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/All/280_Wiggle_Sort.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) time 3 | public void wiggleSort(int[] nums) { 4 | if(nums == null || nums.length == 0) 5 | return; 6 | for(int i = 1; i < nums.length; i++){ 7 | if((i % 2 == 0 && nums[i] > nums[i-1]) || (i % 2 == 1 && nums[i] < nums[i-1])) 8 | swap(nums, i, i-1); 9 | } 10 | return; 11 | } 12 | 13 | private void swap(int[] nums, int i, int j){ 14 | int temp = nums[i]; 15 | nums[i] = nums[j]; 16 | nums[j] = temp; 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/All/287_Find_the_Duplicate_Number.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) space 3 | public int findDuplicate(int[] nums) { 4 | if(nums == null || nums.length == 0) 5 | return 0; 6 | int n = nums.length; 7 | int slow = n; 8 | int fast = n; 9 | while(true){ 10 | slow = nums[slow-1]; 11 | fast = nums[nums[fast-1] - 1]; 12 | if(slow == fast) break; 13 | } 14 | 15 | slow = n; 16 | while(slow != fast){ 17 | slow = nums[slow - 1]; 18 | fast = nums[fast - 1]; 19 | } 20 | return slow; 21 | } 22 | } -------------------------------------------------------------------------------- /Leetcode/All/292_Nim_Game.java: -------------------------------------------------------------------------------- 1 | // Do not touch multiply of 4 2 | public class Solution { 3 | public boolean canWinNim(int n) { 4 | return n % 4 != 0; 5 | } 6 | } -------------------------------------------------------------------------------- /Leetcode/All/300_Longest_Increasing_Subsequence.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // naive dp: 3 | // O(n) space, O(n^2) time 4 | public int lengthOfLIS(int[] nums) { 5 | if(nums == null || nums.length == 0) 6 | return 0; 7 | int max = 1; 8 | int[] dp = new int[nums.length]; 9 | 10 | for(int i = 0; i < nums.length; i++){ 11 | dp[i] = 1; 12 | for(int j = 0; j < i; j++){ 13 | if(nums[j] < nums[i]){ 14 | dp[i] = Math.max(dp[i], dp[j]+1); 15 | max = Math.max(dp[i], max); 16 | } 17 | } 18 | } 19 | return max; 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/All/303_Range_Sum_Query.java: -------------------------------------------------------------------------------- 1 | public class NumArray { 2 | int[] dp; 3 | public NumArray(int[] nums) { 4 | if(nums == null || nums.length == 0) 5 | return; 6 | dp = new int[nums.length + 1]; 7 | dp[0] = 0; 8 | for(int i = 1; i <= nums.length; i++) 9 | dp[i] = dp[i-1] + nums[i-1]; 10 | } 11 | 12 | public int sumRange(int i, int j) { 13 | if(dp == null || dp.length == 0) 14 | return 0; 15 | return dp[j+1] - dp[i]; 16 | } 17 | } 18 | 19 | 20 | // Your NumArray object will be instantiated and called as such: 21 | // NumArray numArray = new NumArray(nums); 22 | // numArray.sumRange(0, 1); 23 | // numArray.sumRange(1, 2); -------------------------------------------------------------------------------- /Leetcode/All/311_Sparse_Matrix_Multiplication.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // Mathmatic method: 3 | // O(n^3) time, O(1) sapce 4 | public int[][] multiply(int[][] A, int[][] B) { 5 | int rowA = A.length; 6 | int colA = A[0].length; 7 | int colB = B[0].length; 8 | int[][] ret = new int[rowA][colB]; 9 | for(int i = 0; i < rowA; i++){ 10 | for(int j = 0; j < colA; j++){ 11 | for(int k = 0; k < colB; k++){ 12 | ret[i][k] += A[i][j] * B[j][k]; 13 | } 14 | } 15 | } 16 | return ret; 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/All/322_Coin_Change.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int coinChange(int[] coins, int amount) { 3 | int[] dp = new int[amount + 1]; 4 | Arrays.fill(dp, amount + 1); 5 | dp[0] = 0; 6 | for(int i = 1; i <= amount; i++){ 7 | for(int j = 0; j < coins.length; j++){ 8 | if(coins[j] <= i){ 9 | dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1); 10 | } 11 | } 12 | } 13 | return dp[amount] > amount ? -1 : dp[amount]; 14 | } 15 | } -------------------------------------------------------------------------------- /Leetcode/All/328_Odd_Even_Linked_List.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public ListNode oddEvenList(ListNode head) { 3 | if(head == null || head.next == null) 4 | return head; 5 | ListNode odd = head; 6 | ListNode oddHead = odd; 7 | ListNode even = head.next; 8 | ListNode evenHead = even; 9 | while(even != null && even.next != null){ 10 | odd.next = even.next; 11 | odd = odd.next; 12 | even.next = odd.next; 13 | even = even.next; 14 | } 15 | odd.next = evenHead; 16 | return oddHead; 17 | } -------------------------------------------------------------------------------- /Leetcode/All/338_Counting_Bits.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[] countBits(int num) { 3 | int[] dp = new int[num + 1]; 4 | dp[0] = 0; 5 | for(int i = 1; i <= num; i++){ 6 | dp[i] = dp[i & (i - 1)] + 1; 7 | } 8 | return dp; 9 | } 10 | } -------------------------------------------------------------------------------- /Leetcode/All/342_Power_of_Four.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // General solution 3 | public boolean isPowerOfFour(int num) { 4 | if(num % 2 != 0) return num == 1; 5 | if(num % 4 != 0) return num == 1; 6 | while(num > 1){ 7 | if(num % 4 != 0) return false; 8 | num /= 4; 9 | } 10 | return num == 1; 11 | } 12 | // Cheating solution 13 | public boolean isPowerOfFour(int num) { 14 | return (num > 0) && ((num & (num - 1)) == 0) && ((num & 0x55555555) == num); 15 | } 16 | // Use radix: 17 | // 4 -> 0x10, 16 -> 0x100, 64 -> 0x1000 18 | public boolean isPowerOfFour(int num) { 19 | return Integer.toString(num, 4).matches("10*"); 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/All/344_Reverse_String.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public String reverseString(String s) { 3 | char[] arr = s.toCharArray(); 4 | int i = 0, j = arr.length - 1; 5 | while(i < j){ 6 | char c = arr[i]; 7 | arr[i++] = arr[j]; 8 | arr[j--] = c; 9 | } 10 | return new String(arr); 11 | } 12 | } -------------------------------------------------------------------------------- /Leetcode/All/357_Count_Numbers_with_Unique_Digits.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int countNumbersWithUniqueDigits(int n) { 3 | if(n < 0 || n > 10) return 0; 4 | if(n == 0) return 1; 5 | 6 | int availableNumber = 9; 7 | int uniqueDigits = 9; 8 | int sum = 10; 9 | for(int i = 2; i <= n; i++){ 10 | uniqueDigits = uniqueDigits * availableNumber; 11 | sum += uniqueDigits; 12 | availableNumber--; 13 | } 14 | return sum; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/All/367_Valid_Perfect_Square.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean isPerfectSquare(int num) { 3 | if (num < 1) return false; 4 | long l = 1, r = num; 5 | while(l <= r){ 6 | long mid = l + (r - l) / 2; 7 | long cur = mid * mid; 8 | if(cur > num) 9 | r = mid - 1; 10 | else if(cur < num) 11 | l = mid + 1; 12 | else 13 | return true; 14 | } 15 | return false; 16 | } 17 | } -------------------------------------------------------------------------------- /Leetcode/All/374_Guess_Number_Higher_or_Lower.java: -------------------------------------------------------------------------------- 1 | /* The guess API is defined in the parent class GuessGame. 2 | @param num, your guess 3 | @return -1 if my number is lower, 1 if my number is higher, otherwise return 0 4 | int guess(int num); */ 5 | 6 | public class Solution extends GuessGame { 7 | public int guessNumber(int n) { 8 | int l = 1, r = n; 9 | while(l <= r){ 10 | int mid = l + (r - l)/2; 11 | int res = guess(mid); 12 | if(res == 0) 13 | return mid; 14 | else if(res == -1) 15 | r = mid - 1; 16 | else 17 | l = mid + 1; 18 | } 19 | return 0; 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/All/383_Ransom_Note.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean canConstruct(String ransomNote, String magazine) { 3 | int[] dict = new int[26]; 4 | for(int i = 0; i < ransomNote.length(); i++) 5 | dict[ransomNote.charAt(i) - 'a']++; 6 | 7 | for(int i = 0; i < magazine.length(); i++){ 8 | dict[magazine.charAt(i) - 'a']--; 9 | } 10 | for(int i = 0; i < 26; i++){ 11 | if(dict[i] > 0) 12 | return false; 13 | } 14 | return true; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/All/388_Longest_Absolute_File_Path.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // Stack: 3 | // O(n) time, O(h) space: h is max level which is n 4 | public int lengthLongestPath(String input) { 5 | String[] strs = input.split("\n"); 6 | Stack stack = new Stack(); 7 | stack.push(0); 8 | int maxLen = 0; 9 | for(int i = 0; i < strs.length; i++){ 10 | int level = strs[i].lastIndexOf("\t") + 1; 11 | while(stack.size() > level + 1) 12 | stack.pop(); 13 | int len = stack.peek() + strs[i].length() - level + 1; 14 | stack.push(len); 15 | if(strs[i].contains(".")) maxLen = Math.max(maxLen, len - 1); 16 | } 17 | return maxLen; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/All/389_Find_the_Difference.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | //O(n) time, O(1) space 3 | public char findTheDifference(String s, String t) { 4 | int[] table = new int[26]; 5 | for(int i = 0; i < t.length(); i++) 6 | table[t.charAt(i) - 'a']++; 7 | 8 | for(int i = 0; i < s.length(); i++) 9 | table[s.charAt(i) -'a']--; 10 | 11 | for(int i = 0; i < 26; i++){ 12 | if(table[i] == 1) 13 | return (char) (i + 'a'); 14 | } 15 | return 'a'; 16 | } 17 | } -------------------------------------------------------------------------------- /Leetcode/All/392_Is_subsequence.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // Two pointer: 3 | // O(n) time, O(1) space 4 | public boolean isSubsequence(String s, String t) { 5 | if(s == null || s.length() == 0) return true; 6 | if(t == null || t.length() == 0) return false; 7 | int i = 0, j = 0; 8 | while(j < t.length()){ 9 | if(t.charAt(j) == s.charAt(i)){ 10 | i++; 11 | if(i == s.length()) return true; 12 | } 13 | j++; 14 | } 15 | return false; 16 | } 17 | } -------------------------------------------------------------------------------- /Leetcode/All/404_Sum_of_Left_Leaves.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int sumOfLeftLeaves(TreeNode root) { 3 | if(root == null) return 0; 4 | Queue q = new LinkedList(); 5 | q.add(root); 6 | int sum = 0; 7 | while(!q.isEmpty()){ 8 | TreeNode cur = q.poll(); 9 | if(cur.left != null && cur.left.left == null && cur.left.right == null) 10 | sum += cur.left.val; 11 | if(cur.left != null) q.add(cur.left); 12 | if(cur.right != null) q.add(cur.right); 13 | } 14 | return sum; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/All/406_Queue_Reconstruction_by_Height.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // Sort and insert 3 | // O(n^2) time, O(n) space 4 | public int[][] reconstructQueue(int[][] people) { 5 | int num = people.length; 6 | List list = new LinkedList(); 7 | Arrays.sort(people, (o1, o2) -> o1[0] == o2[0] ? o1[1] - o2[1] : o2[0] - o1[0]); 8 | for(int[] p : people){ 9 | list.add(p[1], p); 10 | } 11 | return list.toArray(people); 12 | } 13 | // TODO: Binary Indexed Tree solution 14 | } -------------------------------------------------------------------------------- /Leetcode/All/409_Longest_Palindrome.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) space, O(n) time 3 | public int longestPalindrome(String s) { 4 | if(s == null || s.length() == 0) return 0; 5 | 6 | Set set = new HashSet(); 7 | int count = 0; 8 | for(int i = 0; i < s.length(); i++){ 9 | if(set.contains(s.charAt(i))){ 10 | set.remove(s.charAt(i)); 11 | count++; 12 | }else{ 13 | set.add(s.charAt(i)); 14 | } 15 | } 16 | if(!set.isEmpty()) return count * 2 + 1; 17 | return count * 2; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/All/412_Fizz_Buzz.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public List fizzBuzz(int n) { 3 | List ret = new LinkedList(); 4 | if(n < 1) return ret; 5 | for(int i = 1; i <= n; i++){ 6 | if(i % 15 == 0){ 7 | ret.add("FizzBuzz"); 8 | }else if(i % 3 == 0){ 9 | ret.add("Fizz"); 10 | }else if(i % 5 == 0){ 11 | ret.add("Buzz"); 12 | }else{ 13 | ret.add(String.valueOf(i)); 14 | } 15 | } 16 | return ret; 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/All/415_Add_Strings.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) space 3 | public String addStrings(String num1, String num2) { 4 | String sum = ""; 5 | int i = num1.length() - 1, j = num2.length() - 1; 6 | int carry = 0; 7 | while(i >= 0 || j >= 0){ 8 | int v1 = i >= 0 ? num1.charAt(i--) - '0' : 0; 9 | int v2 = j >= 0 ? num2.charAt(j--) - '0' : 0; 10 | int cur = v1 + v2 + carry; 11 | int digit = cur % 10; 12 | carry = cur / 10; 13 | sum = digit + sum; 14 | } 15 | if(carry == 1) sum = "1" + sum; 16 | return sum; 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/All/434_Number_of_Segments_in_a_String.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int countSegments(String s) { 3 | int cnt = 0; 4 | for(int i = 0; i < s.length(); i++) 5 | if(s.charAt(i) != ' ' && (i == 0 || s.charAt(i-1) == ' ')) 6 | cnt++; 7 | return cnt; 8 | } 9 | } -------------------------------------------------------------------------------- /Leetcode/All/437_Path_Sum_III.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // Top-down: 3 | // O(nlogn) if balanced tree, or O(n^2) if linkedlist 4 | // O(1) space or O(n^2) space if consider recursion stack 5 | public int pathSum(TreeNode root, int sum) { 6 | if(root == null) return 0; 7 | return dfs(root,sum) + pathSum(root.left, sum) + pathSum(root.right, sum); 8 | } 9 | public int dfs(TreeNode root, int sum){ 10 | if(root == null) return 0; 11 | int cnt = 0; 12 | if(root.val == sum) cnt++; 13 | return cnt + dfs(root.left, sum - root.val) + dfs(root.right, sum - root.val); 14 | } 15 | } -------------------------------------------------------------------------------- /Leetcode/All/461_Hamming_Distance.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int hammingDistance(int x, int y) { 3 | int xor = x ^ y; 4 | int cnt = 0; 5 | for(int i = 0; i < 32; i++){ 6 | cnt += (xor >> i) & 1; 7 | } 8 | return cnt; 9 | } 10 | } -------------------------------------------------------------------------------- /Leetcode/All/463_Island_Perimeter.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int islandPerimeter(int[][] grid) { 3 | if(grid == null || grid.length == 0) 4 | return 0; 5 | int row = grid.length, col = grid[0].length; 6 | 7 | int count = 0, neighbor = 0; 8 | for(int i = 0; i < row; i++){ 9 | for(int j = 0; j < col; j++){ 10 | if(grid[i][j] == 1){ 11 | count++; 12 | if(i < row - 1 && grid[i + 1][j] == 1) neighbor++; 13 | if(j < col - 1 && grid[i][j + 1] == 1) neighbor++; 14 | } 15 | } 16 | } 17 | return count*4 - neighbor*2; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/All/476_Number_Complement.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int findComplement(int num) { 3 | int mask = (Integer.highestOneBit(num) << 1) - 1; 4 | num = ~num; 5 | return num & mask; 6 | } 7 | } -------------------------------------------------------------------------------- /Leetcode/All/485_Max_Consecutive_Ones.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | //O(n) time, O(1) space 3 | public int findMaxConsecutiveOnes(int[] nums) { 4 | if(nums == null || nums.length == 0) 5 | return 0; 6 | int max = 0, cnt = 0; 7 | for(int i = 0; i < nums.length; i++){ 8 | if(nums[i] == 1) 9 | max = Math.max(max, ++cnt); 10 | else 11 | cnt = 0; 12 | } 13 | return max; 14 | } 15 | } -------------------------------------------------------------------------------- /Leetcode/All/493_Reverse_Pairs.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // Brute-force: 3 | // O(n^2) time, O(1) space 4 | public int reversePairs(int[] nums) { 5 | int cnt = 0; 6 | for(int i = 0; i < nums.length; i++){ 7 | for(int j = i + 1; j < nums.length; j++){ 8 | if(nums[i] > (2 * (long)nums[j])) 9 | cnt++; 10 | } 11 | } 12 | return cnt; 13 | } 14 | } -------------------------------------------------------------------------------- /Leetcode/All/520_Detect_Capital.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean detectCapitalUse(String word) { 3 | int cnt = 0; 4 | for(char c: word.toCharArray()) if('Z' - c >= 0) cnt++; 5 | return ((cnt==0 || cnt==word.length()) || (cnt==1 && 'Z' - word.charAt(0)>=0)); 6 | } 7 | } -------------------------------------------------------------------------------- /Leetcode/All/521_Longest_Uncommon_Subsequence_I.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int findLUSlength(String a, String b) { 3 | return a.equals(b) ? -1 : Math.max(a.length(), b.length()); 4 | } 5 | } -------------------------------------------------------------------------------- /Leetcode/All/523_Continuous_Subarray_Sum.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | //ref: https://discuss.leetcode.com/topic/80793/java-o-n-time-o-k-space 3 | public boolean checkSubarraySum(int[] nums, int k) { 4 | HashMap map = new HashMap(); 5 | map.put(0, -1); 6 | int sum = 0; 7 | for(int i = 0; i < nums.length; i++){ 8 | sum += nums[i]; 9 | if(k != 0) sum %= k; 10 | if(map.containsKey(sum)){ 11 | int idx = map.get(sum); 12 | if(i - idx > 1) return true; 13 | }else{ 14 | map.put(sum, i); 15 | } 16 | } 17 | return false; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/All/541_Reverse_String_II.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public String reverseStr(String s, int k) { 3 | char[] arr = s.toCharArray(); 4 | int i = 0, len = arr.length; 5 | while(i < len){ 6 | int j = Math.min(i + k - 1, len - 1); 7 | reverse(arr, i, j); 8 | i = i + 2 * k; 9 | } 10 | return new String(arr); 11 | } 12 | 13 | public void reverse(char[] arr, int x, int y){ 14 | while(x < y){ 15 | char c = arr[x]; 16 | arr[x++] = arr[y]; 17 | arr[y--] = c; 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/All/544_Output_Contest_Matches.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public String findContestMatch(int n) { 3 | String[] m = new String[n]; 4 | 5 | for(int i = 1; i <= n; i++) 6 | m[i-1] = String.valueOf(i); 7 | 8 | while(n > 1){ 9 | for(int i = 0; i < n/2; i++){ 10 | m[i] = "(" + m[i] + "," + m[n - 1 - i] + ")"; 11 | } 12 | n /= 2; 13 | } 14 | return m[0]; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/All/551_Student_Attendance_Record_I.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean checkRecord(String s) { 3 | int absentCnt = 0; 4 | for(int i = 0; i < s.length(); i++){ 5 | if(i > 1 && s.charAt(i) == 'L' && s.charAt(i - 1) == 'L' && s.charAt(i - 2) =='L') 6 | return false; 7 | if(s.charAt(i) == 'A') 8 | absentCnt++; 9 | } 10 | if(absentCnt > 1) return false; 11 | return true; 12 | } 13 | // Radix 14 | public boolean checkRecord(String s) { 15 | return !s.matches(".*LLL.*|.*A.*A.*"); 16 | } 17 | } -------------------------------------------------------------------------------- /Leetcode/All/553_Optimal_Division.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public String optimalDivision(int[] nums) { 3 | StringBuilder sb = new StringBuilder(); 4 | sb.append(nums[0]); 5 | for (int i = 1; i < nums.length; i++) { 6 | if (i == 1 && nums.length > 2) { 7 | sb.append("/(").append(nums[i]); 8 | } else { 9 | sb.append("/").append(nums[i]); 10 | } 11 | } 12 | return nums.length > 2 ? sb.append(")").toString() : sb.toString(); 13 | } 14 | } -------------------------------------------------------------------------------- /Leetcode/All/560_Subarray_Sum_Equals_K.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int subarraySum(int[] nums, int k) { 3 | HashMap map = new HashMap(); 4 | int sum = 0; 5 | int ret = 0; 6 | map.put(0, 1); 7 | for(int i = 0; i < nums.length; i++){ 8 | sum += nums[i]; 9 | if(map.containsKey(sum - k)){ 10 | ret += map.get(sum - k); 11 | } 12 | map.put(sum, map.getOrDefault(sum, 0) + 1); 13 | } 14 | return ret; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/All/561_Array_Partition_I.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // Sorting: 3 | // O(nlogn) time, O(1) space 4 | public int arrayPairSum(int[] nums) { 5 | if(nums == null || nums.length == 0) 6 | return 0; 7 | Arrays.sort(nums); 8 | int sum = 0; 9 | for(int i = 0; i < nums.length; i+=2) 10 | sum += nums[i]; 11 | return sum; 12 | } 13 | } -------------------------------------------------------------------------------- /Leetcode/All/563_Binary_Tree_Tilt.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | int tilt = 0; 3 | public int findTilt(TreeNode root) { 4 | postOrder(root); 5 | return tilt; 6 | } 7 | 8 | public int postOrder(TreeNode root){ 9 | if(root == null) return 0; 10 | int l = postOrder(root.left); 11 | int r = postOrder(root.right); 12 | tilt += Math.abs(l - r); 13 | return l + r + root.val; 14 | } 15 | } -------------------------------------------------------------------------------- /Leetcode/All/566_Reshape_the_Matrix.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) space 3 | public int[][] matrixReshape(int[][] nums, int r, int c) { 4 | int r1 = nums.length; 5 | int c1 = nums[0].length; 6 | if(r1 * c1 != r * c) return nums; 7 | int[][] ret = new int[r][c]; 8 | for(int i = 0; i < r ; i++){ 9 | for(int j = 0; j < c; j++){ 10 | int cur = i * c + j; 11 | int r2 = cur / c1; 12 | int c2 = cur % c1; 13 | ret[i][j] = nums[r2][c2]; 14 | } 15 | } 16 | return ret; 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/All/572_Subtree_of_Another_Tree.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean isSubtree(TreeNode s, TreeNode t) { 3 | if(s == null) return t == null; 4 | if(t == null) return false; 5 | if(isSame(s, t)) return true; 6 | if(isSubtree(s.left, t) || isSubtree(s.right, t)) 7 | return true; 8 | return false; 9 | } 10 | 11 | public boolean isSame(TreeNode s, TreeNode t){ 12 | if(s == null || t == null) return s == t; 13 | if(s.val != t.val) return false; 14 | return isSame(s.left, t.left) && isSame(s.right, t.right); 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/All/575_Distribute_Candies.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int distributeCandies(int[] candies) { 3 | Set kinds = new HashSet(); 4 | for (int candy : candies) kinds.add(candy); 5 | return kinds.size() >= candies.length / 2 ? candies.length / 2 : kinds.size(); 6 | } 7 | } -------------------------------------------------------------------------------- /Leetcode/All/581_Shortest_Unsorted_Continuous_Subarray.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) space 3 | public int findUnsortedSubarray(int[] nums) { 4 | if(nums == null || nums.length == 0) 5 | return 0; 6 | int s = -1, e = -1; 7 | int max = nums[0], min = nums[nums.length - 1]; 8 | for(int i = 1; i < nums.length; i++){ 9 | if(nums[i] > max) max = nums[i]; 10 | else if(nums[i] < max) e = i; 11 | } 12 | if(e == -1) return 0; 13 | for(int i = nums.length - 2; i >= 0; i--){ 14 | if(nums[i] < min) min = nums[i]; 15 | else if(nums[i] > min) s = i; 16 | } 17 | return e - s + 1; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/Array/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Leetcode/Array/.DS_Store -------------------------------------------------------------------------------- /Leetcode/Array/001_Two_Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector& nums, int target) { 4 | vector ret; 5 | unordered_map map; 6 | for(int i = 0; i < nums.size(); i++){ 7 | if(map.find(target - nums[i]) != map.end()){ 8 | ret.push_back(map[target - nums[i]] + 1); 9 | ret.push_back(i+1); 10 | break; 11 | } 12 | map[nums[i]] = i; 13 | } 14 | return ret; 15 | } 16 | }; -------------------------------------------------------------------------------- /Leetcode/Array/001_Two_Sum.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(n) space 3 | public int[] twoSum(int[] nums, int target) { 4 | int[] ret = new int[2]; 5 | if(nums == null || nums.length == 0) 6 | return ret; 7 | 8 | HashMap map = new HashMap(); 9 | for(int i = 0; i < nums.length; i++){ 10 | if(map.containsKey(target - nums[i])){ 11 | ret[0] = map.get(target - nums[i]) + 1; 12 | ret[1] = i + 1; 13 | } 14 | map.put(nums[i], i); 15 | } 16 | return ret; 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/Array/011_Container_With_Most_Water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxArea(vector& height) { 4 | int i = 0, j = height.size() - 1; 5 | int area = 0; 6 | while(i < j){ 7 | int h = min(height[i], height[j]); 8 | area = max(area, h * (j - i)); 9 | if(height[i] > height[j]) 10 | j--; 11 | else 12 | i++; 13 | } 14 | return area; 15 | } 16 | }; -------------------------------------------------------------------------------- /Leetcode/Array/011_Container_With_Most_Water.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) space 3 | public int maxArea(int[] height) { 4 | if(height == null || height.length == 0) 5 | return 0; 6 | int l = 0, r = height.length - 1; 7 | int area = Integer.MIN_VALUE; 8 | while(l < r){ 9 | int h = Math.min(height[l], height[r]); 10 | area = Math.max(area, (r - l) * h); 11 | if(height[l] < height[r]) l++; 12 | else r--; 13 | } 14 | return area; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/Array/026_Remove_Duplicates_from_Sorted_Array.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) space 3 | public int removeDuplicates(int[] nums) { 4 | if(nums == null || nums.length == 0) 5 | return 0; 6 | int j = 1; 7 | for(int i = 1; i < nums.length; i++){ 8 | if(nums[i] != nums[i-1]){ 9 | nums[j++] = nums[i]; 10 | } 11 | } 12 | return j; 13 | } 14 | } -------------------------------------------------------------------------------- /Leetcode/Array/027_Remove_Element.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) space 3 | public int removeElement(int[] nums, int val) { 4 | if(nums == null || nums.length == 0) 5 | return 0; 6 | int j = 0; 7 | for(int i = 0; i < nums.length; i++){ 8 | if(nums[i] != val) 9 | nums[j++] = nums[i]; 10 | } 11 | return j; 12 | } 13 | } -------------------------------------------------------------------------------- /Leetcode/Array/066_Plus_One.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) space 3 | public int[] plusOne(int[] digits) { 4 | for(int i = digits.length - 1; i >= 0; i--){ 5 | digits[i] = (digits[i] + 1) % 10; 6 | if(digits[i] == 0){ 7 | if(i == 0){ 8 | int[] ret = new int[digits.length + 1]; 9 | ret[0] = 1; 10 | return ret; 11 | } 12 | }else{ 13 | return digits; 14 | } 15 | } 16 | return digits; 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/Array/080_Remove_Duplicates_from_Sorted_Array_II.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) space 3 | public int removeDuplicates(int[] nums) { 4 | if(nums == null || nums.length == 0) 5 | return 0; 6 | int j = 1; 7 | int cnt = 0; 8 | for(int i = 1; i < nums.length; i++){ 9 | if(nums[i] == nums[i-1]){ 10 | cnt++; 11 | if(cnt == 1){ 12 | nums[j++] = nums[i]; 13 | } 14 | }else{ 15 | cnt = 0; 16 | nums[j++] = nums[i]; 17 | } 18 | } 19 | return j; 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/Array/088_Merge_Sorted_Array.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(m+n) space, O(m+n) time 3 | public void merge(int[] nums1, int m, int[] nums2, int n) { 4 | int end = m + n - 1; 5 | int a = m - 1; 6 | int b = n - 1; 7 | while(a >= 0 && b >= 0){ 8 | if(nums1[a] > nums2[b]) 9 | nums1[end--] = nums1[a--]; 10 | else 11 | nums1[end--] = nums2[b--]; 12 | } 13 | while(b >= 0) 14 | nums1[end--] = nums2[b--]; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/Array/119_Pascal's_Triangle_II.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) Space, O(n^2) time 3 | public List getRow(int rowIndex) { 4 | List ret = new LinkedList(); 5 | if(rowIndex < 0) return ret; 6 | ret.add(1); 7 | for(int i = 1; i <= rowIndex; i++){ 8 | for(int j = ret.size() - 2; j >= 0; j--){ 9 | ret.set(j+1, ret.get(j+1)+ret.get(j)); 10 | } 11 | ret.add(1); 12 | } 13 | return ret; 14 | } 15 | } -------------------------------------------------------------------------------- /Leetcode/Array/167_Two_Sum_II(input_array_is_sorted).java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n) time 3 | public int[] twoSum(int[] numbers, int target) { 4 | int[] ret = new int[2]; 5 | int l = 0, r = numbers.length - 1; 6 | while(l < r){ 7 | int sum = numbers[l] + numbers[r]; 8 | if(sum == target){ 9 | ret[0] = l + 1; 10 | ret[1] = r + 1; 11 | return ret; 12 | } 13 | if(sum > target) 14 | r--; 15 | else 16 | l++; 17 | } 18 | return ret; 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/Array/189_Rotate_Array.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) sapce 3 | public void rotate(int[] nums, int k) { 4 | if(nums == null || nums.length <= 1 || k == 0) 5 | return; 6 | k = k % nums.length; 7 | reverse(nums, 0, nums.length-1); 8 | reverse(nums, 0, k-1); 9 | reverse(nums, k, nums.length-1); 10 | } 11 | 12 | private void reverse(int[] nums, int left, int right){ 13 | while(left < right){ 14 | int temp = nums[left]; 15 | nums[left] = nums[right]; 16 | nums[right] = temp; 17 | left++; 18 | right--; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/Array/217_Contains_Duplicate.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) space, O(1) time 3 | 4 | public boolean containsDuplicate(int[] nums) { 5 | if(nums == null || nums.length == 0) 6 | return false; 7 | HashSet set = new HashSet(); 8 | for(int i = 0; i < nums.length; i++){ 9 | if(set.contains(nums[i])) 10 | return true; 11 | else 12 | set.add(nums[i]); 13 | } 14 | return false; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/Array/220_Contains_Duplicate_III.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // window + TreeSet(RB tree) 3 | // O(k) space, O(nlogk) time 4 | public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) { 5 | if(k < 1 || t < 0) 6 | return false; 7 | TreeSet set = new TreeSet(); 8 | for(int i = 0; i < nums.length; i++){ 9 | int c = nums[i]; 10 | if((set.floor(c) != null && c <= set.floor(c) + t) 11 | ||(set.ceiling(c) != null && c >= set.ceiling(c) - t)) 12 | return true; 13 | set.add(c); 14 | if(i >= k) 15 | set.remove(nums[i-k]); 16 | } 17 | return false; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/Array/238_Product_of_Array_Except_Self.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector productExceptSelf(vector& nums) { 4 | vector ret(nums.size()); 5 | 6 | int temp = 1; 7 | for(int i = 0; i < nums.size(); i++){ 8 | ret[i] = temp; 9 | temp *= nums[i]; 10 | } 11 | 12 | temp = 1; 13 | for(int i = nums.size() - 1; i >= 0; i--){ 14 | ret[i] *= temp; 15 | temp *= nums[i]; 16 | } 17 | return ret; 18 | } 19 | }; -------------------------------------------------------------------------------- /Leetcode/Array/238_Product_of_Array_Except_Self.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n) time 3 | public int[] productExceptSelf(int[] nums) { 4 | if(nums == null || nums.length == 0) 5 | return nums; 6 | int[] ret = new int[nums.length]; 7 | int temp = 1; 8 | for(int i = 0; i < nums.length; i++){ 9 | ret[i] = temp; 10 | temp *= nums[i]; 11 | } 12 | 13 | temp = 1; 14 | for(int i = nums.length - 1; i >= 0; i--){ 15 | ret[i] *= temp; 16 | temp *= nums[i]; 17 | } 18 | return ret; 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/Array/239_Sliding_Window_Maximum.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[] maxSlidingWindow(int[] nums, int k) { 3 | if(nums == null || nums.length == 0) 4 | return new int[0]; 5 | int n = nums.length; 6 | int[] ret = new int[n-k+1]; 7 | Deque dq = new LinkedList(); 8 | for(int i = 0; i < nums.length; i++){ 9 | while(!dq.isEmpty() && nums[dq.peekLast()] <= nums[i]) 10 | dq.pollLast(); 11 | while (!dq.isEmpty() && dq.peekFirst() <= i - k) 12 | dq.pollFirst(); 13 | dq.addLast(i); 14 | if(i >= k-1) 15 | ret[i-k+1] = nums[dq.peekFirst()]; 16 | 17 | } 18 | return ret; 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/Array/259_3Sum_Smaller.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n^2) time 3 | public int threeSumSmaller(int[] nums, int target) { 4 | if(nums == null || nums.length <= 2) 5 | return 0; 6 | Arrays.sort(nums); 7 | int cnt = 0; 8 | // 不要手贱去去重 9 | for(int i = 0; i < nums.length - 2; i++){ 10 | int l = i + 1, r = nums.length - 1; 11 | while(l < r){ 12 | int sum = nums[i] + nums[l] + nums[r]; 13 | if(sum < target){ 14 | cnt += r - l; 15 | l++; 16 | }else 17 | r--; 18 | } 19 | } 20 | return cnt; 21 | } 22 | } -------------------------------------------------------------------------------- /Leetcode/Array/275_H-Index_II.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int hIndex(int[] citations) { 3 | if(citations == null || citations.length == 0) 4 | return 0; 5 | int len = citations.length; 6 | int idx = 0; 7 | int lo = 0, hi = citations.length - 1; 8 | while(lo <= hi){ 9 | int mid = lo + (hi - lo)/2; 10 | if(citations[mid] + mid < len){ // search in right part 11 | lo = mid + 1; 12 | }else{ 13 | hi = mid - 1; 14 | idx = len - mid; 15 | } 16 | } 17 | return idx; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/Array/280_Wiggle_Sort.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) time 3 | public void wiggleSort(int[] nums) { 4 | if(nums == null || nums.length == 0) 5 | return; 6 | for(int i = 1; i < nums.length; i++){ 7 | if((i % 2 == 0 && nums[i] > nums[i-1]) || (i % 2 == 1 && nums[i] < nums[i-1])) 8 | swap(nums, i, i-1); 9 | } 10 | return; 11 | } 12 | 13 | private void swap(int[] nums, int i, int j){ 14 | int temp = nums[i]; 15 | nums[i] = nums[j]; 16 | nums[j] = temp; 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/Array/283_Move_Zeroes.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // Two pointer 3 | public void moveZeroes(int[] nums) { 4 | int j = 0; 5 | 6 | for(int i = 0; i < nums.length; i++){ 7 | if(nums[i] != 0){ 8 | if(j != i){ 9 | nums[j] = nums[i]; 10 | nums[i] = 0; 11 | } 12 | j++; 13 | } 14 | } 15 | return; 16 | } 17 | } -------------------------------------------------------------------------------- /Leetcode/Array/287_Find_the_Duplicate_Number.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) space 3 | public int findDuplicate(int[] nums) { 4 | if(nums == null || nums.length == 0) 5 | return 0; 6 | int n = nums.length; 7 | int slow = n; 8 | int fast = n; 9 | while(true){ 10 | slow = nums[slow-1]; 11 | fast = nums[nums[fast-1] - 1]; 12 | if(slow == fast) break; 13 | } 14 | 15 | slow = n; 16 | while(slow != fast){ 17 | slow = nums[slow - 1]; 18 | fast = nums[fast - 1]; 19 | } 20 | return slow; 21 | } 22 | } -------------------------------------------------------------------------------- /Leetcode/Array/303_Range_Sum_Query.java: -------------------------------------------------------------------------------- 1 | public class NumArray { 2 | int[] dp; 3 | public NumArray(int[] nums) { 4 | if(nums == null || nums.length == 0) 5 | return; 6 | dp = new int[nums.length + 1]; 7 | dp[0] = 0; 8 | for(int i = 1; i <= nums.length; i++) 9 | dp[i] = dp[i-1] + nums[i-1]; 10 | } 11 | 12 | public int sumRange(int i, int j) { 13 | if(dp == null || dp.length == 0) 14 | return 0; 15 | return dp[j+1] - dp[i]; 16 | } 17 | } 18 | 19 | 20 | // Your NumArray object will be instantiated and called as such: 21 | // NumArray numArray = new NumArray(nums); 22 | // numArray.sumRange(0, 1); 23 | // numArray.sumRange(1, 2); -------------------------------------------------------------------------------- /Leetcode/Array/311_Sparse_Matrix_Multiplication.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // Mathmatic method: 3 | // O(n^3) time, O(1) sapce 4 | public int[][] multiply(int[][] A, int[][] B) { 5 | int rowA = A.length; 6 | int colA = A[0].length; 7 | int colB = B[0].length; 8 | int[][] ret = new int[rowA][colB]; 9 | for(int i = 0; i < rowA; i++){ 10 | for(int j = 0; j < colA; j++){ 11 | for(int k = 0; k < colB; k++){ 12 | ret[i][k] += A[i][j] * B[j][k]; 13 | } 14 | } 15 | } 16 | return ret; 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/Array/325_Maximum_Size_Subarray_Sum_Equals_k.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxSubArrayLen(int[] nums, int k) { 3 | int sum = 0, max = 0; 4 | HashMap map = new HashMap(); 5 | for (int i = 0; i < nums.length; i++) { 6 | sum = sum + nums[i]; 7 | if (sum == k) max = i + 1; 8 | else if (map.containsKey(sum - k)) max = Math.max(max, i - map.get(sum - k)); 9 | if (!map.containsKey(sum)) map.put(sum, i); 10 | } 11 | return max; 12 | } 13 | } -------------------------------------------------------------------------------- /Leetcode/Array/406_Queue_Reconstruction_by_Height.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // Sort and insert 3 | // O(n^2) time, O(n) space 4 | public int[][] reconstructQueue(int[][] people) { 5 | int num = people.length; 6 | List list = new LinkedList(); 7 | Arrays.sort(people, (o1, o2) -> o1[0] == o2[0] ? o1[1] - o2[1] : o2[0] - o1[0]); 8 | for(int[] p : people){ 9 | list.add(p[1], p); 10 | } 11 | return list.toArray(people); 12 | } 13 | // TODO: Binary Indexed Tree solution 14 | } -------------------------------------------------------------------------------- /Leetcode/Array/435_Non-overlapping_Intervals.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int eraseOverlapIntervals(Interval[] intervals) { 3 | if (intervals == null || intervals.length == 0) 4 | return 0; 5 | Arrays.sort(intervals, (a, b) -> a.end - b.end); 6 | int curEnd = intervals[0].end, count = 0; 7 | for (int i = 1; i < intervals.length; i++){ 8 | if (curEnd <= intervals[i].start){ 9 | curEnd = intervals[i].end; 10 | }else{ 11 | count++; 12 | } 13 | } 14 | return count; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/Array/463_Island_Perimeter.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int islandPerimeter(int[][] grid) { 3 | if(grid == null || grid.length == 0) 4 | return 0; 5 | int row = grid.length, col = grid[0].length; 6 | 7 | int count = 0, neighbor = 0; 8 | for(int i = 0; i < row; i++){ 9 | for(int j = 0; j < col; j++){ 10 | if(grid[i][j] == 1){ 11 | count++; 12 | if(i < row - 1 && grid[i + 1][j] == 1) neighbor++; 13 | if(j < col - 1 && grid[i][j + 1] == 1) neighbor++; 14 | } 15 | } 16 | } 17 | return count*4 - neighbor*2; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/Array/485_Max_Consecutive_Ones.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | //O(n) time, O(1) space 3 | public int findMaxConsecutiveOnes(int[] nums) { 4 | if(nums == null || nums.length == 0) 5 | return 0; 6 | int max = 0, cnt = 0; 7 | for(int i = 0; i < nums.length; i++){ 8 | if(nums[i] == 1) 9 | max = Math.max(max, ++cnt); 10 | else 11 | cnt = 0; 12 | } 13 | return max; 14 | } 15 | } -------------------------------------------------------------------------------- /Leetcode/Array/493_Reverse_Pairs.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // Brute-force: 3 | // O(n^2) time, O(1) space 4 | public int reversePairs(int[] nums) { 5 | int cnt = 0; 6 | for(int i = 0; i < nums.length; i++){ 7 | for(int j = i + 1; j < nums.length; j++){ 8 | if(nums[i] > (2 * (long)nums[j])) 9 | cnt++; 10 | } 11 | } 12 | return cnt; 13 | } 14 | } -------------------------------------------------------------------------------- /Leetcode/Array/495_Teemo_Attacking.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findPoisonedDuration(int[] timeSeries, int duration) { 3 | if(timeSeries == null || timeSeries.length == 0) 4 | return 0; 5 | 6 | int res = 0; 7 | res += duration; 8 | int preEnd = timeSeries[0] + duration - 1; 9 | for(int i = 1; i < timeSeries.length; i++){ 10 | int curEnd = timeSeries[i] + duration - 1; 11 | int curStart = timeSeries[i]; 12 | res += duration; 13 | if(curStart <= preEnd){ 14 | res = res - (preEnd - curStart + 1); 15 | } 16 | preEnd = curEnd; 17 | } 18 | return res; 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/Array/560_Subarray_Sum_Equals_K.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int subarraySum(int[] nums, int k) { 3 | if(nums == null || nums.length == 0) 4 | return 0; 5 | Map map = new HashMap<>(); 6 | int sum = 0, cnt = 0; 7 | map.put(0, 1); 8 | for(int i = 0; i < nums.length; i++){ 9 | sum += nums[i]; 10 | if(map.containsKey(sum - k)) 11 | cnt += map.get(sum - k); 12 | map.put(sum, map.getOrDefault(sum, 0) + 1); 13 | } 14 | return cnt; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/Array/561_Array_Partition_I.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // Sorting: 3 | // O(nlogn) time, O(1) space 4 | public int arrayPairSum(int[] nums) { 5 | if(nums == null || nums.length == 0) 6 | return 0; 7 | Arrays.sort(nums); 8 | int sum = 0; 9 | for(int i = 0; i < nums.length; i+=2) 10 | sum += nums[i]; 11 | return sum; 12 | } 13 | } -------------------------------------------------------------------------------- /Leetcode/Array/566_Reshape_the_Matrix.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) space 3 | public int[][] matrixReshape(int[][] nums, int r, int c) { 4 | int r1 = nums.length; 5 | int c1 = nums[0].length; 6 | if(r1 * c1 != r * c) return nums; 7 | int[][] ret = new int[r][c]; 8 | for(int i = 0; i < r ; i++){ 9 | for(int j = 0; j < c; j++){ 10 | int cur = i * c + j; 11 | int r2 = cur / c1; 12 | int c2 = cur % c1; 13 | ret[i][j] = nums[r2][c2]; 14 | } 15 | } 16 | return ret; 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/Array/575_Distribute_Candies.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int distributeCandies(int[] candies) { 3 | Set kinds = new HashSet(); 4 | for (int candy : candies) kinds.add(candy); 5 | return kinds.size() >= candies.length / 2 ? candies.length / 2 : kinds.size(); 6 | } 7 | } -------------------------------------------------------------------------------- /Leetcode/Array/581_Shortest_Unsorted_Continuous_Subarray.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) space 3 | public int findUnsortedSubarray(int[] nums) { 4 | if(nums == null || nums.length == 0) 5 | return 0; 6 | int s = -1, e = -1; 7 | int max = nums[0], min = nums[nums.length - 1]; 8 | for(int i = 1; i < nums.length; i++){ 9 | if(nums[i] > max) max = nums[i]; 10 | else if(nums[i] < max) e = i; 11 | } 12 | if(e == -1) return 0; 13 | for(int i = nums.length - 2; i >= 0; i--){ 14 | if(nums[i] < min) min = nums[i]; 15 | else if(nums[i] > min) s = i; 16 | } 17 | return e - s + 1; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/Array/674_Longest_Continuous_Increasing_Subsequence.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findLengthOfLCIS(int[] nums) { 3 | if(nums == null || nums.length == 0) 4 | return 0; 5 | int len = 1, temp = 1; 6 | for(int i = 0; i < nums.length - 1; i++){ 7 | if(nums[i] < nums[i + 1]){ 8 | temp++; 9 | len = Math.max(len, temp); 10 | }else{ 11 | temp = 1; 12 | } 13 | } 14 | return len; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/Array/703_Kth_Largest_Element_in_a_Stream.java: -------------------------------------------------------------------------------- 1 | class KthLargest { 2 | PriorityQueue pq; 3 | int k; 4 | public KthLargest(int k, int[] nums) { 5 | pq = new PriorityQueue<>(); 6 | for(int n : nums){ 7 | pq.offer(n); 8 | if(pq.size() > k) 9 | pq.poll(); 10 | } 11 | this.k = k; 12 | 13 | } 14 | 15 | public int add(int val) { 16 | pq.offer(val); 17 | if(pq.size() > k) 18 | pq.poll(); 19 | return pq.peek(); 20 | } 21 | } 22 | 23 | /** 24 | * Your KthLargest object will be instantiated and called as such: 25 | * KthLargest obj = new KthLargest(k, nums); 26 | * int param_1 = obj.add(val); 27 | */ -------------------------------------------------------------------------------- /Leetcode/Array/724_Find_Pivot_Index.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int pivotIndex(int[] nums) { 3 | if(nums == null || nums.length == 0) 4 | return -1; 5 | int sum = 0; 6 | for(int n : nums) 7 | sum += n; 8 | int cur = 0; 9 | for(int i = 0; i < nums.length; i++){ 10 | if(sum - nums[i] == cur + cur) 11 | return i; 12 | cur += nums[i]; 13 | } 14 | return -1; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/Array/731_My_Calendar II.java: -------------------------------------------------------------------------------- 1 | public class MyCalendarTwo { 2 | List calendar; 3 | List overlaps; 4 | 5 | MyCalendarTwo() { 6 | calendar = new ArrayList(); 7 | overlaps = new ArrayList(); 8 | } 9 | 10 | public boolean book(int start, int end) { 11 | for (int[] iv: overlaps) { 12 | if (iv[0] < end && start < iv[1]) return false; 13 | } 14 | for (int[] iv: calendar) { 15 | if (iv[0] < end && start < iv[1]) 16 | overlaps.add(new int[]{Math.max(start, iv[0]), Math.min(end, iv[1])}); 17 | } 18 | calendar.add(new int[]{start, end}); 19 | return true; 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/Array/760_Find_Anagram_Mappings.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] anagramMappings(int[] A, int[] B) { 3 | int[] res = new int[A.length]; 4 | Map> map = new HashMap<>(); 5 | for(int i = 0; i < B.length; i++){ 6 | if(!map.containsKey(B[i])) 7 | map.put(B[i], new LinkedList<>()); 8 | map.get(B[i]).add(i); 9 | } 10 | 11 | for(int i = 0; i < A.length; i++) 12 | res[i] = map.get(A[i]).remove(); 13 | return res; 14 | } 15 | } -------------------------------------------------------------------------------- /Leetcode/Array/766_Toeplitz_Matrix.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isToeplitzMatrix(int[][] matrix) { 3 | if(matrix == null || matrix.length == 0) 4 | return false; 5 | for(int i = 1; i < matrix.length; i++){ 6 | for(int j = 1; j < matrix[0].length; j++){ 7 | if(matrix[i][j] != matrix[i - 1][j - 1]) 8 | return false; 9 | } 10 | } 11 | return true; 12 | } 13 | } -------------------------------------------------------------------------------- /Leetcode/Array/836_Rectangle_Overlap.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isRectangleOverlap(int[] rec1, int[] rec2) { 3 | // left, up, right, down 4 | if(rec1[2] <= rec2[0] || rec1[1] >= rec2[3] 5 | || rec1[0] >= rec2[2] || rec1[3] <= rec2[1]) 6 | return false; 7 | return true; 8 | } 9 | } -------------------------------------------------------------------------------- /Leetcode/Array/849_Maximize_Distance_to_Closest_Person.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxDistToClosest(int[] seats) { 3 | int seatIdx = -1; 4 | int max = 1; 5 | int i = 0; 6 | while(i < seats.length){ 7 | if(seats[i] == 1){ 8 | if(seatIdx == -1) max = Math.max(max, i); 9 | if(seatIdx != -1) max = Math.max(max, (i - seatIdx) / 2); 10 | seatIdx = i; 11 | } 12 | i++; 13 | } 14 | max = Math.max(max, i - 1 - seatIdx); 15 | return max; 16 | } 17 | } -------------------------------------------------------------------------------- /Leetcode/Array/896_Monotonic_Array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isMonotonic(int[] A) { 3 | if(A == null || A.length <= 1) 4 | return true; 5 | 6 | int diff = 0; 7 | for(int i = 1; i < A.length; i++){ 8 | if(A[i] == A[i] - 1) continue; 9 | if(diff == 0) 10 | diff = A[i] - A[i - 1]; 11 | if(A[i] > A[i - 1] && diff < 0) 12 | return false; 13 | if(A[i] < A[i - 1] && diff > 0) 14 | return false; 15 | } 16 | return true; 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/Array/915_Partition_Array_into_Disjoint_Intervals.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int partitionDisjoint(int[] A) { 3 | int max = A[0], preMax = max, idx = 0; 4 | for(int i = 1; i < A.length; i++){ 5 | if(preMax > A[i]){ 6 | preMax = max; 7 | idx = i; 8 | }else{ 9 | max = Math.max(max, A[i]); 10 | } 11 | } 12 | return idx + 1; 13 | } 14 | } -------------------------------------------------------------------------------- /Leetcode/Array/930_Binary_Subarrays_With_Sum.java: -------------------------------------------------------------------------------- 1 | // O(n) time, O(n) space 2 | class Solution { 3 | public int numSubarraysWithSum(int[] A, int S) { 4 | if(A == null || A.length == 0) 5 | return 0; 6 | int preSum = 0; 7 | int total = 0; 8 | Map map = new HashMap<>(); 9 | for(int i = 0; i < A.length; i++){ 10 | preSum += A[i]; 11 | if(map.get(preSum - S) != null) 12 | total += map.get(preSum - S); 13 | if(preSum == S) 14 | total += 1; 15 | 16 | map.put(preSum, map.getOrDefault(preSum, 0) + 1); 17 | } 18 | return total; 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/Array/932_Beautiful_Array.java: -------------------------------------------------------------------------------- 1 | // O(NlogN) time, O(N) space 2 | class Solution { 3 | public int[] beautifulArray(int N) { 4 | ArrayList res = new ArrayList<>(); 5 | res.add(1); 6 | while (res.size() < N) { 7 | ArrayList tmp = new ArrayList<>(); 8 | for (int i : res) if (i * 2 - 1 <= N) tmp.add(i * 2 - 1); 9 | for (int i : res) if (i * 2 <= N) tmp.add(i * 2); 10 | res = tmp; 11 | } 12 | return res.stream().mapToInt(i -> i).toArray(); 13 | } 14 | } -------------------------------------------------------------------------------- /Leetcode/BinarySearch/035_Search_Insert_Position.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int searchInsert(vector& nums, int target) { 4 | if(nums.size() == 0) return 0; 5 | int lo = 0, hi = nums.size() - 1; 6 | while(lo <= hi){ 7 | int mid = lo + (hi - lo) / 2; 8 | if(nums[mid] == target) return mid; 9 | if(nums[mid] < target) 10 | lo = mid + 1; 11 | else 12 | hi = mid - 1; 13 | } 14 | return lo; 15 | } 16 | }; -------------------------------------------------------------------------------- /Leetcode/BinarySearch/154_Find Minimum_in_Rotated_Sorted_Array_II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMin(vector& nums) { 4 | if(nums.size() == 0) 5 | return 0; 6 | int ret = nums[0]; 7 | int lo = 0, hi = nums.size() - 1; 8 | while(lo <= hi){ 9 | int mid = lo + (hi - lo) / 2; 10 | if(nums[mid] >= nums[hi]){ 11 | if(nums[mid] == nums[hi]){ 12 | ret = min(ret, nums[hi]); 13 | hi--; 14 | continue; 15 | } 16 | lo = mid + 1; 17 | }else{ 18 | hi = mid - 1; 19 | } 20 | ret = min(ret, nums[mid]); 21 | } 22 | return ret; 23 | } 24 | }; -------------------------------------------------------------------------------- /Leetcode/BinarySearch/162_Find_Peak_Element.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(logn) time 3 | public int findPeakElement(int[] nums) { 4 | if(nums == null) return -1; 5 | if(nums.length == 1) return 0; 6 | int lo = 0, hi = nums.length-1; 7 | while(lo <= hi){ 8 | if(lo == hi) return lo; 9 | int mid = lo + (hi - lo)/2; 10 | if(nums[mid] < nums[mid+1]){ 11 | lo = mid+1; 12 | }else{ 13 | hi = mid; 14 | } 15 | } 16 | return -1; 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/BinarySearch/240_Search_a_2D_Matrix_II.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(m + n) time 3 | public boolean searchMatrix(int[][] matrix, int target) { 4 | if(matrix == null || matrix.length == 0) 5 | return false; 6 | int row = matrix.length; 7 | int col = matrix[0].length; 8 | int i = 0, j = col - 1; 9 | while(i < row && j >= 0){ 10 | if(target == matrix[i][j]) 11 | return true; 12 | if(target > matrix[i][j]) 13 | i++; 14 | else 15 | j--; 16 | } 17 | return false; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/BinarySearch/367_Valid_Perfect_Square.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean isPerfectSquare(int num) { 3 | if (num < 1) return false; 4 | long l = 1, r = num; 5 | while(l <= r){ 6 | long mid = l + (r - l) / 2; 7 | long cur = mid * mid; 8 | if(cur > num) 9 | r = mid - 1; 10 | else if(cur < num) 11 | l = mid + 1; 12 | else 13 | return true; 14 | } 15 | return false; 16 | } 17 | } -------------------------------------------------------------------------------- /Leetcode/BinarySearch/374_Guess_Number_Higher_or_Lower.java: -------------------------------------------------------------------------------- 1 | /* The guess API is defined in the parent class GuessGame. 2 | @param num, your guess 3 | @return -1 if my number is lower, 1 if my number is higher, otherwise return 0 4 | int guess(int num); */ 5 | 6 | public class Solution extends GuessGame { 7 | public int guessNumber(int n) { 8 | int l = 1, r = n; 9 | while(l <= r){ 10 | int mid = l + (r - l)/2; 11 | int res = guess(mid); 12 | if(res == 0) 13 | return mid; 14 | else if(res == -1) 15 | r = mid - 1; 16 | else 17 | l = mid + 1; 18 | } 19 | return 0; 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/BinarySearch/704_Binary_Search.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int search(int[] nums, int target) { 3 | if(nums == null || nums.length == 0) return -1; 4 | int l = 0, r = nums.length - 1; 5 | while(l <= r){ 6 | int mid = (r + l) >> 1; 7 | if(target == nums[mid]) return mid; 8 | if(target > nums[mid]) 9 | l = mid + 1; 10 | else 11 | r = mid - 1; 12 | } 13 | return -1; 14 | } 15 | } -------------------------------------------------------------------------------- /Leetcode/BinarySearch/852_Peak_Index_in_a_Mountain_Array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int peakIndexInMountainArray(int[] A) { 3 | 4 | int l = 0, r = A.length - 1; 5 | while(l <= r){ 6 | int mid = l + (r - l) / 2; 7 | // peak is in the right part 8 | if(A[mid] > A[mid + 1] && A[mid] > A[mid - 1]) 9 | return mid; 10 | if(A[mid] > A[mid + 1]){ 11 | r = mid - 1; 12 | }else{ 13 | l = mid + 1; 14 | } 15 | } 16 | return -1; 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/BinarySearch/875_Koko_Eating_Bananas.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minEatingSpeed(int[] piles, int H) { 3 | int max = piles[0]; 4 | for(int n : piles) 5 | max = Math.max(max, n); 6 | 7 | int l = 1, r = max; 8 | while(l <= r){ 9 | int mid = l + (r - l) / 2; 10 | int h = 0; 11 | for(int n : piles) 12 | h += (n - 1)/mid + 1; 13 | if(h <= H) 14 | r = mid - 1; 15 | else 16 | l = mid + 1; 17 | } 18 | return l; 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/BitManipulation/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Leetcode/BitManipulation/.DS_Store -------------------------------------------------------------------------------- /Leetcode/BitManipulation/136_Single_Number.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n) time 3 | public int singleNumber(int[] A) { 4 | if(A == null || A.length == 0) return 0; 5 | int ret = A[0]; 6 | for(int i = 1; i < A.length; i++) 7 | ret = A[i] ^ ret; 8 | return ret; 9 | } 10 | } -------------------------------------------------------------------------------- /Leetcode/BitManipulation/137_Single_Number_II.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int singleNumber(int[] nums) { 3 | // O(1) space, O(32n) => O(n) time 4 | int ret = 0; 5 | for(int i = 0; i < 32; i++){ 6 | int cnt = 0, temp = 0; 7 | int mask = 1 << i; 8 | for(int j = 0; j < nums.length; j++){ 9 | temp = nums[j] & mask; 10 | if(temp != 0){ 11 | cnt++; 12 | } 13 | } 14 | if(cnt % 3 == 1){ 15 | ret |= mask; 16 | } 17 | } 18 | return ret; 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/BitManipulation/190_Reverse_Bits.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // you need treat n as an unsigned value 3 | // O(1) space, O(1) time 4 | public int reverseBits(int n) { 5 | int ret = 0; 6 | for(int i = 0; i < 32; i++){ 7 | if((n & (1 << i)) != 0) 8 | ret |= (1 << (31-i)); 9 | } 10 | return ret; 11 | } 12 | } -------------------------------------------------------------------------------- /Leetcode/BitManipulation/191_Number_of_1_Bits.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // you need to treat n as an unsigned value 3 | // Shift 32 times: 4 | // O(1) space, O(1) time 5 | public int hammingWeight(int n) { 6 | int ret = 0; 7 | for(int i = 0; i < 32; i++){ 8 | if((n & (1 << i)) != 0) 9 | ret++; 10 | } 11 | return ret; 12 | } 13 | 14 | // Eliminate the rightmost '1': 15 | // O(1) space, O(1) time 16 | public int hammingWeight2(int n) { 17 | int ret = 0; 18 | while(n != 0){ 19 | ret++; 20 | n = n & (n - 1); 21 | } 22 | return ret; 23 | } 24 | } -------------------------------------------------------------------------------- /Leetcode/BitManipulation/201_Bitwise_AND_of_Numbers_Range.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(1) time 3 | public int rangeBitwiseAnd(int m, int n) { 4 | int c = 0; 5 | while(m != n){ 6 | m >>= 1; 7 | n >>= 1; 8 | c++; 9 | } 10 | return m << c; 11 | } 12 | } -------------------------------------------------------------------------------- /Leetcode/BitManipulation/231_Power_of_Two.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(1) time 3 | public boolean isPowerOfTwo(int n) { 4 | if(n <= 0) return false; 5 | return (n & (n - 1)) == 0; 6 | } 7 | } -------------------------------------------------------------------------------- /Leetcode/BitManipulation/260_Single_Number_III.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // 一路异或得到的是两个single的异或 a^b = diff 3 | // diff&=-diff得到最低位异或为1的mask,即这个1只出现在a或b的一个数字 4 | // 然后原数组继续分两组异或,分别得到这两个数 5 | public int[] singleNumber(int[] nums) { 6 | int diff = 0; 7 | for(int num : nums) 8 | diff ^= num; 9 | diff &= -diff; 10 | int[] ret = {0, 0}; 11 | for(int num : nums){ 12 | if((num & diff) == 0) 13 | ret[0] ^= num; 14 | else 15 | ret[1] ^= num; 16 | } 17 | return ret; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/BitManipulation/268_Missing_Number.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // 数学解法 3 | public int missingNumber(int[] nums) { 4 | int len = nums.length; 5 | int sum = (0 + len) * (len + 1) / 2; 6 | for(int n : nums) 7 | sum -= n; 8 | return sum; 9 | } 10 | 11 | // 位操作 12 | public int missingNumber(int[] nums) { 13 | int res = nums.length; 14 | for(int i = 0; i < nums.length; i++){ 15 | res ^= i; 16 | res ^= nums[i]; 17 | } 18 | return res; 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/BitManipulation/371_Sum_of_Two_Integers.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int getSum(int a, int b) { 3 | if(a == 0) return b; 4 | if(b == 0) return a; 5 | while(b != 0){ 6 | int carry = a & b; 7 | a = a ^ b; 8 | b = carry << 1; 9 | } 10 | return a; 11 | } 12 | } 13 | //https://leetcode.com/problems/sum-of-two-integers/discuss/84290/Java-simple-easy-understand-solution-with-explanation 14 | //https://leetcode.com/problems/sum-of-two-integers/discuss/84278/A-summary%3A-how-to-use-bit-manipulation-to-solve-problems-easily-and-efficiently -------------------------------------------------------------------------------- /Leetcode/BitManipulation/461_Hamming_Distance.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int hammingDistance(int x, int y) { 3 | int xor = x ^ y; 4 | int cnt = 0; 5 | for(int i = 0; i < 32; i++){ 6 | cnt += (xor >> i) & 1; 7 | } 8 | return cnt; 9 | } 10 | } -------------------------------------------------------------------------------- /Leetcode/BitManipulation/476_Number_Complement.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int findComplement(int num) { 3 | int mask = (Integer.highestOneBit(num) << 1) - 1; 4 | num = ~num; 5 | return num & mask; 6 | } 7 | } -------------------------------------------------------------------------------- /Leetcode/DP/070_Climbing_Stairs.java: -------------------------------------------------------------------------------- 1 | public class ClimbingStairs { 2 | // 一维DP: 3 | // O(n) space, O(n) time 4 | public int climbStairs(int n) { 5 | if(n <= 2) return n; 6 | int[] dp = new int[n+1]; 7 | dp[0] = 0; 8 | dp[1] = 1; 9 | dp[2] = 2; 10 | 11 | for(int i = 3; i < n+1; i++){ 12 | dp[i] = dp[i-1]+dp[i-2]; 13 | } 14 | return dp[n]; 15 | } 16 | // 状压dp: 17 | public int climbStairs(int n) { 18 | if(n <= 2) return n; 19 | int dp1 = 1; 20 | int dp2 = 2; 21 | int dp = 0; 22 | for(int i = 3; i <= n; i++){ 23 | dp = dp1 + dp2; 24 | dp1 = dp2; 25 | dp2 = dp; 26 | } 27 | return dp; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Leetcode/DP/096_Unique_Binary_Search_Trees.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // 一维DP: 3 | // O(n) space, O(n^2) time 4 | public int numTrees(int n) { 5 | if(n < 0) return 0; 6 | int[] dp = new int[n+1]; 7 | dp[0] = dp[1] = 1; 8 | // outer loop i means total node in the tree 9 | for(int i = 2; i <= n; i++){ 10 | // inner loop j means total nodes of left subtree 11 | // then i-j-1 means total nodes of right subtree 12 | // -1 since we need to reserve one node as root 13 | for(int j = 0; j < i; j++){ 14 | dp[i] += dp[j] * dp[i-j-1]; 15 | } 16 | } 17 | return dp[n]; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/DP/122_Best_Time_to_Buy_and_Sell_Stock _II.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n) time 3 | public int maxProfit(int[] prices) { 4 | if(prices == null || prices.length <= 1) 5 | return 0; 6 | int max = 0; 7 | for(int i = 1; i < prices.length; i++){ 8 | if(prices[i] > prices[i-1]) 9 | max += prices[i] - prices[i-1]; 10 | } 11 | return max; 12 | } 13 | } -------------------------------------------------------------------------------- /Leetcode/DP/152_Maximum_Product_Subarray.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // DP, 只需要俩常数, O(1) space, O(n) time 3 | public int maxProduct(int[] A) { 4 | int dpMax = A[0]; 5 | int dpMin = A[0]; 6 | int max = A[0]; 7 | for(int i = 1; i < A.length; i++){ 8 | int tempMax = dpMax; // dpmax在dpmin更新前已经更新了, 要记录下 9 | dpMax = Math.max(Math.max(A[i] * dpMax, A[i]), A[i] * dpMin); 10 | dpMin = Math.min(Math.min(A[i] * dpMin, A[i]), A[i] * tempMax); 11 | max = Math.max(dpMax, max); 12 | } 13 | return max; 14 | } 15 | } -------------------------------------------------------------------------------- /Leetcode/DP/276_Paint_Fence.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int numWays(int n, int k) { 3 | if(n == 0) return 0; 4 | if(n == 1) return k; 5 | int same = k; 6 | int diff = k * (k - 1); 7 | for(int i = 2; i < n; i++){ 8 | int temp = diff; 9 | diff = (diff + same) * (k - 1); 10 | same = temp; 11 | } 12 | return same + diff; 13 | } 14 | } -------------------------------------------------------------------------------- /Leetcode/DP/300_Longest_Increasing_Subsequence.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // naive dp: 3 | // O(n) space, O(n^2) time 4 | public int lengthOfLIS(int[] nums) { 5 | if(nums == null || nums.length == 0) 6 | return 0; 7 | int max = 1; 8 | int[] dp = new int[nums.length]; 9 | 10 | for(int i = 0; i < nums.length; i++){ 11 | dp[i] = 1; 12 | for(int j = 0; j < i; j++){ 13 | if(nums[j] < nums[i]){ 14 | dp[i] = Math.max(dp[i], dp[j]+1); 15 | max = Math.max(dp[i], max); 16 | } 17 | } 18 | } 19 | return max; 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/DP/322_Coin_Change.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int coinChange(int[] coins, int amount) { 3 | int[] dp = new int[amount + 1]; 4 | Arrays.fill(dp, amount + 1); 5 | dp[0] = 0; 6 | for(int i = 1; i <= amount; i++){ 7 | for(int j = 0; j < coins.length; j++){ 8 | if(coins[j] <= i){ 9 | dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1); 10 | } 11 | } 12 | } 13 | return dp[amount] > amount ? -1 : dp[amount]; 14 | } 15 | } -------------------------------------------------------------------------------- /Leetcode/DP/338_Counting_Bits.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[] countBits(int num) { 3 | int[] dp = new int[num + 1]; 4 | dp[0] = 0; 5 | for(int i = 1; i <= num; i++){ 6 | dp[i] = dp[i & (i - 1)] + 1; 7 | } 8 | return dp; 9 | } 10 | } -------------------------------------------------------------------------------- /Leetcode/DP/354_Russian_Doll_Envelopes.java: -------------------------------------------------------------------------------- 1 | // 解法和300一样 2 | class Solution { 3 | public int maxEnvelopes(int[][] envelopes) { 4 | if(envelopes == null || envelopes.length == 0) 5 | return 0; 6 | Arrays.sort(envelopes, (a, b) -> a[0] != b[0] ? a[0] - b[0] : b[1] - a[1]); 7 | 8 | int[] dp = new int[envelopes.length]; 9 | int len = 0; 10 | for(int[] e : envelopes){ 11 | int i = Arrays.binarySearch(dp, 0, len, e[1]); 12 | if(i < 0) i = - (i + 1); 13 | dp[i] = e[1]; 14 | if(i == len) len++; 15 | } 16 | return len; 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/DP/357_Count_Numbers_with_Unique_Digits.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int countNumbersWithUniqueDigits(int n) { 3 | if(n < 0 || n > 10) return 0; 4 | if(n == 0) return 1; 5 | 6 | int availableNumber = 9; 7 | int uniqueDigits = 9; 8 | int sum = 10; 9 | for(int i = 2; i <= n; i++){ 10 | uniqueDigits = uniqueDigits * availableNumber; 11 | sum += uniqueDigits; 12 | availableNumber--; 13 | } 14 | return sum; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/DP/486_Predict_the_Winner.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean PredictTheWinner(int[] nums) { 3 | int n = nums.length; 4 | int[][] dp = new int[n][n]; 5 | for(int i = 0; i < n; i++) dp[i][i] = nums[i]; 6 | for (int len = 1; len < n; len++) { 7 | for (int i = 0; i < n - len; i++) { 8 | int j = i + len; 9 | dp[i][j] = Math.max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1]); 10 | } 11 | } 12 | return dp[0][n - 1] >= 0; 13 | } 14 | } -------------------------------------------------------------------------------- /Leetcode/DP/518_Coin_Change_2.java: -------------------------------------------------------------------------------- 1 | // dp[i][j] = dp[i - 1][j] + (j >= coins[i - 1] ? dp[i][j - coins[i - 1]] : 0) 2 | 3 | class Solution { 4 | public int change(int amount, int[] coins) { 5 | int[] dp = new int[amount + 1]; 6 | dp[0] = 1; 7 | for (int coin : coins) { 8 | for (int i = coin; i <= amount; i++) { 9 | dp[i] += dp[i - coin]; 10 | } 11 | } 12 | return dp[amount]; 13 | } 14 | } -------------------------------------------------------------------------------- /Leetcode/DP/523_Continuous_Subarray_Sum.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | //ref: https://discuss.leetcode.com/topic/80793/java-o-n-time-o-k-space 3 | public boolean checkSubarraySum(int[] nums, int k) { 4 | HashMap map = new HashMap(); 5 | map.put(0, -1); 6 | int sum = 0; 7 | for(int i = 0; i < nums.length; i++){ 8 | sum += nums[i]; 9 | if(k != 0) sum %= k; 10 | if(map.containsKey(sum)){ 11 | int idx = map.get(sum); 12 | if(i - idx > 1) return true; 13 | }else{ 14 | map.put(sum, i); 15 | } 16 | } 17 | return false; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/DP/560_Subarray_Sum_Equals_K.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int subarraySum(int[] nums, int k) { 3 | HashMap map = new HashMap(); 4 | int sum = 0; 5 | int ret = 0; 6 | map.put(0, 1); 7 | for(int i = 0; i < nums.length; i++){ 8 | sum += nums[i]; 9 | if(map.containsKey(sum - k)){ 10 | ret += map.get(sum - k); 11 | } 12 | map.put(sum, map.getOrDefault(sum, 0) + 1); 13 | } 14 | return ret; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/DataStructureDesign/389_Find_the_Difference.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | //O(n) time, O(1) space 3 | public char findTheDifference(String s, String t) { 4 | int[] table = new int[26]; 5 | for(int i = 0; i < t.length(); i++) 6 | table[t.charAt(i) - 'a']++; 7 | 8 | for(int i = 0; i < s.length(); i++) 9 | table[s.charAt(i) -'a']--; 10 | 11 | for(int i = 0; i < 26; i++){ 12 | if(table[i] == 1) 13 | return (char) (i + 'a'); 14 | } 15 | return 'a'; 16 | } 17 | } -------------------------------------------------------------------------------- /Leetcode/Greedy/122_Best_Time_to_Buy_and_Sell_Stock _II.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(1) time 3 | public int maxProfit(int[] prices) { 4 | if(prices == null || prices.length <= 1) 5 | return 0; 6 | int max = 0; 7 | for(int i = 1; i < prices.length; i++){ 8 | if(prices[i] > prices[i-1]) 9 | max += prices[i] - prices[i-1]; 10 | } 11 | return max; 12 | } 13 | } -------------------------------------------------------------------------------- /Leetcode/Greedy/134_Gas_Station.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) space 3 | public int canCompleteCircuit(int[] gas, int[] cost) { 4 | int diff = 0; 5 | int sum = 0; 6 | int start = 0; 7 | int total = 0; 8 | for(int i = 0; i < gas.length; i++){ 9 | diff = gas[i] - cost[i]; 10 | sum += diff; 11 | total += diff; 12 | if(sum < 0){ 13 | sum = 0; 14 | start = i+1; 15 | } 16 | } 17 | if(total < 0) 18 | return -1; 19 | return start; 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/Leetcode_todo.pages/Data/Hardcover_bullet_black-13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Leetcode/Leetcode_todo.pages/Data/Hardcover_bullet_black-13.png -------------------------------------------------------------------------------- /Leetcode/Leetcode_todo.pages/Index.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Leetcode/Leetcode_todo.pages/Index.zip -------------------------------------------------------------------------------- /Leetcode/Leetcode_todo.pages/Metadata/BuildVersionHistory.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Template: 00_Note-taking (2014-02-28 10:36) 6 | M5.2.2-1877-1 7 | 8 | 9 | -------------------------------------------------------------------------------- /Leetcode/Leetcode_todo.pages/Metadata/DocumentIdentifier: -------------------------------------------------------------------------------- 1 | 3DBADCC3-AB73-4EA8-9F1C-E8DC6900732D -------------------------------------------------------------------------------- /Leetcode/Leetcode_todo.pages/Metadata/Properties.plist: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Leetcode/Leetcode_todo.pages/Metadata/Properties.plist -------------------------------------------------------------------------------- /Leetcode/Leetcode_todo.pages/preview-micro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Leetcode/Leetcode_todo.pages/preview-micro.jpg -------------------------------------------------------------------------------- /Leetcode/Leetcode_todo.pages/preview-web.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Leetcode/Leetcode_todo.pages/preview-web.jpg -------------------------------------------------------------------------------- /Leetcode/Leetcode_todo.pages/preview.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Leetcode/Leetcode_todo.pages/preview.jpg -------------------------------------------------------------------------------- /Leetcode/LinkedList/019_Remove_Nth_Node_From_End_of_List.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // Two pointer: 3 | // O(1) space, O(n) time 4 | public ListNode removeNthFromEnd(ListNode head, int n) { 5 | ListNode dummy = new ListNode(-1); 6 | dummy.next = head; 7 | ListNode slow = dummy; 8 | ListNode fast = dummy; 9 | for(int i = 0; i <= n; i++){ 10 | fast = fast.next; 11 | } 12 | while(fast != null){ 13 | fast = fast.next; 14 | slow = slow.next; 15 | } 16 | slow.next = slow.next.next; 17 | return dummy.next; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/LinkedList/083_Remove_Duplicates_from_Sorted_List.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n) time 3 | public ListNode deleteDuplicates(ListNode head) { 4 | if(head == null || head.next == null) 5 | return head; 6 | ListNode dummy = new ListNode(-1); 7 | dummy.next = head; 8 | ListNode cur = head; 9 | ListNode next = cur.next; 10 | 11 | while(next != null){ 12 | if(next.val == cur.val){ 13 | next = next.next; 14 | }else{ 15 | cur.next = next; 16 | cur = cur.next; 17 | next = next.next; 18 | } 19 | } 20 | cur.next = next; 21 | return dummy.next; 22 | } 23 | } -------------------------------------------------------------------------------- /Leetcode/LinkedList/141_Linked_List_Cycle.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n) time 3 | public boolean hasCycle(ListNode head) { 4 | if(head == null || head.next == null) 5 | return false; 6 | ListNode slow = head; 7 | ListNode fast = head; 8 | while(fast != null){ 9 | slow = slow.next; 10 | fast = fast.next == null ? null : fast.next.next; 11 | if(slow.equals(fast)) 12 | return true; 13 | } 14 | return false; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/LinkedList/147_Insertion_Sort_List.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n^2) time 3 | public ListNode insertionSortList(ListNode head) { 4 | if(head == null || head.next == null) 5 | return head; 6 | ListNode dummy = new ListNode(-1); 7 | ListNode pre = dummy; 8 | ListNode cur = head; 9 | 10 | while(cur != null){ 11 | ListNode next = cur.next; 12 | pre = dummy; 13 | while(pre.next != null && pre.next.val <= cur.val) 14 | pre = pre.next; 15 | cur.next = pre.next; 16 | pre.next = cur; 17 | cur = next; 18 | } 19 | return dummy.next; 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/LinkedList/203_Remove_Linked_List_Elements.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n) time 3 | public ListNode removeElements(ListNode head, int val) { 4 | if(head == null) return null; 5 | ListNode dummy = new ListNode(-1); 6 | ListNode cur = dummy; 7 | while(head != null) { 8 | if(head.val == val) { 9 | head = head.next; 10 | } else { 11 | cur.next = head; 12 | cur = cur.next; 13 | head = head.next; 14 | } 15 | } 16 | cur.next = head; 17 | return dummy.next; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/LinkedList/237_Delete_Node_in_a_Linked_List.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(1) time 3 | public void deleteNode(ListNode node) { 4 | if(node == null || node.next == null) 5 | return; 6 | node.val = node.next.val; 7 | node.next = node.next.next; 8 | } 9 | } -------------------------------------------------------------------------------- /Leetcode/LinkedList/328_Odd_Even_Linked_List.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public ListNode oddEvenList(ListNode head) { 3 | if(head == null || head.next == null) 4 | return head; 5 | ListNode odd = head; 6 | ListNode oddHead = odd; 7 | ListNode even = head.next; 8 | ListNode evenHead = even; 9 | while(even != null && even.next != null){ 10 | odd.next = even.next; 11 | odd = odd.next; 12 | even.next = odd.next; 13 | even = even.next; 14 | } 15 | odd.next = evenHead; 16 | return oddHead; 17 | } -------------------------------------------------------------------------------- /Leetcode/LinkedList/817_Linked_List_Components.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numComponents(ListNode head, int[] G) { 3 | Set set = new HashSet<>(); 4 | for (int n : G){ 5 | set.add(n); 6 | } 7 | ListNode cur = head; 8 | boolean preMatch = false; 9 | int num = 0; 10 | while(cur != null){ 11 | if(set.contains(cur.val)){ 12 | if(preMatch == false){ 13 | num++; 14 | preMatch = true; 15 | } 16 | cur = cur.next; 17 | }else{ 18 | preMatch = false; 19 | cur = cur.next; 20 | } 21 | } 22 | 23 | return num; 24 | } 25 | } -------------------------------------------------------------------------------- /Leetcode/LinkedList/876_Middle_of_the_Linked_List.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // O(1) space, O(n) time 3 | public ListNode middleNode(ListNode head) { 4 | ListNode slow = head, fast = head; 5 | while(fast != null && fast.next != null){ 6 | slow = slow.next; 7 | fast = fast.next.next; 8 | } 9 | return slow; 10 | } 11 | } -------------------------------------------------------------------------------- /Leetcode/Math/007_Reverse_Integer.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(1) time (integer length/size is constant) 3 | public int reverse(int x) { 4 | if(x == 0) 5 | return 0; 6 | int r = 0; 7 | while(x != 0){ 8 | if(r > Integer.MAX_VALUE/10 || r < Integer.MIN_VALUE/10) 9 | return 0; 10 | r = r * 10 + x % 10; 11 | x /= 10; 12 | } 13 | return r; 14 | } 15 | } -------------------------------------------------------------------------------- /Leetcode/Math/009_Palindrome_Number.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(1) time (integer length/size is constant) 3 | public boolean isPalindrome(int x) { 4 | if(x < 0) return false; 5 | int probe = 1; 6 | while(probe <= x/10){ 7 | probe *= 10; 8 | } 9 | while(x > 0){ 10 | int lowest = x % 10; 11 | int highest = x / probe; 12 | if(lowest != highest) return false; 13 | x = (x - highest * probe)/10; 14 | probe /= 100; 15 | } 16 | return true; 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/Math/168_Excel_Sheet_Column_Title.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) space, O(log26n) time(26为底,n的对数) 3 | public String convertToTitle(int n) { 4 | StringBuilder ret = new StringBuilder(); 5 | if(n < 1) return ret.toString(); 6 | while(n > 0){ 7 | n--; 8 | char c = (char)((n % 26) + 'A'); 9 | ret.insert(0, c); 10 | n /= 26; 11 | } 12 | return ret.toString(); 13 | } 14 | } -------------------------------------------------------------------------------- /Leetcode/Math/171_Excel_Sheet_Column_Number.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n) time 3 | public int titleToNumber(String s) { 4 | int ret = 0; 5 | for(int i = 0; i < s.length(); i++){ 6 | char ch = s.charAt(i); 7 | ret = ret*26 + (ch - 'A' + 1); 8 | } 9 | return ret; 10 | } 11 | } -------------------------------------------------------------------------------- /Leetcode/Math/172_Factorial_Trailing_Zeroes.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(log5n) time 3 | public int trailingZeroes(int n) { 4 | int ret = 0; 5 | while(n > 0){ 6 | n = n / 5; 7 | ret += n; 8 | } 9 | return ret; 10 | } 11 | } -------------------------------------------------------------------------------- /Leetcode/Math/223_Rectangle_Area.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(1) time 3 | public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { 4 | int area = (C - A) * (D - B) + (G - E) * (H - F); 5 | // no overlap: 6 | // (abcd在efgh左边|| abcd在efgh右边) || (abcd在efgh上边||abcd在efgh下边) 7 | if((C < E || G < A) || (B > H || F > D)) 8 | return area; 9 | int left = Math.max(E, A); 10 | int right = Math.min(C, G); 11 | int top = Math.min(D, H); 12 | int bottom = Math.max(B, F); 13 | return area - (right - left) * (top - bottom); 14 | } 15 | } -------------------------------------------------------------------------------- /Leetcode/Math/233_Number_of_Digit_One.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int countDigitOne(int n) { 3 | int ones = 0; 4 | long left, right, pos = 1; 5 | if(n < 1) return 0; 6 | while(n >= pos){ 7 | left = n / pos; 8 | right = n % pos; 9 | long remain = left % 10; 10 | if(remain == 1) 11 | ones += (left/10) * pos + right + 1; 12 | else if(remain > 1) 13 | ones += (left/10 + 1) * pos; 14 | else 15 | ones += (left/10) * pos; 16 | pos *= 10; 17 | } 18 | return ones; 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/Math/258_Add_Digits.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // while loop solution 3 | // O(1) space O(log num) time 4 | // 10 5 | public int addDigits(int num) { 6 | while(num > 9) 7 | num = num/10 + num % 10; 8 | return num; 9 | } 10 | 11 | //Recursion solution 12 | public int addDigits(int num) { 13 | if(num <= 9) return num; 14 | return addDigits(num/10 + num%10); 15 | } 16 | 17 | // One line solution 18 | public int addDigits(int num) { 19 | return num - ((num-1)/9)*9; 20 | } 21 | 22 | public int addDigits(int num) { 23 | return (num-1)%9 + 1; 24 | } 25 | } -------------------------------------------------------------------------------- /Leetcode/Math/292_Nim_Game.java: -------------------------------------------------------------------------------- 1 | // Do not touch multiply of 4 2 | public class Solution { 3 | public boolean canWinNim(int n) { 4 | return n % 4 != 0; 5 | } 6 | } -------------------------------------------------------------------------------- /Leetcode/Math/342_Power_of_Four.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // General solution 3 | public boolean isPowerOfFour(int num) { 4 | if(num % 2 != 0) return num == 1; 5 | if(num % 4 != 0) return num == 1; 6 | while(num > 1){ 7 | if(num % 4 != 0) return false; 8 | num /= 4; 9 | } 10 | return num == 1; 11 | } 12 | // Cheating solution 13 | public boolean isPowerOfFour(int num) { 14 | return (num > 0) && ((num & (num - 1)) == 0) && ((num & 0x55555555) == num); 15 | } 16 | // Use radix: 17 | // 4 -> 0x10, 16 -> 0x100, 64 -> 0x1000 18 | public boolean isPowerOfFour(int num) { 19 | return Integer.toString(num, 4).matches("10*"); 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/Math/412_Fizz_Buzz.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public List fizzBuzz(int n) { 3 | List ret = new LinkedList(); 4 | if(n < 1) return ret; 5 | for(int i = 1; i <= n; i++){ 6 | if(i % 15 == 0){ 7 | ret.add("FizzBuzz"); 8 | }else if(i % 3 == 0){ 9 | ret.add("Fizz"); 10 | }else if(i % 5 == 0){ 11 | ret.add("Buzz"); 12 | }else{ 13 | ret.add(String.valueOf(i)); 14 | } 15 | } 16 | return ret; 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/String/005_Longest_Palindromic_Substring.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLongestSubstring(string s) { 4 | if(s.size() == 0) return 0; 5 | int m[256]; 6 | memset(m, -1, sizeof(m)); 7 | int start = 0, cur = 0, max_len = 0; 8 | 9 | while(cur < s.size()){ 10 | int ch = s[cur]; 11 | if(m[ch] >= start) 12 | start = m[ch] + 1; 13 | max_len = max(max_len, cur - start + 1); 14 | m[ch] = cur; 15 | cur++; 16 | } 17 | return max_len; 18 | } 19 | }; -------------------------------------------------------------------------------- /Leetcode/String/006_ZigZag_Conversion.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public String convert(String s, int numRows) { 3 | if(s == null || s.length() == 0 || numRows <= 0) 4 | return ""; 5 | if(numRows == 1) return s; 6 | 7 | String ret = ""; 8 | int size = 2 * numRows - 2; //k + (k -2) 9 | for(int i = 0; i < numRows; i++){ 10 | for(int j = i; j < s.length(); j += size){ 11 | ret += s.charAt(j); 12 | if(i != 0 && i != numRows - 1){ 13 | int temp = j + size - 2 * i; 14 | if(temp < s.length()) 15 | ret += s.charAt(temp); 16 | } 17 | } 18 | } 19 | return ret; 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/String/012_Integer_to_Roman.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n) time 3 | public String intToRoman(int num) { 4 | String[] table = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; 5 | int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; 6 | StringBuilder ret = new StringBuilder(); 7 | for(int i = 0; i < values.length; i++){ 8 | while(num >= values[i]){ 9 | ret.append(table[i]); 10 | num -= values[i]; 11 | } 12 | } 13 | return ret.toString(); 14 | } 15 | } -------------------------------------------------------------------------------- /Leetcode/String/014_Longest_Common_Prefix.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // 3 | public String longestCommonPrefix(String[] strs) { 4 | int num = strs.length; 5 | String ret = ""; 6 | if(num == 0 || strs.length == 0) 7 | return ret; 8 | for(int i = 0; i < strs[0].length(); i++){ 9 | for(int j = 0; j < num; j++){ 10 | if(strs[j].length() <= i || strs[j].charAt(i) != strs[0].charAt(i)) 11 | return strs[0].substring(0,i); 12 | } 13 | } 14 | return strs[0]; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/String/058_Length_of_Last_Word.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n) time 3 | public int lengthOfLastWord(String s) { 4 | if(s == null || s.length() == 0) 5 | return 0; 6 | 7 | int len = 0; 8 | for(int i = s.length() - 1; i >= 0; i--){ 9 | if(s.charAt(i) != ' ') len++; 10 | if(s.charAt(i) == ' ' && len != 0) 11 | return len; 12 | } 13 | return len; 14 | } 15 | } -------------------------------------------------------------------------------- /Leetcode/String/067_Add_Binary.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space, O(n) time 3 | public String addBinary(String a, String b) { 4 | int alen = a.length(); 5 | int blen = b.length(); 6 | int len = Math.max(alen, blen); 7 | int carry = 0; 8 | 9 | String ret = ""; 10 | for(int i = 0; i < len; i++){ 11 | int v1 = i < alen ? a.charAt(alen -1 - i) - '0' : 0; 12 | int v2 = i < blen ? b.charAt(blen -1 - i) - '0' : 0; 13 | int digit = (carry + v1 + v2) % 2; 14 | carry = (carry + v1 + v2)/ 2; 15 | ret = digit + ret; 16 | } 17 | if(carry == 1) 18 | ret = 1 + ret; 19 | return ret; 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/String/266_Palindrome_Permutation.java: -------------------------------------------------------------------------------- 1 | /* 题意: 2 | * 给一个String s, 其permutation能否构成一个Palindrome 3 | * E.g.: 4 | * "code" -> False, "aab" -> True, "carerac" -> True. 5 | * 6 | * solution: 7 | * 最多一个字符出现奇数次 8 | */ 9 | 10 | public class Solution { 11 | public boolean canPermutePalindrome(String s) { 12 | int[] table = new int[256]; 13 | for(int i = 0; i < s.length(); i++){ 14 | char ch = s.charAt(i); 15 | table[ch] = table[ch] > 0 ? --table[ch] : ++table[ch]; 16 | } 17 | int cnt = 0; 18 | for(int i = 0; i < 256; i++){ 19 | if(table[i] != 0) cnt++; 20 | } 21 | return cnt <= 1; 22 | } 23 | } -------------------------------------------------------------------------------- /Leetcode/String/299_Bulls_and_Cows.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String getHint(String secret, String guess) { 3 | int[] s = new int[10]; 4 | int[] g = new int[10]; 5 | int bulls = 0; 6 | for(int i = 0; i < secret.length(); i++){ 7 | char a = secret.charAt(i); 8 | char b = guess.charAt(i); 9 | if(a == b){ 10 | bulls++; 11 | }else{ 12 | s[a - '0']++; 13 | g[b - '0']++; 14 | } 15 | } 16 | int cows = 0; 17 | for(int i = 0; i < 10; i++) 18 | cows += Math.min(s[i], g[i]); 19 | 20 | return bulls+"A"+cows+"B"; 21 | } 22 | } -------------------------------------------------------------------------------- /Leetcode/String/344_Reverse_String.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public String reverseString(String s) { 3 | char[] arr = s.toCharArray(); 4 | int i = 0, j = arr.length - 1; 5 | while(i < j){ 6 | char c = arr[i]; 7 | arr[i++] = arr[j]; 8 | arr[j--] = c; 9 | } 10 | return new String(arr); 11 | } 12 | } -------------------------------------------------------------------------------- /Leetcode/String/383_Ransom_Note.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean canConstruct(String ransomNote, String magazine) { 3 | int[] dict = new int[26]; 4 | for(int i = 0; i < ransomNote.length(); i++) 5 | dict[ransomNote.charAt(i) - 'a']++; 6 | 7 | for(int i = 0; i < magazine.length(); i++){ 8 | dict[magazine.charAt(i) - 'a']--; 9 | } 10 | for(int i = 0; i < 26; i++){ 11 | if(dict[i] > 0) 12 | return false; 13 | } 14 | return true; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/String/392_Is_subsequence.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // Two pointer: 3 | // O(n) time, O(1) space 4 | public boolean isSubsequence(String s, String t) { 5 | if(s == null || s.length() == 0) return true; 6 | if(t == null || t.length() == 0) return false; 7 | int i = 0, j = 0; 8 | while(j < t.length()){ 9 | if(t.charAt(j) == s.charAt(i)){ 10 | i++; 11 | if(i == s.length()) return true; 12 | } 13 | j++; 14 | } 15 | return false; 16 | } 17 | } -------------------------------------------------------------------------------- /Leetcode/String/409_Longest_Palindrome.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) space, O(n) time 3 | public int longestPalindrome(String s) { 4 | if(s == null || s.length() == 0) return 0; 5 | 6 | Set set = new HashSet(); 7 | int count = 0; 8 | for(int i = 0; i < s.length(); i++){ 9 | if(set.contains(s.charAt(i))){ 10 | set.remove(s.charAt(i)); 11 | count++; 12 | }else{ 13 | set.add(s.charAt(i)); 14 | } 15 | } 16 | if(!set.isEmpty()) return count * 2 + 1; 17 | return count * 2; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/String/415_Add_Strings.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) time, O(1) space 3 | public String addStrings(String num1, String num2) { 4 | String sum = ""; 5 | int i = num1.length() - 1, j = num2.length() - 1; 6 | int carry = 0; 7 | while(i >= 0 || j >= 0){ 8 | int v1 = i >= 0 ? num1.charAt(i--) - '0' : 0; 9 | int v2 = j >= 0 ? num2.charAt(j--) - '0' : 0; 10 | int cur = v1 + v2 + carry; 11 | int digit = cur % 10; 12 | carry = cur / 10; 13 | sum = digit + sum; 14 | } 15 | if(carry == 1) sum = "1" + sum; 16 | return sum; 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/String/422_Valid_Word_Square.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean validWordSquare(List words) { 3 | if(words == null || words.size() == 0) 4 | return false; 5 | int n = words.size(); 6 | 7 | for(int i = 0; i < n; i++){ 8 | for(int j = 0; j < words.get(i).length(); j++){ 9 | if(j >= n || i >= words.get(j).length() || words.get(i).charAt(j) != words.get(j).charAt(i)) 10 | return false; 11 | } 12 | } 13 | return true; 14 | } 15 | } -------------------------------------------------------------------------------- /Leetcode/String/424_Longest_Repeating_Character_Replacement.java: -------------------------------------------------------------------------------- 1 | // sliding window 2 | class Solution { 3 | public int characterReplacement(String s, int k) { 4 | int[] table = new int[26]; 5 | int end = 0, start = 0; 6 | int len = 0; 7 | int maxCount = 0; 8 | while(end < s.length()){ 9 | char c = s.charAt(end); 10 | maxCount = Math.max(maxCount, ++table[c - 'A']); 11 | end++; 12 | while(end - start - maxCount > k){ 13 | char a = s.charAt(start); 14 | table[a - 'A']--; 15 | start++; 16 | } 17 | len = Math.max(len, end - start); 18 | } 19 | return len; 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/String/434_Number_of_Segments_in_a_String.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int countSegments(String s) { 3 | int cnt = 0; 4 | for(int i = 0; i < s.length(); i++) 5 | if(s.charAt(i) != ' ' && (i == 0 || s.charAt(i-1) == ' ')) 6 | cnt++; 7 | return cnt; 8 | } 9 | } -------------------------------------------------------------------------------- /Leetcode/String/443_String_Compression.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int compress(char[] chars) { 3 | if(chars.length < 2) return chars.length; 4 | int idx = 0; 5 | int start = 0; 6 | for(int i = 0; i <= chars.length; i++){ 7 | if(i != chars.length && chars[i] == chars[start]) 8 | continue; 9 | int cnt = i - start; 10 | chars[idx++] = chars[start]; 11 | start = i; 12 | if(cnt == 1) 13 | continue; 14 | char[] num = String.valueOf(cnt).toCharArray(); 15 | for(char n : num) 16 | chars[idx++] = n; 17 | } 18 | return idx; 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/String/520_Detect_Capital.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean detectCapitalUse(String word) { 3 | int cnt = 0; 4 | for(char c: word.toCharArray()) if('Z' - c >= 0) cnt++; 5 | return ((cnt==0 || cnt==word.length()) || (cnt==1 && 'Z' - word.charAt(0)>=0)); 6 | } 7 | } -------------------------------------------------------------------------------- /Leetcode/String/521_Longest_Uncommon_Subsequence_I.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int findLUSlength(String a, String b) { 3 | return a.equals(b) ? -1 : Math.max(a.length(), b.length()); 4 | } 5 | } -------------------------------------------------------------------------------- /Leetcode/String/541_Reverse_String_II.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public String reverseStr(String s, int k) { 3 | char[] arr = s.toCharArray(); 4 | int i = 0, len = arr.length; 5 | while(i < len){ 6 | int j = Math.min(i + k - 1, len - 1); 7 | reverse(arr, i, j); 8 | i = i + 2 * k; 9 | } 10 | return new String(arr); 11 | } 12 | 13 | public void reverse(char[] arr, int x, int y){ 14 | while(x < y){ 15 | char c = arr[x]; 16 | arr[x++] = arr[y]; 17 | arr[y--] = c; 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/String/544_Output_Contest_Matches.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public String findContestMatch(int n) { 3 | String[] m = new String[n]; 4 | 5 | for(int i = 1; i <= n; i++) 6 | m[i-1] = String.valueOf(i); 7 | 8 | while(n > 1){ 9 | for(int i = 0; i < n/2; i++){ 10 | m[i] = "(" + m[i] + "," + m[n - 1 - i] + ")"; 11 | } 12 | n /= 2; 13 | } 14 | return m[0]; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/String/551_Student_Attendance_Record_I.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean checkRecord(String s) { 3 | int absentCnt = 0; 4 | for(int i = 0; i < s.length(); i++){ 5 | if(i > 1 && s.charAt(i) == 'L' && s.charAt(i - 1) == 'L' && s.charAt(i - 2) =='L') 6 | return false; 7 | if(s.charAt(i) == 'A') 8 | absentCnt++; 9 | } 10 | if(absentCnt > 1) return false; 11 | return true; 12 | } 13 | // Radix 14 | public boolean checkRecord(String s) { 15 | return !s.matches(".*LLL.*|.*A.*A.*"); 16 | } 17 | } -------------------------------------------------------------------------------- /Leetcode/String/553_Optimal_Division.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public String optimalDivision(int[] nums) { 3 | StringBuilder sb = new StringBuilder(); 4 | sb.append(nums[0]); 5 | for (int i = 1; i < nums.length; i++) { 6 | if (i == 1 && nums.length > 2) { 7 | sb.append("/(").append(nums[i]); 8 | } else { 9 | sb.append("/").append(nums[i]); 10 | } 11 | } 12 | return nums.length > 2 ? sb.append(")").toString() : sb.toString(); 13 | } 14 | } -------------------------------------------------------------------------------- /Leetcode/String/636_Exclusive_Time_of_Functions.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] exclusiveTime(int n, List logs) { 3 | int[] res = new int[n]; 4 | Stack stack = new Stack<>(); 5 | for(String str : logs){ 6 | String[] s = str.split(":"); 7 | int time = Integer.parseInt(s[2]); 8 | int id = Integer.parseInt(s[0]); 9 | if(s[1].equals("start")){ 10 | stack.push(new int[]{id, time}); 11 | }else{ 12 | int exeTime = time - stack.pop()[1] + 1; 13 | res[id] += exeTime; 14 | if(!stack.isEmpty()) 15 | res[stack.peek()[0]] -= exeTime; 16 | } 17 | } 18 | return res; 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/String/647_Palindromic_Substrings.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int cnt = 0; 3 | public int countSubstrings(String s) { 4 | if(s == null || s.length() == 0) 5 | return cnt; 6 | for(int i = 0; i < s.length(); i++){ 7 | extend(s, i, i); 8 | extend(s, i, i + 1); 9 | } 10 | return cnt; 11 | } 12 | 13 | private void extend(String s, int i, int j){ 14 | while(i >= 0 && j < s.length() && s.charAt(i) == s.charAt(j)){ 15 | cnt++; 16 | i--; 17 | j++; 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/String/844_Backspace_String_Compare.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean backspaceCompare(String S, String T) { 3 | if(S == null) return T == null; 4 | if(T == null) return false; 5 | return compute(S).equals(compute(T)); 6 | } 7 | 8 | private String compute(String s){ 9 | StringBuilder sb = new StringBuilder(); 10 | for(int i = 0; i < s.length(); i++){ 11 | char c = s.charAt(i); 12 | if(c == '#'){ 13 | if(sb.length() > 0) sb.deleteCharAt(sb.length() - 1); 14 | }else{ 15 | sb.append(c); 16 | } 17 | } 18 | return sb.toString(); 19 | } 20 | } -------------------------------------------------------------------------------- /Leetcode/String/893_Groups_of_Special_Equivalent_Strings.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numSpecialEquivGroups(String[] A) { 3 | Set set = new HashSet<>(); 4 | 5 | for(String str: A){ 6 | int[] odd = new int[256]; 7 | int[] even = new int[256]; 8 | for(int i = 0; i < str.length(); i++){ 9 | char c = str.charAt(i); 10 | if(i % 2 == 0){ 11 | even[c - 'a']++; 12 | }else{ 13 | odd[c - 'a']++; 14 | } 15 | } 16 | String unique = Arrays.toString(even) + " " + Arrays.toString(odd); 17 | set.add(unique); 18 | } 19 | 20 | return set.size(); 21 | } 22 | } -------------------------------------------------------------------------------- /Leetcode/Tree/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Leetcode/Tree/.DS_Store -------------------------------------------------------------------------------- /Leetcode/Tree/108_Convert_Sorted_Array_to_Binary_Search_Tree.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space OR O(logn) space if consider recursion stack 3 | // O(n) time 4 | public TreeNode sortedArrayToBST(int[] nums) { 5 | if(nums == null || nums.length == 0) 6 | return null; 7 | return BuildTree(nums, 0, nums.length - 1); 8 | } 9 | 10 | private TreeNode BuildTree(int[] nums, int start, int end){ 11 | if(start > end) 12 | return null; 13 | int mid = start + (end - start)/2; 14 | TreeNode root = new TreeNode(nums[mid]); 15 | root.left = BuildTree(nums, start, mid - 1); 16 | root.right = BuildTree(nums, mid + 1, end); 17 | return root; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/Tree/129_Sum_Root_to_Leaf_Numbers.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(1) space OR O(logn) space if consider recursion stack 3 | // O(n) time 4 | public int sumNumbers(TreeNode root) { 5 | if(root == null) return 0; 6 | return sum(root, 0); 7 | } 8 | 9 | private int sum(TreeNode root, int sum){ 10 | if(root == null) return 0; 11 | int cur = sum * 10 + root.val; 12 | if(root.left == null && root.right == null){ 13 | return cur; 14 | } 15 | return sum(root.left, cur) + sum(root.right, cur); 16 | } 17 | } -------------------------------------------------------------------------------- /Leetcode/Tree/236_Lowest_Common_Ancestor_of_a_Binary_Tree.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // Bottom-Up Solution: 3 | // O(1) space OR O(logn) space if consider recursion stack 4 | // O(n) time 5 | public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { 6 | if(root == null || root == p || root == q) 7 | return root; 8 | TreeNode left = lowestCommonAncestor(root.left, p, q); 9 | TreeNode right = lowestCommonAncestor(root.right, p, q); 10 | if(left != null && right != null) 11 | return root; 12 | return left != null ? left : right; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Leetcode/Tree/255_Verify_Preorder_Sequence_in_Binary_Search_Tree.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // O(n) space, O(n) time 3 | public boolean verifyPreorder(int[] preorder) { 4 | int p = Integer.MIN_VALUE; 5 | Stack stack = new Stack(); 6 | for(int cur : preorder){ 7 | if(cur < p) return false; 8 | while(!stack.isEmpty() && stack.peek() < cur) 9 | p = stack.pop(); 10 | stack.push(cur); 11 | } 12 | return true; 13 | } 14 | // TODO: O(1) space, O(n) time solution 15 | } -------------------------------------------------------------------------------- /Leetcode/Tree/257_Binary_Tree_Paths.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public List binaryTreePaths(TreeNode root) { 3 | List ret = new LinkedList(); 4 | rec(root, "", ret); 5 | return ret; 6 | } 7 | 8 | private void rec(TreeNode root, String path, List ret){ 9 | if(root == null) return; 10 | if(root.left == null && root.right == null){ 11 | path += String.valueOf(root.val); 12 | ret.add(path); 13 | return; 14 | } 15 | path += String.valueOf(root.val) + "->"; 16 | rec(root.left, path, ret); 17 | rec(root.right, path, ret); 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/Tree/437_Path_Sum_III.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // Top-down: 3 | // O(nlogn) if balanced tree, or O(n^2) if linkedlist 4 | // O(1) space or O(n^2) space if consider recursion stack 5 | public int pathSum(TreeNode root, int sum) { 6 | if(root == null) return 0; 7 | return dfs(root,sum) + pathSum(root.left, sum) + pathSum(root.right, sum); 8 | } 9 | public int dfs(TreeNode root, int sum){ 10 | if(root == null) return 0; 11 | int cnt = 0; 12 | if(root.val == sum) cnt++; 13 | return cnt + dfs(root.left, sum - root.val) + dfs(root.right, sum - root.val); 14 | } 15 | } -------------------------------------------------------------------------------- /Leetcode/Tree/543_Diameter_of_Binary_Tree.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int max = 0; 3 | public int diameterOfBinaryTree(TreeNode root) { 4 | dfs(root); 5 | return max; 6 | } 7 | 8 | private int dfs(TreeNode root){ 9 | if(root == null) return 0; 10 | int left = dfs(root.left); 11 | int right = dfs(root.right); 12 | int pathMax = Math.max(left, right) + 1; 13 | max = Math.max(max, left + right); 14 | return pathMax; 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/Tree/563_Binary_Tree_Tilt.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | int tilt = 0; 3 | public int findTilt(TreeNode root) { 4 | postOrder(root); 5 | return tilt; 6 | } 7 | 8 | public int postOrder(TreeNode root){ 9 | if(root == null) return 0; 10 | int l = postOrder(root.left); 11 | int r = postOrder(root.right); 12 | tilt += Math.abs(l - r); 13 | return l + r + root.val; 14 | } 15 | } -------------------------------------------------------------------------------- /Leetcode/Tree/572_Subtree_of_Another_Tree.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean isSubtree(TreeNode s, TreeNode t) { 3 | if(s == null) return t == null; 4 | if(t == null) return false; 5 | if(isSame(s, t)) return true; 6 | if(isSubtree(s.left, t) || isSubtree(s.right, t)) 7 | return true; 8 | return false; 9 | } 10 | 11 | public boolean isSame(TreeNode s, TreeNode t){ 12 | if(s == null || t == null) return s == t; 13 | if(s.val != t.val) return false; 14 | return isSame(s.left, t.left) && isSame(s.right, t.right); 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode/Tree/606_Construct_String_from_Binary_Tree.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String tree2str(TreeNode t) { 3 | if (t == null) return ""; 4 | StringBuilder sb = new StringBuilder(); 5 | construct(t, sb); 6 | sb.deleteCharAt(0); 7 | sb.deleteCharAt(sb.length() - 1); 8 | return sb.toString(); 9 | } 10 | 11 | private void construct(TreeNode root, StringBuilder sb){ 12 | if(root == null) return; 13 | sb.append("("); 14 | sb.append(root.val); 15 | 16 | if(root.left == null && root.right != null) 17 | sb.append("()"); 18 | construct(root.left, sb); 19 | construct(root.right, sb); 20 | sb.append(")"); 21 | } 22 | } -------------------------------------------------------------------------------- /Leetcode/Tree/653_Two_Sum_IV_Input_is_a_BST.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | boolean find = false; 3 | public boolean findTarget(TreeNode root, int k) { 4 | HashSet set = new HashSet<>(); 5 | inorder(root, k, set); 6 | return find; 7 | } 8 | 9 | private void inorder(TreeNode root, int k, HashSet set){ 10 | if(root == null) return; 11 | if(!find){ 12 | inorder(root.left, k, set); 13 | if(set.contains(k - root.val)){ 14 | find = true; 15 | return; 16 | } 17 | set.add(root.val); 18 | inorder(root.right, k, set); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode/Tree/687_Longest_Univalue_Path.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestUnivaluePath(TreeNode root) { 3 | if(root == null) return 0; 4 | int[] max = {0}; 5 | dfs(root.val, root, max); 6 | return max[0]; 7 | } 8 | 9 | private int dfs(int parentValue, TreeNode root, int[] max){ 10 | if(root == null) return 0; 11 | int left = dfs(root.val, root.left, max); 12 | int right = dfs(root.val, root.right, max); 13 | left = root.left != null && root.val == root.left.val ? left + 1 : 0; 14 | right = root.right != null && root.val == root.right.val ? right + 1 : 0; 15 | max[0] = Math.max(max[0], left + right); 16 | return Math.max(left, right); 17 | } 18 | } -------------------------------------------------------------------------------- /Leetcode/Tree/701_Insert_into_a_Binary_Search_Tree.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public TreeNode insertIntoBST(TreeNode root, int val) { 3 | if(root == null) return root; 4 | TreeNode cur = root; 5 | TreeNode p = null; 6 | while(cur != null){ 7 | TreeNode tmp = cur; 8 | if(cur.val > val) cur = cur.left; 9 | else if(cur.val < val) cur = cur.right; 10 | p = tmp; 11 | } 12 | if(p.val < val){ 13 | p.right = new TreeNode(val); 14 | }else{ 15 | p.left = new TreeNode(val); 16 | } 17 | return root; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/Tree/814_Binary_Tree_Pruning.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public TreeNode pruneTree(TreeNode root) { 3 | if(root == null) return null; 4 | root.left = pruneTree(root.left); 5 | root.right = pruneTree(root.right); 6 | if(root.val == 0 && root.left == null && root.right == null) 7 | return null; 8 | return root; 9 | } 10 | } -------------------------------------------------------------------------------- /Lintcode/Fibonacci.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | /** 3 | * @param n: an integer 4 | * @return an integer f(n) 5 | */ 6 | public int fibonacci(int n) { 7 | // write your code here 8 | if(n == 1) return 0; 9 | if(n == 2) return 1; 10 | int f1 = 0, f2 = 1; 11 | int fib = 0; 12 | 13 | for(int i = 2; i <= n-1; i++){ 14 | fib = f1 + f2; 15 | f1 = f2; 16 | f2 = fib; 17 | } 18 | return fib; 19 | } 20 | } 21 | 22 | 23 | -------------------------------------------------------------------------------- /Storage/Storage-EXT_2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Storage/Storage-EXT_2.pdf -------------------------------------------------------------------------------- /Storage/Storage-SSD.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Storage/Storage-SSD.pdf -------------------------------------------------------------------------------- /Storage/Storage.pages/Data/Hardcover_bullet_black-13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Storage/Storage.pages/Data/Hardcover_bullet_black-13.png -------------------------------------------------------------------------------- /Storage/Storage.pages/Index.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Storage/Storage.pages/Index.zip -------------------------------------------------------------------------------- /Storage/Storage.pages/Metadata/BuildVersionHistory.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Template: 00_Note-taking (2014-02-28 10:36) 6 | M5.2.2-1877-1 7 | 8 | 9 | -------------------------------------------------------------------------------- /Storage/Storage.pages/Metadata/DocumentIdentifier: -------------------------------------------------------------------------------- 1 | 594FE2A0-5983-4CE2-855C-4799CB82C308 -------------------------------------------------------------------------------- /Storage/Storage.pages/Metadata/Properties.plist: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Storage/Storage.pages/Metadata/Properties.plist -------------------------------------------------------------------------------- /Storage/Storage.pages/preview-micro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Storage/Storage.pages/preview-micro.jpg -------------------------------------------------------------------------------- /Storage/Storage.pages/preview-web.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Storage/Storage.pages/preview-web.jpg -------------------------------------------------------------------------------- /Storage/Storage.pages/preview.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/Storage/Storage.pages/preview.jpg -------------------------------------------------------------------------------- /interview.numbers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Deadbeef-ECE/Interview/a5bee1fad024e26cc09ff1cb1f1b64f999dbb2c7/interview.numbers --------------------------------------------------------------------------------