This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| My initial thought is doing a DFS pass through the tree. Things to take note of: | |
| - I will go through the tree and calculate the total sum of the tree. | |
| - Knowing the sum of a subtree, the 2nd subtree will therefore be total sum - subtree sum. | |
| - Record the sum of every subtree. | |
| - Maximise S * (Totalsum-S) for all S (Subtree) | |
| - Then we can know the max sum by combining subtrees. | |
| ``` | |
| // Definition for a binary tree node. | |
| // #[derive(Debug, PartialEq, Eq)] | |
| // pub struct TreeNode { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| This is a classic tree transversal problem. The type of tree is not significant here. My go to is a level order (breadth first) transversal, either recursively or iteratively. | |
| - Calculate sum | |
| - Compare the sum and return the max | |
| - If 2 levels have the same max sum, return the smallest level number | |
| - The max sum itself can be negative | |
| - Initialize max_sum at neg infinity | |
| ``` | |
| // Definition for a binary tree node. | |
| // #[derive(Debug, PartialEq, Eq)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| My first approach was to go round all at once, in a greedy approach way. But it was figuring the movement of the negative signs around the matrix. | |
| Watching neetcode helped. | |
| Now, we just sum all numbers as absolute. We also keep track of negative numbers on the go | |
| Then at the end, if we check the modulo of the number of negative numbers, and it remains 1, we can then negate the minimum absolute value and double subtract it from the total sum we had earlier. | |
| ``` | |
| use std::cmp; | |
| impl Solution { | |
| pub fn max_matrix_sum(matrix: Vec<Vec<i32>>) -> i64 { | |
| let mut total_sum: i64 = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Initial approach is to follow through with all numbers, ensuring once their divisor is > 4, they are dismissed immediately. | |
| Further check down the line shows we can find the square root of this numbers, and divide from 1 to the √num. If perfect square, we can multiply divisor by 2 then -1 or if not perfect , we can multiply by 2 and add 0. | |
| Some feedbacks received also showed we can use other mathematical derivatives like: | |
| numbers with 4 divisors have unique features: | |
| a. n = p³ (cube of a prime, divisors: 1, p, p², p³) => e.g 8 = 2^3 | |
| Divisors: 1, 2, 4, 8 (which is 1, 2¹, 2², 2³) | |
| b. n = p × q => sum of 2 dinstinct prime numbers. => 21 = 3 × 7 | |
| Divisors: 1, 3, 7, 21 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Dynamic dp problem. I had the base case in mind, but the loop required some mathematics. | |
| Watching this page over and over again did the magic: https://www.youtube.com/watch?v=W-WvItFiWX8 | |
| impl Solution { | |
| pub fn num_of_ways(n: i32) -> i32 { | |
| let modu = 1_000_000_007i64; | |
| if n == 1 { | |
| return 12; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| My initial approach was to keep an hashmap of scores. The [number, no_of_times]. After then, I can loop over the nums array, and add to my hashmap. | |
| Then after, I convert to a Vec of tuples, sort descending, to get the most frequent element first and return it. | |
| ``` | |
| use std::collections::HashMap; | |
| impl Solution { | |
| pub fn repeated_n_times(nums: Vec<i32>) -> i32 { | |
| let mut scores = HashMap::with_capacity(nums.len()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| To handle this leetcode question, I worked on an iterative solution. | |
| - Reversed the initial array (the rust baseline didn't make the array mutable initially) | |
| - Initialized a carry 1 | |
| - Iterated from the least significant digit, now at index 0 | |
| - I kept moving from left to right, adding 1 if > 9 else add 1 to the first digit | |
| - Reversed the array back. | |
| Pretty easy | |
| https://leetcode.com/problems/plus-one/submissions/1870882523 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} height | |
| * @return {number} | |
| */ | |
| var maxArea = function(height) { | |
| let left = 0 | |
| let right = height.length - 1 | |
| let answer = 0 | |
| while(left < right) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} nums | |
| * @return {number[][]} | |
| */ | |
| var subsets = function(nums) { | |
| if(nums.length < 2) return nums | |
| let result = [] | |
| function recurse(i, temp) { |