This file contains 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
impl Solution { | |
pub fn min_falling_path_sum(a: Vec<Vec<i32>>) -> i32 { | |
let mut prev: Vec<i32> = a[0].clone(); | |
let mut curr: Vec<i32> = vec![0; a[0].len()]; | |
for i in 1..a.len() { | |
for j in 0..a[i].len() { | |
let mut sum_so_far = prev[j]; | |
if j > 0 { | |
sum_so_far = std::cmp::min(sum_so_far, prev[j-1]); | |
} |
This file contains 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
impl Solution { | |
pub fn find_min_difference(time_points: Vec<String>) -> i32 { | |
fn to_minutes(time: &str) -> i32 { | |
let parts: Vec<_> = time.split(":").collect(); | |
let hours: i32 = parts[0].parse().unwrap(); | |
let minutes: i32 = parts[1].parse().unwrap(); | |
hours * 60 + minutes | |
} | |
let mut minutes: Vec<_> = time_points.iter().map(|t| to_minutes(&t)).collect(); | |
minutes.sort_unstable(); |
This file contains 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
class Solution { | |
public: | |
bool buddyStrings(const string& A, const string& B) { | |
if (A.size() != B.size()) return false; | |
vector<int> mismatches; | |
bool has_duplicates = false; | |
int letters = 0; | |
for (int i = 0; i < A.size(); ++i) { | |
int idx = 1 << (A[i] - 'a'); | |
has_duplicates |= letters & idx; |
This file contains 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
class Solution { | |
private: | |
struct Result { int diameter; int depth; }; | |
Result maxDiameterAndDepth(const TreeNode* root) const { | |
if (root == nullptr) return {0, 0}; | |
auto leftResult = maxDiameterAndDepth(root->left); | |
auto rightResult = maxDiameterAndDepth(root->right); | |
return { | |
max({leftResult.diameter, rightResult.diameter, leftResult.depth + rightResult.depth}), | |
1 + max(leftResult.depth, rightResult.depth) |
This file contains 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
class Solution: | |
VALID_VALUES = set(map(str, range(1, 10))) | |
def solveSudoku(self, board): | |
""" | |
:type board: List[List[str]] | |
:rtype: void Do not return anything, modify board in-place instead. | |
""" | |
def rows(row): | |
return board[row] |
This file contains 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
class Solution { | |
public: | |
int countNodes(TreeNode* root) { | |
if (root == nullptr) return 0; | |
int left_height = 0; | |
int right_height = 0; | |
for (auto temp = root; temp != nullptr; temp = temp->left) left_height += 1; | |
for (auto temp = root; temp != nullptr; temp = temp->right) right_height += 1; | |
if (left_height == right_height) return pow(2, left_height) - 1; | |
return 1 + countNodes(root->left) + countNodes(root->right); |
This file contains 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
class Solution { | |
public: | |
TreeNode* insertIntoBST(TreeNode* root, int val) { | |
if (root == nullptr) return new TreeNode(val); | |
if (val < root->val) { | |
root->left = insertIntoBST(root->left, val); | |
} else { | |
root->right = insertIntoBST(root->right, val); | |
} | |
return root; |
This file contains 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
class Solution { | |
public: | |
TreeNode* insertIntoBST(TreeNode* root, int val) { | |
if (root == nullptr) return new TreeNode(val); | |
TreeNode* prev = nullptr; | |
for (auto node = root; node != nullptr; ) { | |
prev = node; | |
node = val < node->val ? node->left : node->right; | |
} | |
auto destination = val < prev->val ? &prev->left : &prev->right; |
This file contains 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
class Solution { | |
public: | |
int numDecodings(const string& s) { | |
if (s.empty()) return 0; | |
if (s.size() == 1) return s[0] != '0'; | |
int n_2 = s.back() != '0'; | |
int n_1 = (s[s.size()-2] != '0') * n_2 + (s[s.size()-2] == '1' || s[s.size()-2] == '2' && s.back() <= '6'); | |
for (int i = s.size()-3; i >= 0; --i) { | |
int num = (s[i] > '0') * n_1 + (s[i] == '1' || s[i] == '2' && s[i+1] <= '6') * n_2; | |
n_2 = n_1; |
This file contains 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
class Solution { | |
public: | |
vector<int> majorityElement(vector<int>& nums) { | |
int count1 = 0; | |
int count2 = 0; | |
int candidate1 = -1; | |
int candidate2 = -1; | |
for (int num : nums) { | |
if (num == candidate1) { | |
count1 += 1; |