diff --git a/articles/arranging-coins.md b/articles/arranging-coins.md new file mode 100644 index 000000000..cb8920a4a --- /dev/null +++ b/articles/arranging-coins.md @@ -0,0 +1,401 @@ +## 1. Brute Force + +::tabs-start + +```python +class Solution: + def arrangeCoins(self, n: int) -> int: + row = 0 + while n - row > 0: + row += 1 + n -= row + return row +``` + +```java +public class Solution { + public int arrangeCoins(int n) { + int row = 0; + while (n - row > 0) { + row++; + n -= row; + } + return row; + } +} +``` + +```cpp +class Solution { +public: + int arrangeCoins(int n) { + int row = 0; + while (n - row > 0) { + row++; + n -= row; + } + return row; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number} n + * @return {number} + */ + arrangeCoins(n) { + let row = 0; + while (n - row > 0) { + row++; + n -= row; + } + return row; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(\sqrt {n})$ +* Space complexity: $O(1)$ + +--- + +## 2. Binary Search + +::tabs-start + +```python +class Solution: + def arrangeCoins(self, n: int) -> int: + l, r = 1, n + res = 0 + + while l <= r: + mid = (l + r) // 2 + coins = (mid * (mid + 1)) // 2 + if coins > n: + r = mid - 1 + else: + l = mid + 1 + res = max(res, mid) + + return res +``` + +```java +public class Solution { + public int arrangeCoins(int n) { + int l = 1, r = n, res = 0; + + while (l <= r) { + int mid = l + (r - l) / 2; + long coins = (long) mid * (mid + 1) / 2; + if (coins > n) { + r = mid - 1; + } else { + l = mid + 1; + res = Math.max(res, mid); + } + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + int arrangeCoins(int n) { + long long l = 1, r = n, res = 0; + + while (l <= r) { + long long mid = l + (r - l) / 2; + long long coins = (mid * (mid + 1)) / 2; + if (coins > n) { + r = mid - 1; + } else { + l = mid + 1; + res = max(res, mid); + } + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number} n + * @return {number} + */ + arrangeCoins(n) { + let l = 1, r = n, res = 0; + + while (l <= r) { + let mid = Math.floor((l + r) / 2); + let coins = (mid * (mid + 1)) / 2; + if (coins > n) { + r = mid - 1; + } else { + l = mid + 1; + res = Math.max(res, mid); + } + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(\log n)$ +* Space complexity: $O(1)$ + +--- + +## 3. Binary Search (Optimal) + +::tabs-start + +```python +class Solution: + def arrangeCoins(self, n: int) -> int: + if n <= 3: + return n if n == 1 else n - 1 + + l, r = 1, (n // 2) + 1 + while l < r: + mid = (l + r) // 2 + if (mid * (mid + 1)) // 2 <= n: + l = mid + 1 + else: + r = mid + + return l - 1 +``` + +```java +public class Solution { + public int arrangeCoins(int n) { + if (n <= 3) { + return n == 1 ? 1 : n - 1; + } + + int l = 1, r = (n / 2) + 1; + while (l < r) { + int mid = l + (r - l) / 2; + long coins = (long) mid * (mid + 1) / 2; + if (coins <= n) { + l = mid + 1; + } else { + r = mid; + } + } + + return l - 1; + } +} +``` + +```cpp +class Solution { +public: + int arrangeCoins(int n) { + if (n <= 3) { + return n == 1 ? 1 : n - 1; + } + + int l = 1, r = (n / 2) + 1; + while (l < r) { + int mid = l + (r - l) / 2; + long long coins = (mid * (mid + 1LL)) / 2; + if (coins <= n) { + l = mid + 1; + } else { + r = mid; + } + } + + return l - 1; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number} n + * @return {number} + */ + arrangeCoins(n) { + if (n <= 3) { + return n == 1 ? 1 : n - 1; + } + + let l = 1, r = (n / 2) + 1; + while (l < r) { + let mid = Math.floor((l + r) / 2); + let coins = (mid * (mid + 1)) / 2; + if (coins <= n) { + l = mid + 1; + } else { + r = mid; + } + } + + return l - 1; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(\log n)$ +* Space complexity: $O(1)$ + +--- + +## 4. Bit Manipulation + +::tabs-start + +```python +class Solution: + def arrangeCoins(self, n: int) -> int: + mask = 1 << 15 + rows = 0 + while mask > 0: + rows |= mask + coins = rows * (rows + 1) // 2 + if coins > n: + rows ^= mask + mask >>= 1 + return rows +``` + +```java +public class Solution { + public int arrangeCoins(int n) { + int mask = 1 << 15; + int rows = 0; + while (mask > 0) { + rows |= mask; + long coins = (long) rows * (rows + 1) / 2; + if (coins > n) { + rows ^= mask; + } + mask >>= 1; + } + return rows; + } +} +``` + +```cpp +class Solution { +public: + int arrangeCoins(int n) { + int mask = 1 << 15; + int rows = 0; + while (mask > 0) { + rows |= mask; + long long coins = (long long) rows * (rows + 1) / 2; + if (coins > n) { + rows ^= mask; + } + mask >>= 1; + } + return rows; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number} n + * @return {number} + */ + arrangeCoins(n) { + let mask = 1 << 15; + let rows = 0; + while (mask > 0) { + rows |= mask; + let coins = (rows * (rows + 1)) / 2; + if (coins > n) { + rows ^= mask; + } + mask >>= 1; + } + return rows; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(1)$ since we iterate $15$ times. +* Space complexity: $O(1)$ + +--- + +## 5. Math + +::tabs-start + +```python +class Solution: + def arrangeCoins(self, n: int) -> int: + return int(sqrt(2 * n + 0.25) - 0.5) +``` + +```java +public class Solution { + public int arrangeCoins(int n) { + return (int) (Math.sqrt(2L * n + 0.25) - 0.5); + } +} +``` + +```cpp +class Solution { +public: + int arrangeCoins(int n) { + return (int)(sqrt(2.0 * n + 0.25) - 0.5); + } +}; +``` + +```javascript +class Solution { + /** + * @param {number} n + * @return {number} + */ + arrangeCoins(n) { + return Math.floor(Math.sqrt(2 * n + 0.25) - 0.5); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(1)$ or $O(\sqrt {n})$ depending on the language. +* Space complexity: $O(1)$ \ No newline at end of file diff --git a/articles/assign-cookies.md b/articles/assign-cookies.md new file mode 100644 index 000000000..b591a8ef9 --- /dev/null +++ b/articles/assign-cookies.md @@ -0,0 +1,300 @@ +## 1. Brute Force + +::tabs-start + +```python +class Solution: + def findContentChildren(self, g: List[int], s: List[int]) -> int: + s.sort() + res = 0 + + for i in g: + minIdx = -1 + for j in range(len(s)): + if s[j] < i: + continue + + if minIdx == -1 or s[minIdx] > s[j]: + minIdx = j + + if minIdx != -1: + s[minIdx] = -1 + res += 1 + + return res +``` + +```java +public class Solution { + public int findContentChildren(int[] g, int[] s) { + Arrays.sort(s); + int res = 0; + + for (int i : g) { + int minIdx = -1; + for (int j = 0; j < s.length; j++) { + if (s[j] < i) continue; + + if (minIdx == -1 || s[minIdx] > s[j]) { + minIdx = j; + } + } + + if (minIdx != -1) { + s[minIdx] = -1; + res++; + } + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + int findContentChildren(vector& g, vector& s) { + sort(s.begin(), s.end()); + int res = 0; + + for (int i : g) { + int minIdx = -1; + for (int j = 0; j < s.size(); j++) { + if (s[j] < i) continue; + + if (minIdx == -1 || s[minIdx] > s[j]) { + minIdx = j; + } + } + + if (minIdx != -1) { + s[minIdx] = -1; + res++; + } + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} g + * @param {number[]} s + * @return {number} + */ + findContentChildren(g, s) { + s.sort((a, b) => a - b); + let res = 0; + + for (let i of g) { + let minIdx = -1; + for (let j = 0; j < s.length; j++) { + if (s[j] < i) continue; + + if (minIdx === -1 || s[minIdx] > s[j]) { + minIdx = j; + } + } + + if (minIdx !== -1) { + s[minIdx] = -1; + res++; + } + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n * m + m \log m)$ +* Space complexity: $O(1)$ or $O(m)$ depending on the sorting algorithm. + +> Where $n$ is the size of the array $g$ and $m$ is the size of the array $s$. + +--- + +## 2. Two Pointers - I + +::tabs-start + +```python +class Solution: + def findContentChildren(self, g: List[int], s: List[int]) -> int: + g.sort() + s.sort() + + i = j = 0 + while i < len(g): + while j < len(s) and g[i] > s[j]: + j += 1 + if j == len(s): + break + i += 1 + j += 1 + return i +``` + +```java +public class Solution { + public int findContentChildren(int[] g, int[] s) { + Arrays.sort(g); + Arrays.sort(s); + + int i = 0, j = 0; + while (i < g.length) { + while (j < s.length && g[i] > s[j]) { + j++; + } + if (j == s.length) break; + i++; + j++; + } + return i; + } +} +``` + +```cpp +class Solution { +public: + int findContentChildren(vector& g, vector& s) { + sort(g.begin(), g.end()); + sort(s.begin(), s.end()); + + int i = 0, j = 0; + while (i < g.size()) { + while (j < s.size() && g[i] > s[j]) { + j++; + } + if (j == s.size()) break; + i++; + j++; + } + return i; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} g + * @param {number[]} s + * @return {number} + */ + findContentChildren(g, s) { + g.sort((a, b) => a - b); + s.sort((a, b) => a - b); + + let i = 0, j = 0; + while (i < g.length) { + while (j < s.length && g[i] > s[j]) { + j++; + } + if (j === s.length) break; + i++; + j++; + } + return i; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n \log n + m \log m)$ +* Space complexity: $O(1)$ or $O(n + m)$ depending on the sorting algorithm. + +> Where $n$ is the size of the array $g$ and $m$ is the size of the array $s$. + +--- + +## 3. Two Pointers - II + +::tabs-start + +```python +class Solution: + def findContentChildren(self, g: List[int], s: List[int]) -> int: + g.sort() + s.sort() + + i = j = 0 + while i < len(g) and j < len(s): + if g[i] <= s[j]: + i += 1 + j += 1 + + return i +``` + +```java +public class Solution { + public int findContentChildren(int[] g, int[] s) { + Arrays.sort(g); + Arrays.sort(s); + + int i = 0; + for (int j = 0; i < g.length && j < s.length; j++) { + if (g[i] <= s[j]) i++; + } + return i; + } +} +``` + +```cpp +class Solution { +public: + int findContentChildren(vector& g, vector& s) { + sort(g.begin(), g.end()); + sort(s.begin(), s.end()); + + int i = 0; + for (int j = 0; i < g.size() && j < s.size(); j++) { + if (g[i] <= s[j]) i++; + } + return i; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} g + * @param {number[]} s + * @return {number} + */ + findContentChildren(g, s) { + g.sort((a, b) => a - b); + s.sort((a, b) => a - b); + + let i = 0; + for (let j = 0; i < g.length && j < s.length; j++) { + if (g[i] <= s[j]) i++; + } + return i; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n \log n + m \log m)$ +* Space complexity: $O(1)$ or $O(n + m)$ depending on the sorting algorithm. + +> Where $n$ is the size of the array $g$ and $m$ is the size of the array $s$. \ No newline at end of file diff --git a/articles/backspace-string-compare.md b/articles/backspace-string-compare.md new file mode 100644 index 000000000..6064e5bd6 --- /dev/null +++ b/articles/backspace-string-compare.md @@ -0,0 +1,506 @@ +## 1. Stack + +::tabs-start + +```python +class Solution: + def backspaceCompare(self, s: str, t: str) -> bool: + def convert(s): + res = [] + for char in s: + if char == '#': + if res: + res.pop() + else: + res.append(char) + return "".join(res) + + return convert(s) == convert(t) +``` + +```java +public class Solution { + public boolean backspaceCompare(String s, String t) { + return convert(s).equals(convert(t)); + } + + private List convert(String s) { + List res = new ArrayList<>(); + for (char c : s.toCharArray()) { + if (c == '#') { + if (!res.isEmpty()) { + res.remove(res.size() - 1); + } + } else { + res.add(c); + } + } + return res; + } +} +``` + +```cpp +class Solution { +public: + bool backspaceCompare(string s, string t) { + return convert(s) == convert(t); + } + +private: + string convert(const string& s) { + string res = ""; + for (char c : s) { + if (c == '#') { + if (!res.empty()) { + res.pop_back(); + } + } else { + res += c; + } + } + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string} s + * @param {string} t + * @return {boolean} + */ + backspaceCompare(s, t) { + const convert = (str) => { + const res = []; + for (const char of str) { + if (char === '#') { + if (res.length > 0) { + res.pop(); + } + } else { + res.push(char); + } + } + return res.join(''); + }; + return convert(s) === convert(t); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n + m)$ +* Space complexity: $O(n + m)$ + +> Where $n$ is the length of the string $s$ and $m$ is the length of the string $t$. + +--- + +## 2. Reverse iteration + +::tabs-start + +```python +class Solution: + def backspaceCompare(self, s: str, t: str) -> bool: + def convert(s): + res = [] + backspace = 0 + for i in range(len(s) - 1, -1, -1): + if s[i] == '#': + backspace += 1 + elif backspace: + backspace -= 1 + else: + res.append(s[i]) + return res + + return convert(s) == convert(t) +``` + +```java +public class Solution { + public boolean backspaceCompare(String s, String t) { + return convert(s).equals(convert(t)); + } + + private List convert(String s) { + List res = new ArrayList<>(); + int backspace = 0; + for (int i = s.length() - 1; i >= 0; i--) { + if (s.charAt(i) == '#') { + backspace++; + } else if (backspace > 0) { + backspace--; + } else { + res.add(s.charAt(i)); + } + } + return res; + } +} +``` + +```cpp +class Solution { +public: + bool backspaceCompare(string s, string t) { + return convert(s) == convert(t); + } + +private: + string convert(string s) { + string res; + int backspace = 0; + for (int i = s.size() - 1; i >= 0; i--) { + if (s[i] == '#') { + backspace++; + } else if (backspace > 0) { + backspace--; + } else { + res += s[i]; + } + } + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string} s + * @param {string} t + * @return {boolean} + */ + backspaceCompare(s, t) { + const convert = (s) => { + const res = []; + let backspace = 0; + for (let i = s.length - 1; i >= 0; i--) { + if (s[i] === '#') { + backspace++; + } else if (backspace > 0) { + backspace--; + } else { + res.push(s[i]); + } + } + return res.join(''); + }; + return convert(s) === convert(t); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n + m)$ +* Space complexity: $O(n + m)$ + +> Where $n$ is the length of the string $s$ and $m$ is the length of the string $t$. + +--- + +## 3. Two Pointers - I + +::tabs-start + +```python +class Solution: + def backspaceCompare(self, s: str, t: str) -> bool: + def nextValidChar(string, index): + backspace = 0 + while index >= 0: + if string[index] == '#': + backspace += 1 + elif backspace > 0: + backspace -= 1 + else: + break + index -= 1 + return index + + index_s, index_t = len(s) - 1, len(t) - 1 + + while index_s >= 0 or index_t >= 0: + index_s = nextValidChar(s, index_s) + index_t = nextValidChar(t, index_t) + + char_s = s[index_s] if index_s >= 0 else "" + char_t = t[index_t] if index_t >= 0 else "" + + if char_s != char_t: + return False + + index_s -= 1 + index_t -= 1 + + return True +``` + +```java +public class Solution { + public boolean backspaceCompare(String s, String t) { + int indexS = s.length() - 1, indexT = t.length() - 1; + + while (indexS >= 0 || indexT >= 0) { + indexS = nextValidChar(s, indexS); + indexT = nextValidChar(t, indexT); + + char charS = indexS >= 0 ? s.charAt(indexS) : '\0'; + char charT = indexT >= 0 ? t.charAt(indexT) : '\0'; + + if (charS != charT) return false; + + indexS--; + indexT--; + } + + return true; + } + + private int nextValidChar(String str, int index) { + int backspace = 0; + + while (index >= 0) { + if (str.charAt(index) == '#') { + backspace++; + } else if (backspace > 0) { + backspace--; + } else { + break; + } + index--; + } + + return index; + } +} +``` + +```cpp +class Solution { +public: + bool backspaceCompare(string s, string t) { + int indexS = s.size() - 1, indexT = t.size() - 1; + + while (indexS >= 0 || indexT >= 0) { + indexS = nextValidChar(s, indexS); + indexT = nextValidChar(t, indexT); + + char charS = indexS >= 0 ? s[indexS] : '\0'; + char charT = indexT >= 0 ? t[indexT] : '\0'; + + if (charS != charT) return false; + + indexS--; + indexT--; + } + + return true; + } + +private: + int nextValidChar(string &str, int index) { + int backspace = 0; + + while (index >= 0) { + if (str[index] == '#') { + backspace++; + } else if (backspace > 0) { + backspace--; + } else { + break; + } + index--; + } + + return index; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string} s + * @param {string} t + * @return {boolean} + */ + backspaceCompare(s, t) { + const nextValidChar = (str, index) => { + let backspace = 0; + + while (index >= 0) { + if (str[index] === '#') { + backspace++; + } else if (backspace > 0) { + backspace--; + } else { + break; + } + index--; + } + + return index; + }; + + let indexS = s.length - 1, indexT = t.length - 1; + + while (indexS >= 0 || indexT >= 0) { + indexS = nextValidChar(s, indexS); + indexT = nextValidChar(t, indexT); + + const charS = indexS >= 0 ? s[indexS] : ''; + const charT = indexT >= 0 ? t[indexT] : ''; + + if (charS !== charT) return false; + + indexS--; + indexT--; + } + + return true; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n + m)$ +* Space complexity: $O(1)$ + +> Where $n$ is the length of the string $s$ and $m$ is the length of the string $t$. + +--- + +## 4. Two Pointers - II + +::tabs-start + +```python +class Solution: + def backspaceCompare(self, s: str, t: str) -> bool: + index_s, index_t = len(s) - 1, len(t) - 1 + backspace_s = backspace_t = 0 + + while True: + while index_s >= 0 and (backspace_s or s[index_s] == '#'): + backspace_s += 1 if s[index_s] == '#' else -1 + index_s -= 1 + + while index_t >= 0 and (backspace_t or t[index_t] == '#'): + backspace_t += 1 if t[index_t] == '#' else -1 + index_t -= 1 + + if not (index_s >= 0 and index_t >= 0 and s[index_s] == t[index_t]): + return index_s == index_t == -1 + index_s, index_t = index_s - 1, index_t - 1 +``` + +```java +public class Solution { + public boolean backspaceCompare(String s, String t) { + int indexS = s.length() - 1, indexT = t.length() - 1; + int backspaceS = 0, backspaceT = 0; + + while (true) { + while (indexS >= 0 && (backspaceS > 0 || s.charAt(indexS) == '#')) { + backspaceS += s.charAt(indexS) == '#' ? 1 : -1; + indexS--; + } + + while (indexT >= 0 && (backspaceT > 0 || t.charAt(indexT) == '#')) { + backspaceT += t.charAt(indexT) == '#' ? 1 : -1; + indexT--; + } + + if (!(indexS >= 0 && indexT >= 0 && s.charAt(indexS) == t.charAt(indexT))) { + return indexS == -1 && indexT == -1; + } + indexS--; + indexT--; + } + } +} +``` + +```cpp +class Solution { +public: + bool backspaceCompare(string s, string t) { + int indexS = s.size() - 1, indexT = t.size() - 1; + int backspaceS = 0, backspaceT = 0; + + while (true) { + + while (indexS >= 0 && (backspaceS > 0 || s[indexS] == '#')) { + backspaceS += (s[indexS] == '#') ? 1 : -1; + indexS--; + } + + while (indexT >= 0 && (backspaceT > 0 || t[indexT] == '#')) { + backspaceT += (t[indexT] == '#') ? 1 : -1; + indexT--; + } + + if (!(indexS >= 0 && indexT >= 0 && s[indexS] == t[indexT])) { + return indexS == -1 && indexT == -1; + } + indexS--; + indexT--; + } + } +}; +``` + +```javascript +class Solution { + /** + * @param {string} s + * @param {string} t + * @return {boolean} + */ + backspaceCompare(s, t) { + let indexS = s.length - 1, indexT = t.length - 1; + let backspaceS = 0, backspaceT = 0; + + while (true) { + while (indexS >= 0 && (backspaceS > 0 || s[indexS] === '#')) { + backspaceS += s[indexS] === '#' ? 1 : -1; + indexS--; + } + + while (indexT >= 0 && (backspaceT > 0 || t[indexT] === '#')) { + backspaceT += t[indexT] === '#' ? 1 : -1; + indexT--; + } + + if (!(indexS >= 0 && indexT >= 0 && s.charAt(indexS) === t.charAt(indexT))) { + return indexS === -1 && indexT === -1; + } + indexS--; + indexT--; + } + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n + m)$ +* Space complexity: $O(1)$ + +> Where $n$ is the length of the string $s$ and $m$ is the length of the string $t$. \ No newline at end of file diff --git a/articles/baseball-game.md b/articles/baseball-game.md new file mode 100644 index 000000000..2d04f4311 --- /dev/null +++ b/articles/baseball-game.md @@ -0,0 +1,223 @@ +## 1. Stack + +::tabs-start + +```python +class Solution: + def calPoints(self, operations: List[str]) -> int: + stack = [] + for op in operations: + if op == "+": + stack.append(stack[-1] + stack[-2]) + elif op == "D": + stack.append(2 * stack[-1]) + elif op == "C": + stack.pop() + else: + stack.append(int(op)) + return sum(stack) +``` + +```java +public class Solution { + public int calPoints(String[] operations) { + Stack stack = new Stack<>(); + for (String op : operations) { + if (op.equals("+")) { + int top = stack.pop(); + int newTop = top + stack.peek(); + stack.push(top); + stack.push(newTop); + } else if (op.equals("D")) { + stack.push(2 * stack.peek()); + } else if (op.equals("C")) { + stack.pop(); + } else { + stack.push(Integer.parseInt(op)); + } + } + int sum = 0; + for (int score : stack) { + sum += score; + } + return sum; + } +} +``` + +```cpp +class Solution { +public: + int calPoints(vector& operations) { + vector stack; + for (const string& op : operations) { + if (op == "+") { + int top = stack.back(); stack.pop_back(); + int newTop = top + stack.back(); + stack.push_back(top); + stack.push_back(newTop); + } else if (op == "D") { + stack.push_back(2 * stack.back()); + } else if (op == "C") { + stack.pop_back(); + } else { + stack.push_back(stoi(op)); + } + } + return accumulate(stack.begin(), stack.end(), 0); + } +}; +``` + +```javascript +class Solution { + /** + * @param {string[]} operations + * @return {number} + */ + calPoints(operations) { + const stack = []; + for (const op of operations) { + if (op === "+") { + const top = stack.pop(); + const newTop = top + stack[stack.length - 1]; + stack.push(top); + stack.push(newTop); + } else if (op === "D") { + stack.push(2 * stack[stack.length - 1]); + } else if (op === "C") { + stack.pop(); + } else { + stack.push(parseInt(op)); + } + } + return stack.reduce((a, b) => a + b, 0); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(n)$ + +--- + +## 2. Stack - II + +::tabs-start + +```python +class Solution: + def calPoints(self, operations: List[str]) -> int: + stack, res = [], 0 + for op in operations: + if op == "+": + res += stack[-1] + stack[-2] + stack.append(stack[-1] + stack[-2]) + elif op == "D": + res += (2 * stack[-1]) + stack.append(2 * stack[-1]) + elif op == "C": + res -= stack.pop() + else: + res += int(op) + stack.append(int(op)) + return res +``` + +```java +public class Solution { + public int calPoints(String[] ops) { + int res = 0; + Stack stack = new Stack<>(); + for (String op : ops) { + if (op.equals("+")) { + int top = stack.pop(); + int newTop = top + stack.peek(); + stack.push(top); + stack.push(newTop); + res += newTop; + } else if (op.equals("D")) { + stack.push(2 * stack.peek()); + res += stack.peek(); + } else if (op.equals("C")) { + res -= stack.pop(); + } else { + stack.push(Integer.parseInt(op)); + res += stack.peek(); + } + } + return res; + } +} +``` + +```cpp +class Solution { +public: + int calPoints(vector& ops) { + stack stack; + int res = 0; + for (const string& op : ops) { + if (op == "+") { + int top = stack.top(); stack.pop(); + int newTop = top + stack.top(); + stack.push(top); + stack.push(newTop); + res += newTop; + } else if (op == "D") { + stack.push(2 * stack.top()); + res += stack.top(); + } else if (op == "C") { + res -= stack.top(); + stack.pop(); + } else { + stack.push(stoi(op)); + res += stack.top(); + } + } + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string[]} operations + * @return {number} + */ + calPoints(operations) { + const stack = []; + let res = 0; + for (const op of operations) { + if (op === "+") { + const top = stack.pop(); + const newTop = top + stack[stack.length - 1]; + stack.push(top); + stack.push(newTop); + res += newTop; + } else if (op === "D") { + stack.push(2 * stack[stack.length - 1]); + res += stack[stack.length - 1]; + } else if (op === "C") { + res -= stack.pop(); + } else { + stack.push(parseInt(op)); + res += stack[stack.length - 1]; + } + } + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(n)$ \ No newline at end of file diff --git a/articles/binary-search.md b/articles/binary-search.md index a05c98fd0..5e88782ce 100644 --- a/articles/binary-search.md +++ b/articles/binary-search.md @@ -591,7 +591,7 @@ class Solution { --- -## 5. Built-In Tool +## 5. Built-In Function ::tabs-start diff --git a/articles/check-if-two-string-arrays-are-equivalent.md b/articles/check-if-two-string-arrays-are-equivalent.md new file mode 100644 index 000000000..39805de58 --- /dev/null +++ b/articles/check-if-two-string-arrays-are-equivalent.md @@ -0,0 +1,270 @@ +## 1. Concatenate Strings + +::tabs-start + +```python +class Solution: + def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool: + return "".join(word1) == "".join(word2) +``` + +```java +public class Solution { + public boolean arrayStringsAreEqual(String[] word1, String[] word2) { + return String.join("", word1).equals(String.join("", word2)); + } +} +``` + +```cpp +class Solution { +public: + bool arrayStringsAreEqual(vector& word1, vector& word2) { + string str1 = accumulate(word1.begin(), word1.end(), string()); + string str2 = accumulate(word2.begin(), word2.end(), string()); + return str1 == str2; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string[]} word1 + * @param {string[]} word2 + * @return {boolean} + */ + arrayStringsAreEqual(word1, word2) { + return word1.join("") === word2.join(""); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n + m)$ +* Space complexity: $O(n + m)$ + +> Where $n$ and $m$ are the total number of characters in both the arrays $word1$ and $word2$, respectively. + +--- + +## 2. Concatenate Strings Of One Array + +::tabs-start + +```python +class Solution: + def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool: + s1 = "".join(word1) + i = 0 + for w in word2: + for c in w: + if i == len(s1) or s1[i] != c: + return False + i += 1 + return i == len(s1) +``` + +```java +public class Solution { + public boolean arrayStringsAreEqual(String[] word1, String[] word2) { + StringBuilder s1 = new StringBuilder(); + for (String w : word1) { + s1.append(w); + } + + int i = 0; + for (String w : word2) { + for (char c : w.toCharArray()) { + if (i == s1.length() || s1.charAt(i) != c) { + return false; + } + i++; + } + } + return i == s1.length(); + } +} +``` + +```cpp +class Solution { +public: + bool arrayStringsAreEqual(vector& word1, vector& word2) { + string s1 = ""; + for (string w : word1) s1 += w; + + int i = 0; + for (string w : word2) { + for (char c : w) { + if (i == s1.length() || s1[i] != c) return false; + i++; + } + } + return i == s1.length(); + } +}; +``` + +```javascript +class Solution { + /** + * @param {string[]} word1 + * @param {string[]} word2 + * @return {boolean} + */ + arrayStringsAreEqual(word1, word2) { + let s1 = word1.join(""); + let i = 0; + + for (let w of word2) { + for (let c of w) { + if (i === s1.length || s1[i] !== c) { + return false; + } + i++; + } + } + return i === s1.length; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n + m)$ +* Space complexity: $O(n)$ + +> Where $n$ and $m$ are the total number of characters in both the arrays $word1$ and $word2$, respectively. + +--- + +## 3. Two Pointers + +::tabs-start + +```python +class Solution: + def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool: + w1 = w2 = 0 # Index of word + i = j = 0 # Index of character + + while w1 < len(word1) and w2 < len(word2): + if word1[w1][i] != word2[w2][j]: + return False + + i, j = i + 1, j + 1 + + if i == len(word1[w1]): + w1 += 1 + i = 0 + if j == len(word2[w2]): + w2 += 1 + j = 0 + + return w1 == len(word1) and w2 == len(word2) +``` + +```java +public class Solution { + public boolean arrayStringsAreEqual(String[] word1, String[] word2) { + int w1 = 0, w2 = 0; // Index of word + int i = 0, j = 0; // Index of character + + while (w1 < word1.length && w2 < word2.length) { + if (word1[w1].charAt(i) != word2[w2].charAt(j)) { + return false; + } + + i++; + j++; + + if (i == word1[w1].length()) { + w1++; + i = 0; + } + if (j == word2[w2].length()) { + w2++; + j = 0; + } + } + return w1 == word1.length && w2 == word2.length; + } +} +``` + +```cpp +class Solution { +public: + bool arrayStringsAreEqual(vector& word1, vector& word2) { + int w1 = 0, w2 = 0; // Index of word + int i = 0, j = 0; // Index of character + + while (w1 < word1.size() && w2 < word2.size()) { + if (word1[w1][i] != word2[w2][j]) { + return false; + } + + i++; + j++; + + if (i == word1[w1].size()) { + w1++; + i = 0; + } + if (j == word2[w2].size()) { + w2++; + j = 0; + } + } + return w1 == word1.size() && w2 == word2.size(); + } +}; +``` + +```javascript +class Solution { + /** + * @param {string[]} word1 + * @param {string[]} word2 + * @return {boolean} + */ + arrayStringsAreEqual(word1, word2) { + let w1 = 0, w2 = 0; // Index of word + let i = 0, j = 0; // Index of character + + while (w1 < word1.length && w2 < word2.length) { + if (word1[w1][i] !== word2[w2][j]) { + return false; + } + + i++; + j++; + + if (i === word1[w1].length) { + w1++; + i = 0; + } + if (j === word2[w2].length) { + w2++; + j = 0; + } + } + return w1 === word1.length && w2 === word2.length; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n + m)$ +* Space complexity: $O(1)$ extra space. + +> Where $n$ and $m$ are the total number of characters in both the arrays $word1$ and $word2$, respectively. \ No newline at end of file diff --git a/articles/contains-duplicate-ii.md b/articles/contains-duplicate-ii.md new file mode 100644 index 000000000..9e1c9367e --- /dev/null +++ b/articles/contains-duplicate-ii.md @@ -0,0 +1,259 @@ +## 1. Brute Force + +::tabs-start + +```python +class Solution: + def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool: + for L in range(len(nums)): + for R in range(L + 1, min(len(nums), L + k + 1)): + if nums[L] == nums[R]: + return True + return False +``` + +```java +public class Solution { + public boolean containsNearbyDuplicate(int[] nums, int k) { + for (int L = 0; L < nums.length; L++) { + for (int R = L + 1; R < Math.min(nums.length, L + k + 1); R++) { + if (nums[L] == nums[R]) { + return true; + } + } + } + return false; + } +} +``` + +```cpp +class Solution { +public: + bool containsNearbyDuplicate(vector& nums, int k) { + for (int L = 0; L < nums.size(); L++) { + for (int R = L + 1; R < min((int)nums.size(), L + k + 1); R++) { + if (nums[L] == nums[R]) { + return true; + } + } + } + return false; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @param {number} k + * @return {boolean} + */ + containsNearbyDuplicate(nums, k) { + for (let L = 0; L < nums.length; L++) { + for (let R = L + 1; R < Math.min(nums.length, L + k + 1); R++) { + if (nums[L] === nums[R]) { + return true; + } + } + } + return false; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n * min(n, k))$ +* Space complexity: $O(1)$ + +> Where $n$ is the size of the array $nums$ and $k$ is the maximum distance between two equal numbers. + +--- + +## 2. Hash Map + +::tabs-start + +```python +class Solution: + def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool: + mp = {} + + for i in range(len(nums)): + if nums[i] in mp and i - mp[nums[i]] <= k: + return True + mp[nums[i]] = i + + return False +``` + +```java +public class Solution { + public boolean containsNearbyDuplicate(int[] nums, int k) { + Map map = new HashMap<>(); + + for (int i = 0; i < nums.length; i++) { + if (map.containsKey(nums[i]) && i - map.get(nums[i]) <= k) { + return true; + } + map.put(nums[i], i); + } + + return false; + } +} +``` + +```cpp +class Solution { +public: + bool containsNearbyDuplicate(vector& nums, int k) { + unordered_map mp; + + for (int i = 0; i < nums.size(); i++) { + if (mp.find(nums[i]) != mp.end() && i - mp[nums[i]] <= k) { + return true; + } + mp[nums[i]] = i; + } + + return false; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @param {number} k + * @return {boolean} + */ + containsNearbyDuplicate(nums, k) { + const map = new Map(); + + for (let i = 0; i < nums.length; i++) { + if (map.has(nums[i]) && i - map.get(nums[i]) <= k) { + return true; + } + map.set(nums[i], i); + } + + return false; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(n)$ + +> Where $n$ is the size of the array $nums$ and $k$ is the maximum distance between two equal numbers. + +--- + +## 3. Hash Set + +::tabs-start + +```python +class Solution: + def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool: + window = set() + L = 0 + + for R in range(len(nums)): + if R - L > k: + window.remove(nums[L]) + L += 1 + if nums[R] in window: + return True + window.add(nums[R]) + + return False +``` + +```java +public class Solution { + public boolean containsNearbyDuplicate(int[] nums, int k) { + Set window = new HashSet<>(); + int L = 0; + + for (int R = 0; R < nums.length; R++) { + if (R - L > k) { + window.remove(nums[L]); + L++; + } + if (window.contains(nums[R])) { + return true; + } + window.add(nums[R]); + } + return false; + } +} +``` + +```cpp +class Solution { +public: + bool containsNearbyDuplicate(vector& nums, int k) { + unordered_set window; + int L = 0; + + for (int R = 0; R < nums.size(); R++) { + if (R - L > k) { + window.erase(nums[L]); + L++; + } + if (window.find(nums[R]) != window.end()) { + return true; + } + window.insert(nums[R]); + } + return false; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @param {number} k + * @return {boolean} + */ + containsNearbyDuplicate(nums, k) { + let window = new Set(); + let L = 0; + + for (let R = 0; R < nums.length; R++) { + if (R - L > k) { + window.delete(nums[L]); + L++; + } + if (window.has(nums[R])) { + return true; + } + window.add(nums[R]); + } + return false; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(min(n, k))$ + +> Where $n$ is the size of the array $nums$ and $k$ is the maximum distance between two equal numbers. \ No newline at end of file diff --git a/articles/find-first-palindromic-string-in-the-array.md b/articles/find-first-palindromic-string-in-the-array.md new file mode 100644 index 000000000..03dd3683d --- /dev/null +++ b/articles/find-first-palindromic-string-in-the-array.md @@ -0,0 +1,147 @@ +## 1. Reverse String + +::tabs-start + +```python +class Solution: + def firstPalindrome(self, words: List[str]) -> str: + for w in words: + if w == w[::-1]: + return w + return "" +``` + +```java +public class Solution { + public String firstPalindrome(String[] words) { + for (String w : words) { + if (w.equals(new StringBuilder(w).reverse().toString())) { + return w; + } + } + return ""; + } +} +``` + +```cpp +class Solution { +public: + string firstPalindrome(vector& words) { + for (const string& w : words) { + string rev = w; + reverse(rev.begin(), rev.end()); + if (w == rev) { + return w; + } + } + return ""; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string[]} words + * @return {string} + */ + firstPalindrome(words) { + for (let w of words) { + if (w === w.split('').reverse().join('')) { + return w; + } + } + return ""; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n * m)$ +* Space complexity: $O(m)$ + +> Where $n$ is the size of the string array $words$ and $m$ is the average length of a word in the array. + +--- + +## 2. Two Pointers + +::tabs-start + +```python +class Solution: + def firstPalindrome(self, words: List[str]) -> str: + for w in words: + l, r = 0, len(w) - 1 + while w[l] == w[r]: + if l >= r: + return w + l, r = l + 1, r - 1 + return "" +``` + +```java +public class Solution { + public String firstPalindrome(String[] words) { + for (String w : words) { + int l = 0, r = w.length() - 1; + while (w.charAt(l) == w.charAt(r)) { + if (l >= r) return w; + l++; + r--; + } + } + return ""; + } +} +``` + +```cpp +class Solution { +public: + string firstPalindrome(vector& words) { + for (const string& w : words) { + int l = 0, r = w.length() - 1; + while (w[l] == w[r]) { + if (l >= r) return w; + l++; + r--; + } + } + return ""; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string[]} words + * @return {string} + */ + firstPalindrome(words) { + for (let w of words) { + let l = 0, r = w.length - 1; + while (w.charAt(l) === w.charAt(r)) { + if (l >= r) return w; + l++; + r--; + } + } + return ""; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n * m)$ +* Space complexity: $O(1)$ extra space. + +> Where $n$ is the size of the string array $words$ and $m$ is the average length of a word in the array. \ No newline at end of file diff --git a/articles/frequency-of-the-most-frequent-element.md b/articles/frequency-of-the-most-frequent-element.md new file mode 100644 index 000000000..51c727d97 --- /dev/null +++ b/articles/frequency-of-the-most-frequent-element.md @@ -0,0 +1,420 @@ +## 1. Brute Force + +::tabs-start + +```python +class Solution: + def maxFrequency(self, nums: List[int], k: int) -> int: + nums.sort() + res = 1 + for i in range(len(nums)): + j = i - 1 + tmpK = k + while j >= 0 and (tmpK - (nums[i] - nums[j])) >= 0: + tmpK -= (nums[i] - nums[j]) + j -= 1 + res = max(res, i - j) + return res +``` + +```java +public class Solution { + public int maxFrequency(int[] nums, int k) { + Arrays.sort(nums); + int res = 1; + + for (int i = 0; i < nums.length; i++) { + int j = i - 1; + long tmpK = k; + + while (j >= 0 && (tmpK - (nums[i] - nums[j])) >= 0) { + tmpK -= (nums[i] - nums[j]); + j--; + } + res = Math.max(res, i - j); + } + return res; + } +} +``` + +```cpp +class Solution { +public: + int maxFrequency(vector& nums, int k) { + sort(nums.begin(), nums.end()); + int res = 1; + + for (int i = 0; i < nums.size(); i++) { + int j = i - 1; + long long tmpK = k; + + while (j >= 0 && (tmpK - (nums[i] - nums[j])) >= 0) { + tmpK -= (nums[i] - nums[j]); + j--; + } + res = max(res, i - j); + } + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ + maxFrequency(nums, k) { + nums.sort((a, b) => a - b); + let res = 1; + + for (let i = 0; i < nums.length; i++) { + let j = i - 1; + let tmpK = k; + + while (j >= 0 && (tmpK - (nums[i] - nums[j])) >= 0) { + tmpK -= (nums[i] - nums[j]); + j--; + } + res = Math.max(res, i - j); + } + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n ^ 2 + n \log n)$ +* Space complexity: $O(1)$ or $O(n)$ depending on the sorting algorithm. + +--- + +## 2. Prefix Sum + Binary Search + +::tabs-start + +```python +class Solution: + def maxFrequency(self, nums: List[int], k: int) -> int: + nums.sort() + n = len(nums) + prefix_sum = [0] * (n + 1) + for i in range(n): + prefix_sum[i + 1] = prefix_sum[i] + nums[i] + + res = 1 + for i in range(n): + l, r = 0, i + while l <= r: + m = (l + r) // 2 + curSum = prefix_sum[i + 1] - prefix_sum[m] + need = (i - m + 1) * nums[i] - curSum + if need <= k: + r = m - 1 + res = max(res, i - m + 1) + else: + l = m + 1 + return res +``` + +```java +public class Solution { + public int maxFrequency(int[] nums, int k) { + Arrays.sort(nums); + int n = nums.length; + long[] prefixSum = new long[n + 1]; + for (int i = 0; i < n; i++) { + prefixSum[i + 1] = prefixSum[i] + nums[i]; + } + + int res = 1; + for (int i = 0; i < n; i++) { + int left = 0, right = i; + while (left <= right) { + int mid = (left + right) / 2; + long curSum = prefixSum[i + 1] - prefixSum[mid]; + long need = (i - mid + 1) * 1L * nums[i] - curSum; + if (need <= k) { + right = mid - 1; + res = Math.max(res, i - mid + 1); + } else { + left = mid + 1; + } + } + } + return res; + } +} +``` + +```cpp +class Solution { +public: + int maxFrequency(vector& nums, int k) { + sort(nums.begin(), nums.end()); + int n = nums.size(); + vector prefixSum(n + 1, 0); + for (int i = 0; i < n; ++i) { + prefixSum[i + 1] = prefixSum[i] + nums[i]; + } + + int res = 1; + for (int i = 0; i < n; ++i) { + int l = 0, r = i; + while (l <= r) { + int m = (l + r) / 2; + long long curSum = prefixSum[i + 1] - prefixSum[m]; + long long need = (i - m + 1) * 1LL * nums[i] - curSum; + if (need <= k) { + r = m - 1; + res = max(res, i - m + 1); + } else { + l = m + 1; + } + } + } + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ + maxFrequency(nums, k) { + nums.sort((a, b) => a - b); + const prefixSum = new Array(nums.length + 1).fill(0); + for (let i = 0; i < nums.length; i++) { + prefixSum[i + 1] = prefixSum[i] + nums[i]; + } + + let res = 1; + for (let i = 0; i < nums.length; i++) { + let left = 0, right = i; + while (left <= right) { + const mid = Math.floor((left + right) / 2); + const curSum = prefixSum[i + 1] - prefixSum[mid]; + const need = (i - mid + 1) * nums[i] - curSum; + if (need <= k) { + right = mid - 1; + res = Math.max(res, i - mid + 1); + } else { + left = mid + 1; + } + } + } + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n \log n)$ +* Space complexity: $O(n)$ + +--- + +## 3. Sliding Window + +::tabs-start + +```python +class Solution: + def maxFrequency(self, nums: List[int], k: int) -> int: + nums.sort() + total = res = 0 + l = 0 + + for r in range(len(nums)): + total += nums[r] + while nums[r] * (r - l + 1) > total + k: + total -= nums[l] + l += 1 + res = max(res, r - l + 1) + + return res +``` + +```java +public class Solution { + public int maxFrequency(int[] nums, int k) { + Arrays.sort(nums); + long total = 0; + int res = 0; + int l = 0; + + for (int r = 0; r < nums.length; r++) { + total += nums[r]; + while ((long) nums[r] * (r - l + 1) > total + k) { + total -= nums[l]; + l++; + } + res = Math.max(res, r - l + 1); + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + int maxFrequency(vector& nums, int k) { + sort(nums.begin(), nums.end()); + long long total = 0; + int res = 0, l = 0; + + for (int r = 0; r < nums.size(); ++r) { + total += nums[r]; + while ((long long)nums[r] * (r - l + 1) > total + k) { + total -= nums[l]; + l++; + } + res = max(res, r - l + 1); + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ + maxFrequency(nums, k) { + nums.sort((a, b) => a - b); + let total = 0, res = 0, l = 0; + + for (let r = 0; r < nums.length; r++) { + total += nums[r]; + while (nums[r] * (r - l + 1) > total + k) { + total -= nums[l]; + l++; + } + res = Math.max(res, r - l + 1); + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n \log n)$ +* Space complexity: $O(1)$ or $O(n)$ depending on the sorting algorithm. + +--- + +## 4. Advanced Sliding Window + +::tabs-start + +```python +class Solution: + def maxFrequency(self, nums: List[int], k: int) -> int: + nums.sort() + l = 0 + total = 0 + + for r in range(len(nums)): + total += nums[r] + + if (r - l + 1) * nums[r] > total + k: + total -= nums[l] + l += 1 + + return len(nums) - l +``` + +```java +public class Solution { + public int maxFrequency(int[] nums, int k) { + Arrays.sort(nums); + long total = 0; + int l = 0; + + for (int r = 0; r < nums.length; r++) { + total += nums[r]; + if ((r - l + 1) * 1L * nums[r] > total + k) { + total -= nums[l]; + l++; + } + } + + return nums.length - l; + } +} +``` + +```cpp +class Solution { +public: + int maxFrequency(vector& nums, int k) { + sort(nums.begin(), nums.end()); + long long total = 0; + int l = 0; + + for (int r = 0; r < nums.size(); ++r) { + total += nums[r]; + if ((r - l + 1) * 1L * nums[r] > total + k) { + total -= nums[l]; + l++; + } + } + + return nums.size() - l; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ + maxFrequency(nums, k) { + nums.sort((a, b) => a - b); + let total = 0, l = 0; + + for (let r = 0; r < nums.length; r++) { + total += nums[r]; + if (nums[r] * (r - l + 1) > total + k) { + total -= nums[l]; + l++; + } + } + + return nums.length - l; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n \log n)$ +* Space complexity: $O(1)$ or $O(n)$ depending on the sorting algorithm. \ No newline at end of file diff --git a/articles/fruit-into-baskets.md b/articles/fruit-into-baskets.md new file mode 100644 index 000000000..7aa1b5bbe --- /dev/null +++ b/articles/fruit-into-baskets.md @@ -0,0 +1,468 @@ +## 1. Brute Force + +::tabs-start + +```python +class Solution: + def totalFruit(self, fruits: List[int]) -> int: + n = len(fruits) + res = 0 + for i in range(n): + types = set() + j = i + while j < n and (len(types) < 2 or fruits[j] in types): + types.add(fruits[j]) + j += 1 + res = max(res, j - i) + return res +``` + +```java +public class Solution { + public int totalFruit(int[] fruits) { + int n = fruits.length, res = 0; + + for (int i = 0; i < n; i++) { + Set types = new HashSet<>(); + int j = i; + + while (j < n && (types.size() < 2 || types.contains(fruits[j]))) { + types.add(fruits[j]); + j++; + } + res = Math.max(res, j - i); + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + int totalFruit(vector& fruits) { + int n = fruits.size(), res = 0; + + for (int i = 0; i < n; i++) { + unordered_set types; + int j = i; + + while (j < n && (types.size() < 2 || types.count(fruits[j]))) { + types.insert(fruits[j]); + j++; + } + res = max(res, j - i); + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} fruits + * @return {number} + */ + totalFruit(fruits) { + let n = fruits.length, res = 0; + + for (let i = 0; i < n; i++) { + let types = new Set(); + let j = i; + + while (j < n && (types.size < 2 || types.has(fruits[j]))) { + types.add(fruits[j]); + j++; + } + res = Math.max(res, j - i); + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n ^ 2)$ +* Space complexity: $O(1)$ + +--- + +## 2. Sliding Window - I + +::tabs-start + +```python +class Solution: + def totalFruit(self, fruits: List[int]) -> int: + count = defaultdict(int) + l, total, res = 0, 0, 0 + + for r in range(len(fruits)): + count[fruits[r]] += 1 + total += 1 + + while len(count) > 2: + f = fruits[l] + count[f] -= 1 + total -= 1 + l += 1 + if not count[f]: + count.pop(f) + + res = max(res, total) + + return res +``` + +```java +public class Solution { + public int totalFruit(int[] fruits) { + HashMap count = new HashMap<>(); + int l = 0, total = 0, res = 0; + + for (int r = 0; r < fruits.length; r++) { + count.put(fruits[r], count.getOrDefault(fruits[r], 0) + 1); + total++; + + while (count.size() > 2) { + int f = fruits[l]; + count.put(f, count.get(f) - 1); + total--; + if (count.get(f) == 0) { + count.remove(f); + } + l++; + } + res = Math.max(res, total); + } + return res; + } +} +``` + +```cpp +class Solution { +public: + int totalFruit(vector& fruits) { + unordered_map count; + int l = 0, total = 0, res = 0; + + for (int r = 0; r < fruits.size(); r++) { + count[fruits[r]]++; + total++; + + while (count.size() > 2) { + int f = fruits[l]; + count[f]--; + total--; + if (count[f] == 0) { + count.erase(f); + } + l++; + } + res = max(res, total); + } + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} fruits + * @return {number} + */ + totalFruit(fruits) { + let count = new Map(); + let l = 0, total = 0, res = 0; + + for (let r = 0; r < fruits.length; r++) { + count.set(fruits[r], (count.get(fruits[r]) || 0) + 1); + total++; + + while (count.size > 2) { + let f = fruits[l]; + count.set(f, count.get(f) - 1); + total--; + if (count.get(f) === 0) { + count.delete(f); + } + l++; + } + res = Math.max(res, total); + } + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ extra space. + +--- + +## 3. Sliding Window - II + +::tabs-start + +```python +class Solution: + def totalFruit(self, fruits: List[int]) -> int: + count = defaultdict(int) + l = 0 + + for r in range(len(fruits)): + count[fruits[r]] += 1 + + if len(count) > 2: + count[fruits[l]] -= 1 + if count[fruits[l]] == 0: + count.pop(fruits[l]) + l += 1 + + return len(fruits) - l +``` + +```java +public class Solution { + public int totalFruit(int[] fruits) { + HashMap count = new HashMap<>(); + int l = 0; + + for (int r = 0; r < fruits.length; r++) { + count.put(fruits[r], count.getOrDefault(fruits[r], 0) + 1); + + if (count.size() > 2) { + count.put(fruits[l], count.get(fruits[l]) - 1); + if (count.get(fruits[l]) == 0) { + count.remove(fruits[l]); + } + l++; + } + } + + return fruits.length - l; + } +} +``` + +```cpp +class Solution { +public: + int totalFruit(vector& fruits) { + unordered_map count; + int l = 0; + + for (int r = 0; r < fruits.size(); r++) { + count[fruits[r]]++; + + if (count.size() > 2) { + count[fruits[l]]--; + if (count[fruits[l]] == 0) { + count.erase(fruits[l]); + } + l++; + } + } + + return fruits.size() - l; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} fruits + * @return {number} + */ + totalFruit(fruits) { + let count = new Map(); + let l = 0; + + for (let r = 0; r < fruits.length; r++) { + count.set(fruits[r], (count.get(fruits[r]) || 0) + 1); + + if (count.size > 2) { + count.set(fruits[l], count.get(fruits[l]) - 1); + if (count.get(fruits[l]) === 0) { + count.delete(fruits[l]); + } + l++; + } + } + + return fruits.length - l; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ extra space. + +--- + +## 4. Sliding Window - III + +::tabs-start + +```python +class Solution: + def totalFruit(self, fruits: list[int]) -> int: + l = 0 + fruit1_lastIdx = 0 + fruit2_lastIdx = -1 + fruit1 = fruits[0] + fruit2 = -1 + total = res = 1 + + for r in range(len(fruits)): + f = fruits[r] + if f == fruit1: + total += 1 + fruit1_lastIdx = r + elif f == fruit2 or fruit2 == -1: + total += 1 + fruit2_lastIdx = r + fruit2 = f + else: + if fruit2_lastIdx == min(fruit1_lastIdx, fruit2_lastIdx): + fruit1_lastIdx, fruit2_lastIdx = fruit2_lastIdx, fruit1_lastIdx + fruit1, fruit2 = fruit2, fruit1 + + total -= (fruit1_lastIdx - l + 1) + l = fruit1_lastIdx + 1 + fruit1 = f + fruit1_lastIdx = r + res = max(res, r - l + 1) + + return res +``` + +```java +public class Solution { + public int totalFruit(int[] fruits) { + int l = 0, fruit1_lastIdx = 0, fruit2_lastIdx = -1; + int fruit1 = fruits[0], fruit2 = -1, total = 1, res = 1; + + for (int r = 0; r < fruits.length; r++) { + int f = fruits[r]; + if (f == fruit1) { + total++; + fruit1_lastIdx = r; + } else if (f == fruit2 || fruit2 == -1) { + total++; + fruit2_lastIdx = r; + fruit2 = f; + } else { + if (fruit2_lastIdx == Math.min(fruit1_lastIdx, fruit2_lastIdx)) { + int tempIdx = fruit1_lastIdx; + fruit1_lastIdx = fruit2_lastIdx; + fruit2_lastIdx = tempIdx; + int tempFruit = fruit1; + fruit1 = fruit2; + fruit2 = tempFruit; + } + total -= (fruit1_lastIdx - l + 1); + l = fruit1_lastIdx + 1; + fruit1 = f; + fruit1_lastIdx = r; + } + res = Math.max(res, r - l + 1); + } + return res; + } +} +``` + +```cpp +class Solution { +public: + int totalFruit(vector& fruits) { + int l = 0, fruit1_lastIdx = 0, fruit2_lastIdx = -1; + int fruit1 = fruits[0], fruit2 = -1, total = 1, res = 1; + + for (int r = 0; r < fruits.size(); r++) { + int f = fruits[r]; + if (f == fruit1) { + total++; + fruit1_lastIdx = r; + } else if (f == fruit2 || fruit2 == -1) { + total++; + fruit2_lastIdx = r; + fruit2 = f; + } else { + if (fruit2_lastIdx == min(fruit1_lastIdx, fruit2_lastIdx)) { + swap(fruit1_lastIdx, fruit2_lastIdx); + swap(fruit1, fruit2); + } + total -= (fruit1_lastIdx - l + 1); + l = fruit1_lastIdx + 1; + fruit1 = f; + fruit1_lastIdx = r; + } + res = max(res, r - l + 1); + } + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} fruits + * @return {number} + */ + totalFruit(fruits) { + let l = 0, fruit1_lastIdx = 0, fruit2_lastIdx = -1; + let fruit1 = fruits[0], fruit2 = -1, total = 1, res = 1; + + for (let r = 0; r < fruits.length; r++) { + let f = fruits[r]; + if (f === fruit1) { + total++; + fruit1_lastIdx = r; + } else if (f === fruit2 || fruit2 === -1) { + total++; + fruit2_lastIdx = r; + fruit2 = f; + } else { + if (fruit2_lastIdx === Math.min(fruit1_lastIdx, fruit2_lastIdx)) { + [fruit1_lastIdx, fruit2_lastIdx] = [fruit2_lastIdx, fruit1_lastIdx]; + [fruit1, fruit2] = [fruit2, fruit1]; + } + total -= (fruit1_lastIdx - l + 1); + l = fruit1_lastIdx + 1; + fruit1 = f; + fruit1_lastIdx = r; + } + res = Math.max(res, r - l + 1); + } + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ extra space. \ No newline at end of file diff --git a/articles/guess-number-higher-or-lower.md b/articles/guess-number-higher-or-lower.md new file mode 100644 index 000000000..b6053e2e8 --- /dev/null +++ b/articles/guess-number-higher-or-lower.md @@ -0,0 +1,352 @@ +## 1. Linear Search + +::tabs-start + +```python +# The guess API is already defined for you. +# @param num, your guess +# @return -1 if num is higher than the picked number +# 1 if num is lower than the picked number +# otherwise return 0 +# def guess(num: int) -> int: + +class Solution: + def guessNumber(self, n: int) -> int: + for num in range(1, n + 1): + if guess(num) == 0: + return num +``` + +```java +/** + * Forward declaration of guess API. + * @param num your guess + * @return -1 if num is higher than the picked number + * 1 if num is lower than the picked number + * otherwise return 0 + * int guess(int num); + */ + +public class Solution extends GuessGame { + public int guessNumber(int n) { + for (int num = 1; num <= n; num++) { + if (guess(num) == 0) return num; + } + return n; + } +} +``` + +```cpp +/** + * Forward declaration of guess API. + * @param num your guess + * @return -1 if num is higher than the picked number + * 1 if num is lower than the picked number + * otherwise return 0 + * int guess(int num); + */ + +class Solution { +public: + int guessNumber(int n) { + for (int num = 1; num <= n; num++) { + if (guess(num) == 0) return num; + } + return n; + } +}; +``` + +```javascript +/** + * Forward declaration of guess API. + * @param {number} num your guess + * @return -1 if num is higher than the picked number + * 1 if num is lower than the picked number + * otherwise return 0 + * var guess = function(num) {} + */ + +class Solution { + /** + * @param {number} n + * @return {number} + */ + guessNumber(n) { + for (let num = 1; num <= n; num++) { + if (guess(num) === 0) return num; + } + return n; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ + +--- + +## 2. Binary Search + +::tabs-start + +```python +# The guess API is already defined for you. +# @param num, your guess +# @return -1 if num is higher than the picked number +# 1 if num is lower than the picked number +# otherwise return 0 +# def guess(num: int) -> int: + +class Solution: + def guessNumber(self, n: int) -> int: + l, r = 1, n + while True: + m = (l + r) // 2 + res = guess(m) + if res > 0: + l = m + 1 + elif res < 0: + r = m - 1 + else: + return m +``` + +```java +/** + * Forward declaration of guess API. + * @param num your guess + * @return -1 if num is higher than the picked number + * 1 if num is lower than the picked number + * otherwise return 0 + * int guess(int num); + */ + +public class Solution extends GuessGame { + public int guessNumber(int n) { + int l = 1, r = n; + while (true) { + int m = l + (r - l) / 2; + int res = guess(m); + if (res > 0) { + l = m + 1; + } else if (res < 0) { + r = m - 1; + } else { + return m; + } + } + } +} +``` + +```cpp +/** + * Forward declaration of guess API. + * @param num your guess + * @return -1 if num is higher than the picked number + * 1 if num is lower than the picked number + * otherwise return 0 + * int guess(int num); + */ + +class Solution { +public: + int guessNumber(int n) { + int l = 1, r = n; + while (true) { + int m = l + (r - l) / 2; + int res = guess(m); + if (res > 0) { + l = m + 1; + } else if (res < 0) { + r = m - 1; + } else { + return m; + } + } + } +}; +``` + +```javascript +/** + * Forward declaration of guess API. + * @param {number} num your guess + * @return -1 if num is higher than the picked number + * 1 if num is lower than the picked number + * otherwise return 0 + * var guess = function(num) {} + */ + +class Solution { + /** + * @param {number} n + * @return {number} + */ + guessNumber(n) { + let l = 1, r = n; + while (true) { + let m = Math.floor((l + r) / 2); + let res = guess(m); + if (res > 0) { + l = m + 1; + } else if (res < 0) { + r = m - 1; + } else { + return m; + } + } + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(\log n)$ +* Space complexity: $O(1)$ + +--- + +## 3. Ternary Search + +::tabs-start + +```python +# The guess API is already defined for you. +# @param num, your guess +# @return -1 if num is higher than the picked number +# 1 if num is lower than the picked number +# otherwise return 0 +# def guess(num: int) -> int: + +class Solution: + def guessNumber(self, n: int) -> int: + l, r = 1, n + while True: + m1 = l + (r - l) // 3 + m2 = r - (r - l) // 3 + if guess(m1) == 0: + return m1 + if guess(m2) == 0: + return m2 + if guess(m1) + guess(m2) == 0: + l = m1 + 1 + r = m2 - 1 + elif guess(m1) == -1: + r = m1 - 1 + else: + l = m2 + 1 +``` + +```java +/** + * Forward declaration of guess API. + * @param num your guess + * @return -1 if num is higher than the picked number + * 1 if num is lower than the picked number + * otherwise return 0 + * int guess(int num); + */ + +public class Solution extends GuessGame { + public int guessNumber(int n) { + int l = 1, r = n; + while (true) { + int m1 = l + (r - l) / 3; + int m2 = r - (r - l) / 3; + if (guess(m1) == 0) return m1; + if (guess(m2) == 0) return m2; + if (guess(m1) + guess(m2) == 0) { + l = m1 + 1; + r = m2 - 1; + } else if (guess(m1) == -1) { + r = m1 - 1; + } else { + l = m2 + 1; + } + } + } +} +``` + +```cpp +/** + * Forward declaration of guess API. + * @param num your guess + * @return -1 if num is higher than the picked number + * 1 if num is lower than the picked number + * otherwise return 0 + * int guess(int num); + */ + +class Solution { +public: + int guessNumber(int n) { + int l = 1, r = n; + while (true) { + int m1 = l + (r - l) / 3; + int m2 = r - (r - l) / 3; + if (guess(m1) == 0) return m1; + if (guess(m2) == 0) return m2; + if (guess(m1) + guess(m2) == 0) { + l = m1 + 1; + r = m2 - 1; + } else if (guess(m1) == -1) { + r = m1 - 1; + } else { + l = m2 + 1; + } + } + } +}; +``` + +```javascript +/** + * Forward declaration of guess API. + * @param {number} num your guess + * @return -1 if num is higher than the picked number + * 1 if num is lower than the picked number + * otherwise return 0 + * var guess = function(num) {} + */ + +class Solution { + /** + * @param {number} n + * @return {number} + */ + guessNumber(n) { + let l = 1, r = n; + while (true) { + let m1 = l + Math.floor((r - l) / 3); + let m2 = r - Math.floor((r - l) / 3); + if (guess(m1) === 0) return m1; + if (guess(m2) === 0) return m2; + if (guess(m1) + guess(m2) === 0) { + l = m1 + 1; + r = m2 - 1; + } else if (guess(m1) === -1) { + r = m1 - 1; + } else { + l = m2 + 1; + } + } + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(\log_3 n)$ +* Space complexity: $O(1)$ \ No newline at end of file diff --git a/articles/maximum-number-of-vowels-in-a-substring-of-given-length.md b/articles/maximum-number-of-vowels-in-a-substring-of-given-length.md new file mode 100644 index 000000000..371aff2b3 --- /dev/null +++ b/articles/maximum-number-of-vowels-in-a-substring-of-given-length.md @@ -0,0 +1,385 @@ +## 1. Brute Force + +::tabs-start + +```python +class Solution: + def maxVowels(self, s: str, k: int) -> int: + vowel = {'a', 'e', 'i', 'o', 'u'} + res = 0 + + for i in range(len(s) - k + 1): + cnt = 0 + for j in range(i, i + k): + cnt += 1 if s[j] in vowel else 0 + res = max(res, cnt) + + return res +``` + +```java +public class Solution { + public int maxVowels(String s, int k) { + Set vowels = Set.of('a', 'e', 'i', 'o', 'u'); + int res = 0; + + for (int i = 0; i <= s.length() - k; i++) { + int cnt = 0; + for (int j = i; j < i + k; j++) { + if (vowels.contains(s.charAt(j))) { + cnt++; + } + } + res = Math.max(res, cnt); + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + int maxVowels(string s, int k) { + unordered_set vowels = {'a', 'e', 'i', 'o', 'u'}; + int res = 0; + + for (int i = 0; i <= s.size() - k; i++) { + int cnt = 0; + for (int j = i; j < i + k; j++) { + if (vowels.count(s[j])) { + cnt++; + } + } + res = max(res, cnt); + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string} s + * @param {number} k + * @return {number} + */ + maxVowels(s, k) { + const vowels = new Set(['a', 'e', 'i', 'o', 'u']); + let res = 0; + + for (let i = 0; i <= s.length - k; i++) { + let cnt = 0; + for (let j = i; j < i + k; j++) { + if (vowels.has(s[j])) { + cnt++; + } + } + res = Math.max(res, cnt); + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n ^ 2)$ +* Space complexity: $O(1)$ extra space. + +--- + +## 2. Prefix Count + +::tabs-start + +```python +class Solution: + def maxVowels(self, s: str, k: int) -> int: + vowel = {'a', 'e', 'i', 'o', 'u'} + prefix = [0] * (len(s) + 1) + for i in range(len(s)): + prefix[i + 1] = prefix[i] + (1 if s[i] in vowel else 0) + + res = 0 + for i in range(k, len(s) + 1): + res = max(res, prefix[i] - prefix[i - k]) + + return res +``` + +```java +public class Solution { + public int maxVowels(String s, int k) { + Set vowels = Set.of('a', 'e', 'i', 'o', 'u'); + int[] prefix = new int[s.length() + 1]; + + for (int i = 0; i < s.length(); i++) { + prefix[i + 1] = prefix[i] + (vowels.contains(s.charAt(i)) ? 1 : 0); + } + + int res = 0; + for (int i = k; i <= s.length(); i++) { + res = Math.max(res, prefix[i] - prefix[i - k]); + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + int maxVowels(string s, int k) { + unordered_set vowels = {'a', 'e', 'i', 'o', 'u'}; + vector prefix(s.size() + 1, 0); + + for (int i = 0; i < s.size(); i++) { + prefix[i + 1] = prefix[i] + (vowels.count(s[i]) ? 1 : 0); + } + + int res = 0; + for (int i = k; i <= s.size(); i++) { + res = max(res, prefix[i] - prefix[i - k]); + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string} s + * @param {number} k + * @return {number} + */ + maxVowels(s, k) { + const vowels = new Set(['a', 'e', 'i', 'o', 'u']); + const prefix = new Array(s.length + 1).fill(0); + + for (let i = 0; i < s.length; i++) { + prefix[i + 1] = prefix[i] + (vowels.has(s[i]) ? 1 : 0); + } + + let res = 0; + for (let i = k; i <= s.length; i++) { + res = Math.max(res, prefix[i] - prefix[i - k]); + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(n)$ + +--- + +## 3. Sliding Window + +::tabs-start + +```python +class Solution: + def maxVowels(self, s: str, k: int) -> int: + vowel = {'a', 'e', 'i', 'o', 'u'} + + l = cnt = res = 0 + for r in range(len(s)): + cnt += 1 if s[r] in vowel else 0 + if r - l + 1 > k: + cnt -= 1 if s[l] in vowel else 0 + l += 1 + res = max(res, cnt) + return res +``` + +```java +public class Solution { + public int maxVowels(String s, int k) { + Set vowels = Set.of('a', 'e', 'i', 'o', 'u'); + + int l = 0, cnt = 0, res = 0; + for (int r = 0; r < s.length(); r++) { + cnt += (vowels.contains(s.charAt(r)) ? 1 : 0); + if (r - l + 1 > k) { + cnt -= (vowels.contains(s.charAt(l)) ? 1 : 0); + l++; + } + res = Math.max(res, cnt); + } + return res; + } +} +``` + +```cpp +class Solution { +public: + int maxVowels(string s, int k) { + unordered_set vowels = {'a', 'e', 'i', 'o', 'u'}; + + int l = 0, cnt = 0, res = 0; + for (int r = 0; r < s.length(); r++) { + cnt += (vowels.count(s[r]) ? 1 : 0); + if (r - l + 1 > k) { + cnt -= (vowels.count(s[l++]) ? 1 : 0); + } + res = max(res, cnt); + } + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string} s + * @param {number} k + * @return {number} + */ + maxVowels(s, k) { + const vowels = new Set(['a', 'e', 'i', 'o', 'u']); + + let l = 0, cnt = 0, res = 0; + for (let r = 0; r < s.length; r++) { + cnt += (vowels.has(s[r]) ? 1 : 0); + if (r - l + 1 > k) { + cnt -= (vowels.has(s[l++]) ? 1 : 0); + } + res = Math.max(res, cnt); + } + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ extra space. + +--- + +## 4. Sliding Window (Bit Mask) + +::tabs-start + +```python +class Solution: + def maxVowels(self, s: str, k: int) -> int: + def getId(c): + return ord(c) - ord('a') + + mask = (1 << getId('a')) | (1 << getId('e')) | \ + (1 << getId('i')) | (1 << getId('o')) | \ + (1 << getId('u')) + + l = cnt = res = 0 + for r in range(len(s)): + cnt += ((mask >> getId(s[r])) & 1) + if r - l + 1 > k: + cnt -= ((mask >> getId(s[l])) & 1) + l += 1 + res = max(res, cnt) + return res +``` + +```java +public class Solution { + public int maxVowels(String s, int k) { + int mask = (1 << ('a' - 'a')) | (1 << ('e' - 'a')) | + (1 << ('i' - 'a')) | (1 << ('o' - 'a')) | + (1 << ('u' - 'a')); + + int l = 0, cnt = 0, res = 0; + for (int r = 0; r < s.length(); r++) { + cnt += (mask >> (s.charAt(r) - 'a')) & 1; + if (r - l + 1 > k) { + cnt -= (mask >> (s.charAt(l) - 'a')) & 1; + l++; + } + res = Math.max(res, cnt); + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + int maxVowels(string s, int k) { + int mask = (1 << ('a' - 'a')) | (1 << ('e' - 'a')) | + (1 << ('i' - 'a')) | (1 << ('o' - 'a')) | + (1 << ('u' - 'a')); + + int l = 0, cnt = 0, res = 0; + for (int r = 0; r < s.size(); r++) { + cnt += (mask >> (s[r] - 'a')) & 1; + if (r - l + 1 > k) { + cnt -= (mask >> (s[l] - 'a')) & 1; + l++; + } + res = max(res, cnt); + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string} s + * @param {number} k + * @return {number} + */ + maxVowels(s, k) { + const getId = (c) => { + return c.charCodeAt(0) - 'a'.charCodeAt(0); + }; + const mask = (1 << getId('a')) | (1 << getId('e')) | + (1 << getId('i')) | (1 << getId('o')) | + (1 << getId('u')); + + let l = 0, cnt = 0, res = 0; + for (let r = 0; r < s.length; r++) { + cnt += (mask >> getId(s.charAt(r))) & 1; + if (r - l + 1 > k) { + cnt -= (mask >> getId(s.charAt(l))) & 1; + l++; + } + res = Math.max(res, cnt); + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ extra space. \ No newline at end of file diff --git a/articles/merge-sorted-array.md b/articles/merge-sorted-array.md new file mode 100644 index 000000000..aab35dc73 --- /dev/null +++ b/articles/merge-sorted-array.md @@ -0,0 +1,374 @@ +## 1. Sorting + +::tabs-start + +```python +class Solution: + def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: + """ + Do not return anything, modify nums1 in-place instead. + """ + nums1[m:] = nums2[:n] + nums1.sort() +``` + +```java +public class Solution { + public void merge(int[] nums1, int m, int[] nums2, int n) { + for (int i = 0; i < n; i++) { + nums1[i + m] = nums2[i]; + } + Arrays.sort(nums1); + } +} +``` + +```cpp +class Solution { +public: + void merge(vector& nums1, int m, vector& nums2, int n) { + for (int i = 0; i < n; i++) { + nums1[i + m] = nums2[i]; + } + sort(nums1.begin(), nums1.end()); + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums1 + * @param {number} m + * @param {number[]} nums2 + * @param {number} n + * @return {void} Do not return anything, modify nums1 in-place instead. + */ + merge(nums1, m, nums2, n) { + for (let i = 0; i < n; i++) { + nums1[i + m] = nums2[i]; + } + nums1.sort((a, b) => a - b); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O((m + n) \log (m + n))$ +* Space complexity: $O(1)$ or $O(m + n)$ depending on the sorting algorithm. + +> Where $m$ and $n$ represent the number of elements in the arrays $nums1$ and $nums2$, respectively. + +--- + +## 2. Three Pointers With Extra Space + +::tabs-start + +```python +class Solution: + def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: + """ + Do not return anything, modify nums1 in-place instead. + """ + nums1_copy = nums1[:m] + idx = 0 + i = j = 0 + while idx < m + n: + if j >= n or (i < m and nums1_copy[i] <= nums2[j]): + nums1[idx] = nums1_copy[i] + i += 1 + else: + nums1[idx] = nums2[j] + j += 1 + idx += 1 +``` + +```java +public class Solution { + public void merge(int[] nums1, int m, int[] nums2, int n) { + int[] nums1Copy = Arrays.copyOf(nums1, m); + int idx = 0, i = 0, j = 0; + + while (idx < m + n) { + if (j >= n || (i < m && nums1Copy[i] <= nums2[j])) { + nums1[idx++] = nums1Copy[i++]; + } else { + nums1[idx++] = nums2[j++]; + } + } + } +} +``` + +```cpp +class Solution { +public: + void merge(vector& nums1, int m, vector& nums2, int n) { + vector nums1Copy(nums1.begin(), nums1.begin() + m); + int idx = 0, i = 0, j = 0; + + while (idx < m + n) { + if (j >= n || (i < m && nums1Copy[i] <= nums2[j])) { + nums1[idx++] = nums1Copy[i++]; + } else { + nums1[idx++] = nums2[j++]; + } + } + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums1 + * @param {number} m + * @param {number[]} nums2 + * @param {number} n + * @return {void} Do not return anything, modify nums1 in-place instead. + */ + merge(nums1, m, nums2, n) { + const nums1Copy = nums1.slice(0, m); + let idx = 0, i = 0, j = 0; + + while (idx < m + n) { + if (j >= n || (i < m && nums1Copy[i] <= nums2[j])) { + nums1[idx++] = nums1Copy[i++]; + } else { + nums1[idx++] = nums2[j++]; + } + } + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(m + n)$ +* Space complexity: $O(m)$ + +> Where $m$ and $n$ represent the number of elements in the arrays $nums1$ and $nums2$, respectively. + +--- + +## 3. Three Pointers Without Extra Space - I + +::tabs-start + +```python +class Solution: + def merge(self, nums1: list[int], m: int, nums2: list[int], n: int) -> None: + """ + Do not return anything, modify nums1 in-place instead. + """ + last = m + n - 1 + + # Merge in reverse order + while m > 0 and n > 0: + if nums1[m - 1] > nums2[n - 1]: + nums1[last] = nums1[m - 1] + m -= 1 + else: + nums1[last] = nums2[n - 1] + n -= 1 + last -= 1 + + # Fill nums1 with leftover nums2 elements + while n > 0: + nums1[last] = nums2[n - 1] + n -= 1 + last -= 1 +``` + +```java +public class Solution { + public void merge(int[] nums1, int m, int[] nums2, int n) { + int last = m + n - 1; + + // Merge in reverse order + while (m > 0 && n > 0) { + if (nums1[m - 1] > nums2[n - 1]) { + nums1[last] = nums1[m - 1]; + m--; + } else { + nums1[last] = nums2[n - 1]; + n--; + } + last--; + } + + // Fill nums1 with leftover nums2 elements + while (n > 0) { + nums1[last] = nums2[n - 1]; + n--; + last--; + } + } +} +``` + +```cpp +class Solution { +public: + void merge(vector& nums1, int m, vector& nums2, int n) { + int last = m + n - 1; + + // Merge in reverse order + while (m > 0 && n > 0) { + if (nums1[m - 1] > nums2[n - 1]) { + nums1[last] = nums1[m - 1]; + m--; + } else { + nums1[last] = nums2[n - 1]; + n--; + } + last--; + } + + // Fill nums1 with leftover nums2 elements + while (n > 0) { + nums1[last] = nums2[n - 1]; + n--; + last--; + } + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums1 + * @param {number} m + * @param {number[]} nums2 + * @param {number} n + * @return {void} Do not return anything, modify nums1 in-place instead. + */ + merge(nums1, m, nums2, n) { + let last = m + n - 1; + + // Merge in reverse order + while (m > 0 && n > 0) { + if (nums1[m - 1] > nums2[n - 1]) { + nums1[last--] = nums1[m-- - 1]; + } else { + nums1[last--] = nums2[n-- - 1]; + } + } + + // Fill nums1 with leftover nums2 elements + while (n > 0) { + nums1[last--] = nums2[n-- - 1]; + } + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(m + n)$ +* Space complexity: $O(1)$ extra space. + +> Where $m$ and $n$ represent the number of elements in the arrays $nums1$ and $nums2$, respectively. + +--- + +## 4. Three Pointers Without Extra Space - II + +::tabs-start + +```python +class Solution: + def merge(self, nums1: list[int], m: int, nums2: list[int], n: int) -> None: + """ + Do not return anything, modify nums1 in-place instead. + """ + last = m + n - 1 + i, j = m - 1, n - 1 + + while j >= 0: + if i >= 0 and nums1[i] > nums2[j]: + nums1[last] = nums1[i] + i -= 1 + else: + nums1[last] = nums2[j] + j -= 1 + + last -= 1 +``` + +```java +public class Solution { + public void merge(int[] nums1, int m, int[] nums2, int n) { + int last = m + n - 1; + int i = m - 1, j = n - 1; + + while (j >= 0) { + if (i >= 0 && nums1[i] > nums2[j]) { + nums1[last--] = nums1[i--]; + } else { + nums1[last--] = nums2[j--]; + } + } + } +} +``` + +```cpp +class Solution { +public: + void merge(vector& nums1, int m, vector& nums2, int n) { + int last = m + n - 1; + int i = m - 1, j = n - 1; + + while (j >= 0) { + if (i >= 0 && nums1[i] > nums2[j]) { + nums1[last--] = nums1[i--]; + } else { + nums1[last--] = nums2[j--]; + } + } + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums1 + * @param {number} m + * @param {number[]} nums2 + * @param {number} n + * @return {void} Do not return anything, modify nums1 in-place instead. + */ + merge(nums1, m, nums2, n) { + let last = m + n - 1; + let i = m - 1, j = n - 1; + + while (j >= 0) { + if (i >= 0 && nums1[i] > nums2[j]) { + nums1[last--] = nums1[i--]; + } else { + nums1[last--] = nums2[j--]; + } + } + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(m + n)$ +* Space complexity: $O(1)$ extra space. + +> Where $m$ and $n$ represent the number of elements in the arrays $nums1$ and $nums2$, respectively. \ No newline at end of file diff --git a/articles/merge-strings-alternately.md b/articles/merge-strings-alternately.md new file mode 100644 index 000000000..04d95926d --- /dev/null +++ b/articles/merge-strings-alternately.md @@ -0,0 +1,250 @@ +## 1. Two Pointers - I + +::tabs-start + +```python +class Solution: + def mergeAlternately(self, word1: str, word2: str) -> str: + i, j = 0, 0 + res = [] + while i < len(word1) and j < len(word2): + res.append(word1[i]) + res.append(word2[j]) + i += 1 + j += 1 + res.append(word1[i:]) + res.append(word2[j:]) + return "".join(res) +``` + +```java +public class Solution { + public String mergeAlternately(String word1, String word2) { + StringBuilder res = new StringBuilder(); + int i = 0, j = 0; + while (i < word1.length() && j < word2.length()) { + res.append(word1.charAt(i++)); + res.append(word2.charAt(j++)); + } + res.append(word1.substring(i)); + res.append(word2.substring(j)); + return res.toString(); + } +} +``` + +```cpp +class Solution { +public: + string mergeAlternately(string word1, string word2) { + string res; + int i = 0, j = 0; + while (i < word1.size() && j < word2.size()) { + res += word1[i++]; + res += word2[j++]; + } + res += word1.substr(i); + res += word2.substr(j); + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string} word1 + * @param {string} word2 + * @return {string} + */ + mergeAlternately(word1, word2) { + let res = []; + let i = 0, j = 0; + while (i < word1.length && j < word2.length) { + res.push(word1[i++], word2[j++]); + } + res.push(word1.slice(i)); + res.push(word2.slice(j)); + return res.join(""); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n + m)$ +* Space complexity: $O(n + m)$ for the output string. + +> Where $n$ and $m$ are the lengths of the strings $word1$ and $word2$ respectively. + +--- + +## 2. Two Pointers - II + +::tabs-start + +```python +class Solution: + def mergeAlternately(self, word1: str, word2: str) -> str: + n, m = len(word1), len(word2) + res = [] + i = j = 0 + while i < n or j < m: + if i < n: + res.append(word1[i]) + if j < m: + res.append(word2[j]) + i += 1 + j += 1 + return "".join(res) +``` + +```java +public class Solution { + public String mergeAlternately(String word1, String word2) { + int n = word1.length(), m = word2.length(); + StringBuilder res = new StringBuilder(); + int i = 0, j = 0; + while (i < n || j < m) { + if (i < n) res.append(word1.charAt(i++)); + if (j < m) res.append(word2.charAt(j++)); + } + return res.toString(); + } +} +``` + +```cpp +class Solution { +public: + string mergeAlternately(string word1, string word2) { + int n = word1.size(), m = word2.size(); + string res; + int i = 0, j = 0; + while (i < n || j < m) { + if (i < n) res += word1[i++]; + if (j < m) res += word2[j++]; + } + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string} word1 + * @param {string} word2 + * @return {string} + */ + mergeAlternately(word1, word2) { + const n = word1.length, m = word2.length; + const res = []; + let i = 0, j = 0; + while (i < n || j < m) { + if (i < n) res.push(word1[i++]); + if (j < m) res.push(word2[j++]); + } + return res.join(""); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n + m)$ +* Space complexity: $O(n + m)$ for the output string. + +> Where $n$ and $m$ are the lengths of the strings $word1$ and $word2$ respectively. + +--- + +## 3. One Pointer + +::tabs-start + +```python +class Solution: + def mergeAlternately(self, word1: str, word2: str) -> str: + n, m = len(word1), len(word2) + res = [] + for i in range(max(m, n)): + if i < n: + res.append(word1[i]) + if i < m: + res.append(word2[i]) + return "".join(res) +``` + +```java +public class Solution { + public String mergeAlternately(String word1, String word2) { + int n = word1.length(), m = word2.length(); + StringBuilder res = new StringBuilder(); + for (int i = 0; i < n || i < m; i++) { + if (i < n) { + res.append(word1.charAt(i)); + } + if (i < m) { + res.append(word2.charAt(i)); + } + } + return res.toString(); + } +} +``` + +```cpp +class Solution { +public: + string mergeAlternately(string word1, string word2) { + int n = word1.size(), m = word2.size(); + string res; + for (int i = 0; i < n || i < m; i++) { + if (i < n) { + res += word1[i]; + } + if (i < m) { + res += word2[i]; + } + } + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string} word1 + * @param {string} word2 + * @return {string} + */ + mergeAlternately(word1, word2) { + const n = word1.length, m = word2.length; + const res = []; + for (let i = 0; i < m || i < n; i++) { + if (i < n) { + res.push(word1.charAt(i)); + } + if (i < m) { + res.push(word2.charAt(i)); + } + } + return res.join(""); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n + m)$ +* Space complexity: $O(n + m)$ for the output string. + +> Where $n$ and $m$ are the lengths of the strings $word1$ and $word2$ respectively. \ No newline at end of file diff --git a/articles/minimum-difference-between-highest-and-lowest-of-k-scores.md b/articles/minimum-difference-between-highest-and-lowest-of-k-scores.md new file mode 100644 index 000000000..de7c35615 --- /dev/null +++ b/articles/minimum-difference-between-highest-and-lowest-of-k-scores.md @@ -0,0 +1,74 @@ +## 1. Sorting + Sliding Window + +::tabs-start + +```python +class Solution: + def minimumDifference(self, nums: List[int], k: int) -> int: + nums.sort() + l, r = 0, k - 1 + res = float("inf") + while r < len(nums): + res = min(res, nums[r] - nums[l]) + l += 1 + r += 1 + return res +``` + +```java +public class Solution { + public int minimumDifference(int[] nums, int k) { + Arrays.sort(nums); + int l = 0, r = k - 1, res = Integer.MAX_VALUE; + while (r < nums.length) { + res = Math.min(res, nums[r] - nums[l]); + l++; + r++; + } + return res; + } +} +``` + +```cpp +class Solution { +public: + int minimumDifference(vector& nums, int k) { + sort(nums.begin(), nums.end()); + int l = 0, r = k - 1, res = INT_MAX; + while (r < nums.size()) { + res = min(res, nums[r] - nums[l]); + l++; + r++; + } + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ + minimumDifference(nums, k) { + nums.sort((a, b) => a - b); + let l = 0, r = k - 1, res = Infinity; + while (r < nums.length) { + res = Math.min(res, nums[r] - nums[l]); + l++; + r++; + } + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n \log n)$ +* Space complexity: $O(1)$ or $O(n)$ depending on the sorting algorithm. \ No newline at end of file diff --git a/articles/minimum-number-of-flips-to-make-the-binary-string-alternating.md b/articles/minimum-number-of-flips-to-make-the-binary-string-alternating.md new file mode 100644 index 000000000..b0afa6412 --- /dev/null +++ b/articles/minimum-number-of-flips-to-make-the-binary-string-alternating.md @@ -0,0 +1,701 @@ +## 1. Brute Force + +::tabs-start + +```python +class Solution: + def minFlips(self, s: str) -> int: + res = n = len(s) + alt1, alt2 = [], [] + for i in range(n): + alt1.append("0" if i % 2 == 0 else "1") + alt2.append("1" if i % 2 == 0 else "0") + + def diff(A, B): + cnt = 0 + for i in range(n): + cnt += 1 if (A[i] != B[i]) else 0 + return cnt + + for i in range(n): + newS = s[i:] + s[:i] + res = min(res, min(diff(alt1, newS), diff(alt2, newS))) + return res +``` + +```java +public class Solution { + public int minFlips(String s) { + int n = s.length(), res = n; + StringBuilder alt1 = new StringBuilder(); + StringBuilder alt2 = new StringBuilder(); + + for (int i = 0; i < n; i++) { + alt1.append(i % 2 == 0 ? '0' : '1'); + alt2.append(i % 2 == 0 ? '1' : '0'); + } + + for (int i = 0; i < n; i++) { + String newS = s.substring(i) + s.substring(0, i); + res = Math.min(res, Math.min(diff(alt1, newS), diff(alt2, newS))); + } + + return res; + } + + private int diff(StringBuilder a, String b) { + int cnt = 0; + for (int i = 0; i < a.length(); i++) { + if (a.charAt(i) != b.charAt(i)) cnt++; + } + return cnt; + } +} +``` + +```cpp +class Solution { +public: + int minFlips(string s) { + int n = s.size(), res = n; + string alt1, alt2; + + for (int i = 0; i < n; i++) { + alt1 += (i % 2 == 0) ? '0' : '1'; + alt2 += (i % 2 == 0) ? '1' : '0'; + } + + for (int i = 0; i < n; i++) { + string newS = s.substr(i) + s.substr(0, i); + res = min(res, min(diff(alt1, newS), diff(alt2, newS))); + } + + return res; + } + +private: + int diff(const string &a, const string &b) { + int cnt = 0; + for (int i = 0; i < a.size(); i++) { + if (a[i] != b[i]) cnt++; + } + return cnt; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string} s + * @return {number} + */ + minFlips(s) { + const n = s.length; + let res = n; + let alt1 = "", alt2 = ""; + + for (let i = 0; i < n; i++) { + alt1 += i % 2 === 0 ? '0' : '1'; + alt2 += i % 2 === 0 ? '1' : '0'; + } + + const diff = (a, b) => { + let cnt = 0; + for (let i = 0; i < a.length; i++) { + if (a[i] !== b[i]) cnt++; + } + return cnt; + }; + + for (let i = 0; i < n; i++) { + const newS = s.slice(i) + s.slice(0, i); + res = Math.min(res, Math.min(diff(alt1, newS), diff(alt2, newS))); + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n ^ 2)$ +* Space complexity: $O(n)$ + +--- + +## 2. Brute Force (Space Optimized) + +::tabs-start + +```python +class Solution: + def minFlips(self, s: str) -> int: + n = res = len(s) + + for i in range(n): + start_0 = 1 if s[i] != '0' else 0 + start_1 = 1 if s[i] != '1' else 0 + c = '0' + j = (i + 1) % n + while j != i: + start_1 += 1 if s[j] != c else 0 + start_0 += 1 if s[j] == c else 0 + c = '0' if c == '1' else '1' + j = (j + 1) % n + + res = min(res, min(start_1, start_0)) + return res +``` + +```java +public class Solution { + public int minFlips(String s) { + int n = s.length(); + int res = n; + + for (int i = 0; i < n; i++) { + int start0 = s.charAt(i) != '0' ? 1 : 0; + int start1 = s.charAt(i) != '1' ? 1 : 0; + char c = '0'; + int j = (i + 1) % n; + + while (j != i) { + start1 += s.charAt(j) != c ? 1 : 0; + start0 += s.charAt(j) == c ? 1 : 0; + c = c == '1' ? '0' : '1'; + j = (j + 1) % n; + } + + res = Math.min(res, Math.min(start1, start0)); + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + int minFlips(string s) { + int n = s.size(), res = n; + + for (int i = 0; i < n; i++) { + int start0 = (s[i] != '0') ? 1 : 0; + int start1 = (s[i] != '1') ? 1 : 0; + char c = '0'; + int j = (i + 1) % n; + + while (j != i) { + start1 += (s[j] != c) ? 1 : 0; + start0 += (s[j] == c) ? 1 : 0; + c = (c == '1') ? '0' : '1'; + j = (j + 1) % n; + } + + res = min(res, min(start1, start0)); + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string} s + * @return {number} + */ + minFlips(s) { + const n = s.length; + let res = n; + + for (let i = 0; i < n; i++) { + let start0 = s[i] !== '0' ? 1 : 0; + let start1 = s[i] !== '1' ? 1 : 0; + let c = '0'; + let j = (i + 1) % n; + + while (j !== i) { + start1 += s[j] !== c ? 1 : 0; + start0 += s[j] === c ? 1 : 0; + c = c === '1' ? '0' : '1'; + j = (j + 1) % n; + } + + res = Math.min(res, Math.min(start1, start0)); + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n ^ 2)$ +* Space complexity: $O(1)$ extra space. + +--- + +## 3. Sliding Window + +::tabs-start + +```python +class Solution: + def minFlips(self, s: str) -> int: + n = len(s) + s = s + s + alt1, alt2 = [], [] + for i in range(len(s)): + alt1.append("0" if i % 2 == 0 else "1") + alt2.append("1" if i % 2 == 0 else "0") + + res = len(s) + diff1, diff2 = 0, 0 + l = 0 + + for r in range(len(s)): + if s[r] != alt1[r]: + diff1 += 1 + if s[r] != alt2[r]: + diff2 += 1 + + if r - l + 1 > n: + if s[l] != alt1[l]: + diff1 -= 1 + if s[l] != alt2[l]: + diff2 -= 1 + l += 1 + + if r - l + 1 == n: + res = min(res, diff1, diff2) + + return res +``` + +```java +public class Solution { + public int minFlips(String s) { + int n = s.length(); + s = s + s; + StringBuilder alt1 = new StringBuilder(); + StringBuilder alt2 = new StringBuilder(); + + for (int i = 0; i < s.length(); i++) { + alt1.append(i % 2 == 0 ? '0' : '1'); + alt2.append(i % 2 == 0 ? '1' : '0'); + } + + int res = n, diff1 = 0, diff2 = 0, l = 0; + + for (int r = 0; r < s.length(); r++) { + if (s.charAt(r) != alt1.charAt(r)) diff1++; + if (s.charAt(r) != alt2.charAt(r)) diff2++; + + if (r - l + 1 > n) { + if (s.charAt(l) != alt1.charAt(l)) diff1--; + if (s.charAt(l) != alt2.charAt(l)) diff2--; + l++; + } + + if (r - l + 1 == n) { + res = Math.min(res, Math.min(diff1, diff2)); + } + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + int minFlips(string s) { + int n = s.size(); + s += s; + string alt1, alt2; + for (int i = 0; i < s.size(); i++) { + alt1 += (i % 2 == 0) ? '0' : '1'; + alt2 += (i % 2 == 0) ? '1' : '0'; + } + + int res = n, diff1 = 0, diff2 = 0, l = 0; + + for (int r = 0; r < s.size(); r++) { + if (s[r] != alt1[r]) diff1++; + if (s[r] != alt2[r]) diff2++; + + if (r - l + 1 > n) { + if (s[l] != alt1[l]) diff1--; + if (s[l] != alt2[l]) diff2--; + l++; + } + + if (r - l + 1 == n) { + res = min(res, min(diff1, diff2)); + } + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string} s + * @return {number} + */ + minFlips(s) { + const n = s.length; + s = s + s; + let alt1 = [], alt2 = []; + + for (let i = 0; i < s.length; i++) { + alt1.push(i % 2 === 0 ? "0" : "1"); + alt2.push(i % 2 === 0 ? "1" : "0"); + } + + let res = n, diff1 = 0, diff2 = 0, l = 0; + + for (let r = 0; r < s.length; r++) { + if (s[r] !== alt1[r]) diff1++; + if (s[r] !== alt2[r]) diff2++; + + if (r - l + 1 > n) { + if (s[l] !== alt1[l]) diff1--; + if (s[l] !== alt2[l]) diff2--; + l++; + } + + if (r - l + 1 === n) { + res = Math.min(res, diff1, diff2); + } + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(n)$ + +--- + +## 4. Sliding Window (Space Optimized) + +::tabs-start + +```python +class Solution: + def minFlips(self, s: str) -> int: + res = n = len(s) + diff1 = diff2 = l = 0 + + rstart_0 = lstart_0 = '0' + + for r in range(2 * n): + if s[r % n] != rstart_0: + diff1 += 1 + if s[r % n] == rstart_0: + diff2 += 1 + + if r - l + 1 > n: + if s[l] != lstart_0: + diff1 -= 1 + if s[l] == lstart_0: + diff2 -= 1 + l += 1 + lstart_0 = '1' if lstart_0 == '0' else '0' + + if r - l + 1 == n: + res = min(res, diff1, diff2) + + rstart_0 = '1' if rstart_0 == '0' else '0' + + return res +``` + +```java +public class Solution { + public int minFlips(String s) { + int n = s.length(); + int res = n, diff1 = 0, diff2 = 0, l = 0; + + char rstart_0 = '0', lstart_0 = '0'; + + for (int r = 0; r < 2 * n; r++) { + if (s.charAt(r % n) != rstart_0) diff1++; + if (s.charAt(r % n) == rstart_0) diff2++; + + if (r - l + 1 > n) { + if (s.charAt(l) != lstart_0) diff1--; + if (s.charAt(l) == lstart_0) diff2--; + l++; + lstart_0 = (lstart_0 == '0') ? '1' : '0'; + } + + if (r - l + 1 == n) { + res = Math.min(res, Math.min(diff1, diff2)); + } + + rstart_0 = (rstart_0 == '0') ? '1' : '0'; + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + int minFlips(string s) { + int n = s.size(); + int res = n, diff1 = 0, diff2 = 0, l = 0; + + char rstart_0 = '0', lstart_0 = '0'; + + for (int r = 0; r < 2 * n; r++) { + if (s[r % n] != rstart_0) diff1++; + if (s[r % n] == rstart_0) diff2++; + + if (r - l + 1 > n) { + if (s[l] != lstart_0) diff1--; + if (s[l] == lstart_0) diff2--; + l++; + lstart_0 = (lstart_0 == '0') ? '1' : '0'; + } + + if (r - l + 1 == n) { + res = min(res, min(diff1, diff2)); + } + + rstart_0 = (rstart_0 == '0') ? '1' : '0'; + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string} s + * @return {number} + */ + minFlips(s) { + const n = s.length; + let res = n, diff1 = 0, diff2 = 0, l = 0; + + let rstart_0 = '0', lstart_0 = '0'; + + for (let r = 0; r < 2 * n; r++) { + if (s[r % n] !== rstart_0) diff1++; + if (s[r % n] === rstart_0) diff2++; + + if (r - l + 1 > n) { + if (s[l] !== lstart_0) diff1--; + if (s[l] === lstart_0) diff2--; + l++; + lstart_0 = lstart_0 === '0' ? '1' : '0'; + } + + if (r - l + 1 === n) { + res = Math.min(res, Math.min(diff1, diff2)); + } + + rstart_0 = rstart_0 === '0' ? '1' : '0'; + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ extra space. + +--- + +## 5. Dynamic Programming + +::tabs-start + +```python +class Solution: + def minFlips(self, s: str) -> int: + start_1 = 0 + for i in range(len(s)): + if i & 1: + start_1 += s[i] == "1" + else: + start_1 += s[i] == "0" + + start_0 = len(s) - start_1 + ans = min(start_0, start_1) + if len(s) % 2 == 0: + return ans + + dp0, dp1 = start_0, start_1 + for c in s: + dp0, dp1 = dp1, dp0 + if c == "1": + dp0 += 1 + dp1 -= 1 + else: + dp0 -= 1 + dp1 += 1 + ans = min(dp0, dp1, ans) + return ans +``` + +```java +public class Solution { + public int minFlips(String s) { + int start_1 = 0; + int n = s.length(); + + for (int i = 0; i < n; i++) { + if ((i & 1) == 1) { + start_1 += s.charAt(i) == '1' ? 1 : 0; + } else { + start_1 += s.charAt(i) == '0' ? 1 : 0; + } + } + + int start_0 = n - start_1; + int ans = Math.min(start_0, start_1); + if (n % 2 == 0) { + return ans; + } + + int dp0 = start_0, dp1 = start_1; + for (char c : s.toCharArray()) { + int temp = dp0; + dp0 = dp1; + dp1 = temp; + if (c == '1') { + dp0++; + dp1--; + } else { + dp0--; + dp1++; + } + ans = Math.min(ans, Math.min(dp0, dp1)); + } + + return ans; + } +} +``` + +```cpp +class Solution { +public: + int minFlips(string s) { + int start_1 = 0, n = s.size(); + + for (int i = 0; i < n; i++) { + if (i & 1) { + start_1 += (s[i] == '1'); + } else { + start_1 += (s[i] == '0'); + } + } + + int start_0 = n - start_1; + int ans = min(start_0, start_1); + if (n % 2 == 0) { + return ans; + } + + int dp0 = start_0, dp1 = start_1; + for (char c : s) { + int temp = dp0; + dp0 = dp1; + dp1 = temp; + if (c == '1') { + dp0++; + dp1--; + } else { + dp0--; + dp1++; + } + ans = min({ans, dp0, dp1}); + } + + return ans; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string} s + * @return {number} + */ + minFlips(s) { + let start_1 = 0; + const n = s.length; + + for (let i = 0; i < n; i++) { + if (i & 1) { + start_1 += s[i] === '1' ? 1 : 0; + } else { + start_1 += s[i] === '0' ? 1 : 0; + } + } + + let start_0 = n - start_1; + let ans = Math.min(start_0, start_1); + if (n % 2 === 0) { + return ans; + } + + let dp0 = start_0, dp1 = start_1; + for (const c of s) { + [dp0, dp1] = [dp1, dp0]; + if (c === '1') { + dp0++; + dp1--; + } else { + dp0--; + dp1++; + } + ans = Math.min(ans, dp0, dp1); + } + + return ans; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ extra space. \ No newline at end of file diff --git a/articles/minimum-size-subarray-sum.md b/articles/minimum-size-subarray-sum.md new file mode 100644 index 000000000..74be08d78 --- /dev/null +++ b/articles/minimum-size-subarray-sum.md @@ -0,0 +1,332 @@ +## 1. Brute Force + +::tabs-start + +```python +class Solution: + def minSubArrayLen(self, target: int, nums: List[int]) -> int: + n = len(nums) + res = float("inf") + + for i in range(n): + curSum = 0 + for j in range(i, n): + curSum += nums[j] + if curSum >= target: + res = min(res, j - i + 1) + break + + return 0 if res == float("inf") else res +``` + +```java +public class Solution { + public int minSubArrayLen(int target, int[] nums) { + int n = nums.length; + int res = Integer.MAX_VALUE; + + for (int i = 0; i < n; i++) { + int curSum = 0, j = i; + while (j < n) { + curSum += nums[j]; + if (curSum >= target) { + res = Math.min(res, j - i + 1); + break; + } + j++; + } + } + + return res == Integer.MAX_VALUE ? 0 : res; + } +} +``` + +```cpp +class Solution { +public: + int minSubArrayLen(int target, vector& nums) { + int n = nums.size(); + int res = INT_MAX; + + for (int i = 0; i < n; i++) { + int curSum = 0, j = i; + while (j < n) { + curSum += nums[j]; + if (curSum >= target) { + res = min(res, j - i + 1); + break; + } + j++; + } + } + + return res == INT_MAX ? 0 : res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number} target + * @param {number[]} nums + * @return {number} + */ + minSubArrayLen(target, nums) { + let n = nums.length; + let res = Infinity; + + for (let i = 0; i < n; i++) { + let curSum = 0, j = i; + while (j < n) { + curSum += nums[j]; + if (curSum >= target) { + res = Math.min(res, j - i + 1); + break; + } + j++; + } + } + + return res == Infinity ? 0 : res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n ^ 2)$ +* Space complexity: $O(1)$ extra space. + +--- + +## 2. Sliding Window + +::tabs-start + +```python +class Solution: + def minSubArrayLen(self, target: int, nums: List[int]) -> int: + l, total = 0, 0 + res = float("inf") + + for r in range(len(nums)): + total += nums[r] + while total >= target: + res = min(r - l + 1, res) + total -= nums[l] + l += 1 + + return 0 if res == float("inf") else res +``` + +```java +public class Solution { + public int minSubArrayLen(int target, int[] nums) { + int l = 0, total = 0; + int res = Integer.MAX_VALUE; + + for (int r = 0; r < nums.length; r++) { + total += nums[r]; + while (total >= target) { + res = Math.min(r - l + 1, res); + total -= nums[l]; + l++; + } + } + + return res == Integer.MAX_VALUE ? 0 : res; + } +} +``` + +```cpp +class Solution { +public: + int minSubArrayLen(int target, vector& nums) { + int l = 0, total = 0, res = INT_MAX; + + for (int r = 0; r < nums.size(); r++) { + total += nums[r]; + while (total >= target) { + res = min(r - l + 1, res); + total -= nums[l]; + l++; + } + } + + return res == INT_MAX ? 0 : res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number} target + * @param {number[]} nums + * @return {number} + */ + minSubArrayLen(target, nums) { + let l = 0, total = 0; + let res = Infinity; + + for (let r = 0; r < nums.length; r++) { + total += nums[r]; + while (total >= target) { + res = Math.min(r - l + 1, res); + total -= nums[l]; + l++; + } + } + + return res === Infinity ? 0 : res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ extra space. + +--- + +## 3. Prefix Sum + Binary Search + +::tabs-start + +```python +class Solution: + def minSubArrayLen(self, target: int, nums: List[int]) -> int: + n = len(nums) + prefixSum = [0] * (n + 1) + for i in range(n): + prefixSum[i + 1] = prefixSum[i] + nums[i] + + res = n + 1 + for i in range(n): + l, r = i, n + while l < r: + mid = (l + r) // 2 + curSum = prefixSum[mid + 1] - prefixSum[i] + if curSum >= target: + r = mid + else: + l = mid + 1 + if l != n: + res = min(res, l - i + 1) + + return res % (n + 1) +``` + +```java +public class Solution { + public int minSubArrayLen(int target, int[] nums) { + int n = nums.length; + int[] prefixSum = new int[n + 1]; + for (int i = 0; i < n; i++) { + prefixSum[i + 1] = prefixSum[i] + nums[i]; + } + + int res = n + 1; + for (int i = 0; i < n; i++) { + int l = i, r = n; + while (l < r) { + int mid = (l + r) / 2; + int curSum = prefixSum[mid + 1] - prefixSum[i]; + if (curSum >= target) { + r = mid; + } else { + l = mid + 1; + } + } + if (l != n) { + res = Math.min(res, l - i + 1); + } + } + + return res % (n + 1); + } +} +``` + +```cpp +class Solution { +public: + int minSubArrayLen(int target, vector& nums) { + int n = nums.size(); + vector prefixSum(n + 1, 0); + for (int i = 0; i < n; i++) { + prefixSum[i + 1] = prefixSum[i] + nums[i]; + } + + int res = n + 1; + for (int i = 0; i < n; i++) { + int l = i, r = n; + while (l < r) { + int mid = (l + r) / 2; + int curSum = prefixSum[mid + 1] - prefixSum[i]; + if (curSum >= target) { + r = mid; + } else { + l = mid + 1; + } + } + if (l != n) { + res = min(res, l - i + 1); + } + } + + return res % (n + 1); + } +}; +``` + +```javascript +class Solution { + /** + * @param {number} target + * @param {number[]} nums + * @return {number} + */ + minSubArrayLen(target, nums) { + const n = nums.length; + const prefixSum = new Array(n + 1).fill(0); + for (let i = 0; i < n; i++) { + prefixSum[i + 1] = prefixSum[i] + nums[i]; + } + + let res = n + 1; + for (let i = 0; i < n; i++) { + let l = i, r = n; + while (l < r) { + const mid = Math.floor((l + r) / 2); + const curSum = prefixSum[mid + 1] - prefixSum[i]; + if (curSum >= target) { + r = mid; + } else { + l = mid + 1; + } + } + if (l !== n) { + res = Math.min(res, l - i + 1); + } + } + + return res % (n + 1); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n \log n)$ +* Space complexity: $O(n)$ \ No newline at end of file diff --git a/articles/move-zeroes.md b/articles/move-zeroes.md new file mode 100644 index 000000000..46a78c170 --- /dev/null +++ b/articles/move-zeroes.md @@ -0,0 +1,253 @@ +## 1. Extra Space + +::tabs-start + +```python +class Solution: + def moveZeroes(self, nums: List[int]) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + tmp = [] + for num in nums: + if num != 0: + tmp.append(num) + + for i in range(len(nums)): + if i < len(tmp): + nums[i] = tmp[i] + else: + nums[i] = 0 +``` + +```java +public class Solution { + public void moveZeroes(int[] nums) { + List tmp = new ArrayList<>(); + for (int num : nums) { + if (num != 0) { + tmp.add(num); + } + } + + for (int i = 0; i < nums.length; i++) { + if (i < tmp.size()) { + nums[i] = tmp.get(i); + } else { + nums[i] = 0; + } + } + } +} +``` + +```cpp +class Solution { +public: + void moveZeroes(vector& nums) { + vector tmp; + for (int num : nums) { + if (num != 0) { + tmp.push_back(num); + } + } + + for (int i = 0; i < nums.size(); ++i) { + if (i < tmp.size()) { + nums[i] = tmp[i]; + } else { + nums[i] = 0; + } + } + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @return {void} Do not return anything, modify nums in-place instead. + */ + moveZeroes(nums) { + let tmp = []; + for (let num of nums) { + if (num !== 0) { + tmp.push(num); + } + } + + for (let i = 0; i < nums.length; i++) { + if (i < tmp.length) { + nums[i] = tmp[i]; + } else { + nums[i] = 0; + } + } + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(n)$ + +--- + +## 2. Two Pointers (Two Pass) +::tabs-start + +```python +class Solution: + def moveZeroes(self, nums: List[int]) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + l = 0 + for r in range(len(nums)): + if nums[r] != 0: + nums[l] = nums[r] + l += 1 + + while l < len(nums): + nums[l] = 0 + l += 1 +``` + +```java +public class Solution { + public void moveZeroes(int[] nums) { + int l = 0; + for (int r = 0; r < nums.length; r++) { + if (nums[r] != 0) { + nums[l++] = nums[r]; + } + } + + while (l < nums.length) { + nums[l++] = 0; + } + } +} +``` + +```cpp +class Solution { +public: + void moveZeroes(vector& nums) { + int l = 0; + for (int r = 0; r < nums.size(); r++) { + if (nums[r] != 0) { + nums[l++] = nums[r]; + } + } + + while (l < nums.size()) { + nums[l++] = 0; + } + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @return {void} Do not return anything, modify nums in-place instead. + */ + moveZeroes(nums) { + let l = 0; + for (let r = 0; r < nums.length; r++) { + if (nums[r] != 0) { + nums[l++] = nums[r]; + } + } + + while (l < nums.length) { + nums[l++] = 0; + } + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ + +--- + +## 3. Two Pointers (One Pass) + +::tabs-start + +```python +class Solution: + def moveZeroes(self, nums: List[int]) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + l = 0 + for r in range(len(nums)): + if nums[r]: + nums[l], nums[r] = nums[r], nums[l] + l += 1 +``` + +```java +public class Solution { + public void moveZeroes(int[] nums) { + int l = 0; + for (int r = 0; r < nums.length; r++) { + if (nums[r] != 0) { + int temp = nums[l]; + nums[l] = nums[r]; + nums[r] = temp; + l++; + } + } + } +} +``` + +```cpp +class Solution { +public: + void moveZeroes(vector& nums) { + for (int l = 0, r = 0; r < nums.size(); r++) { + if (nums[r]) { + swap(nums[l++], nums[r]); + } + } + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @return {void} Do not return anything, modify nums in-place instead. + */ + moveZeroes(nums) { + for (let l = 0, r = 0; r < nums.length; r++) { + if (nums[r] !== 0) { + [nums[l], nums[r]] = [nums[r], nums[l]]; + l++; + } + } + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ \ No newline at end of file diff --git a/articles/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.md b/articles/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.md new file mode 100644 index 000000000..f99cc6f13 --- /dev/null +++ b/articles/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.md @@ -0,0 +1,398 @@ +## 1. Brute Force + +::tabs-start + +```python +class Solution: + def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: + res = 0 + l = 0 + + for r in range(k - 1, len(arr)): + sum_ = 0 + for i in range(l, r + 1): + sum_ += arr[i] + + if sum_ / k >= threshold: + res += 1 + l += 1 + + return res +``` + +```java +public class Solution { + public int numOfSubarrays(int[] arr, int k, int threshold) { + int res = 0, l = 0; + + for (int r = k - 1; r < arr.length; r++) { + int sum = 0; + for (int i = l; i <= r; i++) { + sum += arr[i]; + } + if (sum / k >= threshold) { + res++; + } + l++; + } + return res; + } +} +``` + +```cpp +class Solution { +public: + int numOfSubarrays(vector& arr, int k, int threshold) { + int res = 0, l = 0; + + for (int r = k - 1; r < arr.size(); r++) { + int sum = 0; + for (int i = l; i <= r; i++) { + sum += arr[i]; + } + if (sum / k >= threshold) { + res++; + } + l++; + } + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} arr + * @param {number} k + * @param {number} threshold + * @return {number} + */ + numOfSubarrays(arr, k, threshold) { + let res = 0, l = 0; + + for (let r = k - 1; r < arr.length; r++) { + let sum = 0; + for (let i = l; i <= r; i++) { + sum += arr[i]; + } + if (sum / k >= threshold) { + res++; + } + l++; + } + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n * k)$ +* Space complexity: $O(1)$ + +> Where $n$ is the size of the array $arr$ and $k$ is the size of the sub-array. + +--- + +## 2. Prefix Sum + +::tabs-start + +```python +class Solution: + def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: + prefix_sum = [0] * (len(arr) + 1) + for i in range(len(arr)): + prefix_sum[i + 1] += prefix_sum[i] + arr[i] + + res = l = 0 + for r in range(k - 1, len(arr)): + sum_ = prefix_sum[r + 1] - prefix_sum[l] + if sum_ / k >= threshold: + res += 1 + l += 1 + + return res +``` + +```java +public class Solution { + public int numOfSubarrays(int[] arr, int k, int threshold) { + int[] prefixSum = new int[arr.length + 1]; + for (int i = 0; i < arr.length; i++) { + prefixSum[i + 1] += prefixSum[i] + arr[i]; + } + + int res = 0, l = 0; + for (int r = k - 1; r < arr.length; r++) { + int sum = prefixSum[r + 1] - prefixSum[l]; + if (sum / k >= threshold) { + res++; + } + l++; + } + return res; + } +} +``` + +```cpp +class Solution { +public: + int numOfSubarrays(vector& arr, int k, int threshold) { + vector prefixSum(arr.size() + 1); + for (int i = 0; i < arr.size(); i++) { + prefixSum[i + 1] += prefixSum[i] + arr[i]; + } + + int res = 0, l = 0; + for (int r = k - 1; r < arr.size(); r++) { + int sum = prefixSum[r + 1] - prefixSum[l]; + if (sum / k >= threshold) { + res++; + } + l++; + } + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} arr + * @param {number} k + * @param {number} threshold + * @return {number} + */ + numOfSubarrays(arr, k, threshold) { + const prefixSum = new Int32Array(arr.length + 1); + for (let i = 0; i < arr.length; i++) { + prefixSum[i + 1] += prefixSum[i] + arr[i]; + } + + let res = 0, l = 0; + for (let r = k - 1; r < arr.length; r++) { + const sum = prefixSum[r + 1] - prefixSum[l]; + if (sum / k >= threshold) { + res++; + } + l++; + } + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(n)$ + +> Where $n$ is the size of the array $arr$ and $k$ is the size of the sub-array. + +--- + +## 3. Sliding Window - I + +::tabs-start + +```python +class Solution: + def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: + res = 0 + curSum = sum(arr[:k - 1]) + + for L in range(len(arr) - k + 1): + curSum += arr[L + k - 1] + if (curSum / k) >= threshold: + res += 1 + curSum -= arr[L] + return res +``` + +```java +public class Solution { + public int numOfSubarrays(int[] arr, int k, int threshold) { + int res = 0; + int curSum = 0; + + for (int i = 0; i < k - 1; i++) { + curSum += arr[i]; + } + + for (int L = 0; L <= arr.length - k; L++) { + curSum += arr[L + k - 1]; + if ((curSum / k) >= threshold) { + res++; + } + curSum -= arr[L]; + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + int numOfSubarrays(vector& arr, int k, int threshold) { + int res = 0, curSum = 0; + + for (int i = 0; i < k - 1; i++) { + curSum += arr[i]; + } + + for (int L = 0; L <= arr.size() - k; L++) { + curSum += arr[L + k - 1]; + if ((curSum / k) >= threshold) { + res++; + } + curSum -= arr[L]; + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} arr + * @param {number} k + * @param {number} threshold + * @return {number} + */ + numOfSubarrays(arr, k, threshold) { + let res = 0; + let curSum = 0; + + for (let i = 0; i < k - 1; i++) { + curSum += arr[i]; + } + + for (let L = 0; L <= arr.length - k; L++) { + curSum += arr[L + k - 1]; + if ((curSum / k) >= threshold) { + res++; + } + curSum -= arr[L]; + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ extra space. + +> Where $n$ is the size of the array $arr$ and $k$ is the size of the sub-array. + +--- + +## 4. Sliding Window - II + +::tabs-start + +```python +class Solution: + def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: + threshold *= k + res = curSum = 0 + for R in range(len(arr)): + curSum += arr[R] + if R >= k - 1: + res += curSum >= threshold + curSum -= arr[R - k + 1] + return res +``` + +```java +public class Solution { + public int numOfSubarrays(int[] arr, int k, int threshold) { + threshold *= k; + int res = 0, curSum = 0; + + for (int R = 0; R < arr.length; R++) { + curSum += arr[R]; + if (R >= k - 1) { + if (curSum >= threshold) { + res++; + } + curSum -= arr[R - k + 1]; + } + } + return res; + } +} +``` + +```cpp +class Solution { +public: + int numOfSubarrays(vector& arr, int k, int threshold) { + threshold *= k; + int res = 0, curSum = 0; + + for (int R = 0; R < arr.size(); R++) { + curSum += arr[R]; + if (R >= k - 1) { + if (curSum >= threshold) { + res++; + } + curSum -= arr[R - k + 1]; + } + } + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} arr + * @param {number} k + * @param {number} threshold + * @return {number} + */ + numOfSubarrays(arr, k, threshold) { + threshold *= k; + let res = 0, curSum = 0; + + for (let R = 0; R < arr.length; R++) { + curSum += arr[R]; + if (R >= k - 1) { + if (curSum >= threshold) { + res++; + } + curSum -= arr[R - k + 1]; + } + } + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ extra space. + +> Where $n$ is the size of the array $arr$ and $k$ is the size of the sub-array. \ No newline at end of file diff --git a/articles/remove-duplicates-from-sorted-array.md b/articles/remove-duplicates-from-sorted-array.md new file mode 100644 index 000000000..57bb96cdc --- /dev/null +++ b/articles/remove-duplicates-from-sorted-array.md @@ -0,0 +1,214 @@ +## 1. Sorted Set + +::tabs-start + +```python +class Solution: + def removeDuplicates(self, nums: list[int]) -> int: + unique = sorted(set(nums)) + nums[:len(unique)] = unique + return len(unique) +``` + +```java +public class Solution { + public int removeDuplicates(int[] nums) { + TreeSet unique = new TreeSet<>(); + for (int num : nums) { + unique.add(num); + } + int i = 0; + for (int num : unique) { + nums[i++] = num; + } + return unique.size(); + } +} +``` + +```cpp +class Solution { +public: + int removeDuplicates(vector& nums) { + set unique(nums.begin(), nums.end()); + int i = 0; + for (int num : unique) { + nums[i++] = num; + } + return unique.size(); + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @return {number} + */ + removeDuplicates(nums) { + const unique = Array.from(new Set(nums)).sort((a, b) => a - b); + for (let i = 0; i < unique.length; i++) { + nums[i] = unique[i]; + } + return unique.length; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n \log n)$ +* Space complexity: $O(n)$ + +--- + +## 2. Two Pointers - I + +::tabs-start + +```python +class Solution: + def removeDuplicates(self, nums: list[int]) -> int: + n = len(nums) + l = r = 0 + while r < n: + nums[l] = nums[r] + while r < n and nums[r] == nums[l]: + r += 1 + l += 1 + return l +``` + +```java +public class Solution { + public int removeDuplicates(int[] nums) { + int n = nums.length, l = 0, r = 0; + while (r < n) { + nums[l] = nums[r]; + while (r < n && nums[r] == nums[l]) { + r++; + } + l++; + } + return l; + } +} +``` + +```cpp +class Solution { +public: + int removeDuplicates(vector& nums) { + int n = nums.size(), l = 0, r = 0; + while (r < n) { + nums[l] = nums[r]; + while (r < n && nums[r] == nums[l]) { + r++; + } + l++; + } + return l; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @return {number} + */ + removeDuplicates(nums) { + let n = nums.length, l = 0, r = 0; + while (r < n) { + nums[l] = nums[r]; + while (r < n && nums[r] === nums[l]) { + r++; + } + l++; + } + return l; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ + +--- + +## 3. Two Pointers - II + +::tabs-start + +```python +class Solution: + def removeDuplicates(self, nums: list[int]) -> int: + l = 1 + for r in range(1, len(nums)): + if nums[r] != nums[r - 1]: + nums[l] = nums[r] + l += 1 + return l +``` + +```java +public class Solution { + public int removeDuplicates(int[] nums) { + int l = 1; + for (int r = 1; r < nums.length; r++) { + if (nums[r] != nums[r - 1]) { + nums[l++] = nums[r]; + } + } + return l; + } +} +``` + +```cpp +class Solution { +public: + int removeDuplicates(vector& nums) { + int l = 1; + for (int r = 1; r < nums.size(); r++) { + if (nums[r] != nums[r - 1]) { + nums[l++] = nums[r]; + } + } + return l; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @return {number} + */ + removeDuplicates(nums) { + let l = 1; + for (let r = 1; r < nums.length; r++) { + if (nums[r] !== nums[r - 1]) { + nums[l++] = nums[r]; + } + } + return l; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ \ No newline at end of file diff --git a/articles/reverse-string.md b/articles/reverse-string.md new file mode 100644 index 000000000..ac60401c5 --- /dev/null +++ b/articles/reverse-string.md @@ -0,0 +1,357 @@ +## 1. Array + +::tabs-start + +```python +class Solution: + def reverseString(self, s: List[str]) -> None: + """ + Do not return anything, modify s in-place instead. + """ + tmp = [] + for i in range(len(s) - 1, -1, -1): + tmp.append(s[i]) + for i in range(len(s)): + s[i] = tmp[i] +``` + +```java +public class Solution { + public void reverseString(char[] s) { + char[] tmp = new char[s.length]; + for (int i = s.length - 1, j = 0; i >= 0; i--, j++) { + tmp[j] = s[i]; + } + for (int i = 0; i < s.length; i++) { + s[i] = tmp[i]; + } + } +} +``` + +```cpp +class Solution { +public: + void reverseString(vector& s) { + vector tmp; + for (int i = s.size() - 1; i >= 0; i--) { + tmp.push_back(s[i]); + } + for (int i = 0; i < s.size(); i++) { + s[i] = tmp[i]; + } + } +}; +``` + +```javascript +class Solution { + /** + * @param {character[]} s + * @return {void} Do not return anything, modify s in-place instead. + */ + reverseString(s) { + const tmp = []; + for (let i = s.length - 1; i >= 0; i--) { + tmp.push(s[i]); + } + for (let i = 0; i < s.length; i++) { + s[i] = tmp[i]; + } + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(n)$ + +--- + +## 2. Recursion + +::tabs-start + +```python +class Solution: + def reverseString(self, s: List[str]) -> None: + """ + Do not return anything, modify s in-place instead. + """ + def reverse(l, r): + if l < r: + reverse(l + 1, r - 1) + s[l], s[r] = s[r], s[l] + + reverse(0, len(s) - 1) +``` + +```java +public class Solution { + public void reverseString(char[] s) { + reverse(s, 0, s.length - 1); + } + + private void reverse(char[] s, int l, int r) { + if (l < r) { + reverse(s, l + 1, r - 1); + char temp = s[l]; + s[l] = s[r]; + s[r] = temp; + } + } +} +``` + +```cpp +class Solution { +public: + void reverseString(vector& s) { + reverse(s, 0, s.size() - 1); + } + +private: + void reverse(vector& s, int l, int r) { + if (l < r) { + reverse(s, l + 1, r - 1); + swap(s[l], s[r]); + } + } +}; +``` + +```javascript +class Solution { + /** + * @param {character[]} s + * @return {void} Do not return anything, modify s in-place instead. + */ + reverseString(s) { + const reverse = (l, r) => { + if (l < r) { + reverse(l + 1, r - 1); + [s[l], s[r]] = [s[r], s[l]]; + } + }; + reverse(0, s.length - 1); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(n)$ for recursion stack. + +--- + +## 3. Stack + +::tabs-start + +```python +class Solution: + def reverseString(self, s: List[str]) -> None: + """ + Do not return anything, modify s in-place instead. + """ + stack = [] + for c in s: + stack.append(c) + i = 0 + while stack: + s[i] = stack.pop() + i += 1 +``` + +```java +public class Solution { + public void reverseString(char[] s) { + Stack stack = new Stack<>(); + for (char c : s) { + stack.push(c); + } + int i = 0; + while (!stack.isEmpty()) { + s[i++] = stack.pop(); + } + } +} +``` + +```cpp +class Solution { +public: + void reverseString(vector& s) { + stack stk; + for (char& c : s) { + stk.push(c); + } + int i = 0; + while (!stk.empty()) { + s[i++] = stk.top(); + stk.pop(); + } + } +}; +``` + +```javascript +class Solution { + /** + * @param {character[]} s + * @return {void} Do not return anything, modify s in-place instead. + */ + reverseString(s) { + const stack = []; + for (const c of s) { + stack.push(c); + } + let i = 0; + while (stack.length) { + s[i++] = stack.pop(); + } + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(n)$ + +--- + +## 4. Built-In Function + +::tabs-start + +```python +class Solution: + def reverseString(self, s: List[str]) -> None: + """ + Do not return anything, modify s in-place instead. + """ + s.reverse() +``` + +```java +public class Solution { + public void reverseString(char[] s) { + List list = new ArrayList<>(); + for (char c : s) { + list.add(c); + } + Collections.reverse(list); + + for (int i = 0; i < s.length; i++) { + s[i] = list.get(i); + } + } +} +``` + +```cpp +class Solution { +public: + void reverseString(vector& s) { + reverse(s.begin(), s.end()); + } +}; +``` + +```javascript +class Solution { + /** + * @param {character[]} s + * @return {void} Do not return anything, modify s in-place instead. + */ + reverseString(s) { + s.reverse(); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ + +--- + +## 5. Two Pointers + +::tabs-start + +```python +class Solution: + def reverseString(self, s: List[str]) -> None: + """ + Do not return anything, modify s in-place instead. + """ + l, r = 0, len(s) - 1 + while l < r: + s[l], s[r] = s[r], s[l] + l += 1 + r -= 1 +``` + +```java +public class Solution { + public void reverseString(char[] s) { + int l = 0, r = s.length - 1; + while (l < r) { + char temp = s[l]; + s[l] = s[r]; + s[r] = temp; + l++; + r--; + } + } +} +``` + +```cpp +class Solution { +public: + void reverseString(vector& s) { + int l = 0, r = s.size() - 1; + while (l < r) { + swap(s[l++], s[r--]); + } + } +}; +``` + +```javascript +class Solution { + /** + * @param {character[]} s + * @return {void} Do not return anything, modify s in-place instead. + */ + reverseString(s) { + let l = 0, r = s.length - 1; + while (l < r) { + [s[l], s[r]] = [s[r], s[l]]; + l++; + r--; + } + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ \ No newline at end of file diff --git a/articles/reverse-words-in-a-string-iii.md b/articles/reverse-words-in-a-string-iii.md new file mode 100644 index 000000000..b9fb3b3cb --- /dev/null +++ b/articles/reverse-words-in-a-string-iii.md @@ -0,0 +1,387 @@ +## 1. Convert To String Array + +::tabs-start + +```python +class Solution: + def reverseWords(self, s: str) -> str: + return ' '.join([w[::-1] for w in s.split(' ')]) +``` + +```java +public class Solution { + public String reverseWords(String s) { + String[] words = s.split(" "); + for (int i = 0; i < words.length; i++) { + words[i] = new StringBuilder(words[i]).reverse().toString(); + } + return String.join(" ", words); + } +} +``` + +```cpp +class Solution { +public: + string reverseWords(string s) { + stringstream ss(s); + string word, res; + bool first = true; + + while (ss >> word) { + reverse(word.begin(), word.end()); + if (first) { + res += word; + first = false; + } else { + res += " " + word; + } + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string} s + * @return {string} + */ + reverseWords(s) { + return s.split(' ').map(w => w.split('').reverse().join('')).join(' '); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(n)$ + +--- + +## 2. String Manipulation + +::tabs-start + +```python +class Solution: + def reverseWords(self, s: str) -> str: + tmp_str = "" + res = "" + + for r in range(len(s) + 1): + if r == len(s) or s[r] == ' ': + res += tmp_str + tmp_str = "" + if r != len(s): + res += " " + else: + tmp_str = s[r] + tmp_str + return res +``` + +```java +public class Solution { + public String reverseWords(String s) { + String tmpStr = ""; + StringBuilder res = new StringBuilder(); + + for (int r = 0; r <= s.length(); r++) { + if (r == s.length() || s.charAt(r) == ' ') { + res.append(tmpStr); + tmpStr = ""; + if (r != s.length()) { + res.append(" "); + } + } else { + tmpStr = s.charAt(r) + tmpStr; + } + } + return res.toString(); + } +} +``` + +```cpp +class Solution { +public: + string reverseWords(string s) { + string tmpStr = ""; + string res = ""; + + for (int r = 0; r <= s.size(); r++) { + if (r == s.size() || s[r] == ' ') { + res += tmpStr; + tmpStr = ""; + if (r != s.size()) { + res += " "; + } + } else { + tmpStr = s[r] + tmpStr; + } + } + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string} s + * @return {string} + */ + reverseWords(s) { + let tmpStr = ""; + let res = ""; + + for (let r = 0; r <= s.length; r++) { + if (r === s.length || s[r] === ' ') { + res += tmpStr; + tmpStr = ""; + if (r !== s.length) { + res += " "; + } + } else { + tmpStr = s[r] + tmpStr; + } + } + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n ^ 2)$ +* Space complexity: $O(n)$ + +--- + +## 3. Two Pointers - I + +::tabs-start + +```python +class Solution: + def reverseWords(self, s: str) -> str: + s = list(s) + l = 0 + for r in range(len(s)): + if s[r] == " " or r == len(s) - 1: + temp_l, temp_r = l, r - 1 if s[r] == " " else r + while temp_l < temp_r: + s[temp_l], s[temp_r] = s[temp_r], s[temp_l] + temp_l += 1 + temp_r -= 1 + l = r + 1 + return "".join(s) +``` + +```java +public class Solution { + public String reverseWords(String s) { + char[] chars = s.toCharArray(); + int l = 0; + for (int r = 0; r < chars.length; r++) { + if (chars[r] == ' ' || r == chars.length - 1) { + int tempL = l, tempR = (chars[r] == ' ') ? r - 1 : r; + while (tempL < tempR) { + char temp = chars[tempL]; + chars[tempL] = chars[tempR]; + chars[tempR] = temp; + tempL++; + tempR--; + } + l = r + 1; + } + } + return new String(chars); + } +} +``` + +```cpp +class Solution { +public: + string reverseWords(string s) { + int l = 0; + for (int r = 0; r < s.size(); r++) { + if (r == s.size() - 1 || s[r] == ' ') { + int tempL = l, tempR = s[r] == ' ' ? r - 1 : r; + while (tempL < tempR) { + swap(s[tempL], s[tempR]); + tempL++; + tempR--; + } + l = r + 1; + } + } + return s; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string} s + * @return {string} + */ + reverseWords(s) { + let chars = s.split(''); + let l = 0; + for (let r = 0; r <= chars.length; r++) { + if (r === chars.length || chars[r] === ' ') { + let tempL = l, tempR = r - 1; + while (tempL < tempR) { + [chars[tempL], chars[tempR]] = [chars[tempR], chars[tempL]]; + tempL++; + tempR--; + } + l = r + 1; + } + } + return chars.join(''); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(n)$ + +--- + +## 4. Two Pointers - II + +::tabs-start + +```python +class Solution: + def reverseWords(self, s: str) -> str: + def reverse(i, j): + while i < j: + s[i], s[j] = s[j], s[i] + i += 1 + j -= 1 + + s = list(s) + i = 0 + while i < len(s): + if s[i] != ' ': + j = i + while j < len(s) and s[j] != ' ': + j += 1 + reverse(i, j - 1) + i = j + 1 + return ''.join(s) +``` + +```java +public class Solution { + public String reverseWords(String s) { + char[] arr = s.toCharArray(); + int n = arr.length; + + for (int i = 0; i < n; i++) { + if (arr[i] != ' ') { + int j = i; + while (j < n && arr[j] != ' ') { + j++; + } + reverse(arr, i, j - 1); + i = j; + } + } + return new String(arr); + } + + private void reverse(char[] arr, int i, int j) { + while (i < j) { + char temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + i++; + j--; + } + } +} +``` + +```cpp +class Solution { +public: + string reverseWords(string s) { + int n = s.size(); + for (int i = 0; i < n; i++) { + if (s[i] != ' ') { + int j = i; + while (j < n && s[j] != ' ') { + j++; + } + reverse(s, i, j - 1); + i = j; + } + } + return s; + } + +private: + void reverse(string& s, int i, int j) { + while (i < j) { + swap(s[i], s[j]); + i++; + j--; + } + } +}; +``` + +```javascript +class Solution { + /** + * @param {string} s + * @return {string} + */ + reverseWords(s) { + const arr = s.split(''); + const reverse = (i, j) => { + while (i < j) { + [arr[i], arr[j]] = [arr[j], arr[i]]; + i++; + j--; + } + }; + + for (let i = 0; i < arr.length; i++) { + if (arr[i] !== ' ') { + let j = i; + while (j < arr.length && arr[j] !== ' ') { + j++; + } + reverse(i, j - 1); + i = j; + } + } + return arr.join(''); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(n)$ \ No newline at end of file diff --git a/articles/search-insert-position.md b/articles/search-insert-position.md new file mode 100644 index 000000000..225a7e0a1 --- /dev/null +++ b/articles/search-insert-position.md @@ -0,0 +1,396 @@ +## 1. Linear Search + +::tabs-start + +```python +class Solution: + def searchInsert(self, nums: List[int], target: int) -> int: + for i in range(len(nums)): + if nums[i] >= target: + return i + return len(nums) +``` + +```java +public class Solution { + public int searchInsert(int[] nums, int target) { + for (int i = 0; i < nums.length; i++) { + if (nums[i] >= target) { + return i; + } + } + return nums.length; + } +} +``` + +```cpp +class Solution { +public: + int searchInsert(vector& nums, int target) { + for (int i = 0; i < nums.size(); i++) { + if (nums[i] >= target) { + return i; + } + } + return nums.size(); + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ + searchInsert(nums, target) { + for (let i = 0; i < nums.length; i++) { + if (nums[i] >= target) { + return i; + } + } + return nums.length; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ extra space. + +--- + +## 2. Binary Search - I + +::tabs-start + +```python +class Solution: + def searchInsert(self, nums: List[int], target: int) -> int: + res = len(nums) + l, r = 0, len(nums) - 1 + while l <= r: + mid = (l + r) // 2 + if nums[mid] == target: + return mid + if nums[mid] > target: + res = mid + r = mid - 1 + else: + l = mid + 1 + return res +``` + +```java +public class Solution { + public int searchInsert(int[] nums, int target) { + int res = nums.length; + int l = 0, r = nums.length - 1; + while (l <= r) { + int mid = (l + r) / 2; + if (nums[mid] == target) { + return mid; + } + if (nums[mid] > target) { + res = mid; + r = mid - 1; + } else { + l = mid + 1; + } + } + return res; + } +} +``` + +```cpp +class Solution { +public: + int searchInsert(vector& nums, int target) { + int res = nums.size(); + int l = 0, r = nums.size() - 1; + while (l <= r) { + int mid = (l + r) / 2; + if (nums[mid] == target) { + return mid; + } + if (nums[mid] > target) { + res = mid; + r = mid - 1; + } else { + l = mid + 1; + } + } + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ + searchInsert(nums, target) { + let res = nums.length; + let l = 0, r = nums.length - 1; + while (l <= r) { + const mid = Math.floor((l + r) / 2); + if (nums[mid] === target) { + return mid; + } + if (nums[mid] > target) { + res = mid; + r = mid - 1; + } else { + l = mid + 1; + } + } + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(\log n)$ +* Space complexity: $O(1)$ extra space. + +--- + +## 3. Binary Search - II + +::tabs-start + +```python +class Solution: + def searchInsert(self, nums: List[int], target: int) -> int: + l, r = 0, len(nums) - 1 + while l <= r: + mid = (l + r) // 2 + if nums[mid] == target: + return mid + if nums[mid] > target: + r = mid - 1 + else: + l = mid + 1 + return l +``` + +```java +public class Solution { + public int searchInsert(int[] nums, int target) { + int l = 0, r = nums.length - 1; + while (l <= r) { + int mid = (l + r) / 2; + if (nums[mid] == target) { + return mid; + } + if (nums[mid] > target) { + r = mid - 1; + } else { + l = mid + 1; + } + } + return l; + } +} +``` + +```cpp +class Solution { +public: + int searchInsert(vector& nums, int target) { + int l = 0, r = nums.size() - 1; + while (l <= r) { + int mid = (l + r) / 2; + if (nums[mid] == target) { + return mid; + } + if (nums[mid] > target) { + r = mid - 1; + } else { + l = mid + 1; + } + } + return l; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ + searchInsert(nums, target) { + let l = 0, r = nums.length - 1; + while (l <= r) { + const mid = Math.floor((l + r) / 2); + if (nums[mid] === target) { + return mid; + } + if (nums[mid] > target) { + r = mid - 1; + } else { + l = mid + 1; + } + } + return l; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(\log n)$ +* Space complexity: $O(1)$ extra space. + +--- + +## 4. Binary Search (Lower Bound) + +::tabs-start + +```python +class Solution: + def searchInsert(self, nums: List[int], target: int) -> int: + l, r = 0, len(nums) + while l < r: + m = l + ((r - l) // 2) + if nums[m] >= target: + r = m + elif nums[m] < target: + l = m + 1 + return l +``` + +```java +public class Solution { + public int searchInsert(int[] nums, int target) { + int l = 0, r = nums.length; + while (l < r) { + int m = l + (r - l) / 2; + if (nums[m] >= target) { + r = m; + } else { + l = m + 1; + } + } + return l; + } +} +``` + +```cpp +class Solution { +public: + int searchInsert(vector& nums, int target) { + int l = 0, r = nums.size(); + while (l < r) { + int m = l + (r - l) / 2; + if (nums[m] >= target) { + r = m; + } else { + l = m + 1; + } + } + return l; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ + searchInsert(nums, target) { + let l = 0, r = nums.length; + while (l < r) { + let m = l + Math.floor((r - l) / 2); + if (nums[m] >= target) { + r = m; + } else { + l = m + 1; + } + } + return l; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(\log n)$ +* Space complexity: $O(1)$ + +--- + +## 5. Built-In Binary Search Function + +::tabs-start + +```python +import bisect +class Solution: + def searchInsert(self, nums: List[int], target: int) -> int: + return bisect.bisect_left(nums, target) +``` + +```java +public class Solution { + public int searchInsert(int[] nums, int target) { + int index = Arrays.binarySearch(nums, target); + return index >= 0 ? index : -index - 1; + } +} +``` + +```cpp +class Solution { +public: + int searchInsert(vector& nums, int target) { + return lower_bound(nums.begin(), nums.end(), target) - nums.begin(); + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ + searchInsert(nums, target) { + // There is no built in Binary Search function for JS. + let index = nums.findIndex(x => x >= target); + return index !== -1 ? index : nums.length + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(\log n)$ +* Space complexity: $O(1)$ \ No newline at end of file diff --git a/articles/sort-array-by-parity.md b/articles/sort-array-by-parity.md new file mode 100644 index 000000000..b282d0cea --- /dev/null +++ b/articles/sort-array-by-parity.md @@ -0,0 +1,325 @@ +## 1. Sorting + +::tabs-start + +```python +class Solution: + def sortArrayByParity(self, nums: List[int]) -> List[int]: + nums.sort(key = lambda x: x & 1) + return nums +``` + +```java +public class Solution { + public int[] sortArrayByParity(int[] nums) { + Integer[] A = Arrays.stream(nums).boxed().toArray(Integer[]::new); + Arrays.sort(A, (a, b) -> (a & 1) - (b & 1)); + return Arrays.stream(A).mapToInt(Integer::intValue).toArray(); + } +} +``` + +```cpp +class Solution { +public: + vector sortArrayByParity(vector& nums) { + sort(nums.begin(), nums.end(), [&](int& a, int& b) { + return (a & 1) < (b & 1); + }); + return nums; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @return {number[]} + */ + sortArrayByParity(nums) { + return nums.sort((a, b) => (a & 1) - (b & 1)); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n \log n)$ +* Space complexity: $O(1)$ or $O(n)$ depending on the sorting algorithm. + +--- + +## 2. Array + +::tabs-start + +```python +class Solution: + def sortArrayByParity(self, nums: List[int]) -> List[int]: + even, odd = [], [] + for num in nums: + if num & 1: + odd.append(num) + else: + even.append(num) + + idx = 0 + for e in even: + nums[idx] = e + idx += 1 + for o in odd: + nums[idx] = o + idx += 1 + return nums +``` + +```java +public class Solution { + public int[] sortArrayByParity(int[] nums) { + List even = new ArrayList<>(); + List odd = new ArrayList<>(); + + for (int num : nums) { + if ((num & 1) == 1) { + odd.add(num); + } else { + even.add(num); + } + } + + int idx = 0; + for (int e : even) { + nums[idx++] = e; + } + for (int o : odd) { + nums[idx++] = o; + } + + return nums; + } +} +``` + +```cpp +class Solution { +public: + vector sortArrayByParity(vector& nums) { + vector even, odd; + + for (int& num : nums) { + if (num & 1) { + odd.push_back(num); + } else { + even.push_back(num); + } + } + + int idx = 0; + for (int& e : even) { + nums[idx++] = e; + } + for (int& o : odd) { + nums[idx++] = o; + } + + return nums; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @return {number[]} + */ + sortArrayByParity(nums) { + const even = []; + const odd = []; + + for (let num of nums) { + if (num % 2) { + odd.push(num); + } else { + even.push(num); + } + } + + let idx = 0; + for (let e of even) { + nums[idx++] = e; + } + for (let o of odd) { + nums[idx++] = o; + } + + return nums; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(n)$ + +--- + +## 3. Two Pointers - I + +::tabs-start + +```python +class Solution: + def sortArrayByParity(self, nums: List[int]) -> List[int]: + i, j = 0, len(nums) - 1 + while i < j: + if nums[i] & 1: + nums[i], nums[j] = nums[j], nums[i] + j -= 1 + else: + i += 1 + return nums +``` + +```java +public class Solution { + public int[] sortArrayByParity(int[] nums) { + int i = 0, j = nums.length - 1; + while (i < j) { + if ((nums[i] & 1) == 1) { + int temp = nums[i]; + nums[i] = nums[j]; + nums[j--] = temp; + } else { + i++; + } + } + return nums; + } +} +``` + +```cpp +class Solution { +public: + vector sortArrayByParity(vector& nums) { + int i = 0, j = nums.size() - 1; + while (i < j) { + if ((nums[i] & 1) == 1) { + swap(nums[i], nums[j]); + j--; + } else { + i++; + } + } + return nums; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @return {number[]} + */ + sortArrayByParity(nums) { + let i = 0, j = nums.length - 1; + while (i < j) { + if ((nums[i] & 1) == 1) { + [nums[i], nums[j]] = [nums[j], nums[i]]; + j--; + } else { + i++; + } + } + return nums; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ extra space. + +--- + +## 4. Two Pointers - II + +::tabs-start + +```python +class Solution: + def sortArrayByParity(self, nums: List[int]) -> List[int]: + l = 0 + for r in range(len(nums)): + if nums[r] % 2 == 0: + nums[l], nums[r] = nums[r], nums[l] + l += 1 + return nums +``` + +```java +public class Solution { + public int[] sortArrayByParity(int[] nums) { + for (int l = 0, r = 0; r < nums.length; r++) { + if (nums[r] % 2 == 0) { + int temp = nums[l]; + nums[l] = nums[r]; + nums[r] = temp; + l++; + } + } + return nums; + } +} +``` + +```cpp +class Solution { +public: + vector sortArrayByParity(vector& nums) { + for (int l = 0, r = 0; r < nums.size(); r++) { + if (nums[r] % 2 == 0) { + swap(nums[l], nums[r]); + l++; + } + } + return nums; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @return {number[]} + */ + sortArrayByParity(nums) { + for (let l = 0, r = 0; r < nums.length; r++) { + if (nums[r] % 2 == 0) { + [nums[l], nums[r]] = [nums[r], nums[l]]; + l++; + } + } + return nums; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ extra space. \ No newline at end of file diff --git a/articles/squares-of-a-sorted-array.md b/articles/squares-of-a-sorted-array.md new file mode 100644 index 000000000..c4d76f6b3 --- /dev/null +++ b/articles/squares-of-a-sorted-array.md @@ -0,0 +1,266 @@ +## 1. Sorting + +::tabs-start + +```python +class Solution: + def sortedSquares(self, nums: List[int]) -> List[int]: + for i in range(len(nums)): + nums[i] *= nums[i] + nums.sort() + return nums +``` + +```java +public class Solution { + public int[] sortedSquares(int[] nums) { + for (int i = 0; i < nums.length; i++) { + nums[i] *= nums[i]; + } + Arrays.sort(nums); + return nums; + } +} +``` + +```cpp +class Solution { +public: + vector sortedSquares(vector& nums) { + for (int i = 0; i < nums.size(); i++) { + nums[i] *= nums[i]; + } + sort(nums.begin(), nums.end()); + return nums; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @return {number[]} + */ + sortedSquares(nums) { + for (let i = 0; i < nums.length; i++) { + nums[i] *= nums[i]; + } + nums.sort((a, b) => a - b); + return nums; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n \log n)$ +* Space complexity: $O(1)$ or $O(n)$ depending on the sorting algorithm. + +--- + +## 2. Two Pointers - I + +::tabs-start + +```python +class Solution: + def sortedSquares(self, nums: List[int]) -> List[int]: + l, r, res = 0, len(nums) - 1, [] + + while l <= r: + if (nums[l] * nums[l]) > (nums[r] * nums[r]): + res.append(nums[l] * nums[l]) + l += 1 + else: + res.append(nums[r] * nums[r]) + r -= 1 + + return res[::-1] +``` + +```java +public class Solution { + public int[] sortedSquares(int[] nums) { + int l = 0, r = nums.length - 1; + ArrayList res = new ArrayList<>(); + + while (l <= r) { + if (nums[l] * nums[l] > nums[r] * nums[r]) { + res.add(nums[l] * nums[l]); + l++; + } else { + res.add(nums[r] * nums[r]); + r--; + } + } + + Collections.reverse(res); + return res.stream().mapToInt(i -> i).toArray(); + } +} +``` + +```cpp +class Solution { +public: + vector sortedSquares(vector& nums) { + int l = 0, r = nums.size() - 1; + vector res; + + while (l <= r) { + if (nums[l] * nums[l] > nums[r] * nums[r]) { + res.push_back(nums[l] * nums[l]); + l++; + } else { + res.push_back(nums[r] * nums[r]); + r--; + } + } + + reverse(res.begin(), res.end()); + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @return {number[]} + */ + sortedSquares(nums) { + let l = 0, r = nums.length - 1; + const res = []; + + while (l <= r) { + if (nums[l] * nums[l] > nums[r] * nums[r]) { + res.push(nums[l] * nums[l]); + l++; + } else { + res.push(nums[r] * nums[r]); + r--; + } + } + + return res.reverse(); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(n)$ for the output array. + +--- + +## 3. Two Pointers - II + +::tabs-start + +```python +class Solution: + def sortedSquares(self, nums: List[int]) -> List[int]: + n = len(nums) + res = [0] * n + l, r = 0, n - 1 + res_index = n - 1 + + while l <= r: + if abs(nums[l]) > abs(nums[r]): + res[res_index] = nums[l] * nums[l] + l += 1 + else: + res[res_index] = nums[r] * nums[r] + r -= 1 + res_index -= 1 + + return res +``` + +```java +public class Solution { + public int[] sortedSquares(int[] nums) { + int n = nums.length; + int[] res = new int[n]; + int l = 0, r = n - 1, resIndex = n - 1; + + while (l <= r) { + if (Math.abs(nums[l]) > Math.abs(nums[r])) { + res[resIndex] = nums[l] * nums[l]; + l++; + } else { + res[resIndex] = nums[r] * nums[r]; + r--; + } + resIndex--; + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + vector sortedSquares(vector& nums) { + int n = nums.size(); + vector res(n); + int l = 0, r = n - 1, resIndex = n - 1; + + while (l <= r) { + if (abs(nums[l]) > abs(nums[r])) { + res[resIndex] = nums[l] * nums[l]; + l++; + } else { + res[resIndex] = nums[r] * nums[r]; + r--; + } + resIndex--; + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @return {number[]} + */ + sortedSquares(nums) { + const n = nums.length; + const res = new Array(n); + let l = 0, r = n - 1, resIndex = n - 1; + + while (l <= r) { + if (Math.abs(nums[l]) > Math.abs(nums[r])) { + res[resIndex] = nums[l] * nums[l]; + l++; + } else { + res[resIndex] = nums[r] * nums[r]; + r--; + } + resIndex--; + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(n)$ for the output array. \ No newline at end of file diff --git a/articles/valid-palindrome-ii.md b/articles/valid-palindrome-ii.md new file mode 100644 index 000000000..05367b687 --- /dev/null +++ b/articles/valid-palindrome-ii.md @@ -0,0 +1,395 @@ +## 1. Brute Force + +::tabs-start + +```python +class Solution: + def validPalindrome(self, s: str) -> bool: + if s == s[::-1]: + return True + + for i in range(len(s)): + newS = s[:i] + s[i + 1:] + if newS == newS[::-1]: + return True + + return False +``` + +```java +public class Solution { + public boolean validPalindrome(String s) { + if (isPalindrome(s)) { + return true; + } + + for (int i = 0; i < s.length(); i++) { + String newS = s.substring(0, i) + s.substring(i + 1); + if (isPalindrome(newS)) { + return true; + } + } + + return false; + } + + private boolean isPalindrome(String s) { + int left = 0, right = s.length() - 1; + while (left < right) { + if (s.charAt(left) != s.charAt(right)) { + return false; + } + left++; + right--; + } + return true; + } +} +``` + +```cpp +class Solution { +public: + bool validPalindrome(string s) { + if (isPalindrome(s)) { + return true; + } + + for (int i = 0; i < s.size(); i++) { + string newS = s.substr(0, i) + s.substr(i + 1); + if (isPalindrome(newS)) { + return true; + } + } + + return false; + } + +private: + bool isPalindrome(const string& s) { + int left = 0, right = s.size() - 1; + while (left < right) { + if (s[left] != s[right]) { + return false; + } + left++; + right--; + } + return true; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string} s + * @return {boolean} + */ + validPalindrome(s) { + if (this.isPalindrome(s)) { + return true; + } + + for (let i = 0; i < s.length; i++) { + const newS = s.slice(0, i) + s.slice(i + 1); + if (this.isPalindrome(newS)) { + return true; + } + } + + return false; + } + + /** + * @param {string} s + * @return {boolean} + */ + isPalindrome(s) { + let left = 0, right = s.length - 1; + while (left < right) { + if (s[left] !== s[right]) { + return false; + } + left++; + right--; + } + return true; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n ^ 2)$ +* Space complexity: $O(n)$ + +--- + +## 2. Two Pointers + +::tabs-start + +```python +class Solution: + def validPalindrome(self, s: str) -> bool: + l, r = 0, len(s) - 1 + + while l < r: + if s[l] != s[r]: + skipL = s[l + 1 : r + 1] + skipR = s[l : r] + return skipL == skipL[::-1] or skipR == skipR[::-1] + l, r = l + 1, r - 1 + + return True +``` + +```java +public class Solution { + public boolean validPalindrome(String s) { + int l = 0, r = s.length() - 1; + + while (l < r) { + if (s.charAt(l) != s.charAt(r)) { + return isPalindrome(s.substring(0, l) + s.substring(l + 1)) || + isPalindrome(s.substring(0, r) + s.substring(r + 1)); + } + l++; + r--; + } + + return true; + } + + private boolean isPalindrome(String s) { + int l = 0, r = s.length() - 1; + while (l < r) { + if (s.charAt(l) != s.charAt(r)) { + return false; + } + l++; + r--; + } + return true; + } +} +``` + +```cpp +class Solution { +public: + bool validPalindrome(string s) { + int l = 0, r = s.size() - 1; + + while (l < r) { + if (s[l] != s[r]) { + return isPalindrome(s.substr(0, l) + s.substr(l + 1)) || + isPalindrome(s.substr(0, r) + s.substr(r + 1)); + } + l++; + r--; + } + + return true; + } + +private: + bool isPalindrome(string s) { + int l = 0, r = s.length() - 1; + while (l < r) { + if (s[l] != s[r]) { + return false; + } + l++; + r--; + } + return true; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string} s + * @return {boolean} + */ + validPalindrome(s) { + let l = 0, r = s.length - 1; + + while (l < r) { + if (s[l] !== s[r]) { + return this.isPalindrome(s.slice(0, l) + s.slice(l + 1)) || + this.isPalindrome(s.slice(0, r) + s.slice(r + 1)); + } + l++; + r--; + } + + return true; + } + + /** + * @param {string} s + * @return {boolean} + */ + isPalindrome(s) { + let left = 0, right = s.length - 1; + while (left < right) { + if (s[left] !== s[right]) { + return false; + } + left++; + right--; + } + return true; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(n)$ + +--- + +## 3. Two Pointers (Optimal) + +::tabs-start + +```python +class Solution: + def validPalindrome(self, s: str) -> bool: + def is_palindrome(l, r): + while l < r: + if s[l] != s[r]: + return False + l += 1 + r -= 1 + return True + + l, r = 0, len(s) - 1 + while l < r: + if s[l] != s[r]: + return (is_palindrome(l + 1, r) or + is_palindrome(l, r - 1)) + l += 1 + r -= 1 + + return True +``` + +```java +public class Solution { + public boolean validPalindrome(String s) { + int l = 0, r = s.length() - 1; + + while (l < r) { + if (s.charAt(l) != s.charAt(r)) { + return isPalindrome(s, l + 1, r) || + isPalindrome(s, l, r - 1); + } + l++; + r--; + } + + return true; + } + + private boolean isPalindrome(String s, int l, int r) { + while (l < r) { + if (s.charAt(l) != s.charAt(r)) { + return false; + } + l++; + r--; + } + return true; + } +} +``` + +```cpp +class Solution { +public: + bool validPalindrome(string s) { + int l = 0, r = s.size() - 1; + + while (l < r) { + if (s[l] != s[r]) { + return isPalindrome(s, l + 1, r) || + isPalindrome(s, l, r - 1); + } + l++; + r--; + } + + return true; + } + +private: + bool isPalindrome(const string& s, int l, int r) { + while (l < r) { + if (s[l] != s[r]) { + return false; + } + l++; + r--; + } + return true; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string} s + * @return {boolean} + */ + validPalindrome(s) { + let l = 0, r = s.length - 1; + + while (l < r) { + if (s[l] !== s[r]) { + return this.isPalindrome(s, l + 1, r) || + this.isPalindrome(s, l, r - 1); + } + l++; + r--; + } + + return true; + } + + /** + * @param {string} s + * @param {number} l + * @param {number} r + * @return {boolean} + */ + isPalindrome(s, l, r) { + while (l < r) { + if (s[l] !== s[r]) { + return false; + } + l++; + r--; + } + return true; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ \ No newline at end of file pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy