feat: populate note files with problem descriptions and code stubs
Add populate-notes.mjs that fetches problem descriptions and Python/C++ code stubs from LeetCode's GraphQL API. Populated all 197 NeetCode 150 note files with: - Problem description (examples, constraints) - Python code stub (function signature) - C++ code stub (function signature + includes) API responses cached in leetcode/.cache/leetcode/ for instant re-runs.
This commit is contained in:
@@ -0,0 +1,277 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Populate DSA note files with problem descriptions and code stubs
|
||||||
|
* from LeetCode's GraphQL API.
|
||||||
|
*
|
||||||
|
* Usage:
|
||||||
|
* node populate-notes.mjs # populate all missing
|
||||||
|
* node populate-notes.mjs --force # overwrite existing content
|
||||||
|
* node populate-notes.mjs --dry-run # show what would change
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {
|
||||||
|
readFileSync,
|
||||||
|
writeFileSync,
|
||||||
|
existsSync,
|
||||||
|
mkdirSync,
|
||||||
|
} from "node:fs";
|
||||||
|
import { join, dirname } from "node:path";
|
||||||
|
import { fileURLToPath } from "node:url";
|
||||||
|
|
||||||
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||||
|
|
||||||
|
const args = process.argv.slice(2);
|
||||||
|
const force = args.includes("--force");
|
||||||
|
const dryRun = args.includes("--dry-run");
|
||||||
|
|
||||||
|
const roadmap = JSON.parse(
|
||||||
|
readFileSync(join(__dirname, "out/roadmap.json"), "utf8")
|
||||||
|
);
|
||||||
|
const dsaDir = join(__dirname, "../org/study_deck_02/dsa");
|
||||||
|
const cacheDir = join(__dirname, ".cache/leetcode");
|
||||||
|
mkdirSync(cacheDir, { recursive: true });
|
||||||
|
|
||||||
|
const API = "https://leetcode.com/graphql";
|
||||||
|
const DELAY_MS = 300;
|
||||||
|
|
||||||
|
const topicSlug = (name) =>
|
||||||
|
name
|
||||||
|
.toLowerCase()
|
||||||
|
.replace(/[^a-z0-9]+/g, "-")
|
||||||
|
.replace(/(^-|-$)/g, "");
|
||||||
|
|
||||||
|
function sleep(ms) {
|
||||||
|
return new Promise((r) => setTimeout(r, ms));
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchProblem(titleSlug) {
|
||||||
|
const cachePath = join(cacheDir, `${titleSlug}.json`);
|
||||||
|
if (existsSync(cachePath)) {
|
||||||
|
return JSON.parse(readFileSync(cachePath, "utf8"));
|
||||||
|
}
|
||||||
|
|
||||||
|
const res = await fetch(API, {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({
|
||||||
|
query: `query {
|
||||||
|
question(titleSlug: "${titleSlug}") {
|
||||||
|
title
|
||||||
|
content
|
||||||
|
difficulty
|
||||||
|
codeSnippets { lang langSlug code }
|
||||||
|
}
|
||||||
|
}`,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
console.error(` HTTP ${res.status} for ${titleSlug}`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await res.json();
|
||||||
|
const q = data?.data?.question;
|
||||||
|
if (!q) {
|
||||||
|
console.error(` No data for ${titleSlug}`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
writeFileSync(cachePath, JSON.stringify(q, null, 2), "utf8");
|
||||||
|
return q;
|
||||||
|
}
|
||||||
|
|
||||||
|
function htmlToOrg(html) {
|
||||||
|
if (!html) return "";
|
||||||
|
|
||||||
|
let text = html;
|
||||||
|
|
||||||
|
// Preserve code blocks
|
||||||
|
const codeBlocks = [];
|
||||||
|
text = text.replace(
|
||||||
|
/<pre>([\s\S]*?)<\/pre>/g,
|
||||||
|
(_, code) => {
|
||||||
|
const cleaned = code
|
||||||
|
.replace(/<[^>]+>/g, "")
|
||||||
|
.replace(/</g, "<")
|
||||||
|
.replace(/>/g, ">")
|
||||||
|
.replace(/&/g, "&")
|
||||||
|
.replace(/ /g, " ")
|
||||||
|
.replace(/"/g, '"')
|
||||||
|
.trim();
|
||||||
|
codeBlocks.push(cleaned);
|
||||||
|
return `__CODE_BLOCK_${codeBlocks.length - 1}__`;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Inline code
|
||||||
|
text = text.replace(/<code>([\s\S]*?)<\/code>/g, "~$1~");
|
||||||
|
|
||||||
|
// Bold
|
||||||
|
text = text.replace(/<strong[^>]*>([\s\S]*?)<\/strong>/g, "*$1*");
|
||||||
|
|
||||||
|
// Italic
|
||||||
|
text = text.replace(/<em>([\s\S]*?)<\/em>/g, "/$1/");
|
||||||
|
|
||||||
|
// Line breaks and paragraphs
|
||||||
|
text = text.replace(/<br\s*\/?>/g, "\n");
|
||||||
|
text = text.replace(/<\/p>/g, "\n");
|
||||||
|
text = text.replace(/<p[^>]*>/g, "");
|
||||||
|
|
||||||
|
// Lists
|
||||||
|
text = text.replace(/<li[^>]*>/g, "- ");
|
||||||
|
text = text.replace(/<\/li>/g, "\n");
|
||||||
|
text = text.replace(/<\/?[ou]l[^>]*>/g, "");
|
||||||
|
|
||||||
|
// Sup/sub
|
||||||
|
text = text.replace(/<sup>([\s\S]*?)<\/sup>/g, "^{$1}");
|
||||||
|
text = text.replace(/<sub>([\s\S]*?)<\/sub>/g, "_{$1}");
|
||||||
|
|
||||||
|
// Example blocks — flatten to plain text
|
||||||
|
text = text.replace(
|
||||||
|
/<div class="example-block">([\s\S]*?)<\/div>/g,
|
||||||
|
(_, inner) => inner.replace(/<[^>]+>/g, "").trim() + "\n"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Strip remaining HTML tags
|
||||||
|
text = text.replace(/<[^>]+>/g, "");
|
||||||
|
|
||||||
|
// Decode HTML entities
|
||||||
|
text = text.replace(/</g, "<");
|
||||||
|
text = text.replace(/>/g, ">");
|
||||||
|
text = text.replace(/&/g, "&");
|
||||||
|
text = text.replace(/ /g, " ");
|
||||||
|
text = text.replace(/"/g, '"');
|
||||||
|
text = text.replace(/'/g, "'");
|
||||||
|
|
||||||
|
// Collapse whitespace but preserve intentional newlines
|
||||||
|
text = text.replace(/[ \t]+/g, " ");
|
||||||
|
text = text.replace(/\n\s*\n\s*\n+/g, "\n\n");
|
||||||
|
text = text.trim();
|
||||||
|
|
||||||
|
// Restore code blocks
|
||||||
|
for (let i = 0; i < codeBlocks.length; i++) {
|
||||||
|
text = text.replace(
|
||||||
|
`__CODE_BLOCK_${i}__`,
|
||||||
|
`\n#+begin_src\n${codeBlocks[i]}\n#+end_src\n`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildNoteContent(num, name, diff, description, stubs, relPath) {
|
||||||
|
const py = stubs.find((s) => s.langSlug === "python3");
|
||||||
|
const cpp = stubs.find((s) => s.langSlug === "cpp");
|
||||||
|
|
||||||
|
let out = `#+PROPERTY: STUDY_DECK_02
|
||||||
|
* TODO ${num}. ${name} :${diff}:
|
||||||
|
:PROPERTIES:
|
||||||
|
:NEETCODE: [[file:${relPath}][${num}. ${name}]]
|
||||||
|
:END:
|
||||||
|
`;
|
||||||
|
|
||||||
|
if (description) {
|
||||||
|
out += `\n${description}\n`;
|
||||||
|
}
|
||||||
|
|
||||||
|
out += `
|
||||||
|
** TODO Approach
|
||||||
|
Write your approach here.
|
||||||
|
|
||||||
|
** TODO Python
|
||||||
|
#+begin_src python
|
||||||
|
${py ? py.code.trim() : ""}
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
** TODO C++
|
||||||
|
#+begin_src cpp
|
||||||
|
${cpp ? cpp.code.trim() : ""}
|
||||||
|
#+end_src
|
||||||
|
`;
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Main ────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
const allProblems = [];
|
||||||
|
for (const [topic, problems] of Object.entries(roadmap.problemsByTopic)) {
|
||||||
|
for (const p of problems) {
|
||||||
|
if (p.neetcode150) {
|
||||||
|
allProblems.push({ ...p, topicSlug: topicSlug(topic) });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Processing ${allProblems.length} NeetCode 150 problems...`);
|
||||||
|
if (dryRun) console.log("(dry run — no files written)\n");
|
||||||
|
|
||||||
|
let populated = 0;
|
||||||
|
let skipped = 0;
|
||||||
|
let errors = 0;
|
||||||
|
|
||||||
|
for (let i = 0; i < allProblems.length; i++) {
|
||||||
|
const p = allProblems[i];
|
||||||
|
const titleSlug = p.link.replace(/\/$/, "");
|
||||||
|
const filePath = join(dsaDir, p.topicSlug, `${p.code}.org`);
|
||||||
|
|
||||||
|
process.stdout.write(`[${i + 1}/${allProblems.length}] ${p.code}...`);
|
||||||
|
|
||||||
|
if (!existsSync(filePath)) {
|
||||||
|
console.log(" SKIP (no note file)");
|
||||||
|
skipped++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const existing = readFileSync(filePath, "utf8");
|
||||||
|
const hasDescription = existing.includes("** TODO Approach\nWrite your approach here.") === false ||
|
||||||
|
existing.split("\n").filter(l => l.trim() && !l.startsWith("#") && !l.startsWith(":") && !l.startsWith("*") && !l.startsWith("**") && !l.startsWith("-") && !l.startsWith("Write your approach")).length > 10;
|
||||||
|
|
||||||
|
if (!force && hasDescription && !existing.includes("Write your approach here.")) {
|
||||||
|
console.log(" SKIP (already populated)");
|
||||||
|
skipped++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const question = await fetchProblem(titleSlug);
|
||||||
|
if (!question) {
|
||||||
|
console.log(" ERROR");
|
||||||
|
errors++;
|
||||||
|
await sleep(DELAY_MS);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const description = htmlToOrg(question.content);
|
||||||
|
const stubs = question.codeSnippets || [];
|
||||||
|
const num = p.code.split("-")[0];
|
||||||
|
const relPath = `../../roadmap.org::*${num}. ${p.name}`;
|
||||||
|
|
||||||
|
const content = buildNoteContent(
|
||||||
|
num,
|
||||||
|
p.name,
|
||||||
|
p.difficulty.toLowerCase(),
|
||||||
|
description,
|
||||||
|
stubs,
|
||||||
|
relPath
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!dryRun) {
|
||||||
|
writeFileSync(filePath, content, "utf8");
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(` OK (${description.length} chars)`);
|
||||||
|
populated++;
|
||||||
|
|
||||||
|
await sleep(DELAY_MS);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`\nDone: ${populated} populated, ${skipped} skipped, ${errors} errors`);
|
||||||
|
}
|
||||||
|
|
||||||
|
main().catch((err) => {
|
||||||
|
console.error(err);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
+36
-3
@@ -1,18 +1,51 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0005. Longest Palindromic Substring :medium:
|
* TODO 0005. Longest Palindromic Substring :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0005. Longest Palindromic Substring][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0005. Longest Palindromic Substring][0005. Longest Palindromic Substring]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given a string ~s~, return /the longest/ /palindromic/ /substring/ in ~s~.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: s = "babad"
|
||||||
|
Output: "bab"
|
||||||
|
Explanation: "aba" is also a valid answer.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: s = "cbbd"
|
||||||
|
Output: "bb"
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= s.length <= 1000~
|
||||||
|
|
||||||
|
- ~s~ consist of only digits and English letters.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def longestPalindrome(self, s: str) -> str:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
string longestPalindrome(string s) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,57 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0070. Climbing Stairs :easy:
|
* TODO 0070. Climbing Stairs :easy:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0070. Climbing Stairs][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0070. Climbing Stairs][0070. Climbing Stairs]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are climbing a staircase. It takes ~n~ steps to reach the top.
|
||||||
|
|
||||||
|
Each time you can either climb ~1~ or ~2~ steps. In how many distinct ways can you climb to the top?
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 2
|
||||||
|
Output: 2
|
||||||
|
Explanation: There are two ways to climb to the top.
|
||||||
|
1. 1 step + 1 step
|
||||||
|
2. 2 steps
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 3
|
||||||
|
Output: 3
|
||||||
|
Explanation: There are three ways to climb to the top.
|
||||||
|
1. 1 step + 1 step + 1 step
|
||||||
|
2. 1 step + 2 steps
|
||||||
|
3. 2 steps + 1 step
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= n <= 45~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def climbStairs(self, n: int) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int climbStairs(int n) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,88 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0091. Decode Ways :medium:
|
* TODO 0091. Decode Ways :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0091. Decode Ways][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0091. Decode Ways][0091. Decode Ways]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You have intercepted a secret message encoded as a string of numbers. The message is *decoded* via the following mapping:
|
||||||
|
|
||||||
|
~"1" -> 'A'
|
||||||
|
|
||||||
|
"2" -> 'B'
|
||||||
|
|
||||||
|
...
|
||||||
|
|
||||||
|
"25" -> 'Y'
|
||||||
|
|
||||||
|
"26" -> 'Z'~
|
||||||
|
|
||||||
|
However, while decoding the message, you realize that there are many different ways you can decode the message because some codes are contained in other codes (~"2"~ and ~"5"~ vs ~"25"~).
|
||||||
|
|
||||||
|
For example, ~"11106"~ can be decoded into:
|
||||||
|
|
||||||
|
- ~"AAJF"~ with the grouping ~(1, 1, 10, 6)~
|
||||||
|
|
||||||
|
- ~"KJF"~ with the grouping ~(11, 10, 6)~
|
||||||
|
|
||||||
|
- The grouping ~(1, 11, 06)~ is invalid because ~"06"~ is not a valid code (only ~"6"~ is valid).
|
||||||
|
|
||||||
|
Note: there may be strings that are impossible to decode.
|
||||||
|
|
||||||
|
Given a string s containing only digits, return the *number of ways* to *decode* it. If the entire string cannot be decoded in any valid way, return ~0~.
|
||||||
|
|
||||||
|
The test cases are generated so that the answer fits in a *32-bit* integer.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
*Input:* s = "12"
|
||||||
|
|
||||||
|
*Output:* 2
|
||||||
|
|
||||||
|
*Explanation:*
|
||||||
|
|
||||||
|
"12" could be decoded as "AB" (1 2) or "L" (12).
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
*Input:* s = "226"
|
||||||
|
|
||||||
|
*Output:* 3
|
||||||
|
|
||||||
|
*Explanation:*
|
||||||
|
|
||||||
|
"226" could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
*Input:* s = "06"
|
||||||
|
|
||||||
|
*Output:* 0
|
||||||
|
|
||||||
|
*Explanation:*
|
||||||
|
|
||||||
|
"06" cannot be mapped to "F" because of the leading zero ("6" is different from "06"). In this case, the string is not a valid encoding, so return 0.
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= s.length <= 100~
|
||||||
|
|
||||||
|
- ~s~ contains only digits and may contain leading zero(s).
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def numDecodings(self, s: str) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int numDecodings(string s) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,70 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0139. Word Break :medium:
|
* TODO 0139. Word Break :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0139. Word Break][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0139. Word Break][0139. Word Break]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given a string ~s~ and a dictionary of strings ~wordDict~, return ~true~ if ~s~ can be segmented into a space-separated sequence of one or more dictionary words.
|
||||||
|
|
||||||
|
*Note* that the same word in the dictionary may be reused multiple times in the segmentation.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: s = "leetcode", wordDict = ["leet","code"]
|
||||||
|
Output: true
|
||||||
|
Explanation: Return true because "leetcode" can be segmented as "leet code".
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: s = "applepenapple", wordDict = ["apple","pen"]
|
||||||
|
Output: true
|
||||||
|
Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
|
||||||
|
Note that you are allowed to reuse a dictionary word.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
|
||||||
|
Output: false
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= s.length <= 300~
|
||||||
|
|
||||||
|
- ~1 <= wordDict.length <= 1000~
|
||||||
|
|
||||||
|
- ~1 <= wordDict[i].length <= 20~
|
||||||
|
|
||||||
|
- ~s~ and ~wordDict[i]~ consist of only lowercase English letters.
|
||||||
|
|
||||||
|
- All the strings of ~wordDict~ are *unique*.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
bool wordBreak(string s, vector<string>& wordDict) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,58 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0152. Maximum Product Subarray :medium:
|
* TODO 0152. Maximum Product Subarray :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0152. Maximum Product Subarray][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0152. Maximum Product Subarray][0152. Maximum Product Subarray]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an integer array ~nums~, find a subarray that has the largest product, and return /the product/.
|
||||||
|
|
||||||
|
The test cases are generated so that the answer will fit in a *32-bit* integer.
|
||||||
|
|
||||||
|
*Note* that the product of an array with a single element is the value of that element.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [2,3,-2,4]
|
||||||
|
Output: 6
|
||||||
|
Explanation: [2,3] has the largest product 6.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [-2,0,-1]
|
||||||
|
Output: 0
|
||||||
|
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= nums.length <= 2 * 10^{4}~
|
||||||
|
|
||||||
|
- ~-10 <= nums[i] <= 10~
|
||||||
|
|
||||||
|
- The product of any subarray of ~nums~ is *guaranteed* to fit in a *32-bit* integer.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def maxProduct(self, nums: List[int]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int maxProduct(vector<int>& nums) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,56 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0198. House Robber :medium:
|
* TODO 0198. House Robber :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0198. House Robber][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0198. House Robber][0198. House Robber]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
|
||||||
|
|
||||||
|
Given an integer array ~nums~ representing the amount of money of each house, return /the maximum amount of money you can rob tonight without alerting the police/.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [1,2,3,1]
|
||||||
|
Output: 4
|
||||||
|
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
|
||||||
|
Total amount you can rob = 1 + 3 = 4.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [2,7,9,3,1]
|
||||||
|
Output: 12
|
||||||
|
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
|
||||||
|
Total amount you can rob = 2 + 9 + 1 = 12.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= nums.length <= 100~
|
||||||
|
|
||||||
|
- ~0 <= nums[i] <= 400~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def rob(self, nums: List[int]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int rob(vector<int>& nums) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,64 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0213. House Robber II :medium:
|
* TODO 0213. House Robber II :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0213. House Robber II][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0213. House Robber II][0213. House Robber II]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are *arranged in a circle.* That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and it will automatically contact the police if two adjacent houses were broken into on the same night.
|
||||||
|
|
||||||
|
Given an integer array ~nums~ representing the amount of money of each house, return /the maximum amount of money you can rob tonight *without alerting the police*/.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [2,3,2]
|
||||||
|
Output: 3
|
||||||
|
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), because they are adjacent houses.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [1,2,3,1]
|
||||||
|
Output: 4
|
||||||
|
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
|
||||||
|
Total amount you can rob = 1 + 3 = 4.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [1,2,3]
|
||||||
|
Output: 3
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= nums.length <= 100~
|
||||||
|
|
||||||
|
- ~0 <= nums[i] <= 1000~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def rob(self, nums: List[int]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int rob(vector<int>& nums) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
+47
-3
@@ -1,18 +1,62 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0300. Longest Increasing Subsequence :medium:
|
* TODO 0300. Longest Increasing Subsequence :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0300. Longest Increasing Subsequence][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0300. Longest Increasing Subsequence][0300. Longest Increasing Subsequence]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an integer array ~nums~, return /the length of the longest *strictly increasing *//*subsequence*/.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [10,9,2,5,3,7,101,18]
|
||||||
|
Output: 4
|
||||||
|
Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [0,1,0,3,2,3]
|
||||||
|
Output: 4
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [7,7,7,7,7,7,7]
|
||||||
|
Output: 1
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= nums.length <= 2500~
|
||||||
|
|
||||||
|
- ~-10^{4} <= nums[i] <= 10^{4}~
|
||||||
|
|
||||||
|
Follow up: Can you come up with an algorithm that runs in ~O(n log(n))~ time complexity?
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def lengthOfLIS(self, nums: List[int]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int lengthOfLIS(vector<int>& nums) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,66 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0322. Coin Change :medium:
|
* TODO 0322. Coin Change :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0322. Coin Change][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0322. Coin Change][0322. Coin Change]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are given an integer array ~coins~ representing coins of different denominations and an integer ~amount~ representing a total amount of money.
|
||||||
|
|
||||||
|
Return /the fewest number of coins that you need to make up that amount/. If that amount of money cannot be made up by any combination of the coins, return ~-1~.
|
||||||
|
|
||||||
|
You may assume that you have an infinite number of each kind of coin.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: coins = [1,2,5], amount = 11
|
||||||
|
Output: 3
|
||||||
|
Explanation: 11 = 5 + 5 + 1
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: coins = [2], amount = 3
|
||||||
|
Output: -1
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: coins = [1], amount = 0
|
||||||
|
Output: 0
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= coins.length <= 12~
|
||||||
|
|
||||||
|
- ~1 <= coins[i] <= 2^{31} - 1~
|
||||||
|
|
||||||
|
- ~0 <= amount <= 10^{4}~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def coinChange(self, coins: List[int], amount: int) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int coinChange(vector<int>& coins, int amount) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,52 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0416. Partition Equal Subset Sum :medium:
|
* TODO 0416. Partition Equal Subset Sum :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0416. Partition Equal Subset Sum][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0416. Partition Equal Subset Sum][0416. Partition Equal Subset Sum]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an integer array ~nums~, return ~true~ /if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or /~false~/ otherwise/.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [1,5,11,5]
|
||||||
|
Output: true
|
||||||
|
Explanation: The array can be partitioned as [1, 5, 5] and [11].
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [1,2,3,5]
|
||||||
|
Output: false
|
||||||
|
Explanation: The array cannot be partitioned into equal sum subsets.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= nums.length <= 200~
|
||||||
|
|
||||||
|
- ~1 <= nums[i] <= 100~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def canPartition(self, nums: List[int]) -> bool:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
bool canPartition(vector<int>& nums) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,56 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0647. Palindromic Substrings :medium:
|
* TODO 0647. Palindromic Substrings :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0647. Palindromic Substrings][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0647. Palindromic Substrings][0647. Palindromic Substrings]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given a string ~s~, return /the number of *palindromic substrings* in it/.
|
||||||
|
|
||||||
|
A string is a *palindrome* when it reads the same backward as forward.
|
||||||
|
|
||||||
|
A *substring* is a contiguous sequence of characters within the string.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: s = "abc"
|
||||||
|
Output: 3
|
||||||
|
Explanation: Three palindromic strings: "a", "b", "c".
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: s = "aaa"
|
||||||
|
Output: 6
|
||||||
|
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= s.length <= 1000~
|
||||||
|
|
||||||
|
- ~s~ consists of lowercase English letters.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def countSubstrings(self, s: str) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int countSubstrings(string s) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0656. Coin Path :hard:
|
* TODO 0656. Coin Path :hard:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0656. Coin Path][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0656. Coin Path][0656. Coin Path]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
|
|||||||
@@ -1,18 +1,65 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0746. Min Cost Climbing Stairs :easy:
|
* TODO 0746. Min Cost Climbing Stairs :easy:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0746. Min Cost Climbing Stairs][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0746. Min Cost Climbing Stairs][0746. Min Cost Climbing Stairs]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are given an integer array ~cost~ where ~cost[i]~ is the cost of ~i^{th}~ step on a staircase. Once you pay the cost, you can either climb one or two steps.
|
||||||
|
|
||||||
|
You can either start from the step with index ~0~, or the step with index ~1~.
|
||||||
|
|
||||||
|
Return /the minimum cost to reach the top of the floor/.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: cost = [10,15,20]
|
||||||
|
Output: 15
|
||||||
|
Explanation: You will start at index 1.
|
||||||
|
- Pay 15 and climb two steps to reach the top.
|
||||||
|
The total cost is 15.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: cost = [1,100,1,1,1,100,1,1,100,1]
|
||||||
|
Output: 6
|
||||||
|
Explanation: You will start at index 0.
|
||||||
|
- Pay 1 and climb two steps to reach index 2.
|
||||||
|
- Pay 1 and climb two steps to reach index 4.
|
||||||
|
- Pay 1 and climb two steps to reach index 6.
|
||||||
|
- Pay 1 and climb one step to reach index 7.
|
||||||
|
- Pay 1 and climb two steps to reach index 9.
|
||||||
|
- Pay 1 and climb one step to reach the top.
|
||||||
|
The total cost is 6.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~2 <= cost.length <= 1000~
|
||||||
|
|
||||||
|
- ~0 <= cost[i] <= 999~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def minCostClimbingStairs(self, cost: List[int]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int minCostClimbingStairs(vector<int>& cost) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,74 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0010. Regular Expression Matching :hard:
|
* TODO 0010. Regular Expression Matching :hard:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0010. Regular Expression Matching][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0010. Regular Expression Matching][0010. Regular Expression Matching]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an input string ~s~ and a pattern ~p~, implement regular expression matching with support for ~'.'~ and ~'*'~ where:
|
||||||
|
|
||||||
|
- ~'.'~ Matches any single character.
|
||||||
|
|
||||||
|
- ~'*'~ Matches zero or more of the preceding element.
|
||||||
|
|
||||||
|
Return a boolean indicating whether the matching covers the entire input string (not partial).
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: s = "aa", p = "a"
|
||||||
|
Output: false
|
||||||
|
Explanation: "a" does not match the entire string "aa".
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: s = "aa", p = "a*"
|
||||||
|
Output: true
|
||||||
|
Explanation: '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: s = "ab", p = ".*"
|
||||||
|
Output: true
|
||||||
|
Explanation: ".*" means "zero or more (*) of any character (.)".
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= s.length <= 20~
|
||||||
|
|
||||||
|
- ~1 <= p.length <= 20~
|
||||||
|
|
||||||
|
- ~s~ contains only lowercase English letters.
|
||||||
|
|
||||||
|
- ~p~ contains only lowercase English letters, ~'.'~, and ~'*'~.
|
||||||
|
|
||||||
|
- It is guaranteed for each appearance of the character ~'*'~, there will be a previous valid character to match.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def isMatch(self, s: str, p: str) -> bool:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
bool isMatch(string s, string p) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,56 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0062. Unique Paths :medium:
|
* TODO 0062. Unique Paths :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0062. Unique Paths][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0062. Unique Paths][0062. Unique Paths]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
There is a robot on an ~m x n~ grid. The robot is initially located at the *top-left corner* (i.e., ~grid[0][0]~). The robot tries to move to the *bottom-right corner* (i.e., ~grid[m - 1][n - 1]~). The robot can only move either down or right at any point in time.
|
||||||
|
|
||||||
|
Given the two integers ~m~ and ~n~, return /the number of possible unique paths that the robot can take to reach the bottom-right corner/.
|
||||||
|
|
||||||
|
The test cases are generated so that the answer will be less than or equal to ~2 * 10^{9}~.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: m = 3, n = 7
|
||||||
|
Output: 28
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: m = 3, n = 2
|
||||||
|
Output: 3
|
||||||
|
Explanation: From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
|
||||||
|
1. Right -> Down -> Down
|
||||||
|
2. Down -> Down -> Right
|
||||||
|
3. Down -> Right -> Down
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= m, n <= 100~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def uniquePaths(self, m: int, n: int) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int uniquePaths(int m, int n) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,68 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0072. Edit Distance :medium:
|
* TODO 0072. Edit Distance :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0072. Edit Distance][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0072. Edit Distance][0072. Edit Distance]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given two strings ~word1~ and ~word2~, return /the minimum number of operations required to convert ~word1~ to ~word2~/.
|
||||||
|
|
||||||
|
You have the following three operations permitted on a word:
|
||||||
|
|
||||||
|
- Insert a character
|
||||||
|
|
||||||
|
- Delete a character
|
||||||
|
|
||||||
|
- Replace a character
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: word1 = "horse", word2 = "ros"
|
||||||
|
Output: 3
|
||||||
|
Explanation:
|
||||||
|
horse -> rorse (replace 'h' with 'r')
|
||||||
|
rorse -> rose (remove 'r')
|
||||||
|
rose -> ros (remove 'e')
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: word1 = "intention", word2 = "execution"
|
||||||
|
Output: 5
|
||||||
|
Explanation:
|
||||||
|
intention -> inention (remove 't')
|
||||||
|
inention -> enention (replace 'i' with 'e')
|
||||||
|
enention -> exention (replace 'n' with 'x')
|
||||||
|
exention -> exection (replace 'n' with 'c')
|
||||||
|
exection -> execution (insert 'u')
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~0 <= word1.length, word2.length <= 500~
|
||||||
|
|
||||||
|
- ~word1~ and ~word2~ consist of lowercase English letters.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def minDistance(self, word1: str, word2: str) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int minDistance(string word1, string word2) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,80 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0097. Interleaving String :medium:
|
* TODO 0097. Interleaving String :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0097. Interleaving String][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0097. Interleaving String][0097. Interleaving String]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given strings ~s1~, ~s2~, and ~s3~, find whether ~s3~ is formed by an *interleaving* of ~s1~ and ~s2~.
|
||||||
|
|
||||||
|
An *interleaving* of two strings ~s~ and ~t~ is a configuration where ~s~ and ~t~ are divided into ~n~ and ~m~ substrings respectively, such that:
|
||||||
|
|
||||||
|
- ~s = s_{1} + s_{2} + ... + s_{n}~
|
||||||
|
|
||||||
|
- ~t = t_{1} + t_{2} + ... + t_{m}~
|
||||||
|
|
||||||
|
- ~|n - m| <= 1~
|
||||||
|
|
||||||
|
- The *interleaving* is ~s_{1} + t_{1} + s_{2} + t_{2} + s_{3} + t_{3} + ...~ or ~t_{1} + s_{1} + t_{2} + s_{2} + t_{3} + s_{3} + ...~
|
||||||
|
|
||||||
|
*Note:* ~a + b~ is the concatenation of strings ~a~ and ~b~.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
|
||||||
|
Output: true
|
||||||
|
Explanation: One way to obtain s3 is:
|
||||||
|
Split s1 into s1 = "aa" + "bc" + "c", and s2 into s2 = "dbbc" + "a".
|
||||||
|
Interleaving the two splits, we get "aa" + "dbbc" + "bc" + "a" + "c" = "aadbbcbcac".
|
||||||
|
Since s3 can be obtained by interleaving s1 and s2, we return true.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
|
||||||
|
Output: false
|
||||||
|
Explanation: Notice how it is impossible to interleave s2 with any other string to obtain s3.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: s1 = "", s2 = "", s3 = ""
|
||||||
|
Output: true
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~0 <= s1.length, s2.length <= 100~
|
||||||
|
|
||||||
|
- ~0 <= s3.length <= 200~
|
||||||
|
|
||||||
|
- ~s1~, ~s2~, and ~s3~ consist of lowercase English letters.
|
||||||
|
|
||||||
|
*Follow up:* Could you solve it using only ~O(s2.length)~ additional memory space?
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
bool isInterleave(string s1, string s2, string s3) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,64 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0115. Distinct Subsequences :hard:
|
* TODO 0115. Distinct Subsequences :hard:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0115. Distinct Subsequences][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0115. Distinct Subsequences][0115. Distinct Subsequences]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given two strings s and t, return the number of distinct subsequences of s which equals t.
|
||||||
|
|
||||||
|
The test cases are generated so that the answer fits on a 32-bit signed integer.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: s = "rabbbit", t = "rabbit"
|
||||||
|
Output: 3
|
||||||
|
Explanation:
|
||||||
|
As shown below, there are 3 ways you can generate "rabbit" from s.
|
||||||
|
rabbbit
|
||||||
|
rabbbit
|
||||||
|
rabbbit
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: s = "babgbag", t = "bag"
|
||||||
|
Output: 5
|
||||||
|
Explanation:
|
||||||
|
As shown below, there are 5 ways you can generate "bag" from s.
|
||||||
|
babgbag
|
||||||
|
babgbag
|
||||||
|
babgbag
|
||||||
|
babgbag
|
||||||
|
babgbag
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= s.length, t.length <= 1000~
|
||||||
|
|
||||||
|
- ~s~ and ~t~ consist of English letters.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def numDistinct(self, s: str, t: str) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int numDistinct(string s, string t) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
+42
-3
@@ -1,18 +1,57 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0309. Best Time to Buy And Sell Stock With Cooldown :medium:
|
* TODO 0309. Best Time to Buy And Sell Stock With Cooldown :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0309. Best Time to Buy And Sell Stock With Cooldown][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0309. Best Time to Buy And Sell Stock With Cooldown][0309. Best Time to Buy And Sell Stock With Cooldown]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are given an array ~prices~ where ~prices[i]~ is the price of a given stock on the ~i^{th}~ day.
|
||||||
|
|
||||||
|
Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions:
|
||||||
|
|
||||||
|
- After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day).
|
||||||
|
|
||||||
|
*Note:* You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: prices = [1,2,3,0,2]
|
||||||
|
Output: 3
|
||||||
|
Explanation: transactions = [buy, sell, cooldown, buy, sell]
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: prices = [1]
|
||||||
|
Output: 0
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= prices.length <= 5000~
|
||||||
|
|
||||||
|
- ~0 <= prices[i] <= 1000~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def maxProfit(self, prices: List[int]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int maxProfit(vector<int>& prices) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,59 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0312. Burst Balloons :hard:
|
* TODO 0312. Burst Balloons :hard:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0312. Burst Balloons][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0312. Burst Balloons][0312. Burst Balloons]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are given ~n~ balloons, indexed from ~0~ to ~n - 1~. Each balloon is painted with a number on it represented by an array ~nums~. You are asked to burst all the balloons.
|
||||||
|
|
||||||
|
If you burst the ~i^{th}~ balloon, you will get ~nums[i - 1] * nums[i] * nums[i + 1]~ coins. If ~i - 1~ or ~i + 1~ goes out of bounds of the array, then treat it as if there is a balloon with a ~1~ painted on it.
|
||||||
|
|
||||||
|
Return /the maximum coins you can collect by bursting the balloons wisely/.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [3,1,5,8]
|
||||||
|
Output: 167
|
||||||
|
Explanation:
|
||||||
|
nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
|
||||||
|
coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [1,5]
|
||||||
|
Output: 10
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~n == nums.length~
|
||||||
|
|
||||||
|
- ~1 <= n <= 300~
|
||||||
|
|
||||||
|
- ~0 <= nums[i] <= 100~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def maxCoins(self, nums: List[int]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int maxCoins(vector<int>& nums) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
+52
-3
@@ -1,18 +1,67 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0329. Longest Increasing Path In a Matrix :hard:
|
* TODO 0329. Longest Increasing Path In a Matrix :hard:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0329. Longest Increasing Path In a Matrix][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0329. Longest Increasing Path In a Matrix][0329. Longest Increasing Path In a Matrix]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an ~m x n~ integers ~matrix~, return /the length of the longest increasing path in /~matrix~.
|
||||||
|
|
||||||
|
From each cell, you can either move in four directions: left, right, up, or down. You *may not* move *diagonally* or move *outside the boundary* (i.e., wrap-around is not allowed).
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: matrix = [[9,9,4],[6,6,8],[2,1,1]]
|
||||||
|
Output: 4
|
||||||
|
Explanation: The longest increasing path is [1, 2, 6, 9].
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: matrix = [[3,4,5],[3,2,6],[2,2,1]]
|
||||||
|
Output: 4
|
||||||
|
Explanation: The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: matrix = [[1]]
|
||||||
|
Output: 1
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~m == matrix.length~
|
||||||
|
|
||||||
|
- ~n == matrix[i].length~
|
||||||
|
|
||||||
|
- ~1 <= m, n <= 200~
|
||||||
|
|
||||||
|
- ~0 <= matrix[i][j] <= 2^{31} - 1~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int longestIncreasingPath(vector<vector<int>>& matrix) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,66 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0494. Target Sum :medium:
|
* TODO 0494. Target Sum :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0494. Target Sum][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0494. Target Sum][0494. Target Sum]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are given an integer array ~nums~ and an integer ~target~.
|
||||||
|
|
||||||
|
You want to build an *expression* out of nums by adding one of the symbols ~'+'~ and ~'-'~ before each integer in nums and then concatenate all the integers.
|
||||||
|
|
||||||
|
- For example, if ~nums = [2, 1]~, you can add a ~'+'~ before ~2~ and a ~'-'~ before ~1~ and concatenate them to build the expression ~"+2-1"~.
|
||||||
|
|
||||||
|
Return the number of different *expressions* that you can build, which evaluates to ~target~.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [1,1,1,1,1], target = 3
|
||||||
|
Output: 5
|
||||||
|
Explanation: There are 5 ways to assign symbols to make the sum of nums be target 3.
|
||||||
|
-1 + 1 + 1 + 1 + 1 = 3
|
||||||
|
+1 - 1 + 1 + 1 + 1 = 3
|
||||||
|
+1 + 1 - 1 + 1 + 1 = 3
|
||||||
|
+1 + 1 + 1 - 1 + 1 = 3
|
||||||
|
+1 + 1 + 1 + 1 - 1 = 3
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [1], target = 1
|
||||||
|
Output: 1
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= nums.length <= 20~
|
||||||
|
|
||||||
|
- ~0 <= nums[i] <= 1000~
|
||||||
|
|
||||||
|
- ~0 <= sum(nums[i]) <= 1000~
|
||||||
|
|
||||||
|
- ~-1000 <= target <= 1000~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def findTargetSumWays(self, nums: List[int], target: int) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int findTargetSumWays(vector<int>& nums, int target) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,75 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0518. Coin Change II :medium:
|
* TODO 0518. Coin Change II :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0518. Coin Change II][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0518. Coin Change II][0518. Coin Change II]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are given an integer array ~coins~ representing coins of different denominations and an integer ~amount~ representing a total amount of money.
|
||||||
|
|
||||||
|
Return /the number of combinations that make up that amount/. If that amount of money cannot be made up by any combination of the coins, return ~0~.
|
||||||
|
|
||||||
|
You may assume that you have an infinite number of each kind of coin.
|
||||||
|
|
||||||
|
The answer is *guaranteed* to fit into a signed *32-bit* integer.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: amount = 5, coins = [1,2,5]
|
||||||
|
Output: 4
|
||||||
|
Explanation: there are four ways to make up the amount:
|
||||||
|
5=5
|
||||||
|
5=2+2+1
|
||||||
|
5=2+1+1+1
|
||||||
|
5=1+1+1+1+1
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: amount = 3, coins = [2]
|
||||||
|
Output: 0
|
||||||
|
Explanation: the amount of 3 cannot be made up just with coins of 2.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: amount = 10, coins = [10]
|
||||||
|
Output: 1
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= coins.length <= 300~
|
||||||
|
|
||||||
|
- ~1 <= coins[i] <= 5000~
|
||||||
|
|
||||||
|
- All the values of ~coins~ are *unique*.
|
||||||
|
|
||||||
|
- ~0 <= amount <= 5000~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def change(self, amount: int, coins: List[int]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int change(int amount, vector<int>& coins) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,68 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 1143. Longest Common Subsequence :medium:
|
* TODO 1143. Longest Common Subsequence :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*1143. Longest Common Subsequence][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*1143. Longest Common Subsequence][1143. Longest Common Subsequence]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given two strings ~text1~ and ~text2~, return /the length of their longest *common subsequence*. /If there is no *common subsequence*, return ~0~.
|
||||||
|
|
||||||
|
A *subsequence* of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
|
||||||
|
|
||||||
|
- For example, ~"ace"~ is a subsequence of ~"abcde"~.
|
||||||
|
|
||||||
|
A *common subsequence* of two strings is a subsequence that is common to both strings.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: text1 = "abcde", text2 = "ace"
|
||||||
|
Output: 3
|
||||||
|
Explanation: The longest common subsequence is "ace" and its length is 3.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: text1 = "abc", text2 = "abc"
|
||||||
|
Output: 3
|
||||||
|
Explanation: The longest common subsequence is "abc" and its length is 3.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: text1 = "abc", text2 = "def"
|
||||||
|
Output: 0
|
||||||
|
Explanation: There is no such common subsequence, so the result is 0.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= text1.length, text2.length <= 1000~
|
||||||
|
|
||||||
|
- ~text1~ and ~text2~ consist of only lowercase English characters.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int longestCommonSubsequence(string text1, string text2) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,73 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 1220. Count Vowels Permutation :hard:
|
* TODO 1220. Count Vowels Permutation :hard:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*1220. Count Vowels Permutation][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*1220. Count Vowels Permutation][1220. Count Vowels Permutation]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an integer ~n~, your task is to count how many strings of length ~n~ can be formed under the following rules:
|
||||||
|
|
||||||
|
- Each character is a lower case vowel (~'a'~, ~'e'~, ~'i'~, ~'o'~, ~'u'~)
|
||||||
|
|
||||||
|
- Each vowel ~'a'~ may only be followed by an ~'e'~.
|
||||||
|
|
||||||
|
- Each vowel ~'e'~ may only be followed by an ~'a'~ or an ~'i'~.
|
||||||
|
|
||||||
|
- Each vowel ~'i'~ *may not* be followed by another ~'i'~.
|
||||||
|
|
||||||
|
- Each vowel ~'o'~ may only be followed by an ~'i'~ or a ~'u'~.
|
||||||
|
|
||||||
|
- Each vowel ~'u'~ may only be followed by an ~'a'~.
|
||||||
|
|
||||||
|
Since the answer may be too large, return it modulo ~10^9 + 7~.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 1
|
||||||
|
Output: 5
|
||||||
|
Explanation: All possible strings are: "a", "e", "i" , "o" and "u".
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 2
|
||||||
|
Output: 10
|
||||||
|
Explanation: All possible strings are: "ae", "ea", "ei", "ia", "ie", "io", "iu", "oi", "ou" and "ua".
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3: *
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 5
|
||||||
|
Output: 68
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= n <= 2 * 10^4~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def countVowelPermutation(self, n: int) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int countVowelPermutation(int n) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
+53
-3
@@ -1,18 +1,68 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 1911. Maximum Alternating Subsequence Sum :medium:
|
* TODO 1911. Maximum Alternating Subsequence Sum :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*1911. Maximum Alternating Subsequence Sum][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*1911. Maximum Alternating Subsequence Sum][1911. Maximum Alternating Subsequence Sum]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
The *alternating sum* of a *0-indexed* array is defined as the *sum* of the elements at *even* indices *minus* the *sum* of the elements at *odd* indices.
|
||||||
|
|
||||||
|
- For example, the alternating sum of ~[4,2,5,3]~ is ~(4 + 5) - (2 + 3) = 4~.
|
||||||
|
|
||||||
|
Given an array ~nums~, return /the *maximum alternating sum* of any subsequence of /~nums~/ (after *reindexing* the elements of the subsequence)/.
|
||||||
|
|
||||||
|
A *subsequence* of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the remaining elements' relative order. For example, ~[2,7,4]~ is a subsequence of ~[4,2,3,7,2,1,4]~ (the underlined elements), while ~[2,4,2]~ is not.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [4,2,5,3]
|
||||||
|
Output: 7
|
||||||
|
Explanation: It is optimal to choose the subsequence [4,2,5] with alternating sum (4 + 5) - 2 = 7.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [5,6,7,8]
|
||||||
|
Output: 8
|
||||||
|
Explanation: It is optimal to choose the subsequence [8] with alternating sum 8.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [6,2,1,2,4,5]
|
||||||
|
Output: 10
|
||||||
|
Explanation: It is optimal to choose the subsequence [6,1,5] with alternating sum (6 + 5) - 1 = 10.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= nums.length <= 10^{5}~
|
||||||
|
|
||||||
|
- ~1 <= nums[i] <= 10^{5}~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def maxAlternatingSum(self, nums: List[int]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
long long maxAlternatingSum(vector<int>& nums) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0269. Alien Dictionary :hard:
|
* TODO 0269. Alien Dictionary :hard:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0269. Alien Dictionary][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0269. Alien Dictionary][0269. Alien Dictionary]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
|
|||||||
@@ -1,18 +1,65 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0332. Reconstruct Itinerary :hard:
|
* TODO 0332. Reconstruct Itinerary :hard:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0332. Reconstruct Itinerary][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0332. Reconstruct Itinerary][0332. Reconstruct Itinerary]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are given a list of airline ~tickets~ where ~tickets[i] = [from_{i}, to_{i}]~ represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.
|
||||||
|
|
||||||
|
All of the tickets belong to a man who departs from ~"JFK"~, thus, the itinerary must begin with ~"JFK"~. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.
|
||||||
|
|
||||||
|
- For example, the itinerary ~["JFK", "LGA"]~ has a smaller lexical order than ~["JFK", "LGB"]~.
|
||||||
|
|
||||||
|
You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
|
||||||
|
Output: ["JFK","MUC","LHR","SFO","SJC"]
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
|
||||||
|
Output: ["JFK","ATL","JFK","SFO","ATL","SFO"]
|
||||||
|
Explanation: Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= tickets.length <= 300~
|
||||||
|
|
||||||
|
- ~tickets[i].length == 2~
|
||||||
|
|
||||||
|
- ~from_{i}.length == 3~
|
||||||
|
|
||||||
|
- ~to_{i}.length == 3~
|
||||||
|
|
||||||
|
- ~from_{i}~ and ~to_{i}~ consist of uppercase English letters.
|
||||||
|
|
||||||
|
- ~from_{i} != to_{i}~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def findItinerary(self, tickets: List[List[str]]) -> List[str]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<string> findItinerary(vector<vector<string>>& tickets) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,71 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0743. Network Delay Time :medium:
|
* TODO 0743. Network Delay Time :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0743. Network Delay Time][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0743. Network Delay Time][0743. Network Delay Time]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are given a network of ~n~ nodes, labeled from ~1~ to ~n~. You are also given ~times~, a list of travel times as directed edges ~times[i] = (u_{i}, v_{i}, w_{i})~, where ~u_{i}~ is the source node, ~v_{i}~ is the target node, and ~w_{i}~ is the time it takes for a signal to travel from source to target.
|
||||||
|
|
||||||
|
We will send a signal from a given node ~k~. Return /the *minimum* time it takes for all the/ ~n~ /nodes to receive the signal/. If it is impossible for all the ~n~ nodes to receive the signal, return ~-1~.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2
|
||||||
|
Output: 2
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: times = [[1,2,1]], n = 2, k = 1
|
||||||
|
Output: 1
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: times = [[1,2,1]], n = 2, k = 2
|
||||||
|
Output: -1
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= k <= n <= 100~
|
||||||
|
|
||||||
|
- ~1 <= times.length <= 6000~
|
||||||
|
|
||||||
|
- ~times[i].length == 3~
|
||||||
|
|
||||||
|
- ~1 <= u_{i}, v_{i} <= n~
|
||||||
|
|
||||||
|
- ~u_{i} != v_{i}~
|
||||||
|
|
||||||
|
- ~0 <= w_{i} <= 100~
|
||||||
|
|
||||||
|
- All the pairs ~(u_{i}, v_{i})~ are *unique*. (i.e., no multiple edges.)
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,69 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0778. Swim In Rising Water :hard:
|
* TODO 0778. Swim In Rising Water :hard:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0778. Swim In Rising Water][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0778. Swim In Rising Water][0778. Swim In Rising Water]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are given an ~n x n~ integer matrix ~grid~ where each value ~grid[i][j]~ represents the elevation at that point ~(i, j)~.
|
||||||
|
|
||||||
|
It starts raining, and water gradually rises over time. At time ~t~, the water level is ~t~, meaning *any* cell with elevation less than equal to ~t~ is submerged or reachable.
|
||||||
|
|
||||||
|
You can swim from a square to another 4-directionally adjacent square if and only if the elevation of both squares individually are at most ~t~. You can swim infinite distances in zero time. Of course, you must stay within the boundaries of the grid during your swim.
|
||||||
|
|
||||||
|
Return /the minimum time until you can reach the bottom right square /~(n - 1, n - 1)~/ if you start at the top left square /~(0, 0)~.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: grid = [[0,2],[1,3]]
|
||||||
|
Output: 3
|
||||||
|
Explanation:
|
||||||
|
At time 0, you are in grid location (0, 0).
|
||||||
|
You cannot go anywhere else because 4-directionally adjacent neighbors have a higher elevation than t = 0.
|
||||||
|
You cannot reach point (1, 1) until time 3.
|
||||||
|
When the depth of water is 3, we can swim anywhere inside the grid.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: grid = [[0,1,2,3,4],[24,23,22,21,5],[12,13,14,15,16],[11,17,18,19,20],[10,9,8,7,6]]
|
||||||
|
Output: 16
|
||||||
|
Explanation: The final route is shown.
|
||||||
|
We need to wait until time 16 so that (0, 0) and (4, 4) are connected.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~n == grid.length~
|
||||||
|
|
||||||
|
- ~n == grid[i].length~
|
||||||
|
|
||||||
|
- ~1 <= n <= 50~
|
||||||
|
|
||||||
|
- ~0 <= grid[i][j] < n^{2}~
|
||||||
|
|
||||||
|
- Each value ~grid[i][j]~ is *unique*.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def swimInWater(self, grid: List[List[int]]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int swimInWater(vector<vector<int>>& grid) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,85 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0787. Cheapest Flights Within K Stops :medium:
|
* TODO 0787. Cheapest Flights Within K Stops :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0787. Cheapest Flights Within K Stops][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0787. Cheapest Flights Within K Stops][0787. Cheapest Flights Within K Stops]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
There are ~n~ cities connected by some number of flights. You are given an array ~flights~ where ~flights[i] = [from_{i}, to_{i}, price_{i}]~ indicates that there is a flight from city ~from_{i}~ to city ~to_{i}~ with cost ~price_{i}~.
|
||||||
|
|
||||||
|
You are also given three integers ~src~, ~dst~, and ~k~, return /*the cheapest price* from /~src~/ to /~dst~/ with at most /~k~/ stops. /If there is no such route, return/ /~-1~.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 4, flights = [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]], src = 0, dst = 3, k = 1
|
||||||
|
Output: 700
|
||||||
|
Explanation:
|
||||||
|
The graph is shown above.
|
||||||
|
The optimal path with at most 1 stop from city 0 to 3 is marked in red and has cost 100 + 600 = 700.
|
||||||
|
Note that the path through cities [0,1,2,3] is cheaper but is invalid because it uses 2 stops.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1
|
||||||
|
Output: 200
|
||||||
|
Explanation:
|
||||||
|
The graph is shown above.
|
||||||
|
The optimal path with at most 1 stop from city 0 to 2 is marked in red and has cost 100 + 100 = 200.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0
|
||||||
|
Output: 500
|
||||||
|
Explanation:
|
||||||
|
The graph is shown above.
|
||||||
|
The optimal path with no stops from city 0 to 2 is marked in red and has cost 500.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~2 <= n <= 100~
|
||||||
|
|
||||||
|
- ~0 <= flights.length <= (n * (n - 1) / 2)~
|
||||||
|
|
||||||
|
- ~flights[i].length == 3~
|
||||||
|
|
||||||
|
- ~0 <= from_{i}, to_{i} < n~
|
||||||
|
|
||||||
|
- ~from_{i} != to_{i}~
|
||||||
|
|
||||||
|
- ~1 <= price_{i} <= 10^{4}~
|
||||||
|
|
||||||
|
- There will not be any multiple flights between two cities.
|
||||||
|
|
||||||
|
- ~0 <= src, dst, k < n~
|
||||||
|
|
||||||
|
- ~src != dst~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int k) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,60 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 1584. Min Cost to Connect All Points :medium:
|
* TODO 1584. Min Cost to Connect All Points :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*1584. Min Cost to Connect All Points][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*1584. Min Cost to Connect All Points][1584. Min Cost to Connect All Points]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are given an array ~points~ representing integer coordinates of some points on a 2D-plane, where ~points[i] = [x_{i}, y_{i}]~.
|
||||||
|
|
||||||
|
The cost of connecting two points ~[x_{i}, y_{i}]~ and ~[x_{j}, y_{j}]~ is the *manhattan distance* between them: ~|x_{i} - x_{j}| + |y_{i} - y_{j}|~, where ~|val|~ denotes the absolute value of ~val~.
|
||||||
|
|
||||||
|
Return /the minimum cost to make all points connected./ All points are connected if there is *exactly one* simple path between any two points.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: points = [[0,0],[2,2],[3,10],[5,2],[7,0]]
|
||||||
|
Output: 20
|
||||||
|
Explanation:
|
||||||
|
|
||||||
|
We can connect the points as shown above to get the minimum cost of 20.
|
||||||
|
Notice that there is a unique path between every pair of points.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: points = [[3,12],[-2,5],[-4,1]]
|
||||||
|
Output: 18
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= points.length <= 1000~
|
||||||
|
|
||||||
|
- ~-10^{6} <= x_{i}, y_{i} <= 10^{6}~
|
||||||
|
|
||||||
|
- All pairs ~(x_{i}, y_{i})~ are distinct.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def minCostConnectPoints(self, points: List[List[int]]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int minCostConnectPoints(vector<vector<int>>& points) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
+62
-3
@@ -1,18 +1,77 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 2493. Divide Nodes Into the Maximum Number of Groups :hard:
|
* TODO 2493. Divide Nodes Into the Maximum Number of Groups :hard:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*2493. Divide Nodes Into the Maximum Number of Groups][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*2493. Divide Nodes Into the Maximum Number of Groups][2493. Divide Nodes Into the Maximum Number of Groups]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are given a positive integer ~n~ representing the number of nodes in an *undirected* graph. The nodes are labeled from ~1~ to ~n~.
|
||||||
|
|
||||||
|
You are also given a 2D integer array ~edges~, where ~edges[i] = [a_{i, }b_{i}]~ indicates that there is a *bidirectional* edge between nodes ~a_{i}~ and ~b_{i}~. *Notice* that the given graph may be disconnected.
|
||||||
|
|
||||||
|
Divide the nodes of the graph into ~m~ groups (*1-indexed*) such that:
|
||||||
|
|
||||||
|
- Each node in the graph belongs to exactly one group.
|
||||||
|
|
||||||
|
- For every pair of nodes in the graph that are connected by an edge ~[a_{i, }b_{i}]~, if ~a_{i}~ belongs to the group with index ~x~, and ~b_{i}~ belongs to the group with index ~y~, then ~|y - x| = 1~.
|
||||||
|
|
||||||
|
Return /the maximum number of groups (i.e., maximum /~m~/) into which you can divide the nodes/. Return ~-1~ /if it is impossible to group the nodes with the given conditions/.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 6, edges = [[1,2],[1,4],[1,5],[2,6],[2,3],[4,6]]
|
||||||
|
Output: 4
|
||||||
|
Explanation: As shown in the image we:
|
||||||
|
- Add node 5 to the first group.
|
||||||
|
- Add node 1 to the second group.
|
||||||
|
- Add nodes 2 and 4 to the third group.
|
||||||
|
- Add nodes 3 and 6 to the fourth group.
|
||||||
|
We can see that every edge is satisfied.
|
||||||
|
It can be shown that that if we create a fifth group and move any node from the third or fourth group to it, at least on of the edges will not be satisfied.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 3, edges = [[1,2],[2,3],[3,1]]
|
||||||
|
Output: -1
|
||||||
|
Explanation: If we add node 1 to the first group, node 2 to the second group, and node 3 to the third group to satisfy the first two edges, we can see that the third edge will not be satisfied.
|
||||||
|
It can be shown that no grouping is possible.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= n <= 500~
|
||||||
|
|
||||||
|
- ~1 <= edges.length <= 10^{4}~
|
||||||
|
|
||||||
|
- ~edges[i].length == 2~
|
||||||
|
|
||||||
|
- ~1 <= a_{i}, b_{i} <= n~
|
||||||
|
|
||||||
|
- ~a_{i} != b_{i}~
|
||||||
|
|
||||||
|
- There is at most one edge between any pair of vertices.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def magnificentSets(self, n: int, edges: List[List[int]]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int magnificentSets(int n, vector<vector<int>>& edges) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,85 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 2812. Find the Safest Path in a Grid :medium:
|
* TODO 2812. Find the Safest Path in a Grid :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*2812. Find the Safest Path in a Grid][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*2812. Find the Safest Path in a Grid][2812. Find the Safest Path in a Grid]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are given a *0-indexed* 2D matrix ~grid~ of size ~n x n~, where ~(r, c)~ represents:
|
||||||
|
|
||||||
|
- A cell containing a thief if ~grid[r][c] = 1~
|
||||||
|
|
||||||
|
- An empty cell if ~grid[r][c] = 0~
|
||||||
|
|
||||||
|
You are initially positioned at cell ~(0, 0)~. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.
|
||||||
|
|
||||||
|
The *safeness factor* of a path on the grid is defined as the *minimum* manhattan distance from any cell in the path to any thief in the grid.
|
||||||
|
|
||||||
|
Return /the *maximum safeness factor* of all paths leading to cell /~(n - 1, n - 1)~/./
|
||||||
|
|
||||||
|
An *adjacent* cell of cell ~(r, c)~, is one of the cells ~(r, c + 1)~, ~(r, c - 1)~, ~(r + 1, c)~ and ~(r - 1, c)~ if it exists.
|
||||||
|
|
||||||
|
The *Manhattan distance* between two cells ~(a, b)~ and ~(x, y)~ is equal to ~|a - x| + |b - y|~, where ~|val|~ denotes the absolute value of val.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: grid = [[1,0,0],[0,0,0],[0,0,1]]
|
||||||
|
Output: 0
|
||||||
|
Explanation: All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: grid = [[0,0,1],[0,0,0],[0,0,0]]
|
||||||
|
Output: 2
|
||||||
|
Explanation: The path depicted in the picture above has a safeness factor of 2 since:
|
||||||
|
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
|
||||||
|
It can be shown that there are no other paths with a higher safeness factor.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
|
||||||
|
Output: 2
|
||||||
|
Explanation: The path depicted in the picture above has a safeness factor of 2 since:
|
||||||
|
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
|
||||||
|
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
|
||||||
|
It can be shown that there are no other paths with a higher safeness factor.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= grid.length == n <= 400~
|
||||||
|
|
||||||
|
- ~grid[i].length == n~
|
||||||
|
|
||||||
|
- ~grid[i][j]~ is either ~0~ or ~1~.
|
||||||
|
|
||||||
|
- There is at least one thief in the ~grid~.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def maximumSafenessFactor(self, grid: List[List[int]]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int maximumSafenessFactor(vector<vector<int>>& grid) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,70 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0001. Two Sum :easy:
|
* TODO 0001. Two Sum :easy:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0001. Two Sum][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0001. Two Sum][0001. Two Sum]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an array of integers ~nums~ and an integer ~target~, return /indices of the two numbers such that they add up to ~target~/.
|
||||||
|
|
||||||
|
You may assume that each input would have */exactly/ one solution*, and you may not use the /same/ element twice.
|
||||||
|
|
||||||
|
You can return the answer in any order.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [2,7,11,15], target = 9
|
||||||
|
Output: [0,1]
|
||||||
|
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [3,2,4], target = 6
|
||||||
|
Output: [1,2]
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [3,3], target = 6
|
||||||
|
Output: [0,1]
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~2 <= nums.length <= 10^{4}~
|
||||||
|
|
||||||
|
- ~-10^{9} <= nums[i] <= 10^{9}~
|
||||||
|
|
||||||
|
- ~-10^{9} <= target <= 10^{9}~
|
||||||
|
|
||||||
|
- *Only one valid answer exists.*
|
||||||
|
|
||||||
|
*Follow-up: *Can you come up with an algorithm that is less than ~O(n^{2})~ time complexity?
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def twoSum(self, nums: List[int], target: int) -> List[int]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<int> twoSum(vector<int>& nums, int target) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,83 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0036. Valid Sudoku :medium:
|
* TODO 0036. Valid Sudoku :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0036. Valid Sudoku][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0036. Valid Sudoku][0036. Valid Sudoku]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Determine if a ~9 x 9~ Sudoku board is valid. Only the filled cells need to be validated *according to the following rules*:
|
||||||
|
|
||||||
|
- Each row must contain the digits ~1-9~ without repetition.
|
||||||
|
|
||||||
|
- Each column must contain the digits ~1-9~ without repetition.
|
||||||
|
|
||||||
|
- Each of the nine ~3 x 3~ sub-boxes of the grid must contain the digits ~1-9~ without repetition.
|
||||||
|
|
||||||
|
*Note:*
|
||||||
|
|
||||||
|
- A Sudoku board (partially filled) could be valid but is not necessarily solvable.
|
||||||
|
|
||||||
|
- Only the filled cells need to be validated according to the mentioned rules.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: board =
|
||||||
|
[["5","3",".",".","7",".",".",".","."]
|
||||||
|
,["6",".",".","1","9","5",".",".","."]
|
||||||
|
,[".","9","8",".",".",".",".","6","."]
|
||||||
|
,["8",".",".",".","6",".",".",".","3"]
|
||||||
|
,["4",".",".","8",".","3",".",".","1"]
|
||||||
|
,["7",".",".",".","2",".",".",".","6"]
|
||||||
|
,[".","6",".",".",".",".","2","8","."]
|
||||||
|
,[".",".",".","4","1","9",".",".","5"]
|
||||||
|
,[".",".",".",".","8",".",".","7","9"]]
|
||||||
|
Output: true
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: board =
|
||||||
|
[["8","3",".",".","7",".",".",".","."]
|
||||||
|
,["6",".",".","1","9","5",".",".","."]
|
||||||
|
,[".","9","8",".",".",".",".","6","."]
|
||||||
|
,["8",".",".",".","6",".",".",".","3"]
|
||||||
|
,["4",".",".","8",".","3",".",".","1"]
|
||||||
|
,["7",".",".",".","2",".",".",".","6"]
|
||||||
|
,[".","6",".",".",".",".","2","8","."]
|
||||||
|
,[".",".",".","4","1","9",".",".","5"]
|
||||||
|
,[".",".",".",".","8",".",".","7","9"]]
|
||||||
|
Output: false
|
||||||
|
Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~board.length == 9~
|
||||||
|
|
||||||
|
- ~board[i].length == 9~
|
||||||
|
|
||||||
|
- ~board[i][j]~ is a digit ~1-9~ or ~'.'~.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def isValidSudoku(self, board: List[List[str]]) -> bool:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
bool isValidSudoku(vector<vector<char>>& board) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,60 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0049. Group Anagrams :medium:
|
* TODO 0049. Group Anagrams :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0049. Group Anagrams][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0049. Group Anagrams][0049. Group Anagrams]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an array of strings ~strs~, group the anagrams together. You can return the answer in *any order*.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
*Input:* strs = ["eat","tea","tan","ate","nat","bat"]
|
||||||
|
|
||||||
|
*Output:* [["bat"],["nat","tan"],["ate","eat","tea"]]
|
||||||
|
|
||||||
|
*Explanation:*
|
||||||
|
|
||||||
|
- There is no string in strs that can be rearranged to form ~"bat"~.
|
||||||
|
|
||||||
|
- The strings ~"nat"~ and ~"tan"~ are anagrams as they can be rearranged to form each other.
|
||||||
|
|
||||||
|
- The strings ~"ate"~, ~"eat"~, and ~"tea"~ are anagrams as they can be rearranged to form each other.
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
*Input:* strs = [""]
|
||||||
|
|
||||||
|
*Output:* [[""]]
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
*Input:* strs = ["a"]
|
||||||
|
|
||||||
|
*Output:* [["a"]]
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= strs.length <= 10^{4}~
|
||||||
|
|
||||||
|
- ~0 <= strs[i].length <= 100~
|
||||||
|
|
||||||
|
- ~strs[i]~ consists of lowercase English letters.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<vector<string>> groupAnagrams(vector<string>& strs) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,62 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0128. Longest Consecutive Sequence :medium:
|
* TODO 0128. Longest Consecutive Sequence :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0128. Longest Consecutive Sequence][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0128. Longest Consecutive Sequence][0128. Longest Consecutive Sequence]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an unsorted array of integers ~nums~, return /the length of the longest consecutive elements sequence./
|
||||||
|
|
||||||
|
You must write an algorithm that runs in ~O(n)~ time.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [100,4,200,1,3,2]
|
||||||
|
Output: 4
|
||||||
|
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [0,3,7,2,5,8,4,6,0,1]
|
||||||
|
Output: 9
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [1,0,1,2]
|
||||||
|
Output: 3
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~0 <= nums.length <= 10^{5}~
|
||||||
|
|
||||||
|
- ~-10^{9} <= nums[i] <= 10^{9}~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def longestConsecutive(self, nums: List[int]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int longestConsecutive(vector<int>& nums) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,58 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0217. Contains Duplicate :easy:
|
* TODO 0217. Contains Duplicate :easy:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0217. Contains Duplicate][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0217. Contains Duplicate][0217. Contains Duplicate]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an integer array ~nums~, return ~true~ if any value appears *at least twice* in the array, and return ~false~ if every element is distinct.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
*Input:* nums = [1,2,3,1]
|
||||||
|
|
||||||
|
*Output:* true
|
||||||
|
|
||||||
|
*Explanation:*
|
||||||
|
|
||||||
|
The element 1 occurs at the indices 0 and 3.
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
*Input:* nums = [1,2,3,4]
|
||||||
|
|
||||||
|
*Output:* false
|
||||||
|
|
||||||
|
*Explanation:*
|
||||||
|
|
||||||
|
All elements are distinct.
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
*Input:* nums = [1,1,1,3,3,4,3,2,4,2]
|
||||||
|
|
||||||
|
*Output:* true
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= nums.length <= 10^{5}~
|
||||||
|
|
||||||
|
- ~-10^{9} <= nums[i] <= 10^{9}~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def containsDuplicate(self, nums: List[int]) -> bool:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
bool containsDuplicate(vector<int>& nums) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,56 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0238. Product of Array Except Self :medium:
|
* TODO 0238. Product of Array Except Self :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0238. Product of Array Except Self][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0238. Product of Array Except Self][0238. Product of Array Except Self]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an integer array ~nums~, return /an array/ ~answer~ /such that/ ~answer[i]~ /is equal to the product of all the elements of/ ~nums~ /except/ ~nums[i]~.
|
||||||
|
|
||||||
|
The product of any prefix or suffix of ~nums~ is *guaranteed* to fit in a *32-bit* integer.
|
||||||
|
|
||||||
|
You must write an algorithm that runs in ~O(n)~ time and without using the division operation.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [1,2,3,4]
|
||||||
|
Output: [24,12,8,6]
|
||||||
|
#+end_src
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [-1,1,0,-3,3]
|
||||||
|
Output: [0,0,9,0,0]
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~2 <= nums.length <= 10^{5}~
|
||||||
|
|
||||||
|
- ~-30 <= nums[i] <= 30~
|
||||||
|
|
||||||
|
- The input is generated such that ~answer[i]~ is *guaranteed* to fit in a *32-bit* integer.
|
||||||
|
|
||||||
|
*Follow up:* Can you solve the problem in ~O(1)~ extra space complexity? (The output array *does not* count as extra space for space complexity analysis.)
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def productExceptSelf(self, nums: List[int]) -> List[int]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<int> productExceptSelf(vector<int>& nums) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,46 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0242. Valid Anagram :easy:
|
* TODO 0242. Valid Anagram :easy:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0242. Valid Anagram][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0242. Valid Anagram][0242. Valid Anagram]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given two strings ~s~ and ~t~, return ~true~ if ~t~ is an anagram of ~s~, and ~false~ otherwise.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
*Input:* s = "anagram", t = "nagaram"
|
||||||
|
|
||||||
|
*Output:* true
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
*Input:* s = "rat", t = "car"
|
||||||
|
|
||||||
|
*Output:* false
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= s.length, t.length <= 5 * 10^{4}~
|
||||||
|
|
||||||
|
- ~s~ and ~t~ consist of lowercase English letters.
|
||||||
|
|
||||||
|
*Follow up:* What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def isAnagram(self, s: str, t: str) -> bool:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
bool isAnagram(string s, string t) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0271. Encode and Decode Strings :medium:
|
* TODO 0271. Encode and Decode Strings :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0271. Encode and Decode Strings][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0271. Encode and Decode Strings][0271. Encode and Decode Strings]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
|
|||||||
@@ -1,18 +1,56 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0347. Top K Frequent Elements :medium:
|
* TODO 0347. Top K Frequent Elements :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0347. Top K Frequent Elements][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0347. Top K Frequent Elements][0347. Top K Frequent Elements]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an integer array ~nums~ and an integer ~k~, return /the/ ~k~ /most frequent elements/. You may return the answer in *any order*.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
*Input:* nums = [1,1,1,2,2,3], k = 2
|
||||||
|
|
||||||
|
*Output:* [1,2]
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
*Input:* nums = [1], k = 1
|
||||||
|
|
||||||
|
*Output:* [1]
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
*Input:* nums = [1,2,1,2,1,2,3,1,3,2], k = 2
|
||||||
|
|
||||||
|
*Output:* [1,2]
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= nums.length <= 10^{5}~
|
||||||
|
|
||||||
|
- ~-10^{4} <= nums[i] <= 10^{4}~
|
||||||
|
|
||||||
|
- ~k~ is in the range ~[1, the number of unique elements in the array]~.
|
||||||
|
|
||||||
|
- It is *guaranteed* that the answer is *unique*.
|
||||||
|
|
||||||
|
*Follow up:* Your algorithm's time complexity must be better than ~O(n log n)~, where n is the array's size.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<int> topKFrequent(vector<int>& nums, int k) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,67 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 1408. String Matching in an Array :easy:
|
* TODO 1408. String Matching in an Array :easy:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*1408. String Matching in an Array][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*1408. String Matching in an Array][1408. String Matching in an Array]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an array of string ~words~, return all strings in/ /~words~/ /that are a substring of another word. You can return the answer in *any order*.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: words = ["mass","as","hero","superhero"]
|
||||||
|
Output: ["as","hero"]
|
||||||
|
Explanation: "as" is substring of "mass" and "hero" is substring of "superhero".
|
||||||
|
["hero","as"] is also a valid answer.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: words = ["leetcode","et","code"]
|
||||||
|
Output: ["et","code"]
|
||||||
|
Explanation: "et", "code" are substring of "leetcode".
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: words = ["blue","green","bu"]
|
||||||
|
Output: []
|
||||||
|
Explanation: No string of words is substring of another string.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= words.length <= 100~
|
||||||
|
|
||||||
|
- ~1 <= words[i].length <= 30~
|
||||||
|
|
||||||
|
- ~words[i]~ contains only lowercase English letters.
|
||||||
|
|
||||||
|
- All the strings of ~words~ are *unique*.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def stringMatching(self, words: List[str]) -> List[str]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<string> stringMatching(vector<string>& words) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
+47
-3
@@ -1,18 +1,62 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 1769. Minimum Number of Operations to Move All Balls to Each Box :medium:
|
* TODO 1769. Minimum Number of Operations to Move All Balls to Each Box :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*1769. Minimum Number of Operations to Move All Balls to Each Box][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*1769. Minimum Number of Operations to Move All Balls to Each Box][1769. Minimum Number of Operations to Move All Balls to Each Box]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You have ~n~ boxes. You are given a binary string ~boxes~ of length ~n~, where ~boxes[i]~ is ~'0'~ if the ~i^{th}~ box is *empty*, and ~'1'~ if it contains *one* ball.
|
||||||
|
|
||||||
|
In one operation, you can move *one* ball from a box to an adjacent box. Box ~i~ is adjacent to box ~j~ if ~abs(i - j) == 1~. Note that after doing so, there may be more than one ball in some boxes.
|
||||||
|
|
||||||
|
Return an array ~answer~ of size ~n~, where ~answer[i]~ is the *minimum* number of operations needed to move all the balls to the ~i^{th}~ box.
|
||||||
|
|
||||||
|
Each ~answer[i]~ is calculated considering the *initial* state of the boxes.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: boxes = "110"
|
||||||
|
Output: [1,1,3]
|
||||||
|
Explanation: The answer for each box is as follows:
|
||||||
|
1) First box: you will have to move one ball from the second box to the first box in one operation.
|
||||||
|
2) Second box: you will have to move one ball from the first box to the second box in one operation.
|
||||||
|
3) Third box: you will have to move one ball from the first box to the third box in two operations, and move one ball from the second box to the third box in one operation.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: boxes = "001011"
|
||||||
|
Output: [11,8,5,4,3,4]
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~n == boxes.length~
|
||||||
|
|
||||||
|
- ~1 <= n <= 2000~
|
||||||
|
|
||||||
|
- ~boxes[i]~ is either ~'0'~ or ~'1'~.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def minOperations(self, boxes: str) -> List[int]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<int> minOperations(string boxes) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,68 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 2678. Number of Senior Citizens :easy:
|
* TODO 2678. Number of Senior Citizens :easy:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*2678. Number of Senior Citizens][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*2678. Number of Senior Citizens][2678. Number of Senior Citizens]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are given a *0-indexed* array of strings ~details~. Each element of ~details~ provides information about a given passenger compressed into a string of length ~15~. The system is such that:
|
||||||
|
|
||||||
|
- The first ten characters consist of the phone number of passengers.
|
||||||
|
|
||||||
|
- The next character denotes the gender of the person.
|
||||||
|
|
||||||
|
- The following two characters are used to indicate the age of the person.
|
||||||
|
|
||||||
|
- The last two characters determine the seat allotted to that person.
|
||||||
|
|
||||||
|
Return /the number of passengers who are *strictly **more than 60 years old*./
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
|
||||||
|
Output: 2
|
||||||
|
Explanation: The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: details = ["1313579440F2036","2921522980M5644"]
|
||||||
|
Output: 0
|
||||||
|
Explanation: None of the passengers are older than 60.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= details.length <= 100~
|
||||||
|
|
||||||
|
- ~details[i].length == 15~
|
||||||
|
|
||||||
|
- ~details[i] consists of digits from '0' to '9'.~
|
||||||
|
|
||||||
|
- ~details[i][10] is either 'M' or 'F' or 'O'.~
|
||||||
|
|
||||||
|
- The phone numbers and seat numbers of the passengers are distinct.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def countSeniors(self, details: List[str]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int countSeniors(vector<string>& details) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,52 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0017. Letter Combinations of a Phone Number :medium:
|
* TODO 0017. Letter Combinations of a Phone Number :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0017. Letter Combinations of a Phone Number][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0017. Letter Combinations of a Phone Number][0017. Letter Combinations of a Phone Number]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given a string containing digits from ~2-9~ inclusive, return all possible letter combinations that the number could represent. Return the answer in *any order*.
|
||||||
|
|
||||||
|
A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: digits = "23"
|
||||||
|
Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: digits = "2"
|
||||||
|
Output: ["a","b","c"]
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= digits.length <= 4~
|
||||||
|
|
||||||
|
- ~digits[i]~ is a digit in the range ~['2', '9']~.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def letterCombinations(self, digits: str) -> List[str]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<string> letterCombinations(string digits) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,46 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0022. Generate Parentheses :medium:
|
* TODO 0022. Generate Parentheses :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0022. Generate Parentheses][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0022. Generate Parentheses][0022. Generate Parentheses]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given ~n~ pairs of parentheses, write a function to /generate all combinations of well-formed parentheses/.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 3
|
||||||
|
Output: ["((()))","(()())","(())()","()(())","()()()"]
|
||||||
|
#+end_src
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 1
|
||||||
|
Output: ["()"]
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= n <= 8~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def generateParenthesis(self, n: int) -> List[str]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<string> generateParenthesis(int n) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,71 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0039. Combination Sum :medium:
|
* TODO 0039. Combination Sum :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0039. Combination Sum][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0039. Combination Sum][0039. Combination Sum]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an array of *distinct* integers ~candidates~ and a target integer ~target~, return /a list of all *unique combinations* of /~candidates~/ where the chosen numbers sum to /~target~/./ You may return the combinations in *any order*.
|
||||||
|
|
||||||
|
The *same* number may be chosen from ~candidates~ an *unlimited number of times*. Two combinations are unique if the frequency of at least one of the chosen numbers is different.
|
||||||
|
|
||||||
|
The test cases are generated such that the number of unique combinations that sum up to ~target~ is less than ~150~ combinations for the given input.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: candidates = [2,3,6,7], target = 7
|
||||||
|
Output: [[2,2,3],[7]]
|
||||||
|
Explanation:
|
||||||
|
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
|
||||||
|
7 is a candidate, and 7 = 7.
|
||||||
|
These are the only two combinations.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: candidates = [2,3,5], target = 8
|
||||||
|
Output: [[2,2,2,2],[2,3,3],[3,5]]
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: candidates = [2], target = 1
|
||||||
|
Output: []
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= candidates.length <= 30~
|
||||||
|
|
||||||
|
- ~2 <= candidates[i] <= 40~
|
||||||
|
|
||||||
|
- All elements of ~candidates~ are *distinct*.
|
||||||
|
|
||||||
|
- ~1 <= target <= 40~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,66 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0040. Combination Sum II :medium:
|
* TODO 0040. Combination Sum II :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0040. Combination Sum II][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0040. Combination Sum II][0040. Combination Sum II]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given a collection of candidate numbers (~candidates~) and a target number (~target~), find all unique combinations in ~candidates~ where the candidate numbers sum to ~target~.
|
||||||
|
|
||||||
|
Each number in ~candidates~ may only be used *once* in the combination.
|
||||||
|
|
||||||
|
*Note:* The solution set must not contain duplicate combinations.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: candidates = [10,1,2,7,6,1,5], target = 8
|
||||||
|
Output:
|
||||||
|
[
|
||||||
|
[1,1,6],
|
||||||
|
[1,2,5],
|
||||||
|
[1,7],
|
||||||
|
[2,6]
|
||||||
|
]
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: candidates = [2,5,2,1,2], target = 5
|
||||||
|
Output:
|
||||||
|
[
|
||||||
|
[1,2,2],
|
||||||
|
[5]
|
||||||
|
]
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= candidates.length <= 100~
|
||||||
|
|
||||||
|
- ~1 <= candidates[i] <= 50~
|
||||||
|
|
||||||
|
- ~1 <= target <= 30~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,57 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0046. Permutations :medium:
|
* TODO 0046. Permutations :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0046. Permutations][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0046. Permutations][0046. Permutations]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an array ~nums~ of distinct integers, return all the possible permutations. You can return the answer in *any order*.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [1,2,3]
|
||||||
|
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
|
||||||
|
#+end_src
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [0,1]
|
||||||
|
Output: [[0,1],[1,0]]
|
||||||
|
#+end_src
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [1]
|
||||||
|
Output: [[1]]
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= nums.length <= 6~
|
||||||
|
|
||||||
|
- ~-10 <= nums[i] <= 10~
|
||||||
|
|
||||||
|
- All the integers of ~nums~ are *unique*.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def permute(self, nums: List[int]) -> List[List[int]]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<vector<int>> permute(vector<int>& nums) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,53 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0051. N Queens :hard:
|
* TODO 0051. N Queens :hard:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0051. N Queens][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0051. N Queens][0051. N Queens]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
The *n-queens* puzzle is the problem of placing ~n~ queens on an ~n x n~ chessboard such that no two queens attack each other.
|
||||||
|
|
||||||
|
Given an integer ~n~, return /all distinct solutions to the *n-queens puzzle*/. You may return the answer in *any order*.
|
||||||
|
|
||||||
|
Each solution contains a distinct board configuration of the n-queens' placement, where ~'Q'~ and ~'.'~ both indicate a queen and an empty space, respectively.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 4
|
||||||
|
Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
|
||||||
|
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 1
|
||||||
|
Output: [["Q"]]
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= n <= 9~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def solveNQueens(self, n: int) -> List[List[str]]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<vector<string>> solveNQueens(int n) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,51 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0052. N Queens II :hard:
|
* TODO 0052. N Queens II :hard:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0052. N Queens II][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0052. N Queens II][0052. N Queens II]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
The *n-queens* puzzle is the problem of placing ~n~ queens on an ~n x n~ chessboard such that no two queens attack each other.
|
||||||
|
|
||||||
|
Given an integer ~n~, return /the number of distinct solutions to the *n-queens puzzle*/.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 4
|
||||||
|
Output: 2
|
||||||
|
Explanation: There are two distinct solutions to the 4-queens puzzle as shown.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 1
|
||||||
|
Output: 1
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= n <= 9~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def totalNQueens(self, n: int) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int totalNQueens(int n) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,55 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0077. Combinations :medium:
|
* TODO 0077. Combinations :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0077. Combinations][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0077. Combinations][0077. Combinations]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given two integers ~n~ and ~k~, return /all possible combinations of/ ~k~ /numbers chosen from the range/ ~[1, n]~.
|
||||||
|
|
||||||
|
You may return the answer in *any order*.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 4, k = 2
|
||||||
|
Output: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
|
||||||
|
Explanation: There are 4 choose 2 = 6 total combinations.
|
||||||
|
Note that combinations are unordered, i.e., [1,2] and [2,1] are considered to be the same combination.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 1, k = 1
|
||||||
|
Output: [[1]]
|
||||||
|
Explanation: There is 1 choose 1 = 1 total combination.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= n <= 20~
|
||||||
|
|
||||||
|
- ~1 <= k <= n~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def combine(self, n: int, k: int) -> List[List[int]]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<vector<int>> combine(int n, int k) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,54 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0078. Subsets :medium:
|
* TODO 0078. Subsets :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0078. Subsets][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0078. Subsets][0078. Subsets]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an integer array ~nums~ of *unique* elements, return /all possible/ /subsets/ /(the power set)/.
|
||||||
|
|
||||||
|
The solution set *must not* contain duplicate subsets. Return the solution in *any order*.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [1,2,3]
|
||||||
|
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [0]
|
||||||
|
Output: [[],[0]]
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= nums.length <= 10~
|
||||||
|
|
||||||
|
- ~-10 <= nums[i] <= 10~
|
||||||
|
|
||||||
|
- All the numbers of ~nums~ are *unique*.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def subsets(self, nums: List[int]) -> List[List[int]]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<vector<int>> subsets(vector<int>& nums) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,69 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0079. Word Search :medium:
|
* TODO 0079. Word Search :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0079. Word Search][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0079. Word Search][0079. Word Search]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an ~m x n~ grid of characters ~board~ and a string ~word~, return ~true~ /if/ ~word~ /exists in the grid/.
|
||||||
|
|
||||||
|
The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
|
||||||
|
Output: true
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
|
||||||
|
Output: true
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
|
||||||
|
Output: false
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~m == board.length~
|
||||||
|
|
||||||
|
- ~n = board[i].length~
|
||||||
|
|
||||||
|
- ~1 <= m, n <= 6~
|
||||||
|
|
||||||
|
- ~1 <= word.length <= 15~
|
||||||
|
|
||||||
|
- ~board~ and ~word~ consists of only lowercase and uppercase English letters.
|
||||||
|
|
||||||
|
*Follow up:* Could you use search pruning to make your solution faster with a larger ~board~?
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def exist(self, board: List[List[str]], word: str) -> bool:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
bool exist(vector<vector<char>>& board, string word) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,50 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0090. Subsets II :medium:
|
* TODO 0090. Subsets II :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0090. Subsets II][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0090. Subsets II][0090. Subsets II]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an integer array ~nums~ that may contain duplicates, return /all possible/ /subsets// (the power set)/.
|
||||||
|
|
||||||
|
The solution set *must not* contain duplicate subsets. Return the solution in *any order*.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [1,2,2]
|
||||||
|
Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]
|
||||||
|
#+end_src
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [0]
|
||||||
|
Output: [[],[0]]
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= nums.length <= 10~
|
||||||
|
|
||||||
|
- ~-10 <= nums[i] <= 10~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,48 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0131. Palindrome Partitioning :medium:
|
* TODO 0131. Palindrome Partitioning :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0131. Palindrome Partitioning][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0131. Palindrome Partitioning][0131. Palindrome Partitioning]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given a string ~s~, partition ~s~ such that every substring of the partition is a *palindrome*. Return /all possible palindrome partitioning of /~s~.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: s = "aab"
|
||||||
|
Output: [["a","a","b"],["aa","b"]]
|
||||||
|
#+end_src
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: s = "a"
|
||||||
|
Output: [["a"]]
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= s.length <= 16~
|
||||||
|
|
||||||
|
- ~s~ contains only lowercase English letters.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def partition(self, s: str) -> List[List[str]]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<vector<string>> partition(string s) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0351. Android Unlock Patterns :medium:
|
* TODO 0351. Android Unlock Patterns :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0351. Android Unlock Patterns][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0351. Android Unlock Patterns][0351. Android Unlock Patterns]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
|
|||||||
@@ -1,18 +1,62 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 1079. Letter Tile Possibilities :medium:
|
* TODO 1079. Letter Tile Possibilities :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*1079. Letter Tile Possibilities][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*1079. Letter Tile Possibilities][1079. Letter Tile Possibilities]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You have ~n~ ~tiles~, where each tile has one letter ~tiles[i]~ printed on it.
|
||||||
|
|
||||||
|
Return /the number of possible non-empty sequences of letters/ you can make using the letters printed on those ~tiles~.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: tiles = "AAB"
|
||||||
|
Output: 8
|
||||||
|
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: tiles = "AAABBC"
|
||||||
|
Output: 188
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: tiles = "V"
|
||||||
|
Output: 1
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= tiles.length <= 7~
|
||||||
|
|
||||||
|
- ~tiles~ consists of uppercase English letters.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def numTilePossibilities(self, tiles: str) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int numTilePossibilities(string tiles) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,62 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0004. Median of Two Sorted Arrays :hard:
|
* TODO 0004. Median of Two Sorted Arrays :hard:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0004. Median of Two Sorted Arrays][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0004. Median of Two Sorted Arrays][0004. Median of Two Sorted Arrays]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given two sorted arrays ~nums1~ and ~nums2~ of size ~m~ and ~n~ respectively, return *the median* of the two sorted arrays.
|
||||||
|
|
||||||
|
The overall run time complexity should be ~O(log (m+n))~.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums1 = [1,3], nums2 = [2]
|
||||||
|
Output: 2.00000
|
||||||
|
Explanation: merged array = [1,2,3] and median is 2.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums1 = [1,2], nums2 = [3,4]
|
||||||
|
Output: 2.50000
|
||||||
|
Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~nums1.length == m~
|
||||||
|
|
||||||
|
- ~nums2.length == n~
|
||||||
|
|
||||||
|
- ~0 <= m <= 1000~
|
||||||
|
|
||||||
|
- ~0 <= n <= 1000~
|
||||||
|
|
||||||
|
- ~1 <= m + n <= 2000~
|
||||||
|
|
||||||
|
- ~-10^{6} <= nums1[i], nums2[i] <= 10^{6}~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,67 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0033. Search In Rotated Sorted Array :medium:
|
* TODO 0033. Search In Rotated Sorted Array :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0033. Search In Rotated Sorted Array][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0033. Search In Rotated Sorted Array][0033. Search In Rotated Sorted Array]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
There is an integer array ~nums~ sorted in ascending order (with *distinct* values).
|
||||||
|
|
||||||
|
Prior to being passed to your function, ~nums~ is *possibly left rotated* at an unknown index ~k~ (~1 <= k < nums.length~) such that the resulting array is ~[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]~ (*0-indexed*). For example, ~[0,1,2,4,5,6,7]~ might be left rotated by ~3~ indices and become ~[4,5,6,7,0,1,2]~.
|
||||||
|
|
||||||
|
Given the array ~nums~ *after* the possible rotation and an integer ~target~, return /the index of /~target~/ if it is in /~nums~/, or /~-1~/ if it is not in /~nums~.
|
||||||
|
|
||||||
|
You must write an algorithm with ~O(log n)~ runtime complexity.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [4,5,6,7,0,1,2], target = 0
|
||||||
|
Output: 4
|
||||||
|
#+end_src
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [4,5,6,7,0,1,2], target = 3
|
||||||
|
Output: -1
|
||||||
|
#+end_src
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [1], target = 0
|
||||||
|
Output: -1
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= nums.length <= 5000~
|
||||||
|
|
||||||
|
- ~-10^{4} <= nums[i] <= 10^{4}~
|
||||||
|
|
||||||
|
- All values of ~nums~ are *unique*.
|
||||||
|
|
||||||
|
- ~nums~ is an ascending array that is possibly rotated.
|
||||||
|
|
||||||
|
- ~-10^{4} <= target <= 10^{4}~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def search(self, nums: List[int], target: int) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int search(vector<int>& nums, int target) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,62 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0074. Search a 2D Matrix :medium:
|
* TODO 0074. Search a 2D Matrix :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0074. Search a 2D Matrix][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0074. Search a 2D Matrix][0074. Search a 2D Matrix]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are given an ~m x n~ integer matrix ~matrix~ with the following two properties:
|
||||||
|
|
||||||
|
- Each row is sorted in non-decreasing order.
|
||||||
|
|
||||||
|
- The first integer of each row is greater than the last integer of the previous row.
|
||||||
|
|
||||||
|
Given an integer ~target~, return ~true~ /if/ ~target~ /is in/ ~matrix~ /or/ ~false~ /otherwise/.
|
||||||
|
|
||||||
|
You must write a solution in ~O(log(m * n))~ time complexity.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
|
||||||
|
Output: true
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
|
||||||
|
Output: false
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~m == matrix.length~
|
||||||
|
|
||||||
|
- ~n == matrix[i].length~
|
||||||
|
|
||||||
|
- ~1 <= m, n <= 100~
|
||||||
|
|
||||||
|
- ~-10^{4} <= matrix[i][j], target <= 10^{4}~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
bool searchMatrix(vector<vector<int>>& matrix, int target) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,78 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0153. Find Minimum In Rotated Sorted Array :medium:
|
* TODO 0153. Find Minimum In Rotated Sorted Array :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0153. Find Minimum In Rotated Sorted Array][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0153. Find Minimum In Rotated Sorted Array][0153. Find Minimum In Rotated Sorted Array]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Suppose an array of length ~n~ sorted in ascending order is *rotated* between ~1~ and ~n~ times. For example, the array ~nums = [0,1,2,4,5,6,7]~ might become:
|
||||||
|
|
||||||
|
- ~[4,5,6,7,0,1,2]~ if it was rotated ~4~ times.
|
||||||
|
|
||||||
|
- ~[0,1,2,4,5,6,7]~ if it was rotated ~7~ times.
|
||||||
|
|
||||||
|
Notice that *rotating* an array ~[a[0], a[1], a[2], ..., a[n-1]]~ 1 time results in the array ~[a[n-1], a[0], a[1], a[2], ..., a[n-2]]~.
|
||||||
|
|
||||||
|
Given the sorted rotated array ~nums~ of *unique* elements, return /the minimum element of this array/.
|
||||||
|
|
||||||
|
You must write an algorithm that runs in ~O(log n) time~.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [3,4,5,1,2]
|
||||||
|
Output: 1
|
||||||
|
Explanation: The original array was [1,2,3,4,5] rotated 3 times.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [4,5,6,7,0,1,2]
|
||||||
|
Output: 0
|
||||||
|
Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [11,13,15,17]
|
||||||
|
Output: 11
|
||||||
|
Explanation: The original array was [11,13,15,17] and it was rotated 4 times.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~n == nums.length~
|
||||||
|
|
||||||
|
- ~1 <= n <= 5000~
|
||||||
|
|
||||||
|
- ~-5000 <= nums[i] <= 5000~
|
||||||
|
|
||||||
|
- All the integers of ~nums~ are *unique*.
|
||||||
|
|
||||||
|
- ~nums~ is sorted and rotated between ~1~ and ~n~ times.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def findMin(self, nums: List[int]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int findMin(vector<int>& nums) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,58 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0704. Binary Search :easy:
|
* TODO 0704. Binary Search :easy:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0704. Binary Search][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0704. Binary Search][0704. Binary Search]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an array of integers ~nums~ which is sorted in ascending order, and an integer ~target~, write a function to search ~target~ in ~nums~. If ~target~ exists, then return its index. Otherwise, return ~-1~.
|
||||||
|
|
||||||
|
You must write an algorithm with ~O(log n)~ runtime complexity.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [-1,0,3,5,9,12], target = 9
|
||||||
|
Output: 4
|
||||||
|
Explanation: 9 exists in nums and its index is 4
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [-1,0,3,5,9,12], target = 2
|
||||||
|
Output: -1
|
||||||
|
Explanation: 2 does not exist in nums so return -1
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= nums.length <= 10^{4}~
|
||||||
|
|
||||||
|
- ~-10^{4} < nums[i], target < 10^{4}~
|
||||||
|
|
||||||
|
- All the integers in ~nums~ are *unique*.
|
||||||
|
|
||||||
|
- ~nums~ is sorted in ascending order.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def search(self, nums: List[int], target: int) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int search(vector<int>& nums, int target) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,70 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0719. Find K-th Smallest Pair Distance :hard:
|
* TODO 0719. Find K-th Smallest Pair Distance :hard:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0719. Find K-th Smallest Pair Distance][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0719. Find K-th Smallest Pair Distance][0719. Find K-th Smallest Pair Distance]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
The *distance of a pair* of integers ~a~ and ~b~ is defined as the absolute difference between ~a~ and ~b~.
|
||||||
|
|
||||||
|
Given an integer array ~nums~ and an integer ~k~, return /the/ ~k^{th}~ /smallest *distance among all the pairs*/ ~nums[i]~ /and/ ~nums[j]~ /where/ ~0 <= i < j < nums.length~.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [1,3,1], k = 1
|
||||||
|
Output: 0
|
||||||
|
Explanation: Here are all the pairs:
|
||||||
|
(1,3) -> 2
|
||||||
|
(1,1) -> 0
|
||||||
|
(3,1) -> 2
|
||||||
|
Then the 1st smallest distance pair is (1,1), and its distance is 0.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [1,1,1], k = 2
|
||||||
|
Output: 0
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [1,6,1], k = 3
|
||||||
|
Output: 5
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~n == nums.length~
|
||||||
|
|
||||||
|
- ~2 <= n <= 10^{4}~
|
||||||
|
|
||||||
|
- ~0 <= nums[i] <= 10^{6}~
|
||||||
|
|
||||||
|
- ~1 <= k <= n * (n - 1) / 2~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def smallestDistancePair(self, nums: List[int], k: int) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int smallestDistancePair(vector<int>& nums, int k) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,67 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0875. Koko Eating Bananas :medium:
|
* TODO 0875. Koko Eating Bananas :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0875. Koko Eating Bananas][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0875. Koko Eating Bananas][0875. Koko Eating Bananas]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Koko loves to eat bananas. There are ~n~ piles of bananas, the ~i^{th}~ pile has ~piles[i]~ bananas. The guards have gone and will come back in ~h~ hours.
|
||||||
|
|
||||||
|
Koko can decide her bananas-per-hour eating speed of ~k~. Each hour, she chooses some pile of bananas and eats ~k~ bananas from that pile. If the pile has less than ~k~ bananas, she eats all of them instead and will not eat any more bananas during this hour.
|
||||||
|
|
||||||
|
Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.
|
||||||
|
|
||||||
|
Return /the minimum integer/ ~k~ /such that she can eat all the bananas within/ ~h~ /hours/.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: piles = [3,6,7,11], h = 8
|
||||||
|
Output: 4
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: piles = [30,11,23,4,20], h = 5
|
||||||
|
Output: 30
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: piles = [30,11,23,4,20], h = 6
|
||||||
|
Output: 23
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= piles.length <= 10^{4}~
|
||||||
|
|
||||||
|
- ~piles.length <= h <= 10^{9}~
|
||||||
|
|
||||||
|
- ~1 <= piles[i] <= 10^{9}~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def minEatingSpeed(self, piles: List[int], h: int) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int minEatingSpeed(vector<int>& piles, int h) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,96 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0981. Time Based Key Value Store :medium:
|
* TODO 0981. Time Based Key Value Store :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0981. Time Based Key Value Store][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0981. Time Based Key Value Store][0981. Time Based Key Value Store]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Design a time-based key-value data structure that can store multiple values for the same key at different time stamps and retrieve the key's value at a certain timestamp.
|
||||||
|
|
||||||
|
Implement the ~TimeMap~ class:
|
||||||
|
|
||||||
|
- ~TimeMap()~ Initializes the object of the data structure.
|
||||||
|
|
||||||
|
- ~void set(String key, String value, int timestamp)~ Stores the key ~key~ with the value ~value~ at the given time ~timestamp~.
|
||||||
|
|
||||||
|
- ~String get(String key, int timestamp)~ Returns a value such that ~set~ was called previously, with ~timestamp_prev <= timestamp~. If there are multiple such values, it returns the value associated with the largest ~timestamp_prev~. If there are no values, it returns ~""~.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input
|
||||||
|
["TimeMap", "set", "get", "get", "set", "get", "get"]
|
||||||
|
[[], ["foo", "bar", 1], ["foo", 1], ["foo", 3], ["foo", "bar2", 4], ["foo", 4], ["foo", 5]]
|
||||||
|
Output
|
||||||
|
[null, null, "bar", "bar", null, "bar2", "bar2"]
|
||||||
|
|
||||||
|
Explanation
|
||||||
|
TimeMap timeMap = new TimeMap();
|
||||||
|
timeMap.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1.
|
||||||
|
timeMap.get("foo", 1); // return "bar"
|
||||||
|
timeMap.get("foo", 3); // return "bar", since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 is "bar".
|
||||||
|
timeMap.set("foo", "bar2", 4); // store the key "foo" and value "bar2" along with timestamp = 4.
|
||||||
|
timeMap.get("foo", 4); // return "bar2"
|
||||||
|
timeMap.get("foo", 5); // return "bar2"
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= key.length, value.length <= 100~
|
||||||
|
|
||||||
|
- ~key~ and ~value~ consist of lowercase English letters and digits.
|
||||||
|
|
||||||
|
- ~1 <= timestamp <= 10^{7}~
|
||||||
|
|
||||||
|
- All the timestamps ~timestamp~ of ~set~ are strictly increasing.
|
||||||
|
|
||||||
|
- At most ~2 * 10^{5}~ calls will be made to ~set~ and ~get~.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class TimeMap:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
|
||||||
|
|
||||||
|
def set(self, key: str, value: str, timestamp: int) -> None:
|
||||||
|
|
||||||
|
|
||||||
|
def get(self, key: str, timestamp: int) -> str:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Your TimeMap object will be instantiated and called as such:
|
||||||
|
# obj = TimeMap()
|
||||||
|
# obj.set(key,value,timestamp)
|
||||||
|
# param_2 = obj.get(key,timestamp)
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class TimeMap {
|
||||||
|
public:
|
||||||
|
TimeMap() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void set(string key, string value, int timestamp) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
string get(string key, int timestamp) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Your TimeMap object will be instantiated and called as such:
|
||||||
|
* TimeMap* obj = new TimeMap();
|
||||||
|
* obj->set(key,value,timestamp);
|
||||||
|
* string param_2 = obj->get(key,timestamp);
|
||||||
|
*/
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
+55
-3
@@ -1,18 +1,70 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 2300. Successful Pairs of Spells and Potions :medium:
|
* TODO 2300. Successful Pairs of Spells and Potions :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*2300. Successful Pairs of Spells and Potions][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*2300. Successful Pairs of Spells and Potions][2300. Successful Pairs of Spells and Potions]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are given two positive integer arrays ~spells~ and ~potions~, of length ~n~ and ~m~ respectively, where ~spells[i]~ represents the strength of the ~i^{th}~ spell and ~potions[j]~ represents the strength of the ~j^{th}~ potion.
|
||||||
|
|
||||||
|
You are also given an integer ~success~. A spell and potion pair is considered *successful* if the *product* of their strengths is *at least* ~success~.
|
||||||
|
|
||||||
|
Return /an integer array /~pairs~/ of length /~n~/ where /~pairs[i]~/ is the number of *potions* that will form a successful pair with the /~i^{th}~/ spell./
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: spells = [5,1,3], potions = [1,2,3,4,5], success = 7
|
||||||
|
Output: [4,0,3]
|
||||||
|
Explanation:
|
||||||
|
- 0th spell: 5 * [1,2,3,4,5] = [5,10,15,20,25]. 4 pairs are successful.
|
||||||
|
- 1st spell: 1 * [1,2,3,4,5] = [1,2,3,4,5]. 0 pairs are successful.
|
||||||
|
- 2nd spell: 3 * [1,2,3,4,5] = [3,6,9,12,15]. 3 pairs are successful.
|
||||||
|
Thus, [4,0,3] is returned.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: spells = [3,1,2], potions = [8,5,8], success = 16
|
||||||
|
Output: [2,0,2]
|
||||||
|
Explanation:
|
||||||
|
- 0th spell: 3 * [8,5,8] = [24,15,24]. 2 pairs are successful.
|
||||||
|
- 1st spell: 1 * [8,5,8] = [8,5,8]. 0 pairs are successful.
|
||||||
|
- 2nd spell: 2 * [8,5,8] = [16,10,16]. 2 pairs are successful.
|
||||||
|
Thus, [2,0,2] is returned.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~n == spells.length~
|
||||||
|
|
||||||
|
- ~m == potions.length~
|
||||||
|
|
||||||
|
- ~1 <= n, m <= 10^{5}~
|
||||||
|
|
||||||
|
- ~1 <= spells[i], potions[i] <= 10^{5}~
|
||||||
|
|
||||||
|
- ~1 <= success <= 10^{10}~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,59 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0007. Reverse Integer :medium:
|
* TODO 0007. Reverse Integer :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0007. Reverse Integer][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0007. Reverse Integer][0007. Reverse Integer]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given a signed 32-bit integer ~x~, return ~x~/ with its digits reversed/. If reversing ~x~ causes the value to go outside the signed 32-bit integer range ~[-2^{31}, 2^{31} - 1]~, then return ~0~.
|
||||||
|
|
||||||
|
*Assume the environment does not allow you to store 64-bit integers (signed or unsigned).*
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: x = 123
|
||||||
|
Output: 321
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: x = -123
|
||||||
|
Output: -321
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: x = 120
|
||||||
|
Output: 21
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~-2^{31} <= x <= 2^{31} - 1~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def reverse(self, x: int) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int reverse(int x) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,54 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0136. Single Number :easy:
|
* TODO 0136. Single Number :easy:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0136. Single Number][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0136. Single Number][0136. Single Number]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given a *non-empty* array of integers ~nums~, every element appears /twice/ except for one. Find that single one.
|
||||||
|
|
||||||
|
You must implement a solution with a linear runtime complexity and use only constant extra space.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
*Input:* nums = [2,2,1]
|
||||||
|
|
||||||
|
*Output:* 1
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
*Input:* nums = [4,1,2,1,2]
|
||||||
|
|
||||||
|
*Output:* 4
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
*Input:* nums = [1]
|
||||||
|
|
||||||
|
*Output:* 1
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= nums.length <= 3 * 10^{4}~
|
||||||
|
|
||||||
|
- ~-3 * 10^{4} <= nums[i] <= 3 * 10^{4}~
|
||||||
|
|
||||||
|
- Each element in the array appears twice except for one element which appears only once.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def singleNumber(self, nums: List[int]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int singleNumber(vector<int>& nums) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,68 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0190. Reverse Bits :easy:
|
* TODO 0190. Reverse Bits :easy:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0190. Reverse Bits][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0190. Reverse Bits][0190. Reverse Bits]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Reverse bits of a given 32 bits signed integer.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
*Input:* n = 43261596
|
||||||
|
|
||||||
|
*Output:* 964176192
|
||||||
|
|
||||||
|
*Explanation:*
|
||||||
|
|
||||||
|
Integer
|
||||||
|
Binary
|
||||||
|
|
||||||
|
43261596
|
||||||
|
00000010100101000001111010011100
|
||||||
|
|
||||||
|
964176192
|
||||||
|
00111001011110000010100101000000
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
*Input:* n = 2147483644
|
||||||
|
|
||||||
|
*Output:* 1073741822
|
||||||
|
|
||||||
|
*Explanation:*
|
||||||
|
|
||||||
|
Integer
|
||||||
|
Binary
|
||||||
|
|
||||||
|
2147483644
|
||||||
|
01111111111111111111111111111100
|
||||||
|
|
||||||
|
1073741822
|
||||||
|
00111111111111111111111111111110
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~0 <= n <= 2^{31} - 2~
|
||||||
|
|
||||||
|
- ~n~ is even.
|
||||||
|
|
||||||
|
*Follow up:* If this function is called many times, how would you optimize it?
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def reverseBits(self, n: int) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int reverseBits(int n) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,62 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0191. Number of 1 Bits :easy:
|
* TODO 0191. Number of 1 Bits :easy:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0191. Number of 1 Bits][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0191. Number of 1 Bits][0191. Number of 1 Bits]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given a positive integer ~n~, write a function that returns the number of set bits in its binary representation (also known as the Hamming weight).
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
*Input:* n = 11
|
||||||
|
|
||||||
|
*Output:* 3
|
||||||
|
|
||||||
|
*Explanation:*
|
||||||
|
|
||||||
|
The input binary string *1011* has a total of three set bits.
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
*Input:* n = 128
|
||||||
|
|
||||||
|
*Output:* 1
|
||||||
|
|
||||||
|
*Explanation:*
|
||||||
|
|
||||||
|
The input binary string *10000000* has a total of one set bit.
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
*Input:* n = 2147483645
|
||||||
|
|
||||||
|
*Output:* 30
|
||||||
|
|
||||||
|
*Explanation:*
|
||||||
|
|
||||||
|
The input binary string *1111111111111111111111111111101* has a total of thirty set bits.
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= n <= 2^{31} - 1~
|
||||||
|
|
||||||
|
*Follow up:* If this function is called many times, how would you optimize it?
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def hammingWeight(self, n: int) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int hammingWeight(int n) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,63 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0231. Power of Two :easy:
|
* TODO 0231. Power of Two :easy:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0231. Power of Two][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0231. Power of Two][0231. Power of Two]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an integer ~n~, return /~true~ if it is a power of two. Otherwise, return ~false~/.
|
||||||
|
|
||||||
|
An integer ~n~ is a power of two, if there exists an integer ~x~ such that ~n == 2^{x}~.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 1
|
||||||
|
Output: true
|
||||||
|
Explanation: 20 = 1
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 16
|
||||||
|
Output: true
|
||||||
|
Explanation: 24 = 16
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 3
|
||||||
|
Output: false
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~-2^{31} <= n <= 2^{31} - 1~
|
||||||
|
|
||||||
|
*Follow up:* Could you solve it without loops/recursion?
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def isPowerOfTwo(self, n: int) -> bool:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
bool isPowerOfTwo(int n) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,64 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0260. Single Number III :medium:
|
* TODO 0260. Single Number III :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0260. Single Number III][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0260. Single Number III][0260. Single Number III]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an integer array ~nums~, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in *any order*.
|
||||||
|
|
||||||
|
You must write an algorithm that runs in linear runtime complexity and uses only constant extra space.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [1,2,1,3,2,5]
|
||||||
|
Output: [3,5]
|
||||||
|
Explanation: [5, 3] is also a valid answer.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [-1,0]
|
||||||
|
Output: [-1,0]
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [0,1]
|
||||||
|
Output: [1,0]
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~2 <= nums.length <= 3 * 10^{4}~
|
||||||
|
|
||||||
|
- ~-2^{31} <= nums[i] <= 2^{31} - 1~
|
||||||
|
|
||||||
|
- Each integer in ~nums~ will appear twice, only two integers will appear once.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def singleNumber(self, nums: List[int]) -> List[int]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<int> singleNumber(vector<int>& nums) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,68 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0268. Missing Number :easy:
|
* TODO 0268. Missing Number :easy:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0268. Missing Number][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0268. Missing Number][0268. Missing Number]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an array ~nums~ containing ~n~ distinct numbers in the range ~[0, n]~, return /the only number in the range that is missing from the array./
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
*Input:* nums = [3,0,1]
|
||||||
|
|
||||||
|
*Output:* 2
|
||||||
|
|
||||||
|
*Explanation:*
|
||||||
|
|
||||||
|
~n = 3~ since there are 3 numbers, so all numbers are in the range ~[0,3]~. 2 is the missing number in the range since it does not appear in ~nums~.
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
*Input:* nums = [0,1]
|
||||||
|
|
||||||
|
*Output:* 2
|
||||||
|
|
||||||
|
*Explanation:*
|
||||||
|
|
||||||
|
~n = 2~ since there are 2 numbers, so all numbers are in the range ~[0,2]~. 2 is the missing number in the range since it does not appear in ~nums~.
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
*Input:* nums = [9,6,4,2,3,5,7,0,1]
|
||||||
|
|
||||||
|
*Output:* 8
|
||||||
|
|
||||||
|
*Explanation:*
|
||||||
|
|
||||||
|
~n = 9~ since there are 9 numbers, so all numbers are in the range ~[0,9]~. 8 is the missing number in the range since it does not appear in ~nums~.
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~n == nums.length~
|
||||||
|
|
||||||
|
- ~1 <= n <= 10^{4}~
|
||||||
|
|
||||||
|
- ~0 <= nums[i] <= n~
|
||||||
|
|
||||||
|
- All the numbers of ~nums~ are *unique*.
|
||||||
|
|
||||||
|
*Follow up:* Could you implement a solution using only ~O(1)~ extra space complexity and ~O(n)~ runtime complexity?
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def missingNumber(self, nums: List[int]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int missingNumber(vector<int>& nums) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,65 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0338. Counting Bits :easy:
|
* TODO 0338. Counting Bits :easy:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0338. Counting Bits][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0338. Counting Bits][0338. Counting Bits]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an integer ~n~, return /an array /~ans~/ of length /~n + 1~/ such that for each /~i~/ /(~0 <= i <= n~)/, /~ans[i]~/ is the *number of */~1~/*'s* in the binary representation of /~i~.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 2
|
||||||
|
Output: [0,1,1]
|
||||||
|
Explanation:
|
||||||
|
0 --> 0
|
||||||
|
1 --> 1
|
||||||
|
2 --> 10
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 5
|
||||||
|
Output: [0,1,1,2,1,2]
|
||||||
|
Explanation:
|
||||||
|
0 --> 0
|
||||||
|
1 --> 1
|
||||||
|
2 --> 10
|
||||||
|
3 --> 11
|
||||||
|
4 --> 100
|
||||||
|
5 --> 101
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~0 <= n <= 10^{5}~
|
||||||
|
|
||||||
|
*Follow up:*
|
||||||
|
|
||||||
|
- It is very easy to come up with a solution with a runtime of ~O(n log n)~. Can you do it in linear time ~O(n)~ and possibly in a single pass?
|
||||||
|
|
||||||
|
- Can you do it without using any built-in function (i.e., like ~__builtin_popcount~ in C++)?
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def countBits(self, n: int) -> List[int]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<int> countBits(int n) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,46 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0371. Sum of Two Integers :medium:
|
* TODO 0371. Sum of Two Integers :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0371. Sum of Two Integers][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0371. Sum of Two Integers][0371. Sum of Two Integers]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given two integers ~a~ and ~b~, return /the sum of the two integers without using the operators/ ~+~ /and/ ~-~.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: a = 1, b = 2
|
||||||
|
Output: 3
|
||||||
|
#+end_src
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: a = 2, b = 3
|
||||||
|
Output: 5
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~-1000 <= a, b <= 1000~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def getSum(self, a: int, b: int) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int getSum(int a, int b) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
+49
-3
@@ -1,18 +1,64 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 2220. Minimum Bit Flips to Convert Number :easy:
|
* TODO 2220. Minimum Bit Flips to Convert Number :easy:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*2220. Minimum Bit Flips to Convert Number][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*2220. Minimum Bit Flips to Convert Number][2220. Minimum Bit Flips to Convert Number]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
A *bit flip* of a number ~x~ is choosing a bit in the binary representation of ~x~ and *flipping* it from either ~0~ to ~1~ or ~1~ to ~0~.
|
||||||
|
|
||||||
|
- For example, for ~x = 7~, the binary representation is ~111~ and we may choose any bit (including any leading zeros not shown) and flip it. We can flip the first bit from the right to get ~110~, flip the second bit from the right to get ~101~, flip the fifth bit from the right (a leading zero) to get ~10111~, etc.
|
||||||
|
|
||||||
|
Given two integers ~start~ and ~goal~, return/ the *minimum* number of *bit flips* to convert /~start~/ to /~goal~.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: start = 10, goal = 7
|
||||||
|
Output: 3
|
||||||
|
Explanation: The binary representation of 10 and 7 are 1010 and 0111 respectively. We can convert 10 to 7 in 3 steps:
|
||||||
|
- Flip the first bit from the right: 1010 -> 1011.
|
||||||
|
- Flip the third bit from the right: 1011 -> 1111.
|
||||||
|
- Flip the fourth bit from the right: 1111 -> 0111.
|
||||||
|
It can be shown we cannot convert 10 to 7 in less than 3 steps. Hence, we return 3.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: start = 3, goal = 4
|
||||||
|
Output: 3
|
||||||
|
Explanation: The binary representation of 3 and 4 are 011 and 100 respectively. We can convert 3 to 4 in 3 steps:
|
||||||
|
- Flip the first bit from the right: 011 -> 010.
|
||||||
|
- Flip the second bit from the right: 010 -> 000.
|
||||||
|
- Flip the third bit from the right: 000 -> 100.
|
||||||
|
It can be shown we cannot convert 3 to 4 in less than 3 steps. Hence, we return 3.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~0 <= start, goal <= 10^{9}~
|
||||||
|
|
||||||
|
*Note:* This question is the same as 461: Hamming Distance.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def minBitFlips(self, start: int, goal: int) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int minBitFlips(int start, int goal) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,70 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0127. Word Ladder :hard:
|
* TODO 0127. Word Ladder :hard:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0127. Word Ladder][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0127. Word Ladder][0127. Word Ladder]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
A *transformation sequence* from word ~beginWord~ to word ~endWord~ using a dictionary ~wordList~ is a sequence of words ~beginWord -> s_{1} -> s_{2} -> ... -> s_{k}~ such that:
|
||||||
|
|
||||||
|
- Every adjacent pair of words differs by a single letter.
|
||||||
|
|
||||||
|
- Every ~s_{i}~ for ~1 <= i <= k~ is in ~wordList~. Note that ~beginWord~ does not need to be in ~wordList~.
|
||||||
|
|
||||||
|
- ~s_{k} == endWord~
|
||||||
|
|
||||||
|
Given two words, ~beginWord~ and ~endWord~, and a dictionary ~wordList~, return /the *number of words* in the *shortest transformation sequence* from/ ~beginWord~ /to/ ~endWord~/, or /~0~/ if no such sequence exists./
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
|
||||||
|
Output: 5
|
||||||
|
Explanation: One shortest transformation sequence is "hit" -> "hot" -> "dot" -> "dog" -> cog", which is 5 words long.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
|
||||||
|
Output: 0
|
||||||
|
Explanation: The endWord "cog" is not in wordList, therefore there is no valid transformation sequence.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= beginWord.length <= 10~
|
||||||
|
|
||||||
|
- ~endWord.length == beginWord.length~
|
||||||
|
|
||||||
|
- ~1 <= wordList.length <= 5000~
|
||||||
|
|
||||||
|
- ~wordList[i].length == beginWord.length~
|
||||||
|
|
||||||
|
- ~beginWord~, ~endWord~, and ~wordList[i]~ consist of lowercase English letters.
|
||||||
|
|
||||||
|
- ~beginWord != endWord~
|
||||||
|
|
||||||
|
- All the words in ~wordList~ are *unique*.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,63 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0130. Surrounded Regions :medium:
|
* TODO 0130. Surrounded Regions :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0130. Surrounded Regions][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0130. Surrounded Regions][0130. Surrounded Regions]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are given an ~m x n~ matrix ~board~ containing *letters* ~'X'~ and ~'O'~, *capture regions* that are *surrounded*:
|
||||||
|
|
||||||
|
- *Connect*: A cell is connected to adjacent cells horizontally or vertically.
|
||||||
|
|
||||||
|
- *Region*: To form a region *connect every* ~'O'~ cell.
|
||||||
|
|
||||||
|
- *Surround*: A region is surrounded if none of the ~'O'~ cells in that region are on the edge of the board. Such regions are *completely enclosed *by ~'X'~ cells.
|
||||||
|
|
||||||
|
To capture a *surrounded region*, replace all ~'O'~s with ~'X'~s *in-place* within the original board. You do not need to return anything.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
*Input:* board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
|
||||||
|
|
||||||
|
*Output:* [["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
|
||||||
|
|
||||||
|
*Explanation:*
|
||||||
|
|
||||||
|
In the above diagram, the bottom region is not captured because it is on the edge of the board and cannot be surrounded.
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
*Input:* board = [["X"]]
|
||||||
|
|
||||||
|
*Output:* [["X"]]
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~m == board.length~
|
||||||
|
|
||||||
|
- ~n == board[i].length~
|
||||||
|
|
||||||
|
- ~1 <= m, n <= 200~
|
||||||
|
|
||||||
|
- ~board[i][j]~ is ~'X'~ or ~'O'~.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def solve(self, board: List[List[str]]) -> None:
|
||||||
|
"""
|
||||||
|
Do not return anything, modify board in-place instead.
|
||||||
|
"""
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
void solve(vector<vector<char>>& board) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,123 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0133. Clone Graph :medium:
|
* TODO 0133. Clone Graph :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0133. Clone Graph][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0133. Clone Graph][0133. Clone Graph]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given a reference of a node in a *connected* undirected graph.
|
||||||
|
|
||||||
|
Return a *deep copy* (clone) of the graph.
|
||||||
|
|
||||||
|
Each node in the graph contains a value (~int~) and a list (~List[Node]~) of its neighbors.
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
class Node {
|
||||||
|
public int val;
|
||||||
|
public List<Node> neighbors;
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Test case format:*
|
||||||
|
|
||||||
|
For simplicity, each node's value is the same as the node's index (1-indexed). For example, the first node with ~val == 1~, the second node with ~val == 2~, and so on. The graph is represented in the test case using an adjacency list.
|
||||||
|
|
||||||
|
An adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a node in the graph.
|
||||||
|
|
||||||
|
The given node will always be the first node with ~val = 1~. You must return the *copy of the given node* as a reference to the cloned graph.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: adjList = [[2,4],[1,3],[2,4],[1,3]]
|
||||||
|
Output: [[2,4],[1,3],[2,4],[1,3]]
|
||||||
|
Explanation: There are 4 nodes in the graph.
|
||||||
|
1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
|
||||||
|
2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
|
||||||
|
3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
|
||||||
|
4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: adjList = [[]]
|
||||||
|
Output: [[]]
|
||||||
|
Explanation: Note that the input contains one empty list. The graph consists of only one node with val = 1 and it does not have any neighbors.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: adjList = []
|
||||||
|
Output: []
|
||||||
|
Explanation: This an empty graph, it does not have any nodes.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- The number of nodes in the graph is in the range ~[0, 100]~.
|
||||||
|
|
||||||
|
- ~1 <= Node.val <= 100~
|
||||||
|
|
||||||
|
- ~Node.val~ is unique for each node.
|
||||||
|
|
||||||
|
- There are no repeated edges and no self-loops in the graph.
|
||||||
|
|
||||||
|
- The Graph is connected and all nodes can be visited starting from the given node.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
"""
|
||||||
|
# Definition for a Node.
|
||||||
|
class Node:
|
||||||
|
def __init__(self, val = 0, neighbors = None):
|
||||||
|
self.val = val
|
||||||
|
self.neighbors = neighbors if neighbors is not None else []
|
||||||
|
"""
|
||||||
|
|
||||||
|
from typing import Optional
|
||||||
|
class Solution:
|
||||||
|
def cloneGraph(self, node: Optional['Node']) -> Optional['Node']:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
/*
|
||||||
|
// Definition for a Node.
|
||||||
|
class Node {
|
||||||
|
public:
|
||||||
|
int val;
|
||||||
|
vector<Node*> neighbors;
|
||||||
|
Node() {
|
||||||
|
val = 0;
|
||||||
|
neighbors = vector<Node*>();
|
||||||
|
}
|
||||||
|
Node(int _val) {
|
||||||
|
val = _val;
|
||||||
|
neighbors = vector<Node*>();
|
||||||
|
}
|
||||||
|
Node(int _val, vector<Node*> _neighbors) {
|
||||||
|
val = _val;
|
||||||
|
neighbors = _neighbors;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
Node* cloneGraph(Node* node) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,66 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0200. Number of Islands :medium:
|
* TODO 0200. Number of Islands :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0200. Number of Islands][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0200. Number of Islands][0200. Number of Islands]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an ~m x n~ 2D binary grid ~grid~ which represents a map of ~'1'~s (land) and ~'0'~s (water), return /the number of islands/.
|
||||||
|
|
||||||
|
An *island* is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: grid = [
|
||||||
|
["1","1","1","1","0"],
|
||||||
|
["1","1","0","1","0"],
|
||||||
|
["1","1","0","0","0"],
|
||||||
|
["0","0","0","0","0"]
|
||||||
|
]
|
||||||
|
Output: 1
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: grid = [
|
||||||
|
["1","1","0","0","0"],
|
||||||
|
["1","1","0","0","0"],
|
||||||
|
["0","0","1","0","0"],
|
||||||
|
["0","0","0","1","1"]
|
||||||
|
]
|
||||||
|
Output: 3
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~m == grid.length~
|
||||||
|
|
||||||
|
- ~n == grid[i].length~
|
||||||
|
|
||||||
|
- ~1 <= m, n <= 300~
|
||||||
|
|
||||||
|
- ~grid[i][j]~ is ~'0'~ or ~'1'~.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def numIslands(self, grid: List[List[str]]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int numIslands(vector<vector<char>>& grid) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,64 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0207. Course Schedule :medium:
|
* TODO 0207. Course Schedule :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0207. Course Schedule][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0207. Course Schedule][0207. Course Schedule]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
There are a total of ~numCourses~ courses you have to take, labeled from ~0~ to ~numCourses - 1~. You are given an array ~prerequisites~ where ~prerequisites[i] = [a_{i}, b_{i}]~ indicates that you *must* take course ~b_{i}~ first if you want to take course ~a_{i}~.
|
||||||
|
|
||||||
|
- For example, the pair ~[0, 1]~, indicates that to take course ~0~ you have to first take course ~1~.
|
||||||
|
|
||||||
|
Return ~true~ if you can finish all courses. Otherwise, return ~false~.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: numCourses = 2, prerequisites = [[1,0]]
|
||||||
|
Output: true
|
||||||
|
Explanation: There are a total of 2 courses to take.
|
||||||
|
To take course 1 you should have finished course 0. So it is possible.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
|
||||||
|
Output: false
|
||||||
|
Explanation: There are a total of 2 courses to take.
|
||||||
|
To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= numCourses <= 2000~
|
||||||
|
|
||||||
|
- ~0 <= prerequisites.length <= 5000~
|
||||||
|
|
||||||
|
- ~prerequisites[i].length == 2~
|
||||||
|
|
||||||
|
- ~0 <= a_{i}, b_{i} < numCourses~
|
||||||
|
|
||||||
|
- All the pairs prerequisites[i] are *unique*.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,74 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0210. Course Schedule II :medium:
|
* TODO 0210. Course Schedule II :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0210. Course Schedule II][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0210. Course Schedule II][0210. Course Schedule II]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
There are a total of ~numCourses~ courses you have to take, labeled from ~0~ to ~numCourses - 1~. You are given an array ~prerequisites~ where ~prerequisites[i] = [a_{i}, b_{i}]~ indicates that you *must* take course ~b_{i}~ first if you want to take course ~a_{i}~.
|
||||||
|
|
||||||
|
- For example, the pair ~[0, 1]~, indicates that to take course ~0~ you have to first take course ~1~.
|
||||||
|
|
||||||
|
Return /the ordering of courses you should take to finish all courses/. If there are many valid answers, return *any* of them. If it is impossible to finish all courses, return *an empty array*.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: numCourses = 2, prerequisites = [[1,0]]
|
||||||
|
Output: [0,1]
|
||||||
|
Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1].
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
|
||||||
|
Output: [0,2,1,3]
|
||||||
|
Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0.
|
||||||
|
So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3].
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: numCourses = 1, prerequisites = []
|
||||||
|
Output: [0]
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= numCourses <= 2000~
|
||||||
|
|
||||||
|
- ~0 <= prerequisites.length <= numCourses * (numCourses - 1)~
|
||||||
|
|
||||||
|
- ~prerequisites[i].length == 2~
|
||||||
|
|
||||||
|
- ~0 <= a_{i}, b_{i} < numCourses~
|
||||||
|
|
||||||
|
- ~a_{i} != b_{i}~
|
||||||
|
|
||||||
|
- All the pairs ~[a_{i}, b_{i}]~ are *distinct*.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0261. Graph Valid Tree :medium:
|
* TODO 0261. Graph Valid Tree :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0261. Graph Valid Tree][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0261. Graph Valid Tree][0261. Graph Valid Tree]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0286. Walls And Gates :medium:
|
* TODO 0286. Walls And Gates :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0286. Walls And Gates][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0286. Walls And Gates][0286. Walls And Gates]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0323. Number of Connected Components In An Undirected Graph :medium:
|
* TODO 0323. Number of Connected Components In An Undirected Graph :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0323. Number of Connected Components In An Undirected Graph][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0323. Number of Connected Components In An Undirected Graph][0323. Number of Connected Components In An Undirected Graph]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
|
|||||||
@@ -1,18 +1,77 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0417. Pacific Atlantic Water Flow :medium:
|
* TODO 0417. Pacific Atlantic Water Flow :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0417. Pacific Atlantic Water Flow][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0417. Pacific Atlantic Water Flow][0417. Pacific Atlantic Water Flow]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
There is an ~m x n~ rectangular island that borders both the *Pacific Ocean* and *Atlantic Ocean*. The *Pacific Ocean* touches the island's left and top edges, and the *Atlantic Ocean* touches the island's right and bottom edges.
|
||||||
|
|
||||||
|
The island is partitioned into a grid of square cells. You are given an ~m x n~ integer matrix ~heights~ where ~heights[r][c]~ represents the *height above sea level* of the cell at coordinate ~(r, c)~.
|
||||||
|
|
||||||
|
The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell's height is *less than or equal to* the current cell's height. Water can flow from any cell adjacent to an ocean into the ocean.
|
||||||
|
|
||||||
|
Return /a *2D list* of grid coordinates /~result~/ where /~result[i] = [r_{i}, c_{i}]~/ denotes that rain water can flow from cell /~(r_{i}, c_{i})~/ to *both* the Pacific and Atlantic oceans/.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
|
||||||
|
Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
|
||||||
|
Explanation: The following cells can flow to the Pacific and Atlantic oceans, as shown below:
|
||||||
|
[0,4]: [0,4] -> Pacific Ocean
|
||||||
|
[0,4] -> Atlantic Ocean
|
||||||
|
[1,3]: [1,3] -> [0,3] -> Pacific Ocean
|
||||||
|
[1,3] -> [1,4] -> Atlantic Ocean
|
||||||
|
[1,4]: [1,4] -> [1,3] -> [0,3] -> Pacific Ocean
|
||||||
|
[1,4] -> Atlantic Ocean
|
||||||
|
[2,2]: [2,2] -> [1,2] -> [0,2] -> Pacific Ocean
|
||||||
|
[2,2] -> [2,3] -> [2,4] -> Atlantic Ocean
|
||||||
|
[3,0]: [3,0] -> Pacific Ocean
|
||||||
|
[3,0] -> [4,0] -> Atlantic Ocean
|
||||||
|
[3,1]: [3,1] -> [3,0] -> Pacific Ocean
|
||||||
|
[3,1] -> [4,1] -> Atlantic Ocean
|
||||||
|
[4,0]: [4,0] -> Pacific Ocean
|
||||||
|
[4,0] -> Atlantic Ocean
|
||||||
|
Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: heights = [[1]]
|
||||||
|
Output: [[0,0]]
|
||||||
|
Explanation: The water can flow from the only cell to the Pacific and Atlantic oceans.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~m == heights.length~
|
||||||
|
|
||||||
|
- ~n == heights[r].length~
|
||||||
|
|
||||||
|
- ~1 <= m, n <= 200~
|
||||||
|
|
||||||
|
- ~0 <= heights[r][c] <= 10^{5}~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,64 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0684. Redundant Connection :medium:
|
* TODO 0684. Redundant Connection :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0684. Redundant Connection][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0684. Redundant Connection][0684. Redundant Connection]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
In this problem, a tree is an *undirected graph* that is connected and has no cycles.
|
||||||
|
|
||||||
|
You are given a graph that started as a tree with ~n~ nodes labeled from ~1~ to ~n~, with one additional edge added. The added edge has two *different* vertices chosen from ~1~ to ~n~, and was not an edge that already existed. The graph is represented as an array ~edges~ of length ~n~ where ~edges[i] = [a_{i}, b_{i}]~ indicates that there is an edge between nodes ~a_{i}~ and ~b_{i}~ in the graph.
|
||||||
|
|
||||||
|
Return /an edge that can be removed so that the resulting graph is a tree of /~n~/ nodes/. If there are multiple answers, return the answer that occurs last in the input.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: edges = [[1,2],[1,3],[2,3]]
|
||||||
|
Output: [2,3]
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]
|
||||||
|
Output: [1,4]
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~n == edges.length~
|
||||||
|
|
||||||
|
- ~3 <= n <= 1000~
|
||||||
|
|
||||||
|
- ~edges[i].length == 2~
|
||||||
|
|
||||||
|
- ~1 <= a_{i} < b_{i} <= edges.length~
|
||||||
|
|
||||||
|
- ~a_{i} != b_{i}~
|
||||||
|
|
||||||
|
- There are no repeated edges.
|
||||||
|
|
||||||
|
- The given graph is connected.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,59 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0695. Max Area of Island :medium:
|
* TODO 0695. Max Area of Island :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0695. Max Area of Island][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0695. Max Area of Island][0695. Max Area of Island]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are given an ~m x n~ binary matrix ~grid~. An island is a group of ~1~'s (representing land) connected *4-directionally* (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
|
||||||
|
|
||||||
|
The *area* of an island is the number of cells with a value ~1~ in the island.
|
||||||
|
|
||||||
|
Return /the maximum *area* of an island in /~grid~. If there is no island, return ~0~.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]
|
||||||
|
Output: 6
|
||||||
|
Explanation: The answer is not 11, because the island must be connected 4-directionally.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: grid = [[0,0,0,0,0,0,0,0]]
|
||||||
|
Output: 0
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~m == grid.length~
|
||||||
|
|
||||||
|
- ~n == grid[i].length~
|
||||||
|
|
||||||
|
- ~1 <= m, n <= 50~
|
||||||
|
|
||||||
|
- ~grid[i][j]~ is either ~0~ or ~1~.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int maxAreaOfIsland(vector<vector<int>>& grid) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,69 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0802. Find Eventual Safe States :medium:
|
* TODO 0802. Find Eventual Safe States :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0802. Find Eventual Safe States][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0802. Find Eventual Safe States][0802. Find Eventual Safe States]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
There is a directed graph of ~n~ nodes with each node labeled from ~0~ to ~n - 1~. The graph is represented by a *0-indexed* 2D integer array ~graph~ where ~graph[i]~ is an integer array of nodes adjacent to node ~i~, meaning there is an edge from node ~i~ to each node in ~graph[i]~.
|
||||||
|
|
||||||
|
A node is a *terminal node* if there are no outgoing edges. A node is a *safe node* if every possible path starting from that node leads to a *terminal node* (or another safe node).
|
||||||
|
|
||||||
|
Return /an array containing all the *safe nodes* of the graph/. The answer should be sorted in *ascending* order.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: graph = [[1,2],[2,3],[5],[0],[5],[],[]]
|
||||||
|
Output: [2,4,5,6]
|
||||||
|
Explanation: The given graph is shown above.
|
||||||
|
Nodes 5 and 6 are terminal nodes as there are no outgoing edges from either of them.
|
||||||
|
Every path starting at nodes 2, 4, 5, and 6 all lead to either node 5 or 6.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: graph = [[1,2,3,4],[1,2],[3,4],[0,4],[]]
|
||||||
|
Output: [4]
|
||||||
|
Explanation:
|
||||||
|
Only node 4 is a terminal node, and every path starting at node 4 leads to node 4.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~n == graph.length~
|
||||||
|
|
||||||
|
- ~1 <= n <= 10^{4}~
|
||||||
|
|
||||||
|
- ~0 <= graph[i].length <= n~
|
||||||
|
|
||||||
|
- ~0 <= graph[i][j] <= n - 1~
|
||||||
|
|
||||||
|
- ~graph[i]~ is sorted in a strictly increasing order.
|
||||||
|
|
||||||
|
- The graph may contain self-loops.
|
||||||
|
|
||||||
|
- The number of edges in the graph will be in the range ~[1, 4 * 10^{4}]~.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def eventualSafeNodes(self, graph: List[List[int]]) -> List[int]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<int> eventualSafeNodes(vector<vector<int>>& graph) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,75 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0994. Rotting Oranges :medium:
|
* TODO 0994. Rotting Oranges :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0994. Rotting Oranges][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0994. Rotting Oranges][0994. Rotting Oranges]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are given an ~m x n~ ~grid~ where each cell can have one of three values:
|
||||||
|
|
||||||
|
- ~0~ representing an empty cell,
|
||||||
|
|
||||||
|
- ~1~ representing a fresh orange, or
|
||||||
|
|
||||||
|
- ~2~ representing a rotten orange.
|
||||||
|
|
||||||
|
Every minute, any fresh orange that is *4-directionally adjacent* to a rotten orange becomes rotten.
|
||||||
|
|
||||||
|
Return /the minimum number of minutes that must elapse until no cell has a fresh orange/. If /this is impossible, return/ ~-1~.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: grid = [[2,1,1],[1,1,0],[0,1,1]]
|
||||||
|
Output: 4
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: grid = [[2,1,1],[0,1,1],[1,0,1]]
|
||||||
|
Output: -1
|
||||||
|
Explanation: The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: grid = [[0,2]]
|
||||||
|
Output: 0
|
||||||
|
Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~m == grid.length~
|
||||||
|
|
||||||
|
- ~n == grid[i].length~
|
||||||
|
|
||||||
|
- ~1 <= m, n <= 10~
|
||||||
|
|
||||||
|
- ~grid[i][j]~ is ~0~, ~1~, or ~2~.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def orangesRotting(self, grid: List[List[int]]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int orangesRotting(vector<vector<int>>& grid) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,62 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 1905. Count Sub Islands :medium:
|
* TODO 1905. Count Sub Islands :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*1905. Count Sub Islands][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*1905. Count Sub Islands][1905. Count Sub Islands]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are given two ~m x n~ binary matrices ~grid1~ and ~grid2~ containing only ~0~'s (representing water) and ~1~'s (representing land). An *island* is a group of ~1~'s connected *4-directionally* (horizontal or vertical). Any cells outside of the grid are considered water cells.
|
||||||
|
|
||||||
|
An island in ~grid2~ is considered a *sub-island *if there is an island in ~grid1~ that contains *all* the cells that make up *this* island in ~grid2~.
|
||||||
|
|
||||||
|
Return the /*number* of islands in /~grid2~ /that are considered *sub-islands*/.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: grid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]], grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]
|
||||||
|
Output: 3
|
||||||
|
Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
|
||||||
|
The 1s colored red in grid2 are those considered to be part of a sub-island. There are three sub-islands.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]
|
||||||
|
Output: 2
|
||||||
|
Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
|
||||||
|
The 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~m == grid1.length == grid2.length~
|
||||||
|
|
||||||
|
- ~n == grid1[i].length == grid2[i].length~
|
||||||
|
|
||||||
|
- ~1 <= m, n <= 500~
|
||||||
|
|
||||||
|
- ~grid1[i][j]~ and ~grid2[i][j]~ are either ~0~ or ~1~.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def countSubIslands(self, grid1: List[List[int]], grid2: List[List[int]]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,92 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 2092. Find All People With Secret :hard:
|
* TODO 2092. Find All People With Secret :hard:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*2092. Find All People With Secret][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*2092. Find All People With Secret][2092. Find All People With Secret]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are given an integer ~n~ indicating there are ~n~ people numbered from ~0~ to ~n - 1~. You are also given a *0-indexed* 2D integer array ~meetings~ where ~meetings[i] = [x_{i}, y_{i}, time_{i}]~ indicates that person ~x_{i}~ and person ~y_{i}~ have a meeting at ~time_{i}~. A person may attend *multiple meetings* at the same time. Finally, you are given an integer ~firstPerson~.
|
||||||
|
|
||||||
|
Person ~0~ has a *secret* and initially shares the secret with a person ~firstPerson~ at time ~0~. This secret is then shared every time a meeting takes place with a person that has the secret. More formally, for every meeting, if a person ~x_{i}~ has the secret at ~time_{i}~, then they will share the secret with person ~y_{i}~, and vice versa.
|
||||||
|
|
||||||
|
The secrets are shared *instantaneously*. That is, a person may receive the secret and share it with people in other meetings within the same time frame.
|
||||||
|
|
||||||
|
Return /a list of all the people that have the secret after all the meetings have taken place. /You may return the answer in *any order*.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 6, meetings = [[1,2,5],[2,3,8],[1,5,10]], firstPerson = 1
|
||||||
|
Output: [0,1,2,3,5]
|
||||||
|
Explanation:
|
||||||
|
At time 0, person 0 shares the secret with person 1.
|
||||||
|
At time 5, person 1 shares the secret with person 2.
|
||||||
|
At time 8, person 2 shares the secret with person 3.
|
||||||
|
At time 10, person 1 shares the secret with person 5.
|
||||||
|
Thus, people 0, 1, 2, 3, and 5 know the secret after all the meetings.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 4, meetings = [[3,1,3],[1,2,2],[0,3,3]], firstPerson = 3
|
||||||
|
Output: [0,1,3]
|
||||||
|
Explanation:
|
||||||
|
At time 0, person 0 shares the secret with person 3.
|
||||||
|
At time 2, neither person 1 nor person 2 know the secret.
|
||||||
|
At time 3, person 3 shares the secret with person 0 and person 1.
|
||||||
|
Thus, people 0, 1, and 3 know the secret after all the meetings.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 5, meetings = [[3,4,2],[1,2,1],[2,3,1]], firstPerson = 1
|
||||||
|
Output: [0,1,2,3,4]
|
||||||
|
Explanation:
|
||||||
|
At time 0, person 0 shares the secret with person 1.
|
||||||
|
At time 1, person 1 shares the secret with person 2, and person 2 shares the secret with person 3.
|
||||||
|
Note that person 2 can share the secret at the same time as receiving it.
|
||||||
|
At time 2, person 3 shares the secret with person 4.
|
||||||
|
Thus, people 0, 1, 2, 3, and 4 know the secret after all the meetings.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~2 <= n <= 10^{5}~
|
||||||
|
|
||||||
|
- ~1 <= meetings.length <= 10^{5}~
|
||||||
|
|
||||||
|
- ~meetings[i].length == 3~
|
||||||
|
|
||||||
|
- ~0 <= x_{i}, y_{i }<= n - 1~
|
||||||
|
|
||||||
|
- ~x_{i} != y_{i}~
|
||||||
|
|
||||||
|
- ~1 <= time_{i} <= 10^{5}~
|
||||||
|
|
||||||
|
- ~1 <= firstPerson <= n - 1~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def findAllPeople(self, n: int, meetings: List[List[int]], firstPerson: int) -> List[int]:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<int> findAllPeople(int n, vector<vector<int>>& meetings, int firstPerson) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,70 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 2658. Maximum Number of Fish in a Grid :medium:
|
* TODO 2658. Maximum Number of Fish in a Grid :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*2658. Maximum Number of Fish in a Grid][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*2658. Maximum Number of Fish in a Grid][2658. Maximum Number of Fish in a Grid]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are given a *0-indexed* 2D matrix ~grid~ of size ~m x n~, where ~(r, c)~ represents:
|
||||||
|
|
||||||
|
- A *land* cell if ~grid[r][c] = 0~, or
|
||||||
|
|
||||||
|
- A *water* cell containing ~grid[r][c]~ fish, if ~grid[r][c] > 0~.
|
||||||
|
|
||||||
|
A fisher can start at any *water* cell ~(r, c)~ and can do the following operations any number of times:
|
||||||
|
|
||||||
|
- Catch all the fish at cell ~(r, c)~, or
|
||||||
|
|
||||||
|
- Move to any adjacent *water* cell.
|
||||||
|
|
||||||
|
Return /the *maximum* number of fish the fisher can catch if he chooses his starting cell optimally, or /~0~ if no water cell exists.
|
||||||
|
|
||||||
|
An *adjacent* cell of the cell ~(r, c)~, is one of the cells ~(r, c + 1)~, ~(r, c - 1)~, ~(r + 1, c)~ or ~(r - 1, c)~ if it exists.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: grid = [[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]]
|
||||||
|
Output: 7
|
||||||
|
Explanation: The fisher can start at cell (1,3) and collect 3 fish, then move to cell (2,3) and collect 4 fish.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]
|
||||||
|
Output: 1
|
||||||
|
Explanation: The fisher can start at cells (0,0) or (3,3) and collect a single fish.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~m == grid.length~
|
||||||
|
|
||||||
|
- ~n == grid[i].length~
|
||||||
|
|
||||||
|
- ~1 <= m, n <= 10~
|
||||||
|
|
||||||
|
- ~0 <= grid[i][j] <= 10~
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def findMaxFish(self, grid: List[List[int]]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int findMaxFish(vector<vector<int>>& grid) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,78 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 2924. Find Champion II :medium:
|
* TODO 2924. Find Champion II :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*2924. Find Champion II][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*2924. Find Champion II][2924. Find Champion II]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
There are ~n~ teams numbered from ~0~ to ~n - 1~ in a tournament; each team is also a node in a *DAG*.
|
||||||
|
|
||||||
|
You are given the integer ~n~ and a *0-indexed* 2D integer array ~edges~ of length ~m~ representing the *DAG*, where ~edges[i] = [u_{i}, v_{i}]~ indicates that there is a directed edge from team ~u_{i}~ to team ~v_{i}~ in the graph.
|
||||||
|
|
||||||
|
A directed edge from ~a~ to ~b~ in the graph means that team ~a~ is *stronger* than team ~b~ and team ~b~ is *weaker* than team ~a~.
|
||||||
|
|
||||||
|
Team ~a~ will be the *champion* of the tournament if there is no team ~b~ that is *stronger* than team ~a~.
|
||||||
|
|
||||||
|
Return /the team that will be the *champion* of the tournament if there is a *unique* champion, otherwise, return /~-1~/./
|
||||||
|
|
||||||
|
*Notes*
|
||||||
|
|
||||||
|
- A *cycle* is a series of nodes ~a_{1}, a_{2}, ..., a_{n}, a_{n+1}~ such that node ~a_{1}~ is the same node as node ~a_{n+1}~, the nodes ~a_{1}, a_{2}, ..., a_{n}~ are distinct, and there is a directed edge from the node ~a_{i}~ to node ~a_{i+1}~ for every ~i~ in the range ~[1, n]~.
|
||||||
|
|
||||||
|
- A *DAG* is a directed graph that does not have any *cycle*.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 3, edges = [[0,1],[1,2]]
|
||||||
|
Output: 0
|
||||||
|
Explanation: Team 1 is weaker than team 0. Team 2 is weaker than team 1. So the champion is team 0.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: n = 4, edges = [[0,2],[1,3],[1,2]]
|
||||||
|
Output: -1
|
||||||
|
Explanation: Team 2 is weaker than team 0 and team 1. Team 3 is weaker than team 1. But team 1 and team 0 are not weaker than any other teams. So the answer is -1.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= n <= 100~
|
||||||
|
|
||||||
|
- ~m == edges.length~
|
||||||
|
|
||||||
|
- ~0 <= m <= n * (n - 1) / 2~
|
||||||
|
|
||||||
|
- ~edges[i].length == 2~
|
||||||
|
|
||||||
|
- ~0 <= edge[i][j] <= n - 1~
|
||||||
|
|
||||||
|
- ~edges[i][0] != edges[i][1]~
|
||||||
|
|
||||||
|
- The input is generated such that if team ~a~ is stronger than team ~b~, team ~b~ is not stronger than team ~a~.
|
||||||
|
|
||||||
|
- The input is generated such that if team ~a~ is stronger than team ~b~ and team ~b~ is stronger than team ~c~, then team ~a~ is stronger than team ~c~.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def findChampion(self, n: int, edges: List[List[int]]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int findChampion(int n, vector<vector<int>>& edges) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,61 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0045. Jump Game II :medium:
|
* TODO 0045. Jump Game II :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0045. Jump Game II][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0045. Jump Game II][0045. Jump Game II]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
You are given a *0-indexed* array of integers ~nums~ of length ~n~. You are initially positioned at index 0.
|
||||||
|
|
||||||
|
Each element ~nums[i]~ represents the maximum length of a forward jump from index ~i~. In other words, if you are at index ~i~, you can jump to any index ~(i + j)~ where:
|
||||||
|
|
||||||
|
- ~0 <= j <= nums[i]~ and
|
||||||
|
|
||||||
|
- ~i + j < n~
|
||||||
|
|
||||||
|
Return /the minimum number of jumps to reach index /~n - 1~. The test cases are generated such that you can reach index ~n - 1~.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [2,3,1,1,4]
|
||||||
|
Output: 2
|
||||||
|
Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [2,3,0,1,4]
|
||||||
|
Output: 2
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= nums.length <= 10^{4}~
|
||||||
|
|
||||||
|
- ~0 <= nums[i] <= 1000~
|
||||||
|
|
||||||
|
- It's guaranteed that you can reach ~nums[n - 1]~.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def jump(self, nums: List[int]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int jump(vector<int>& nums) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -1,18 +1,64 @@
|
|||||||
#+PROPERTY: STUDY_DECK_02
|
#+PROPERTY: STUDY_DECK_02
|
||||||
* TODO 0053. Maximum Subarray :medium:
|
* TODO 0053. Maximum Subarray :medium:
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:NEETCODE: [[file:../../roadmap.org::*0053. Maximum Subarray][Roadmap]]
|
:NEETCODE: [[file:../../roadmap.org::*0053. Maximum Subarray][0053. Maximum Subarray]]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
|
Given an integer array ~nums~, find the subarray with the largest sum, and return /its sum/.
|
||||||
|
|
||||||
|
*Example 1:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
|
||||||
|
Output: 6
|
||||||
|
Explanation: The subarray [4,-1,2,1] has the largest sum 6.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 2:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [1]
|
||||||
|
Output: 1
|
||||||
|
Explanation: The subarray [1] has the largest sum 1.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Example 3:*
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src
|
||||||
|
Input: nums = [5,4,-1,7,8]
|
||||||
|
Output: 23
|
||||||
|
Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
*Constraints:*
|
||||||
|
|
||||||
|
- ~1 <= nums.length <= 10^{5}~
|
||||||
|
|
||||||
|
- ~-10^{4} <= nums[i] <= 10^{4}~
|
||||||
|
|
||||||
|
*Follow up:* If you have figured out the ~O(n)~ solution, try coding another solution using the *divide and conquer* approach, which is more subtle.
|
||||||
|
|
||||||
** TODO Approach
|
** TODO Approach
|
||||||
Write your approach here.
|
Write your approach here.
|
||||||
|
|
||||||
** TODO Python
|
** TODO Python
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
|
class Solution:
|
||||||
|
def maxSubArray(self, nums: List[int]) -> int:
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** TODO C++
|
** TODO C++
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int maxSubArray(vector<int>& nums) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user