Skip to content

Instantly share code, notes, and snippets.

@any9527
any9527 / leetcode_924_desc.md
Last active September 25, 2021 17:22
Problem description

You are given a network of n nodes represented as an n x n adjacency matrix graph, where the ith node is directly connected to the jth node if graph[i][j] == 1.

Some nodes initial are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected by malware, both nodes will be infected by malware. This spread of malware will continue until no more nodes can be infected in this manner.

Suppose M(initial) is the final number of nodes infected with malware in the entire network after the spread of malware stops. We will remove exactly one node from initial.

Return the node that, if removed, would minimize M(initial). If multiple nodes could be removed to minimize M(initial), return such a node with the smallest index.

Note that if a node was removed from the initial list of infected nodes, it might still be infected later due to the malware spread.

@any9527
any9527 / leetcode_924_idea.md
Last active September 25, 2021 17:24
Idea for leetcode 924. Minimize Malware Spread
  1. The goal is to find out the node which, if removed from initial, will minimize the number of infected nodes by malware (M(initial)).
  2. So naively we can try to remove each node and find the corresponding M(initial) using DFS or BFS, then we can find the minimum M(initial).
  3. But the naive approach would be of complexity m * O(n^2), m being the length of initial, and n being the number of nodes in the graph.
  4. Can we do better?
  5. If you think about it, for all nodes in the same connected component in initial, if we remove one of them, it does not change M(initial) because the remaining node will affect every other nodes in the same component. (Note the last condition in the problem description says the removed node can be still infected, otherwise this logic won't work)
  6. So if we can find all the nodes in initial that is the only node in its component, then we can compare the sizes of all components. Because the size of each component is the reduced number of infected nodes.
@any9527
any9527 / leetcode_928.md
Last active September 26, 2021 14:36
Leetcode 928 problem description & idea & solution in Javascript

Minimize Malware Spread II

Problem Description

You are given a network of n nodes represented as an n x n adjacency matrix graph, where the ith node is directly connected to the jth node if graph[i][j] == 1.

Some nodes initial are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected by malware, both nodes will be infected by malware. This spread of malware will continue until no more nodes can be infected in this manner.

Suppose M(initial) is the final number of nodes infected with malware in the entire network after the spread of malware stops.

@any9527
any9527 / leetcode_827.md
Last active September 29, 2021 22:06
Leetcode 827: Making a large island

Description

You are given an n x n binary matrix grid. You are allowed to change at most one 0 to be 1.

Return the size of the largest island in grid after applying this operation.

An island is a 4-directionally connected group of 1s.

Idea

  1. The idea is pretty straight-forward.
@any9527
any9527 / leetcode_224.md
Last active September 30, 2021 11:52
Leetcode 224: Basic Calculator

Description

Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation.

Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval().

Example 1:

Input: s = "1 + 1"
Output: 2
@any9527
any9527 / leetcode_301.md
Created October 1, 2021 03:29
Leetcode 301: Remove Invalid Parentheses

Description

Given a string s that contains parentheses and letters, remove the minimum number of invalid parentheses to make the input string valid.

Return all the possible results. You may return the answer in any order.

Example 1:

Input: s = "()())()"
Output: ["(())()","()()()"]
@any9527
any9527 / leetcode_227.md
Created October 2, 2021 15:55
Leetcode 227: Basic Calculator II

Description

Given a string s which represents an expression, evaluate this expression and return its value. 

The integer division should truncate toward zero.

You may assume that the given expression is always valid. All intermediate results will be in the range of [-2^31, 2^31 - 1].

Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval().

@any9527
any9527 / leetcode_772.md
Last active October 2, 2021 16:37
Leetcode 772: Basic Calculator III

Description

Implement a basic calculator to evaluate a simple expression string.

The expression string contains only non-negative integers, '+', '-', '*', '/' operators, and open '(' and closing parentheses ')'. The integer division should truncate toward zero.

You may assume that the given expression is always valid. All intermediate results will be in the range of [-2^31, 2^31 - 1].

Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval().

@any9527
any9527 / leetcode_774.md
Last active October 2, 2021 19:01
Leetcode 770: Basic Calculator IV

Description

Given an expression such as expression = "e + 8 - a + 5" and an evaluation map such as {"e": 1} (given in terms of evalvars = ["e"] and evalints = [1]), return a list of tokens representing the simplified expression, such as ["-1*a","14"]

  • An expression alternates chunks and symbols, with a space separating each chunk and symbol.
  • A chunk is either an expression in parentheses, a variable, or a non-negative integer.
  • A variable is a string of lowercase letters (not including digits.) Note that variables can be multiple letters, and note that variables never have a leading coefficient or unary operator like "2x" or "-x".

Expressions are evaluated in the usual order: brackets first, then multiplication, then addition and subtraction.

  • For example, expression = "1 + 2 * 3" has an answer of ["7"].
@any9527
any9527 / leetcode_76.md
Created October 3, 2021 00:49
Leetcode 76: Minimum Window Substring

Description

Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string "".

The testcases will be generated such that the answer is unique.

A substring is a contiguous sequence of characters within the string.

Example 1:

Input: s = "ADOBECODEBANC", t = "ABC"