id
int64
1
3.64k
title
stringlengths
3
79
difficulty
stringclasses
3 values
description
stringlengths
430
25.4k
tags
stringlengths
0
131
language
stringclasses
19 values
solution
stringlengths
47
20.6k
3,223
Minimum Length of String After Operations
Medium
<p>You are given a string <code>s</code>.</p> <p>You can perform the following process on <code>s</code> <strong>any</strong> number of times:</p> <ul> <li>Choose an index <code>i</code> in the string such that there is <strong>at least</strong> one character to the left of index <code>i</code> that is equal to <code>s[i]</code>, and <strong>at least</strong> one character to the right that is also equal to <code>s[i]</code>.</li> <li>Delete the <strong>closest</strong> occurrence of <code>s[i]</code> located to the <strong>left</strong> of <code>i</code>.</li> <li>Delete the <strong>closest</strong> occurrence of <code>s[i]</code> located to the <strong>right</strong> of <code>i</code>.</li> </ul> <p>Return the <strong>minimum</strong> length of the final string <code>s</code> that you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abaacbcbb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong><br /> We do the following operations:</p> <ul> <li>Choose index 2, then remove the characters at indices 0 and 3. The resulting string is <code>s = &quot;bacbcbb&quot;</code>.</li> <li>Choose index 3, then remove the characters at indices 0 and 5. The resulting string is <code>s = &quot;acbcb&quot;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aa&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong><br /> We cannot perform any operations, so we return the length of the original string.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters.</li> </ul>
Hash Table; String; Counting
C++
class Solution { public: int minimumLength(string s) { int cnt[26]{}; for (char& c : s) { ++cnt[c - 'a']; } int ans = 0; for (int x : cnt) { if (x) { ans += x % 2 ? 1 : 2; } } return ans; } };
3,223
Minimum Length of String After Operations
Medium
<p>You are given a string <code>s</code>.</p> <p>You can perform the following process on <code>s</code> <strong>any</strong> number of times:</p> <ul> <li>Choose an index <code>i</code> in the string such that there is <strong>at least</strong> one character to the left of index <code>i</code> that is equal to <code>s[i]</code>, and <strong>at least</strong> one character to the right that is also equal to <code>s[i]</code>.</li> <li>Delete the <strong>closest</strong> occurrence of <code>s[i]</code> located to the <strong>left</strong> of <code>i</code>.</li> <li>Delete the <strong>closest</strong> occurrence of <code>s[i]</code> located to the <strong>right</strong> of <code>i</code>.</li> </ul> <p>Return the <strong>minimum</strong> length of the final string <code>s</code> that you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abaacbcbb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong><br /> We do the following operations:</p> <ul> <li>Choose index 2, then remove the characters at indices 0 and 3. The resulting string is <code>s = &quot;bacbcbb&quot;</code>.</li> <li>Choose index 3, then remove the characters at indices 0 and 5. The resulting string is <code>s = &quot;acbcb&quot;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aa&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong><br /> We cannot perform any operations, so we return the length of the original string.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters.</li> </ul>
Hash Table; String; Counting
Go
func minimumLength(s string) (ans int) { cnt := [26]int{} for _, c := range s { cnt[c-'a']++ } for _, x := range cnt { if x > 0 { if x&1 == 1 { ans += 1 } else { ans += 2 } } } return }
3,223
Minimum Length of String After Operations
Medium
<p>You are given a string <code>s</code>.</p> <p>You can perform the following process on <code>s</code> <strong>any</strong> number of times:</p> <ul> <li>Choose an index <code>i</code> in the string such that there is <strong>at least</strong> one character to the left of index <code>i</code> that is equal to <code>s[i]</code>, and <strong>at least</strong> one character to the right that is also equal to <code>s[i]</code>.</li> <li>Delete the <strong>closest</strong> occurrence of <code>s[i]</code> located to the <strong>left</strong> of <code>i</code>.</li> <li>Delete the <strong>closest</strong> occurrence of <code>s[i]</code> located to the <strong>right</strong> of <code>i</code>.</li> </ul> <p>Return the <strong>minimum</strong> length of the final string <code>s</code> that you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abaacbcbb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong><br /> We do the following operations:</p> <ul> <li>Choose index 2, then remove the characters at indices 0 and 3. The resulting string is <code>s = &quot;bacbcbb&quot;</code>.</li> <li>Choose index 3, then remove the characters at indices 0 and 5. The resulting string is <code>s = &quot;acbcb&quot;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aa&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong><br /> We cannot perform any operations, so we return the length of the original string.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters.</li> </ul>
Hash Table; String; Counting
Java
class Solution { public int minimumLength(String s) { int[] cnt = new int[26]; for (int i = 0; i < s.length(); ++i) { ++cnt[s.charAt(i) - 'a']; } int ans = 0; for (int x : cnt) { if (x > 0) { ans += x % 2 == 1 ? 1 : 2; } } return ans; } }
3,223
Minimum Length of String After Operations
Medium
<p>You are given a string <code>s</code>.</p> <p>You can perform the following process on <code>s</code> <strong>any</strong> number of times:</p> <ul> <li>Choose an index <code>i</code> in the string such that there is <strong>at least</strong> one character to the left of index <code>i</code> that is equal to <code>s[i]</code>, and <strong>at least</strong> one character to the right that is also equal to <code>s[i]</code>.</li> <li>Delete the <strong>closest</strong> occurrence of <code>s[i]</code> located to the <strong>left</strong> of <code>i</code>.</li> <li>Delete the <strong>closest</strong> occurrence of <code>s[i]</code> located to the <strong>right</strong> of <code>i</code>.</li> </ul> <p>Return the <strong>minimum</strong> length of the final string <code>s</code> that you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abaacbcbb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong><br /> We do the following operations:</p> <ul> <li>Choose index 2, then remove the characters at indices 0 and 3. The resulting string is <code>s = &quot;bacbcbb&quot;</code>.</li> <li>Choose index 3, then remove the characters at indices 0 and 5. The resulting string is <code>s = &quot;acbcb&quot;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aa&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong><br /> We cannot perform any operations, so we return the length of the original string.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters.</li> </ul>
Hash Table; String; Counting
Python
class Solution: def minimumLength(self, s: str) -> int: cnt = Counter(s) return sum(1 if x & 1 else 2 for x in cnt.values())
3,223
Minimum Length of String After Operations
Medium
<p>You are given a string <code>s</code>.</p> <p>You can perform the following process on <code>s</code> <strong>any</strong> number of times:</p> <ul> <li>Choose an index <code>i</code> in the string such that there is <strong>at least</strong> one character to the left of index <code>i</code> that is equal to <code>s[i]</code>, and <strong>at least</strong> one character to the right that is also equal to <code>s[i]</code>.</li> <li>Delete the <strong>closest</strong> occurrence of <code>s[i]</code> located to the <strong>left</strong> of <code>i</code>.</li> <li>Delete the <strong>closest</strong> occurrence of <code>s[i]</code> located to the <strong>right</strong> of <code>i</code>.</li> </ul> <p>Return the <strong>minimum</strong> length of the final string <code>s</code> that you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abaacbcbb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong><br /> We do the following operations:</p> <ul> <li>Choose index 2, then remove the characters at indices 0 and 3. The resulting string is <code>s = &quot;bacbcbb&quot;</code>.</li> <li>Choose index 3, then remove the characters at indices 0 and 5. The resulting string is <code>s = &quot;acbcb&quot;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aa&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong><br /> We cannot perform any operations, so we return the length of the original string.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters.</li> </ul>
Hash Table; String; Counting
TypeScript
function minimumLength(s: string): number { const cnt = new Map<string, number>(); for (const c of s) { cnt.set(c, (cnt.get(c) || 0) + 1); } let ans = 0; for (const x of cnt.values()) { ans += x & 1 ? 1 : 2; } return ans; }
3,224
Minimum Array Changes to Make Differences Equal
Medium
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in the range from <code>0</code> to <code>k</code>.</p> <p>You need to perform some changes (possibly none) such that the final array satisfies the following condition:</p> <ul> <li>There exists an integer <code>X</code> such that <code>abs(a[i] - a[n - i - 1]) = X</code> for all <code>(0 &lt;= i &lt; n)</code>.</li> </ul> <p>Return the <strong>minimum</strong> number of changes required to satisfy the above condition.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,1,2,4,3], k = 4</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong><br /> We can perform the following changes:</p> <ul> <li>Replace <code>nums[1]</code> by 2. The resulting array is <code>nums = [1,<u><strong>2</strong></u>,1,2,4,3]</code>.</li> <li>Replace <code>nums[3]</code> by 3. The resulting array is <code>nums = [1,2,1,<u><strong>3</strong></u>,4,3]</code>.</li> </ul> <p>The integer <code>X</code> will be 2.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,2,3,3,6,5,4], k = 6</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong><br /> We can perform the following operations:</p> <ul> <li>Replace <code>nums[3]</code> by 0. The resulting array is <code>nums = [0,1,2,<u><strong>0</strong></u>,3,6,5,4]</code>.</li> <li>Replace <code>nums[4]</code> by 4. The resulting array is <code>nums = [0,1,2,0,<strong><u>4</u></strong>,6,5,4]</code>.</li> </ul> <p>The integer <code>X</code> will be 4.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 10<sup>5</sup></code></li> <li><code>n</code> is even.</li> <li><code>0 &lt;= nums[i] &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
Array; Hash Table; Prefix Sum
C++
class Solution { public: int minChanges(vector<int>& nums, int k) { int d[k + 2]; memset(d, 0, sizeof(d)); int n = nums.size(); for (int i = 0; i < n / 2; ++i) { int x = min(nums[i], nums[n - i - 1]); int y = max(nums[i], nums[n - i - 1]); d[0] += 1; d[y - x] -= 1; d[y - x + 1] += 1; d[max(y, k - x) + 1] -= 1; d[max(y, k - x) + 1] += 2; } int ans = n, s = 0; for (int x : d) { s += x; ans = min(ans, s); } return ans; } };
3,224
Minimum Array Changes to Make Differences Equal
Medium
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in the range from <code>0</code> to <code>k</code>.</p> <p>You need to perform some changes (possibly none) such that the final array satisfies the following condition:</p> <ul> <li>There exists an integer <code>X</code> such that <code>abs(a[i] - a[n - i - 1]) = X</code> for all <code>(0 &lt;= i &lt; n)</code>.</li> </ul> <p>Return the <strong>minimum</strong> number of changes required to satisfy the above condition.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,1,2,4,3], k = 4</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong><br /> We can perform the following changes:</p> <ul> <li>Replace <code>nums[1]</code> by 2. The resulting array is <code>nums = [1,<u><strong>2</strong></u>,1,2,4,3]</code>.</li> <li>Replace <code>nums[3]</code> by 3. The resulting array is <code>nums = [1,2,1,<u><strong>3</strong></u>,4,3]</code>.</li> </ul> <p>The integer <code>X</code> will be 2.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,2,3,3,6,5,4], k = 6</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong><br /> We can perform the following operations:</p> <ul> <li>Replace <code>nums[3]</code> by 0. The resulting array is <code>nums = [0,1,2,<u><strong>0</strong></u>,3,6,5,4]</code>.</li> <li>Replace <code>nums[4]</code> by 4. The resulting array is <code>nums = [0,1,2,0,<strong><u>4</u></strong>,6,5,4]</code>.</li> </ul> <p>The integer <code>X</code> will be 4.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 10<sup>5</sup></code></li> <li><code>n</code> is even.</li> <li><code>0 &lt;= nums[i] &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
Array; Hash Table; Prefix Sum
Go
func minChanges(nums []int, k int) int { d := make([]int, k+2) n := len(nums) for i := 0; i < n/2; i++ { x, y := nums[i], nums[n-1-i] if x > y { x, y = y, x } d[0] += 1 d[y-x] -= 1 d[y-x+1] += 1 d[max(y, k-x)+1] -= 1 d[max(y, k-x)+1] += 2 } ans, s := n, 0 for _, x := range d { s += x ans = min(ans, s) } return ans }
3,224
Minimum Array Changes to Make Differences Equal
Medium
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in the range from <code>0</code> to <code>k</code>.</p> <p>You need to perform some changes (possibly none) such that the final array satisfies the following condition:</p> <ul> <li>There exists an integer <code>X</code> such that <code>abs(a[i] - a[n - i - 1]) = X</code> for all <code>(0 &lt;= i &lt; n)</code>.</li> </ul> <p>Return the <strong>minimum</strong> number of changes required to satisfy the above condition.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,1,2,4,3], k = 4</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong><br /> We can perform the following changes:</p> <ul> <li>Replace <code>nums[1]</code> by 2. The resulting array is <code>nums = [1,<u><strong>2</strong></u>,1,2,4,3]</code>.</li> <li>Replace <code>nums[3]</code> by 3. The resulting array is <code>nums = [1,2,1,<u><strong>3</strong></u>,4,3]</code>.</li> </ul> <p>The integer <code>X</code> will be 2.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,2,3,3,6,5,4], k = 6</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong><br /> We can perform the following operations:</p> <ul> <li>Replace <code>nums[3]</code> by 0. The resulting array is <code>nums = [0,1,2,<u><strong>0</strong></u>,3,6,5,4]</code>.</li> <li>Replace <code>nums[4]</code> by 4. The resulting array is <code>nums = [0,1,2,0,<strong><u>4</u></strong>,6,5,4]</code>.</li> </ul> <p>The integer <code>X</code> will be 4.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 10<sup>5</sup></code></li> <li><code>n</code> is even.</li> <li><code>0 &lt;= nums[i] &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
Array; Hash Table; Prefix Sum
Java
class Solution { public int minChanges(int[] nums, int k) { int[] d = new int[k + 2]; int n = nums.length; for (int i = 0; i < n / 2; ++i) { int x = Math.min(nums[i], nums[n - i - 1]); int y = Math.max(nums[i], nums[n - i - 1]); d[0] += 1; d[y - x] -= 1; d[y - x + 1] += 1; d[Math.max(y, k - x) + 1] -= 1; d[Math.max(y, k - x) + 1] += 2; } int ans = n, s = 0; for (int x : d) { s += x; ans = Math.min(ans, s); } return ans; } }
3,224
Minimum Array Changes to Make Differences Equal
Medium
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in the range from <code>0</code> to <code>k</code>.</p> <p>You need to perform some changes (possibly none) such that the final array satisfies the following condition:</p> <ul> <li>There exists an integer <code>X</code> such that <code>abs(a[i] - a[n - i - 1]) = X</code> for all <code>(0 &lt;= i &lt; n)</code>.</li> </ul> <p>Return the <strong>minimum</strong> number of changes required to satisfy the above condition.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,1,2,4,3], k = 4</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong><br /> We can perform the following changes:</p> <ul> <li>Replace <code>nums[1]</code> by 2. The resulting array is <code>nums = [1,<u><strong>2</strong></u>,1,2,4,3]</code>.</li> <li>Replace <code>nums[3]</code> by 3. The resulting array is <code>nums = [1,2,1,<u><strong>3</strong></u>,4,3]</code>.</li> </ul> <p>The integer <code>X</code> will be 2.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,2,3,3,6,5,4], k = 6</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong><br /> We can perform the following operations:</p> <ul> <li>Replace <code>nums[3]</code> by 0. The resulting array is <code>nums = [0,1,2,<u><strong>0</strong></u>,3,6,5,4]</code>.</li> <li>Replace <code>nums[4]</code> by 4. The resulting array is <code>nums = [0,1,2,0,<strong><u>4</u></strong>,6,5,4]</code>.</li> </ul> <p>The integer <code>X</code> will be 4.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 10<sup>5</sup></code></li> <li><code>n</code> is even.</li> <li><code>0 &lt;= nums[i] &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
Array; Hash Table; Prefix Sum
Python
class Solution: def minChanges(self, nums: List[int], k: int) -> int: d = [0] * (k + 2) n = len(nums) for i in range(n // 2): x, y = nums[i], nums[-i - 1] if x > y: x, y = y, x d[0] += 1 d[y - x] -= 1 d[y - x + 1] += 1 d[max(y, k - x) + 1] -= 1 d[max(y, k - x) + 1] += 2 return min(accumulate(d))
3,224
Minimum Array Changes to Make Differences Equal
Medium
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in the range from <code>0</code> to <code>k</code>.</p> <p>You need to perform some changes (possibly none) such that the final array satisfies the following condition:</p> <ul> <li>There exists an integer <code>X</code> such that <code>abs(a[i] - a[n - i - 1]) = X</code> for all <code>(0 &lt;= i &lt; n)</code>.</li> </ul> <p>Return the <strong>minimum</strong> number of changes required to satisfy the above condition.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,1,2,4,3], k = 4</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong><br /> We can perform the following changes:</p> <ul> <li>Replace <code>nums[1]</code> by 2. The resulting array is <code>nums = [1,<u><strong>2</strong></u>,1,2,4,3]</code>.</li> <li>Replace <code>nums[3]</code> by 3. The resulting array is <code>nums = [1,2,1,<u><strong>3</strong></u>,4,3]</code>.</li> </ul> <p>The integer <code>X</code> will be 2.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,2,3,3,6,5,4], k = 6</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong><br /> We can perform the following operations:</p> <ul> <li>Replace <code>nums[3]</code> by 0. The resulting array is <code>nums = [0,1,2,<u><strong>0</strong></u>,3,6,5,4]</code>.</li> <li>Replace <code>nums[4]</code> by 4. The resulting array is <code>nums = [0,1,2,0,<strong><u>4</u></strong>,6,5,4]</code>.</li> </ul> <p>The integer <code>X</code> will be 4.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 10<sup>5</sup></code></li> <li><code>n</code> is even.</li> <li><code>0 &lt;= nums[i] &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
Array; Hash Table; Prefix Sum
TypeScript
function minChanges(nums: number[], k: number): number { const d: number[] = Array(k + 2).fill(0); const n = nums.length; for (let i = 0; i < n >> 1; ++i) { const x = Math.min(nums[i], nums[n - 1 - i]); const y = Math.max(nums[i], nums[n - 1 - i]); d[0] += 1; d[y - x] -= 1; d[y - x + 1] += 1; d[Math.max(y, k - x) + 1] -= 1; d[Math.max(y, k - x) + 1] += 2; } let [ans, s] = [n, 0]; for (const x of d) { s += x; ans = Math.min(ans, s); } return ans; }
3,226
Number of Bit Changes to Make Two Integers Equal
Easy
<p>You are given two positive integers <code>n</code> and <code>k</code>.</p> <p>You can choose <strong>any</strong> bit in the <strong>binary representation</strong> of <code>n</code> that is equal to 1 and change it to 0.</p> <p>Return the <em>number of changes</em> needed to make <code>n</code> equal to <code>k</code>. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 13, k = 4</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong><br /> Initially, the binary representations of <code>n</code> and <code>k</code> are <code>n = (1101)<sub>2</sub></code> and <code>k = (0100)<sub>2</sub></code>.<br /> We can change the first and fourth bits of <code>n</code>. The resulting integer is <code>n = (<u><strong>0</strong></u>10<u><strong>0</strong></u>)<sub>2</sub> = k</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 21, k = 21</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong><br /> <code>n</code> and <code>k</code> are already equal, so no changes are needed.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 14, k = 13</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is not possible to make <code>n</code> equal to <code>k</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n, k &lt;= 10<sup>6</sup></code></li> </ul>
Bit Manipulation
C++
class Solution { public: int minChanges(int n, int k) { return (n & k) != k ? -1 : __builtin_popcount(n ^ k); } };
3,226
Number of Bit Changes to Make Two Integers Equal
Easy
<p>You are given two positive integers <code>n</code> and <code>k</code>.</p> <p>You can choose <strong>any</strong> bit in the <strong>binary representation</strong> of <code>n</code> that is equal to 1 and change it to 0.</p> <p>Return the <em>number of changes</em> needed to make <code>n</code> equal to <code>k</code>. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 13, k = 4</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong><br /> Initially, the binary representations of <code>n</code> and <code>k</code> are <code>n = (1101)<sub>2</sub></code> and <code>k = (0100)<sub>2</sub></code>.<br /> We can change the first and fourth bits of <code>n</code>. The resulting integer is <code>n = (<u><strong>0</strong></u>10<u><strong>0</strong></u>)<sub>2</sub> = k</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 21, k = 21</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong><br /> <code>n</code> and <code>k</code> are already equal, so no changes are needed.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 14, k = 13</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is not possible to make <code>n</code> equal to <code>k</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n, k &lt;= 10<sup>6</sup></code></li> </ul>
Bit Manipulation
Go
func minChanges(n int, k int) int { if n&k != k { return -1 } return bits.OnesCount(uint(n ^ k)) }
3,226
Number of Bit Changes to Make Two Integers Equal
Easy
<p>You are given two positive integers <code>n</code> and <code>k</code>.</p> <p>You can choose <strong>any</strong> bit in the <strong>binary representation</strong> of <code>n</code> that is equal to 1 and change it to 0.</p> <p>Return the <em>number of changes</em> needed to make <code>n</code> equal to <code>k</code>. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 13, k = 4</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong><br /> Initially, the binary representations of <code>n</code> and <code>k</code> are <code>n = (1101)<sub>2</sub></code> and <code>k = (0100)<sub>2</sub></code>.<br /> We can change the first and fourth bits of <code>n</code>. The resulting integer is <code>n = (<u><strong>0</strong></u>10<u><strong>0</strong></u>)<sub>2</sub> = k</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 21, k = 21</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong><br /> <code>n</code> and <code>k</code> are already equal, so no changes are needed.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 14, k = 13</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is not possible to make <code>n</code> equal to <code>k</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n, k &lt;= 10<sup>6</sup></code></li> </ul>
Bit Manipulation
Java
class Solution { public int minChanges(int n, int k) { return (n & k) != k ? -1 : Integer.bitCount(n ^ k); } }
3,226
Number of Bit Changes to Make Two Integers Equal
Easy
<p>You are given two positive integers <code>n</code> and <code>k</code>.</p> <p>You can choose <strong>any</strong> bit in the <strong>binary representation</strong> of <code>n</code> that is equal to 1 and change it to 0.</p> <p>Return the <em>number of changes</em> needed to make <code>n</code> equal to <code>k</code>. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 13, k = 4</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong><br /> Initially, the binary representations of <code>n</code> and <code>k</code> are <code>n = (1101)<sub>2</sub></code> and <code>k = (0100)<sub>2</sub></code>.<br /> We can change the first and fourth bits of <code>n</code>. The resulting integer is <code>n = (<u><strong>0</strong></u>10<u><strong>0</strong></u>)<sub>2</sub> = k</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 21, k = 21</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong><br /> <code>n</code> and <code>k</code> are already equal, so no changes are needed.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 14, k = 13</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is not possible to make <code>n</code> equal to <code>k</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n, k &lt;= 10<sup>6</sup></code></li> </ul>
Bit Manipulation
Python
class Solution: def minChanges(self, n: int, k: int) -> int: return -1 if n & k != k else (n ^ k).bit_count()
3,226
Number of Bit Changes to Make Two Integers Equal
Easy
<p>You are given two positive integers <code>n</code> and <code>k</code>.</p> <p>You can choose <strong>any</strong> bit in the <strong>binary representation</strong> of <code>n</code> that is equal to 1 and change it to 0.</p> <p>Return the <em>number of changes</em> needed to make <code>n</code> equal to <code>k</code>. If it is impossible, return -1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 13, k = 4</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong><br /> Initially, the binary representations of <code>n</code> and <code>k</code> are <code>n = (1101)<sub>2</sub></code> and <code>k = (0100)<sub>2</sub></code>.<br /> We can change the first and fourth bits of <code>n</code>. The resulting integer is <code>n = (<u><strong>0</strong></u>10<u><strong>0</strong></u>)<sub>2</sub> = k</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 21, k = 21</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong><br /> <code>n</code> and <code>k</code> are already equal, so no changes are needed.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 14, k = 13</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is not possible to make <code>n</code> equal to <code>k</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n, k &lt;= 10<sup>6</sup></code></li> </ul>
Bit Manipulation
TypeScript
function minChanges(n: number, k: number): number { return (n & k) !== k ? -1 : bitCount(n ^ k); } function bitCount(i: number): number { i = i - ((i >>> 1) & 0x55555555); i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); i = (i + (i >>> 4)) & 0x0f0f0f0f; i = i + (i >>> 8); i = i + (i >>> 16); return i & 0x3f; }
3,227
Vowels Game in a String
Medium
<p>Alice and Bob are playing a game on a string.</p> <p>You are given a string <code>s</code>, Alice and Bob will take turns playing the following game where Alice starts <strong>first</strong>:</p> <ul> <li>On Alice&#39;s turn, she has to remove any <strong>non-empty</strong> <span data-keyword="substring">substring</span> from <code>s</code> that contains an <strong>odd</strong> number of vowels.</li> <li>On Bob&#39;s turn, he has to remove any <strong>non-empty</strong> <span data-keyword="substring">substring</span> from <code>s</code> that contains an <strong>even</strong> number of vowels.</li> </ul> <p>The first player who cannot make a move on their turn loses the game. We assume that both Alice and Bob play <strong>optimally</strong>.</p> <p>Return <code>true</code> if Alice wins the game, and <code>false</code> otherwise.</p> <p>The English vowels are: <code>a</code>, <code>e</code>, <code>i</code>, <code>o</code>, and <code>u</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcoder&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong><br /> Alice can win the game as follows:</p> <ul> <li>Alice plays first, she can delete the underlined substring in <code>s = &quot;<u><strong>leetco</strong></u>der&quot;</code> which contains 3 vowels. The resulting string is <code>s = &quot;der&quot;</code>.</li> <li>Bob plays second, he can delete the underlined substring in <code>s = &quot;<u><strong>d</strong></u>er&quot;</code> which contains 0 vowels. The resulting string is <code>s = &quot;er&quot;</code>.</li> <li>Alice plays third, she can delete the whole string <code>s = &quot;<strong><u>er</u></strong>&quot;</code> which contains 1 vowel.</li> <li>Bob plays fourth, since the string is empty, there is no valid play for Bob. So Alice wins the game.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;bbcd&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong><br /> There is no valid play for Alice in her first turn, so Alice loses the game.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters.</li> </ul>
Brainteaser; Math; String; Game Theory
C++
class Solution { public: bool doesAliceWin(string s) { string vowels = "aeiou"; for (char c : s) { if (vowels.find(c) != string::npos) { return true; } } return false; } };
3,227
Vowels Game in a String
Medium
<p>Alice and Bob are playing a game on a string.</p> <p>You are given a string <code>s</code>, Alice and Bob will take turns playing the following game where Alice starts <strong>first</strong>:</p> <ul> <li>On Alice&#39;s turn, she has to remove any <strong>non-empty</strong> <span data-keyword="substring">substring</span> from <code>s</code> that contains an <strong>odd</strong> number of vowels.</li> <li>On Bob&#39;s turn, he has to remove any <strong>non-empty</strong> <span data-keyword="substring">substring</span> from <code>s</code> that contains an <strong>even</strong> number of vowels.</li> </ul> <p>The first player who cannot make a move on their turn loses the game. We assume that both Alice and Bob play <strong>optimally</strong>.</p> <p>Return <code>true</code> if Alice wins the game, and <code>false</code> otherwise.</p> <p>The English vowels are: <code>a</code>, <code>e</code>, <code>i</code>, <code>o</code>, and <code>u</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcoder&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong><br /> Alice can win the game as follows:</p> <ul> <li>Alice plays first, she can delete the underlined substring in <code>s = &quot;<u><strong>leetco</strong></u>der&quot;</code> which contains 3 vowels. The resulting string is <code>s = &quot;der&quot;</code>.</li> <li>Bob plays second, he can delete the underlined substring in <code>s = &quot;<u><strong>d</strong></u>er&quot;</code> which contains 0 vowels. The resulting string is <code>s = &quot;er&quot;</code>.</li> <li>Alice plays third, she can delete the whole string <code>s = &quot;<strong><u>er</u></strong>&quot;</code> which contains 1 vowel.</li> <li>Bob plays fourth, since the string is empty, there is no valid play for Bob. So Alice wins the game.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;bbcd&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong><br /> There is no valid play for Alice in her first turn, so Alice loses the game.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters.</li> </ul>
Brainteaser; Math; String; Game Theory
Go
func doesAliceWin(s string) bool { vowels := "aeiou" for _, c := range s { if strings.ContainsRune(vowels, c) { return true } } return false }
3,227
Vowels Game in a String
Medium
<p>Alice and Bob are playing a game on a string.</p> <p>You are given a string <code>s</code>, Alice and Bob will take turns playing the following game where Alice starts <strong>first</strong>:</p> <ul> <li>On Alice&#39;s turn, she has to remove any <strong>non-empty</strong> <span data-keyword="substring">substring</span> from <code>s</code> that contains an <strong>odd</strong> number of vowels.</li> <li>On Bob&#39;s turn, he has to remove any <strong>non-empty</strong> <span data-keyword="substring">substring</span> from <code>s</code> that contains an <strong>even</strong> number of vowels.</li> </ul> <p>The first player who cannot make a move on their turn loses the game. We assume that both Alice and Bob play <strong>optimally</strong>.</p> <p>Return <code>true</code> if Alice wins the game, and <code>false</code> otherwise.</p> <p>The English vowels are: <code>a</code>, <code>e</code>, <code>i</code>, <code>o</code>, and <code>u</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcoder&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong><br /> Alice can win the game as follows:</p> <ul> <li>Alice plays first, she can delete the underlined substring in <code>s = &quot;<u><strong>leetco</strong></u>der&quot;</code> which contains 3 vowels. The resulting string is <code>s = &quot;der&quot;</code>.</li> <li>Bob plays second, he can delete the underlined substring in <code>s = &quot;<u><strong>d</strong></u>er&quot;</code> which contains 0 vowels. The resulting string is <code>s = &quot;er&quot;</code>.</li> <li>Alice plays third, she can delete the whole string <code>s = &quot;<strong><u>er</u></strong>&quot;</code> which contains 1 vowel.</li> <li>Bob plays fourth, since the string is empty, there is no valid play for Bob. So Alice wins the game.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;bbcd&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong><br /> There is no valid play for Alice in her first turn, so Alice loses the game.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters.</li> </ul>
Brainteaser; Math; String; Game Theory
Java
class Solution { public boolean doesAliceWin(String s) { for (int i = 0; i < s.length(); ++i) { if ("aeiou".indexOf(s.charAt(i)) != -1) { return true; } } return false; } }
3,227
Vowels Game in a String
Medium
<p>Alice and Bob are playing a game on a string.</p> <p>You are given a string <code>s</code>, Alice and Bob will take turns playing the following game where Alice starts <strong>first</strong>:</p> <ul> <li>On Alice&#39;s turn, she has to remove any <strong>non-empty</strong> <span data-keyword="substring">substring</span> from <code>s</code> that contains an <strong>odd</strong> number of vowels.</li> <li>On Bob&#39;s turn, he has to remove any <strong>non-empty</strong> <span data-keyword="substring">substring</span> from <code>s</code> that contains an <strong>even</strong> number of vowels.</li> </ul> <p>The first player who cannot make a move on their turn loses the game. We assume that both Alice and Bob play <strong>optimally</strong>.</p> <p>Return <code>true</code> if Alice wins the game, and <code>false</code> otherwise.</p> <p>The English vowels are: <code>a</code>, <code>e</code>, <code>i</code>, <code>o</code>, and <code>u</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcoder&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong><br /> Alice can win the game as follows:</p> <ul> <li>Alice plays first, she can delete the underlined substring in <code>s = &quot;<u><strong>leetco</strong></u>der&quot;</code> which contains 3 vowels. The resulting string is <code>s = &quot;der&quot;</code>.</li> <li>Bob plays second, he can delete the underlined substring in <code>s = &quot;<u><strong>d</strong></u>er&quot;</code> which contains 0 vowels. The resulting string is <code>s = &quot;er&quot;</code>.</li> <li>Alice plays third, she can delete the whole string <code>s = &quot;<strong><u>er</u></strong>&quot;</code> which contains 1 vowel.</li> <li>Bob plays fourth, since the string is empty, there is no valid play for Bob. So Alice wins the game.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;bbcd&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong><br /> There is no valid play for Alice in her first turn, so Alice loses the game.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters.</li> </ul>
Brainteaser; Math; String; Game Theory
Python
class Solution: def doesAliceWin(self, s: str) -> bool: vowels = set("aeiou") return any(c in vowels for c in s)
3,227
Vowels Game in a String
Medium
<p>Alice and Bob are playing a game on a string.</p> <p>You are given a string <code>s</code>, Alice and Bob will take turns playing the following game where Alice starts <strong>first</strong>:</p> <ul> <li>On Alice&#39;s turn, she has to remove any <strong>non-empty</strong> <span data-keyword="substring">substring</span> from <code>s</code> that contains an <strong>odd</strong> number of vowels.</li> <li>On Bob&#39;s turn, he has to remove any <strong>non-empty</strong> <span data-keyword="substring">substring</span> from <code>s</code> that contains an <strong>even</strong> number of vowels.</li> </ul> <p>The first player who cannot make a move on their turn loses the game. We assume that both Alice and Bob play <strong>optimally</strong>.</p> <p>Return <code>true</code> if Alice wins the game, and <code>false</code> otherwise.</p> <p>The English vowels are: <code>a</code>, <code>e</code>, <code>i</code>, <code>o</code>, and <code>u</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcoder&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong><br /> Alice can win the game as follows:</p> <ul> <li>Alice plays first, she can delete the underlined substring in <code>s = &quot;<u><strong>leetco</strong></u>der&quot;</code> which contains 3 vowels. The resulting string is <code>s = &quot;der&quot;</code>.</li> <li>Bob plays second, he can delete the underlined substring in <code>s = &quot;<u><strong>d</strong></u>er&quot;</code> which contains 0 vowels. The resulting string is <code>s = &quot;er&quot;</code>.</li> <li>Alice plays third, she can delete the whole string <code>s = &quot;<strong><u>er</u></strong>&quot;</code> which contains 1 vowel.</li> <li>Bob plays fourth, since the string is empty, there is no valid play for Bob. So Alice wins the game.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;bbcd&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong><br /> There is no valid play for Alice in her first turn, so Alice loses the game.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters.</li> </ul>
Brainteaser; Math; String; Game Theory
TypeScript
function doesAliceWin(s: string): boolean { const vowels = 'aeiou'; for (const c of s) { if (vowels.includes(c)) { return true; } } return false; }
3,228
Maximum Number of Operations to Move Ones to the End
Medium
<p>You are given a <span data-keyword="binary-string">binary string</span> <code>s</code>.</p> <p>You can perform the following operation on the string <strong>any</strong> number of times:</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the string where <code>i + 1 &lt; s.length</code> such that <code>s[i] == &#39;1&#39;</code> and <code>s[i + 1] == &#39;0&#39;</code>.</li> <li>Move the character <code>s[i]</code> to the <strong>right</strong> until it reaches the end of the string or another <code>&#39;1&#39;</code>. For example, for <code>s = &quot;010010&quot;</code>, if we choose <code>i = 1</code>, the resulting string will be <code>s = &quot;0<strong><u>001</u></strong>10&quot;</code>.</li> </ul> <p>Return the <strong>maximum</strong> number of operations that you can perform.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1001101&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>We can perform the following operations:</p> <ul> <li>Choose index <code>i = 0</code>. The resulting string is <code>s = &quot;<u><strong>001</strong></u>1101&quot;</code>.</li> <li>Choose index <code>i = 4</code>. The resulting string is <code>s = &quot;0011<u><strong>01</strong></u>1&quot;</code>.</li> <li>Choose index <code>i = 3</code>. The resulting string is <code>s = &quot;001<strong><u>01</u></strong>11&quot;</code>.</li> <li>Choose index <code>i = 2</code>. The resulting string is <code>s = &quot;00<strong><u>01</u></strong>111&quot;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;00111&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
Greedy; String; Counting
C++
class Solution { public: int maxOperations(string s) { int ans = 0, cnt = 0; int n = s.size(); for (int i = 0; i < n; ++i) { if (s[i] == '1') { ++cnt; } else if (i && s[i - 1] == '1') { ans += cnt; } } return ans; } };
3,228
Maximum Number of Operations to Move Ones to the End
Medium
<p>You are given a <span data-keyword="binary-string">binary string</span> <code>s</code>.</p> <p>You can perform the following operation on the string <strong>any</strong> number of times:</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the string where <code>i + 1 &lt; s.length</code> such that <code>s[i] == &#39;1&#39;</code> and <code>s[i + 1] == &#39;0&#39;</code>.</li> <li>Move the character <code>s[i]</code> to the <strong>right</strong> until it reaches the end of the string or another <code>&#39;1&#39;</code>. For example, for <code>s = &quot;010010&quot;</code>, if we choose <code>i = 1</code>, the resulting string will be <code>s = &quot;0<strong><u>001</u></strong>10&quot;</code>.</li> </ul> <p>Return the <strong>maximum</strong> number of operations that you can perform.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1001101&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>We can perform the following operations:</p> <ul> <li>Choose index <code>i = 0</code>. The resulting string is <code>s = &quot;<u><strong>001</strong></u>1101&quot;</code>.</li> <li>Choose index <code>i = 4</code>. The resulting string is <code>s = &quot;0011<u><strong>01</strong></u>1&quot;</code>.</li> <li>Choose index <code>i = 3</code>. The resulting string is <code>s = &quot;001<strong><u>01</u></strong>11&quot;</code>.</li> <li>Choose index <code>i = 2</code>. The resulting string is <code>s = &quot;00<strong><u>01</u></strong>111&quot;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;00111&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
Greedy; String; Counting
Go
func maxOperations(s string) (ans int) { cnt := 0 for i, c := range s { if c == '1' { cnt++ } else if i > 0 && s[i-1] == '1' { ans += cnt } } return }
3,228
Maximum Number of Operations to Move Ones to the End
Medium
<p>You are given a <span data-keyword="binary-string">binary string</span> <code>s</code>.</p> <p>You can perform the following operation on the string <strong>any</strong> number of times:</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the string where <code>i + 1 &lt; s.length</code> such that <code>s[i] == &#39;1&#39;</code> and <code>s[i + 1] == &#39;0&#39;</code>.</li> <li>Move the character <code>s[i]</code> to the <strong>right</strong> until it reaches the end of the string or another <code>&#39;1&#39;</code>. For example, for <code>s = &quot;010010&quot;</code>, if we choose <code>i = 1</code>, the resulting string will be <code>s = &quot;0<strong><u>001</u></strong>10&quot;</code>.</li> </ul> <p>Return the <strong>maximum</strong> number of operations that you can perform.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1001101&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>We can perform the following operations:</p> <ul> <li>Choose index <code>i = 0</code>. The resulting string is <code>s = &quot;<u><strong>001</strong></u>1101&quot;</code>.</li> <li>Choose index <code>i = 4</code>. The resulting string is <code>s = &quot;0011<u><strong>01</strong></u>1&quot;</code>.</li> <li>Choose index <code>i = 3</code>. The resulting string is <code>s = &quot;001<strong><u>01</u></strong>11&quot;</code>.</li> <li>Choose index <code>i = 2</code>. The resulting string is <code>s = &quot;00<strong><u>01</u></strong>111&quot;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;00111&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
Greedy; String; Counting
Java
class Solution { public int maxOperations(String s) { int ans = 0, cnt = 0; int n = s.length(); for (int i = 0; i < n; ++i) { if (s.charAt(i) == '1') { ++cnt; } else if (i > 0 && s.charAt(i - 1) == '1') { ans += cnt; } } return ans; } }
3,228
Maximum Number of Operations to Move Ones to the End
Medium
<p>You are given a <span data-keyword="binary-string">binary string</span> <code>s</code>.</p> <p>You can perform the following operation on the string <strong>any</strong> number of times:</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the string where <code>i + 1 &lt; s.length</code> such that <code>s[i] == &#39;1&#39;</code> and <code>s[i + 1] == &#39;0&#39;</code>.</li> <li>Move the character <code>s[i]</code> to the <strong>right</strong> until it reaches the end of the string or another <code>&#39;1&#39;</code>. For example, for <code>s = &quot;010010&quot;</code>, if we choose <code>i = 1</code>, the resulting string will be <code>s = &quot;0<strong><u>001</u></strong>10&quot;</code>.</li> </ul> <p>Return the <strong>maximum</strong> number of operations that you can perform.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1001101&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>We can perform the following operations:</p> <ul> <li>Choose index <code>i = 0</code>. The resulting string is <code>s = &quot;<u><strong>001</strong></u>1101&quot;</code>.</li> <li>Choose index <code>i = 4</code>. The resulting string is <code>s = &quot;0011<u><strong>01</strong></u>1&quot;</code>.</li> <li>Choose index <code>i = 3</code>. The resulting string is <code>s = &quot;001<strong><u>01</u></strong>11&quot;</code>.</li> <li>Choose index <code>i = 2</code>. The resulting string is <code>s = &quot;00<strong><u>01</u></strong>111&quot;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;00111&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
Greedy; String; Counting
Python
class Solution: def maxOperations(self, s: str) -> int: ans = cnt = 0 for i, c in enumerate(s): if c == "1": cnt += 1 elif i and s[i - 1] == "1": ans += cnt return ans
3,228
Maximum Number of Operations to Move Ones to the End
Medium
<p>You are given a <span data-keyword="binary-string">binary string</span> <code>s</code>.</p> <p>You can perform the following operation on the string <strong>any</strong> number of times:</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the string where <code>i + 1 &lt; s.length</code> such that <code>s[i] == &#39;1&#39;</code> and <code>s[i + 1] == &#39;0&#39;</code>.</li> <li>Move the character <code>s[i]</code> to the <strong>right</strong> until it reaches the end of the string or another <code>&#39;1&#39;</code>. For example, for <code>s = &quot;010010&quot;</code>, if we choose <code>i = 1</code>, the resulting string will be <code>s = &quot;0<strong><u>001</u></strong>10&quot;</code>.</li> </ul> <p>Return the <strong>maximum</strong> number of operations that you can perform.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1001101&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>We can perform the following operations:</p> <ul> <li>Choose index <code>i = 0</code>. The resulting string is <code>s = &quot;<u><strong>001</strong></u>1101&quot;</code>.</li> <li>Choose index <code>i = 4</code>. The resulting string is <code>s = &quot;0011<u><strong>01</strong></u>1&quot;</code>.</li> <li>Choose index <code>i = 3</code>. The resulting string is <code>s = &quot;001<strong><u>01</u></strong>11&quot;</code>.</li> <li>Choose index <code>i = 2</code>. The resulting string is <code>s = &quot;00<strong><u>01</u></strong>111&quot;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;00111&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
Greedy; String; Counting
TypeScript
function maxOperations(s: string): number { let [ans, cnt] = [0, 0]; const n = s.length; for (let i = 0; i < n; ++i) { if (s[i] === '1') { ++cnt; } else if (i && s[i - 1] === '1') { ans += cnt; } } return ans; }
3,229
Minimum Operations to Make Array Equal to Target
Hard
<p>You are given two positive integer arrays <code>nums</code> and <code>target</code>, of the same length.</p> <p>In a single operation, you can select any subarray of <code>nums</code> and increment each element within that subarray by 1 or decrement each element within that subarray by 1.</p> <p>Return the <strong>minimum</strong> number of operations required to make <code>nums</code> equal to the array <code>target</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,5,1,2], target = [4,6,2,4]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>We will perform the following operations to make <code>nums</code> equal to <code>target</code>:<br /> - Increment&nbsp;<code>nums[0..3]</code> by 1, <code>nums = [4,6,2,3]</code>.<br /> - Increment&nbsp;<code>nums[3..3]</code> by 1, <code>nums = [4,6,2,4]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,2], target = [2,1,4]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p>We will perform the following operations to make <code>nums</code> equal to <code>target</code>:<br /> - Increment&nbsp;<code>nums[0..0]</code> by 1, <code>nums = [2,3,2]</code>.<br /> - Decrement&nbsp;<code>nums[1..1]</code> by 1, <code>nums = [2,2,2]</code>.<br /> - Decrement&nbsp;<code>nums[1..1]</code> by 1, <code>nums = [2,1,2]</code>.<br /> - Increment&nbsp;<code>nums[2..2]</code> by 1, <code>nums = [2,1,3]</code>.<br /> - Increment&nbsp;<code>nums[2..2]</code> by 1, <code>nums = [2,1,4]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length == target.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i], target[i] &lt;= 10<sup>8</sup></code></li> </ul>
Stack; Greedy; Array; Dynamic Programming; Monotonic Stack
C++
class Solution { public: long long minimumOperations(vector<int>& nums, vector<int>& target) { using ll = long long; ll f = abs(target[0] - nums[0]); for (int i = 1; i < nums.size(); ++i) { long x = target[i] - nums[i]; long y = target[i - 1] - nums[i - 1]; if (x * y > 0) { ll d = abs(x) - abs(y); if (d > 0) { f += d; } } else { f += abs(x); } } return f; } };
3,229
Minimum Operations to Make Array Equal to Target
Hard
<p>You are given two positive integer arrays <code>nums</code> and <code>target</code>, of the same length.</p> <p>In a single operation, you can select any subarray of <code>nums</code> and increment each element within that subarray by 1 or decrement each element within that subarray by 1.</p> <p>Return the <strong>minimum</strong> number of operations required to make <code>nums</code> equal to the array <code>target</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,5,1,2], target = [4,6,2,4]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>We will perform the following operations to make <code>nums</code> equal to <code>target</code>:<br /> - Increment&nbsp;<code>nums[0..3]</code> by 1, <code>nums = [4,6,2,3]</code>.<br /> - Increment&nbsp;<code>nums[3..3]</code> by 1, <code>nums = [4,6,2,4]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,2], target = [2,1,4]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p>We will perform the following operations to make <code>nums</code> equal to <code>target</code>:<br /> - Increment&nbsp;<code>nums[0..0]</code> by 1, <code>nums = [2,3,2]</code>.<br /> - Decrement&nbsp;<code>nums[1..1]</code> by 1, <code>nums = [2,2,2]</code>.<br /> - Decrement&nbsp;<code>nums[1..1]</code> by 1, <code>nums = [2,1,2]</code>.<br /> - Increment&nbsp;<code>nums[2..2]</code> by 1, <code>nums = [2,1,3]</code>.<br /> - Increment&nbsp;<code>nums[2..2]</code> by 1, <code>nums = [2,1,4]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length == target.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i], target[i] &lt;= 10<sup>8</sup></code></li> </ul>
Stack; Greedy; Array; Dynamic Programming; Monotonic Stack
Go
func minimumOperations(nums []int, target []int) int64 { f := abs(target[0] - nums[0]) for i := 1; i < len(target); i++ { x := target[i] - nums[i] y := target[i-1] - nums[i-1] if x*y > 0 { if d := abs(x) - abs(y); d > 0 { f += d } } else { f += abs(x) } } return int64(f) } func abs(x int) int { if x < 0 { return -x } return x }
3,229
Minimum Operations to Make Array Equal to Target
Hard
<p>You are given two positive integer arrays <code>nums</code> and <code>target</code>, of the same length.</p> <p>In a single operation, you can select any subarray of <code>nums</code> and increment each element within that subarray by 1 or decrement each element within that subarray by 1.</p> <p>Return the <strong>minimum</strong> number of operations required to make <code>nums</code> equal to the array <code>target</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,5,1,2], target = [4,6,2,4]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>We will perform the following operations to make <code>nums</code> equal to <code>target</code>:<br /> - Increment&nbsp;<code>nums[0..3]</code> by 1, <code>nums = [4,6,2,3]</code>.<br /> - Increment&nbsp;<code>nums[3..3]</code> by 1, <code>nums = [4,6,2,4]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,2], target = [2,1,4]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p>We will perform the following operations to make <code>nums</code> equal to <code>target</code>:<br /> - Increment&nbsp;<code>nums[0..0]</code> by 1, <code>nums = [2,3,2]</code>.<br /> - Decrement&nbsp;<code>nums[1..1]</code> by 1, <code>nums = [2,2,2]</code>.<br /> - Decrement&nbsp;<code>nums[1..1]</code> by 1, <code>nums = [2,1,2]</code>.<br /> - Increment&nbsp;<code>nums[2..2]</code> by 1, <code>nums = [2,1,3]</code>.<br /> - Increment&nbsp;<code>nums[2..2]</code> by 1, <code>nums = [2,1,4]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length == target.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i], target[i] &lt;= 10<sup>8</sup></code></li> </ul>
Stack; Greedy; Array; Dynamic Programming; Monotonic Stack
Java
class Solution { public long minimumOperations(int[] nums, int[] target) { long f = Math.abs(target[0] - nums[0]); for (int i = 1; i < nums.length; ++i) { long x = target[i] - nums[i]; long y = target[i - 1] - nums[i - 1]; if (x * y > 0) { long d = Math.abs(x) - Math.abs(y); if (d > 0) { f += d; } } else { f += Math.abs(x); } } return f; } }
3,229
Minimum Operations to Make Array Equal to Target
Hard
<p>You are given two positive integer arrays <code>nums</code> and <code>target</code>, of the same length.</p> <p>In a single operation, you can select any subarray of <code>nums</code> and increment each element within that subarray by 1 or decrement each element within that subarray by 1.</p> <p>Return the <strong>minimum</strong> number of operations required to make <code>nums</code> equal to the array <code>target</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,5,1,2], target = [4,6,2,4]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>We will perform the following operations to make <code>nums</code> equal to <code>target</code>:<br /> - Increment&nbsp;<code>nums[0..3]</code> by 1, <code>nums = [4,6,2,3]</code>.<br /> - Increment&nbsp;<code>nums[3..3]</code> by 1, <code>nums = [4,6,2,4]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,2], target = [2,1,4]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p>We will perform the following operations to make <code>nums</code> equal to <code>target</code>:<br /> - Increment&nbsp;<code>nums[0..0]</code> by 1, <code>nums = [2,3,2]</code>.<br /> - Decrement&nbsp;<code>nums[1..1]</code> by 1, <code>nums = [2,2,2]</code>.<br /> - Decrement&nbsp;<code>nums[1..1]</code> by 1, <code>nums = [2,1,2]</code>.<br /> - Increment&nbsp;<code>nums[2..2]</code> by 1, <code>nums = [2,1,3]</code>.<br /> - Increment&nbsp;<code>nums[2..2]</code> by 1, <code>nums = [2,1,4]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length == target.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i], target[i] &lt;= 10<sup>8</sup></code></li> </ul>
Stack; Greedy; Array; Dynamic Programming; Monotonic Stack
Python
class Solution: def minimumOperations(self, nums: List[int], target: List[int]) -> int: n = len(nums) f = abs(target[0] - nums[0]) for i in range(1, n): x = target[i] - nums[i] y = target[i - 1] - nums[i - 1] if x * y > 0: d = abs(x) - abs(y) if d > 0: f += d else: f += abs(x) return f
3,229
Minimum Operations to Make Array Equal to Target
Hard
<p>You are given two positive integer arrays <code>nums</code> and <code>target</code>, of the same length.</p> <p>In a single operation, you can select any subarray of <code>nums</code> and increment each element within that subarray by 1 or decrement each element within that subarray by 1.</p> <p>Return the <strong>minimum</strong> number of operations required to make <code>nums</code> equal to the array <code>target</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,5,1,2], target = [4,6,2,4]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>We will perform the following operations to make <code>nums</code> equal to <code>target</code>:<br /> - Increment&nbsp;<code>nums[0..3]</code> by 1, <code>nums = [4,6,2,3]</code>.<br /> - Increment&nbsp;<code>nums[3..3]</code> by 1, <code>nums = [4,6,2,4]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,2], target = [2,1,4]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p>We will perform the following operations to make <code>nums</code> equal to <code>target</code>:<br /> - Increment&nbsp;<code>nums[0..0]</code> by 1, <code>nums = [2,3,2]</code>.<br /> - Decrement&nbsp;<code>nums[1..1]</code> by 1, <code>nums = [2,2,2]</code>.<br /> - Decrement&nbsp;<code>nums[1..1]</code> by 1, <code>nums = [2,1,2]</code>.<br /> - Increment&nbsp;<code>nums[2..2]</code> by 1, <code>nums = [2,1,3]</code>.<br /> - Increment&nbsp;<code>nums[2..2]</code> by 1, <code>nums = [2,1,4]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length == target.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i], target[i] &lt;= 10<sup>8</sup></code></li> </ul>
Stack; Greedy; Array; Dynamic Programming; Monotonic Stack
TypeScript
function minimumOperations(nums: number[], target: number[]): number { const n = nums.length; let f = Math.abs(target[0] - nums[0]); for (let i = 1; i < n; ++i) { const x = target[i] - nums[i]; const y = target[i - 1] - nums[i - 1]; if (x * y > 0) { const d = Math.abs(x) - Math.abs(y); if (d > 0) { f += d; } } else { f += Math.abs(x); } } return f; }
3,230
Customer Purchasing Behavior Analysis
Medium
<p>Table: <code>Transactions</code></p> <pre> +------------------+---------+ | Column Name | Type | +------------------+---------+ | transaction_id | int | | customer_id | int | | product_id | int | | transaction_date | date | | amount | decimal | +------------------+---------+ transaction_id is the unique identifier for this table. Each row of this table contains information about a transaction, including the customer ID, product ID, date, and amount spent. </pre> <p>Table: <code>Products</code></p> <pre> +-------------+---------+ | Column Name | Type | +-------------+---------+ | product_id | int | | category | varchar | | price | decimal | +-------------+---------+ product_id is the unique identifier for this table. Each row of this table contains information about a product, including its category and price. </pre> <p>Write a solution to analyze customer purchasing behavior. For <strong>each customer</strong>, calculate:</p> <ul> <li>The total amount spent.</li> <li>The number of transactions.</li> <li>The number of <strong>unique</strong> product categories purchased.</li> <li>The average amount spent.&nbsp;</li> <li>The <strong>most frequently</strong> purchased product category&nbsp;(if there is a tie, choose the one with the most recent transaction).</li> <li>A <strong>loyalty score</strong>&nbsp;defined as: (Number of transactions * 10) + (Total amount spent / 100).</li> </ul> <p>Round <code>total_amount</code>, <code>avg_transaction_amount</code>, and <code>loyalty_score</code> to <code>2</code> decimal places.</p> <p>Return <em>the result table ordered by</em> <code>loyalty_score</code> <em>in <strong>descending</strong> order</em>, <em>then by </em><code>customer_id</code><em> in <strong>ascending</strong> order</em>.</p> <p>The query result format is in the following example.</p> <p>&nbsp;</p> <p><strong class="example">Example:</strong></p> <div class="example-block"> <p><strong>Input:</strong></p> <p><code>Transactions</code> table:</p> <pre class="example-io"> +----------------+-------------+------------+------------------+--------+ | transaction_id | customer_id | product_id | transaction_date | amount | +----------------+-------------+------------+------------------+--------+ | 1 | 101 | 1 | 2023-01-01 | 100.00 | | 2 | 101 | 2 | 2023-01-15 | 150.00 | | 3 | 102 | 1 | 2023-01-01 | 100.00 | | 4 | 102 | 3 | 2023-01-22 | 200.00 | | 5 | 101 | 3 | 2023-02-10 | 200.00 | +----------------+-------------+------------+------------------+--------+ </pre> <p><code>Products</code> table:</p> <pre class="example-io"> +------------+----------+--------+ | product_id | category | price | +------------+----------+--------+ | 1 | A | 100.00 | | 2 | B | 150.00 | | 3 | C | 200.00 | +------------+----------+--------+ </pre> <p><strong>Output:</strong></p> <pre class="example-io"> +-------------+--------------+-------------------+-------------------+------------------------+--------------+---------------+ | customer_id | total_amount | transaction_count | unique_categories | avg_transaction_amount | top_category | loyalty_score | +-------------+--------------+-------------------+-------------------+------------------------+--------------+---------------+ | 101 | 450.00 | 3 | 3 | 150.00 | C | 34.50 | | 102 | 300.00 | 2 | 2 | 150.00 | C | 23.00 | +-------------+--------------+-------------------+-------------------+------------------------+--------------+---------------+ </pre> <p><strong>Explanation:</strong></p> <ul> <li>For customer 101: <ul> <li>Total amount spent: 100.00 + 150.00 + 200.00 = 450.00</li> <li>Number of transactions: 3</li> <li>Unique categories: A, B, C (3 categories)</li> <li>Average transaction amount: 450.00 / 3 = 150.00</li> <li>Top category: C (Customer 101 made 1 purchase each in categories A, B, and C. Since the count is the same for all categories, we choose the most recent transaction, which is category C on 2023-02-10)</li> <li>Loyalty score: (3 * 10) + (450.00 / 100) = 34.50</li> </ul> </li> <li>For customer 102: <ul> <li>Total amount spent: 100.00 + 200.00 = 300.00</li> <li>Number of transactions: 2</li> <li>Unique categories: A, C (2 categories)</li> <li>Average transaction amount: 300.00 / 2 = 150.00</li> <li>Top category: C (Customer 102 made 1 purchase each in categories A and C. Since the count is the same for both categories, we choose the most recent transaction, which is category C on 2023-01-22)</li> <li>Loyalty score: (2 * 10) + (300.00 / 100) = 23.00</li> </ul> </li> </ul> <p><strong>Note:</strong> The output is ordered by loyalty_score in descending order, then by customer_id in ascending order.</p> </div>
Database
SQL
# Write your MySQL query statement below WITH T AS ( SELECT * FROM Transactions JOIN Products USING (product_id) ), P AS ( SELECT customer_id, category, COUNT(1) cnt, MAX(transaction_date) max_date FROM T GROUP BY 1, 2 ), R AS ( SELECT customer_id, category, RANK() OVER ( PARTITION BY customer_id ORDER BY cnt DESC, max_date DESC ) rk FROM P ) SELECT t.customer_id, ROUND(SUM(amount), 2) total_amount, COUNT(1) transaction_count, COUNT(DISTINCT t.category) unique_categories, ROUND(AVG(amount), 2) avg_transaction_amount, r.category top_category, ROUND(COUNT(1) * 10 + SUM(amount) / 100, 2) loyalty_score FROM T t JOIN R r ON t.customer_id = r.customer_id AND r.rk = 1 GROUP BY 1 ORDER BY 7 DESC, 1;
3,231
Minimum Number of Increasing Subsequence to Be Removed
Hard
<p>Given an array of integers <code>nums</code>, you are allowed to perform the following operation any number of times:</p> <ul> <li>Remove a <strong>strictly increasing</strong> <span data-keyword="subsequence-array">subsequence</span> from the array.</li> </ul> <p>Your task is to find the <strong>minimum</strong> number of operations required to make the array <strong>empty</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [5,3,1,4,2]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>We remove subsequences <code>[1, 2]</code>, <code>[3, 4]</code>, <code>[5]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [5,4,3,2,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> </ul>
Array; Binary Search
C++
class Solution { public: int minOperations(vector<int>& nums) { vector<int> g; for (int x : nums) { int l = 0, r = g.size(); while (l < r) { int mid = (l + r) >> 1; if (g[mid] < x) { r = mid; } else { l = mid + 1; } } if (l == g.size()) { g.push_back(x); } else { g[l] = x; } } return g.size(); } };
3,231
Minimum Number of Increasing Subsequence to Be Removed
Hard
<p>Given an array of integers <code>nums</code>, you are allowed to perform the following operation any number of times:</p> <ul> <li>Remove a <strong>strictly increasing</strong> <span data-keyword="subsequence-array">subsequence</span> from the array.</li> </ul> <p>Your task is to find the <strong>minimum</strong> number of operations required to make the array <strong>empty</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [5,3,1,4,2]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>We remove subsequences <code>[1, 2]</code>, <code>[3, 4]</code>, <code>[5]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [5,4,3,2,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> </ul>
Array; Binary Search
Go
func minOperations(nums []int) int { g := []int{} for _, x := range nums { l, r := 0, len(g) for l < r { mid := (l + r) >> 1 if g[mid] < x { r = mid } else { l = mid + 1 } } if l == len(g) { g = append(g, x) } else { g[l] = x } } return len(g) }
3,231
Minimum Number of Increasing Subsequence to Be Removed
Hard
<p>Given an array of integers <code>nums</code>, you are allowed to perform the following operation any number of times:</p> <ul> <li>Remove a <strong>strictly increasing</strong> <span data-keyword="subsequence-array">subsequence</span> from the array.</li> </ul> <p>Your task is to find the <strong>minimum</strong> number of operations required to make the array <strong>empty</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [5,3,1,4,2]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>We remove subsequences <code>[1, 2]</code>, <code>[3, 4]</code>, <code>[5]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [5,4,3,2,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> </ul>
Array; Binary Search
Java
class Solution { public int minOperations(int[] nums) { List<Integer> g = new ArrayList<>(); for (int x : nums) { int l = 0, r = g.size(); while (l < r) { int mid = (l + r) >> 1; if (g.get(mid) < x) { r = mid; } else { l = mid + 1; } } if (l == g.size()) { g.add(x); } else { g.set(l, x); } } return g.size(); } }
3,231
Minimum Number of Increasing Subsequence to Be Removed
Hard
<p>Given an array of integers <code>nums</code>, you are allowed to perform the following operation any number of times:</p> <ul> <li>Remove a <strong>strictly increasing</strong> <span data-keyword="subsequence-array">subsequence</span> from the array.</li> </ul> <p>Your task is to find the <strong>minimum</strong> number of operations required to make the array <strong>empty</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [5,3,1,4,2]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>We remove subsequences <code>[1, 2]</code>, <code>[3, 4]</code>, <code>[5]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [5,4,3,2,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> </ul>
Array; Binary Search
Python
class Solution: def minOperations(self, nums: List[int]) -> int: g = [] for x in nums: l, r = 0, len(g) while l < r: mid = (l + r) >> 1 if g[mid] < x: r = mid else: l = mid + 1 if l == len(g): g.append(x) else: g[l] = x return len(g)
3,231
Minimum Number of Increasing Subsequence to Be Removed
Hard
<p>Given an array of integers <code>nums</code>, you are allowed to perform the following operation any number of times:</p> <ul> <li>Remove a <strong>strictly increasing</strong> <span data-keyword="subsequence-array">subsequence</span> from the array.</li> </ul> <p>Your task is to find the <strong>minimum</strong> number of operations required to make the array <strong>empty</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [5,3,1,4,2]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>We remove subsequences <code>[1, 2]</code>, <code>[3, 4]</code>, <code>[5]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [5,4,3,2,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> </ul>
Array; Binary Search
Rust
impl Solution { pub fn min_operations(nums: Vec<i32>) -> i32 { let mut g = Vec::new(); for &x in nums.iter() { let mut l = 0; let mut r = g.len(); while l < r { let mid = (l + r) / 2; if g[mid] < x { r = mid; } else { l = mid + 1; } } if l == g.len() { g.push(x); } else { g[l] = x; } } g.len() as i32 } }
3,231
Minimum Number of Increasing Subsequence to Be Removed
Hard
<p>Given an array of integers <code>nums</code>, you are allowed to perform the following operation any number of times:</p> <ul> <li>Remove a <strong>strictly increasing</strong> <span data-keyword="subsequence-array">subsequence</span> from the array.</li> </ul> <p>Your task is to find the <strong>minimum</strong> number of operations required to make the array <strong>empty</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [5,3,1,4,2]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>We remove subsequences <code>[1, 2]</code>, <code>[3, 4]</code>, <code>[5]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [5,4,3,2,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> </ul>
Array; Binary Search
TypeScript
function minOperations(nums: number[]): number { const g: number[] = []; for (const x of nums) { let [l, r] = [0, g.length]; while (l < r) { const mid = (l + r) >> 1; if (g[mid] < x) { r = mid; } else { l = mid + 1; } } if (l === g.length) { g.push(x); } else { g[l] = x; } } return g.length; }
3,232
Find if Digit Game Can Be Won
Easy
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Alice and Bob are playing a game. In the game, Alice can choose <strong>either</strong> all single-digit numbers or all double-digit numbers from <code>nums</code>, and the rest of the numbers are given to Bob. Alice wins if the sum of her numbers is <strong>strictly greater</strong> than the sum of Bob&#39;s numbers.</p> <p>Return <code>true</code> if Alice can win this game, otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,10]</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Alice cannot win by choosing either single-digit or double-digit numbers.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,14]</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Alice can win by choosing single-digit numbers which have a sum equal to 15.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [5,5,5,25]</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Alice can win by choosing double-digit numbers which have a sum equal to 25.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 100</code></li> <li><code>1 &lt;= nums[i] &lt;= 99</code></li> </ul>
Array; Math
C++
class Solution { public: bool canAliceWin(vector<int>& nums) { int a = 0, b = 0; for (int x : nums) { if (x < 10) { a += x; } else { b += x; } } return a != b; } };
3,232
Find if Digit Game Can Be Won
Easy
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Alice and Bob are playing a game. In the game, Alice can choose <strong>either</strong> all single-digit numbers or all double-digit numbers from <code>nums</code>, and the rest of the numbers are given to Bob. Alice wins if the sum of her numbers is <strong>strictly greater</strong> than the sum of Bob&#39;s numbers.</p> <p>Return <code>true</code> if Alice can win this game, otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,10]</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Alice cannot win by choosing either single-digit or double-digit numbers.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,14]</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Alice can win by choosing single-digit numbers which have a sum equal to 15.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [5,5,5,25]</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Alice can win by choosing double-digit numbers which have a sum equal to 25.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 100</code></li> <li><code>1 &lt;= nums[i] &lt;= 99</code></li> </ul>
Array; Math
Go
func canAliceWin(nums []int) bool { a, b := 0, 0 for _, x := range nums { if x < 10 { a += x } else { b += x } } return a != b }
3,232
Find if Digit Game Can Be Won
Easy
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Alice and Bob are playing a game. In the game, Alice can choose <strong>either</strong> all single-digit numbers or all double-digit numbers from <code>nums</code>, and the rest of the numbers are given to Bob. Alice wins if the sum of her numbers is <strong>strictly greater</strong> than the sum of Bob&#39;s numbers.</p> <p>Return <code>true</code> if Alice can win this game, otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,10]</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Alice cannot win by choosing either single-digit or double-digit numbers.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,14]</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Alice can win by choosing single-digit numbers which have a sum equal to 15.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [5,5,5,25]</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Alice can win by choosing double-digit numbers which have a sum equal to 25.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 100</code></li> <li><code>1 &lt;= nums[i] &lt;= 99</code></li> </ul>
Array; Math
Java
class Solution { public boolean canAliceWin(int[] nums) { int a = 0, b = 0; for (int x : nums) { if (x < 10) { a += x; } else { b += x; } } return a != b; } }
3,232
Find if Digit Game Can Be Won
Easy
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Alice and Bob are playing a game. In the game, Alice can choose <strong>either</strong> all single-digit numbers or all double-digit numbers from <code>nums</code>, and the rest of the numbers are given to Bob. Alice wins if the sum of her numbers is <strong>strictly greater</strong> than the sum of Bob&#39;s numbers.</p> <p>Return <code>true</code> if Alice can win this game, otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,10]</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Alice cannot win by choosing either single-digit or double-digit numbers.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,14]</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Alice can win by choosing single-digit numbers which have a sum equal to 15.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [5,5,5,25]</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Alice can win by choosing double-digit numbers which have a sum equal to 25.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 100</code></li> <li><code>1 &lt;= nums[i] &lt;= 99</code></li> </ul>
Array; Math
Python
class Solution: def canAliceWin(self, nums: List[int]) -> bool: a = sum(x for x in nums if x < 10) b = sum(x for x in nums if x > 9) return a != b
3,232
Find if Digit Game Can Be Won
Easy
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Alice and Bob are playing a game. In the game, Alice can choose <strong>either</strong> all single-digit numbers or all double-digit numbers from <code>nums</code>, and the rest of the numbers are given to Bob. Alice wins if the sum of her numbers is <strong>strictly greater</strong> than the sum of Bob&#39;s numbers.</p> <p>Return <code>true</code> if Alice can win this game, otherwise, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,10]</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Alice cannot win by choosing either single-digit or double-digit numbers.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,14]</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Alice can win by choosing single-digit numbers which have a sum equal to 15.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [5,5,5,25]</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Alice can win by choosing double-digit numbers which have a sum equal to 25.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 100</code></li> <li><code>1 &lt;= nums[i] &lt;= 99</code></li> </ul>
Array; Math
TypeScript
function canAliceWin(nums: number[]): boolean { let [a, b] = [0, 0]; for (const x of nums) { if (x < 10) { a += x; } else { b += x; } } return a !== b; }
3,233
Find the Count of Numbers Which Are Not Special
Medium
<p>You are given 2 <strong>positive</strong> integers <code>l</code> and <code>r</code>. For any number <code>x</code>, all positive divisors of <code>x</code> <em>except</em> <code>x</code> are called the <strong>proper divisors</strong> of <code>x</code>.</p> <p>A number is called <strong>special</strong> if it has exactly 2 <strong>proper divisors</strong>. For example:</p> <ul> <li>The number 4 is <em>special</em> because it has proper divisors 1 and 2.</li> <li>The number 6 is <em>not special</em> because it has proper divisors 1, 2, and 3.</li> </ul> <p>Return the count of numbers in the range <code>[l, r]</code> that are <strong>not</strong> <strong>special</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">l = 5, r = 7</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>There are no special numbers in the range <code>[5, 7]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">l = 4, r = 16</span></p> <p><strong>Output:</strong> <span class="example-io">11</span></p> <p><strong>Explanation:</strong></p> <p>The special numbers in the range <code>[4, 16]</code> are 4 and 9.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= l &lt;= r &lt;= 10<sup>9</sup></code></li> </ul>
Array; Math; Number Theory
C++
const int m = 31623; bool primes[m + 1]; auto init = [] { memset(primes, true, sizeof(primes)); primes[0] = primes[1] = false; for (int i = 2; i <= m; ++i) { if (primes[i]) { for (int j = i * 2; j <= m; j += i) { primes[j] = false; } } } return 0; }(); class Solution { public: int nonSpecialCount(int l, int r) { int lo = ceil(sqrt(l)); int hi = floor(sqrt(r)); int cnt = 0; for (int i = lo; i <= hi; ++i) { if (primes[i]) { ++cnt; } } return r - l + 1 - cnt; } };
3,233
Find the Count of Numbers Which Are Not Special
Medium
<p>You are given 2 <strong>positive</strong> integers <code>l</code> and <code>r</code>. For any number <code>x</code>, all positive divisors of <code>x</code> <em>except</em> <code>x</code> are called the <strong>proper divisors</strong> of <code>x</code>.</p> <p>A number is called <strong>special</strong> if it has exactly 2 <strong>proper divisors</strong>. For example:</p> <ul> <li>The number 4 is <em>special</em> because it has proper divisors 1 and 2.</li> <li>The number 6 is <em>not special</em> because it has proper divisors 1, 2, and 3.</li> </ul> <p>Return the count of numbers in the range <code>[l, r]</code> that are <strong>not</strong> <strong>special</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">l = 5, r = 7</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>There are no special numbers in the range <code>[5, 7]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">l = 4, r = 16</span></p> <p><strong>Output:</strong> <span class="example-io">11</span></p> <p><strong>Explanation:</strong></p> <p>The special numbers in the range <code>[4, 16]</code> are 4 and 9.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= l &lt;= r &lt;= 10<sup>9</sup></code></li> </ul>
Array; Math; Number Theory
Go
const m = 31623 var primes [m + 1]bool func init() { for i := range primes { primes[i] = true } primes[0] = false primes[1] = false for i := 2; i <= m; i++ { if primes[i] { for j := i * 2; j <= m; j += i { primes[j] = false } } } } func nonSpecialCount(l int, r int) int { lo := int(math.Ceil(math.Sqrt(float64(l)))) hi := int(math.Floor(math.Sqrt(float64(r)))) cnt := 0 for i := lo; i <= hi; i++ { if primes[i] { cnt++ } } return r - l + 1 - cnt }
3,233
Find the Count of Numbers Which Are Not Special
Medium
<p>You are given 2 <strong>positive</strong> integers <code>l</code> and <code>r</code>. For any number <code>x</code>, all positive divisors of <code>x</code> <em>except</em> <code>x</code> are called the <strong>proper divisors</strong> of <code>x</code>.</p> <p>A number is called <strong>special</strong> if it has exactly 2 <strong>proper divisors</strong>. For example:</p> <ul> <li>The number 4 is <em>special</em> because it has proper divisors 1 and 2.</li> <li>The number 6 is <em>not special</em> because it has proper divisors 1, 2, and 3.</li> </ul> <p>Return the count of numbers in the range <code>[l, r]</code> that are <strong>not</strong> <strong>special</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">l = 5, r = 7</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>There are no special numbers in the range <code>[5, 7]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">l = 4, r = 16</span></p> <p><strong>Output:</strong> <span class="example-io">11</span></p> <p><strong>Explanation:</strong></p> <p>The special numbers in the range <code>[4, 16]</code> are 4 and 9.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= l &lt;= r &lt;= 10<sup>9</sup></code></li> </ul>
Array; Math; Number Theory
Java
class Solution { static int m = 31623; static boolean[] primes = new boolean[m + 1]; static { Arrays.fill(primes, true); primes[0] = primes[1] = false; for (int i = 2; i <= m; i++) { if (primes[i]) { for (int j = i + i; j <= m; j += i) { primes[j] = false; } } } } public int nonSpecialCount(int l, int r) { int lo = (int) Math.ceil(Math.sqrt(l)); int hi = (int) Math.floor(Math.sqrt(r)); int cnt = 0; for (int i = lo; i <= hi; i++) { if (primes[i]) { cnt++; } } return r - l + 1 - cnt; } }
3,233
Find the Count of Numbers Which Are Not Special
Medium
<p>You are given 2 <strong>positive</strong> integers <code>l</code> and <code>r</code>. For any number <code>x</code>, all positive divisors of <code>x</code> <em>except</em> <code>x</code> are called the <strong>proper divisors</strong> of <code>x</code>.</p> <p>A number is called <strong>special</strong> if it has exactly 2 <strong>proper divisors</strong>. For example:</p> <ul> <li>The number 4 is <em>special</em> because it has proper divisors 1 and 2.</li> <li>The number 6 is <em>not special</em> because it has proper divisors 1, 2, and 3.</li> </ul> <p>Return the count of numbers in the range <code>[l, r]</code> that are <strong>not</strong> <strong>special</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">l = 5, r = 7</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>There are no special numbers in the range <code>[5, 7]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">l = 4, r = 16</span></p> <p><strong>Output:</strong> <span class="example-io">11</span></p> <p><strong>Explanation:</strong></p> <p>The special numbers in the range <code>[4, 16]</code> are 4 and 9.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= l &lt;= r &lt;= 10<sup>9</sup></code></li> </ul>
Array; Math; Number Theory
Python
m = 31623 primes = [True] * (m + 1) primes[0] = primes[1] = False for i in range(2, m + 1): if primes[i]: for j in range(i + i, m + 1, i): primes[j] = False class Solution: def nonSpecialCount(self, l: int, r: int) -> int: lo = ceil(sqrt(l)) hi = floor(sqrt(r)) cnt = sum(primes[i] for i in range(lo, hi + 1)) return r - l + 1 - cnt
3,233
Find the Count of Numbers Which Are Not Special
Medium
<p>You are given 2 <strong>positive</strong> integers <code>l</code> and <code>r</code>. For any number <code>x</code>, all positive divisors of <code>x</code> <em>except</em> <code>x</code> are called the <strong>proper divisors</strong> of <code>x</code>.</p> <p>A number is called <strong>special</strong> if it has exactly 2 <strong>proper divisors</strong>. For example:</p> <ul> <li>The number 4 is <em>special</em> because it has proper divisors 1 and 2.</li> <li>The number 6 is <em>not special</em> because it has proper divisors 1, 2, and 3.</li> </ul> <p>Return the count of numbers in the range <code>[l, r]</code> that are <strong>not</strong> <strong>special</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">l = 5, r = 7</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>There are no special numbers in the range <code>[5, 7]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">l = 4, r = 16</span></p> <p><strong>Output:</strong> <span class="example-io">11</span></p> <p><strong>Explanation:</strong></p> <p>The special numbers in the range <code>[4, 16]</code> are 4 and 9.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= l &lt;= r &lt;= 10<sup>9</sup></code></li> </ul>
Array; Math; Number Theory
TypeScript
const m = 31623; const primes: boolean[] = Array(m + 1).fill(true); (() => { primes[0] = primes[1] = false; for (let i = 2; i <= m; ++i) { if (primes[i]) { for (let j = i * 2; j <= m; j += i) { primes[j] = false; } } } })(); function nonSpecialCount(l: number, r: number): number { const lo = Math.ceil(Math.sqrt(l)); const hi = Math.floor(Math.sqrt(r)); let cnt = 0; for (let i = lo; i <= hi; ++i) { if (primes[i]) { ++cnt; } } return r - l + 1 - cnt; }
3,235
Check if the Rectangle Corner Is Reachable
Hard
<p>You are given two positive integers <code>xCorner</code> and <code>yCorner</code>, and a 2D array <code>circles</code>, where <code>circles[i] = [x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub>]</code> denotes a circle with center at <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and radius <code>r<sub>i</sub></code>.</p> <p>There is a rectangle in the coordinate plane with its bottom left corner at the origin and top right corner at the coordinate <code>(xCorner, yCorner)</code>. You need to check whether there is a path from the bottom left corner to the top right corner such that the <strong>entire path</strong> lies inside the rectangle, <strong>does not</strong> touch or lie inside <strong>any</strong> circle, and touches the rectangle <strong>only</strong> at the two corners.</p> <p>Return <code>true</code> if such a path exists, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 3, yCorner = 4, circles = [[2,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/example2circle1.png" style="width: 346px; height: 264px;" /></p> <p>The black curve shows a possible path between <code>(0, 0)</code> and <code>(3, 4)</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 3, yCorner = 3, circles = [[1,1,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/example1circle.png" style="width: 346px; height: 264px;" /></p> <p>No path exists from <code>(0, 0)</code> to <code>(3, 3)</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 3, yCorner = 3, circles = [[2,1,1],[1,2,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/example0circle.png" style="width: 346px; height: 264px;" /></p> <p>No path exists from <code>(0, 0)</code> to <code>(3, 3)</code>.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 4, yCorner = 4, circles = [[5,5,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/rectangles.png" style="width: 346px; height: 264px;" /></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= xCorner, yCorner &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= circles.length &lt;= 1000</code></li> <li><code>circles[i].length == 3</code></li> <li><code>1 &lt;= x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
Depth-First Search; Breadth-First Search; Union Find; Geometry; Array; Math
C++
class Solution { public: bool canReachCorner(int xCorner, int yCorner, vector<vector<int>>& circles) { using ll = long long; auto inCircle = [&](ll x, ll y, ll cx, ll cy, ll r) { return (x - cx) * (x - cx) + (y - cy) * (y - cy) <= r * r; }; auto crossLeftTop = [&](ll cx, ll cy, ll r) { bool a = abs(cx) <= r && (cy >= 0 && cy <= yCorner); bool b = abs(cy - yCorner) <= r && (cx >= 0 && cx <= xCorner); return a || b; }; auto crossRightBottom = [&](ll cx, ll cy, ll r) { bool a = abs(cx - xCorner) <= r && (cy >= 0 && cy <= yCorner); bool b = abs(cy) <= r && (cx >= 0 && cx <= xCorner); return a || b; }; int n = circles.size(); vector<bool> vis(n); auto dfs = [&](this auto&& dfs, int i) -> bool { auto c = circles[i]; ll x1 = c[0], y1 = c[1], r1 = c[2]; if (crossRightBottom(x1, y1, r1)) { return true; } vis[i] = true; for (int j = 0; j < n; ++j) { if (vis[j]) { continue; } auto c2 = circles[j]; ll x2 = c2[0], y2 = c2[1], r2 = c2[2]; if ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) > (r1 + r2) * (r1 + r2)) { continue; } if (x1 * r2 + x2 * r1 < (r1 + r2) * xCorner && y1 * r2 + y2 * r1 < (r1 + r2) * yCorner && dfs(j)) { return true; } } return false; }; for (int i = 0; i < n; ++i) { auto c = circles[i]; ll x = c[0], y = c[1], r = c[2]; if (inCircle(0, 0, x, y, r) || inCircle(xCorner, yCorner, x, y, r)) { return false; } if (!vis[i] && crossLeftTop(x, y, r) && dfs(i)) { return false; } } return true; } };
3,235
Check if the Rectangle Corner Is Reachable
Hard
<p>You are given two positive integers <code>xCorner</code> and <code>yCorner</code>, and a 2D array <code>circles</code>, where <code>circles[i] = [x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub>]</code> denotes a circle with center at <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and radius <code>r<sub>i</sub></code>.</p> <p>There is a rectangle in the coordinate plane with its bottom left corner at the origin and top right corner at the coordinate <code>(xCorner, yCorner)</code>. You need to check whether there is a path from the bottom left corner to the top right corner such that the <strong>entire path</strong> lies inside the rectangle, <strong>does not</strong> touch or lie inside <strong>any</strong> circle, and touches the rectangle <strong>only</strong> at the two corners.</p> <p>Return <code>true</code> if such a path exists, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 3, yCorner = 4, circles = [[2,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/example2circle1.png" style="width: 346px; height: 264px;" /></p> <p>The black curve shows a possible path between <code>(0, 0)</code> and <code>(3, 4)</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 3, yCorner = 3, circles = [[1,1,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/example1circle.png" style="width: 346px; height: 264px;" /></p> <p>No path exists from <code>(0, 0)</code> to <code>(3, 3)</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 3, yCorner = 3, circles = [[2,1,1],[1,2,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/example0circle.png" style="width: 346px; height: 264px;" /></p> <p>No path exists from <code>(0, 0)</code> to <code>(3, 3)</code>.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 4, yCorner = 4, circles = [[5,5,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/rectangles.png" style="width: 346px; height: 264px;" /></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= xCorner, yCorner &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= circles.length &lt;= 1000</code></li> <li><code>circles[i].length == 3</code></li> <li><code>1 &lt;= x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
Depth-First Search; Breadth-First Search; Union Find; Geometry; Array; Math
Go
func canReachCorner(xCorner int, yCorner int, circles [][]int) bool { inCircle := func(x, y, cx, cy, r int) bool { dx, dy := x-cx, y-cy return dx*dx+dy*dy <= r*r } crossLeftTop := func(cx, cy, r int) bool { a := abs(cx) <= r && cy >= 0 && cy <= yCorner b := abs(cy-yCorner) <= r && cx >= 0 && cx <= xCorner return a || b } crossRightBottom := func(cx, cy, r int) bool { a := abs(cx-xCorner) <= r && cy >= 0 && cy <= yCorner b := abs(cy) <= r && cx >= 0 && cx <= xCorner return a || b } vis := make([]bool, len(circles)) var dfs func(int) bool dfs = func(i int) bool { c := circles[i] x1, y1, r1 := c[0], c[1], c[2] if crossRightBottom(x1, y1, r1) { return true } vis[i] = true for j, c2 := range circles { if vis[j] { continue } x2, y2, r2 := c2[0], c2[1], c2[2] if (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2) > (r1+r2)*(r1+r2) { continue } if x1*r2+x2*r1 < (r1+r2)*xCorner && y1*r2+y2*r1 < (r1+r2)*yCorner && dfs(j) { return true } } return false } for i, c := range circles { x, y, r := c[0], c[1], c[2] if inCircle(0, 0, x, y, r) || inCircle(xCorner, yCorner, x, y, r) { return false } if !vis[i] && crossLeftTop(x, y, r) && dfs(i) { return false } } return true } func abs(x int) int { if x < 0 { return -x } return x }
3,235
Check if the Rectangle Corner Is Reachable
Hard
<p>You are given two positive integers <code>xCorner</code> and <code>yCorner</code>, and a 2D array <code>circles</code>, where <code>circles[i] = [x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub>]</code> denotes a circle with center at <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and radius <code>r<sub>i</sub></code>.</p> <p>There is a rectangle in the coordinate plane with its bottom left corner at the origin and top right corner at the coordinate <code>(xCorner, yCorner)</code>. You need to check whether there is a path from the bottom left corner to the top right corner such that the <strong>entire path</strong> lies inside the rectangle, <strong>does not</strong> touch or lie inside <strong>any</strong> circle, and touches the rectangle <strong>only</strong> at the two corners.</p> <p>Return <code>true</code> if such a path exists, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 3, yCorner = 4, circles = [[2,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/example2circle1.png" style="width: 346px; height: 264px;" /></p> <p>The black curve shows a possible path between <code>(0, 0)</code> and <code>(3, 4)</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 3, yCorner = 3, circles = [[1,1,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/example1circle.png" style="width: 346px; height: 264px;" /></p> <p>No path exists from <code>(0, 0)</code> to <code>(3, 3)</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 3, yCorner = 3, circles = [[2,1,1],[1,2,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/example0circle.png" style="width: 346px; height: 264px;" /></p> <p>No path exists from <code>(0, 0)</code> to <code>(3, 3)</code>.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 4, yCorner = 4, circles = [[5,5,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/rectangles.png" style="width: 346px; height: 264px;" /></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= xCorner, yCorner &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= circles.length &lt;= 1000</code></li> <li><code>circles[i].length == 3</code></li> <li><code>1 &lt;= x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
Depth-First Search; Breadth-First Search; Union Find; Geometry; Array; Math
Java
class Solution { private int[][] circles; private int xCorner, yCorner; private boolean[] vis; public boolean canReachCorner(int xCorner, int yCorner, int[][] circles) { int n = circles.length; this.circles = circles; this.xCorner = xCorner; this.yCorner = yCorner; vis = new boolean[n]; for (int i = 0; i < n; ++i) { var c = circles[i]; int x = c[0], y = c[1], r = c[2]; if (inCircle(0, 0, x, y, r) || inCircle(xCorner, yCorner, x, y, r)) { return false; } if (!vis[i] && crossLeftTop(x, y, r) && dfs(i)) { return false; } } return true; } private boolean inCircle(long x, long y, long cx, long cy, long r) { return (x - cx) * (x - cx) + (y - cy) * (y - cy) <= r * r; } private boolean crossLeftTop(long cx, long cy, long r) { boolean a = Math.abs(cx) <= r && (cy >= 0 && cy <= yCorner); boolean b = Math.abs(cy - yCorner) <= r && (cx >= 0 && cx <= xCorner); return a || b; } private boolean crossRightBottom(long cx, long cy, long r) { boolean a = Math.abs(cx - xCorner) <= r && (cy >= 0 && cy <= yCorner); boolean b = Math.abs(cy) <= r && (cx >= 0 && cx <= xCorner); return a || b; } private boolean dfs(int i) { var c = circles[i]; long x1 = c[0], y1 = c[1], r1 = c[2]; if (crossRightBottom(x1, y1, r1)) { return true; } vis[i] = true; for (int j = 0; j < circles.length; ++j) { var c2 = circles[j]; long x2 = c2[0], y2 = c2[1], r2 = c2[2]; if (vis[j]) { continue; } if ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) > (r1 + r2) * (r1 + r2)) { continue; } if (x1 * r2 + x2 * r1 < (r1 + r2) * xCorner && y1 * r2 + y2 * r1 < (r1 + r2) * yCorner && dfs(j)) { return true; } } return false; } }
3,235
Check if the Rectangle Corner Is Reachable
Hard
<p>You are given two positive integers <code>xCorner</code> and <code>yCorner</code>, and a 2D array <code>circles</code>, where <code>circles[i] = [x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub>]</code> denotes a circle with center at <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and radius <code>r<sub>i</sub></code>.</p> <p>There is a rectangle in the coordinate plane with its bottom left corner at the origin and top right corner at the coordinate <code>(xCorner, yCorner)</code>. You need to check whether there is a path from the bottom left corner to the top right corner such that the <strong>entire path</strong> lies inside the rectangle, <strong>does not</strong> touch or lie inside <strong>any</strong> circle, and touches the rectangle <strong>only</strong> at the two corners.</p> <p>Return <code>true</code> if such a path exists, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 3, yCorner = 4, circles = [[2,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/example2circle1.png" style="width: 346px; height: 264px;" /></p> <p>The black curve shows a possible path between <code>(0, 0)</code> and <code>(3, 4)</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 3, yCorner = 3, circles = [[1,1,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/example1circle.png" style="width: 346px; height: 264px;" /></p> <p>No path exists from <code>(0, 0)</code> to <code>(3, 3)</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 3, yCorner = 3, circles = [[2,1,1],[1,2,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/example0circle.png" style="width: 346px; height: 264px;" /></p> <p>No path exists from <code>(0, 0)</code> to <code>(3, 3)</code>.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 4, yCorner = 4, circles = [[5,5,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/rectangles.png" style="width: 346px; height: 264px;" /></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= xCorner, yCorner &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= circles.length &lt;= 1000</code></li> <li><code>circles[i].length == 3</code></li> <li><code>1 &lt;= x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
Depth-First Search; Breadth-First Search; Union Find; Geometry; Array; Math
Python
class Solution: def canReachCorner( self, xCorner: int, yCorner: int, circles: List[List[int]] ) -> bool: def in_circle(x: int, y: int, cx: int, cy: int, r: int) -> int: return (x - cx) ** 2 + (y - cy) ** 2 <= r**2 def cross_left_top(cx: int, cy: int, r: int) -> bool: a = abs(cx) <= r and 0 <= cy <= yCorner b = abs(cy - yCorner) <= r and 0 <= cx <= xCorner return a or b def cross_right_bottom(cx: int, cy: int, r: int) -> bool: a = abs(cx - xCorner) <= r and 0 <= cy <= yCorner b = abs(cy) <= r and 0 <= cx <= xCorner return a or b def dfs(i: int) -> bool: x1, y1, r1 = circles[i] if cross_right_bottom(x1, y1, r1): return True vis[i] = True for j, (x2, y2, r2) in enumerate(circles): if vis[j] or not ((x1 - x2) ** 2 + (y1 - y2) ** 2 <= (r1 + r2) ** 2): continue if ( (x1 * r2 + x2 * r1 < (r1 + r2) * xCorner) and (y1 * r2 + y2 * r1 < (r1 + r2) * yCorner) and dfs(j) ): return True return False vis = [False] * len(circles) for i, (x, y, r) in enumerate(circles): if in_circle(0, 0, x, y, r) or in_circle(xCorner, yCorner, x, y, r): return False if (not vis[i]) and cross_left_top(x, y, r) and dfs(i): return False return True
3,235
Check if the Rectangle Corner Is Reachable
Hard
<p>You are given two positive integers <code>xCorner</code> and <code>yCorner</code>, and a 2D array <code>circles</code>, where <code>circles[i] = [x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub>]</code> denotes a circle with center at <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and radius <code>r<sub>i</sub></code>.</p> <p>There is a rectangle in the coordinate plane with its bottom left corner at the origin and top right corner at the coordinate <code>(xCorner, yCorner)</code>. You need to check whether there is a path from the bottom left corner to the top right corner such that the <strong>entire path</strong> lies inside the rectangle, <strong>does not</strong> touch or lie inside <strong>any</strong> circle, and touches the rectangle <strong>only</strong> at the two corners.</p> <p>Return <code>true</code> if such a path exists, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 3, yCorner = 4, circles = [[2,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/example2circle1.png" style="width: 346px; height: 264px;" /></p> <p>The black curve shows a possible path between <code>(0, 0)</code> and <code>(3, 4)</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 3, yCorner = 3, circles = [[1,1,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/example1circle.png" style="width: 346px; height: 264px;" /></p> <p>No path exists from <code>(0, 0)</code> to <code>(3, 3)</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 3, yCorner = 3, circles = [[2,1,1],[1,2,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/example0circle.png" style="width: 346px; height: 264px;" /></p> <p>No path exists from <code>(0, 0)</code> to <code>(3, 3)</code>.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 4, yCorner = 4, circles = [[5,5,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/rectangles.png" style="width: 346px; height: 264px;" /></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= xCorner, yCorner &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= circles.length &lt;= 1000</code></li> <li><code>circles[i].length == 3</code></li> <li><code>1 &lt;= x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
Depth-First Search; Breadth-First Search; Union Find; Geometry; Array; Math
Rust
impl Solution { pub fn can_reach_corner(x_corner: i32, y_corner: i32, circles: Vec<Vec<i32>>) -> bool { let n = circles.len(); let mut vis = vec![false; n]; let in_circle = |x: i64, y: i64, cx: i64, cy: i64, r: i64| -> bool { (x - cx) * (x - cx) + (y - cy) * (y - cy) <= r * r }; let cross_left_top = |cx: i64, cy: i64, r: i64| -> bool { let a = cx.abs() <= r && (cy >= 0 && cy <= y_corner as i64); let b = (cy - y_corner as i64).abs() <= r && (cx >= 0 && cx <= x_corner as i64); a || b }; let cross_right_bottom = |cx: i64, cy: i64, r: i64| -> bool { let a = (cx - x_corner as i64).abs() <= r && (cy >= 0 && cy <= y_corner as i64); let b = cy.abs() <= r && (cx >= 0 && cx <= x_corner as i64); a || b }; fn dfs( circles: &Vec<Vec<i32>>, vis: &mut Vec<bool>, i: usize, x_corner: i32, y_corner: i32, cross_right_bottom: &dyn Fn(i64, i64, i64) -> bool, ) -> bool { let c = &circles[i]; let (x1, y1, r1) = (c[0] as i64, c[1] as i64, c[2] as i64); if cross_right_bottom(x1, y1, r1) { return true; } vis[i] = true; for j in 0..circles.len() { if vis[j] { continue; } let c2 = &circles[j]; let (x2, y2, r2) = (c2[0] as i64, c2[1] as i64, c2[2] as i64); if (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) > (r1 + r2) * (r1 + r2) { continue; } if x1 * r2 + x2 * r1 < (r1 + r2) * x_corner as i64 && y1 * r2 + y2 * r1 < (r1 + r2) * y_corner as i64 && dfs(circles, vis, j, x_corner, y_corner, cross_right_bottom) { return true; } } false } for i in 0..n { let c = &circles[i]; let (x, y, r) = (c[0] as i64, c[1] as i64, c[2] as i64); if in_circle(0, 0, x, y, r) || in_circle(x_corner as i64, y_corner as i64, x, y, r) { return false; } if !vis[i] && cross_left_top(x, y, r) && dfs( &circles, &mut vis, i, x_corner, y_corner, &cross_right_bottom, ) { return false; } } true } }
3,235
Check if the Rectangle Corner Is Reachable
Hard
<p>You are given two positive integers <code>xCorner</code> and <code>yCorner</code>, and a 2D array <code>circles</code>, where <code>circles[i] = [x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub>]</code> denotes a circle with center at <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and radius <code>r<sub>i</sub></code>.</p> <p>There is a rectangle in the coordinate plane with its bottom left corner at the origin and top right corner at the coordinate <code>(xCorner, yCorner)</code>. You need to check whether there is a path from the bottom left corner to the top right corner such that the <strong>entire path</strong> lies inside the rectangle, <strong>does not</strong> touch or lie inside <strong>any</strong> circle, and touches the rectangle <strong>only</strong> at the two corners.</p> <p>Return <code>true</code> if such a path exists, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 3, yCorner = 4, circles = [[2,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/example2circle1.png" style="width: 346px; height: 264px;" /></p> <p>The black curve shows a possible path between <code>(0, 0)</code> and <code>(3, 4)</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 3, yCorner = 3, circles = [[1,1,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/example1circle.png" style="width: 346px; height: 264px;" /></p> <p>No path exists from <code>(0, 0)</code> to <code>(3, 3)</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 3, yCorner = 3, circles = [[2,1,1],[1,2,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/example0circle.png" style="width: 346px; height: 264px;" /></p> <p>No path exists from <code>(0, 0)</code> to <code>(3, 3)</code>.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">xCorner = 4, yCorner = 4, circles = [[5,5,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/images/rectangles.png" style="width: 346px; height: 264px;" /></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= xCorner, yCorner &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= circles.length &lt;= 1000</code></li> <li><code>circles[i].length == 3</code></li> <li><code>1 &lt;= x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
Depth-First Search; Breadth-First Search; Union Find; Geometry; Array; Math
TypeScript
function canReachCorner(xCorner: number, yCorner: number, circles: number[][]): boolean { const inCircle = (x: bigint, y: bigint, cx: bigint, cy: bigint, r: bigint): boolean => { const dx = x - cx; const dy = y - cy; return dx * dx + dy * dy <= r * r; }; const crossLeftTop = (cx: bigint, cy: bigint, r: bigint): boolean => { const a = BigInt(Math.abs(Number(cx))) <= r && cy >= 0n && cy <= BigInt(yCorner); const b = BigInt(Math.abs(Number(cy - BigInt(yCorner)))) <= r && cx >= 0n && cx <= BigInt(xCorner); return a || b; }; const crossRightBottom = (cx: bigint, cy: bigint, r: bigint): boolean => { const a = BigInt(Math.abs(Number(cx - BigInt(xCorner)))) <= r && cy >= 0n && cy <= BigInt(yCorner); const b = BigInt(Math.abs(Number(cy))) <= r && cx >= 0n && cx <= BigInt(xCorner); return a || b; }; const n = circles.length; const vis: boolean[] = new Array(n).fill(false); const dfs = (i: number): boolean => { const [x1, y1, r1] = circles[i].map(BigInt); if (crossRightBottom(x1, y1, r1)) { return true; } vis[i] = true; for (let j = 0; j < n; j++) { if (vis[j]) continue; const [x2, y2, r2] = circles[j].map(BigInt); if ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) > (r1 + r2) * (r1 + r2)) { continue; } if ( x1 * r2 + x2 * r1 < (r1 + r2) * BigInt(xCorner) && y1 * r2 + y2 * r1 < (r1 + r2) * BigInt(yCorner) && dfs(j) ) { return true; } } return false; }; for (let i = 0; i < n; i++) { const [x, y, r] = circles[i].map(BigInt); if (inCircle(0n, 0n, x, y, r) || inCircle(BigInt(xCorner), BigInt(yCorner), x, y, r)) { return false; } if (!vis[i] && crossLeftTop(x, y, r) && dfs(i)) { return false; } } return true; }
3,236
CEO Subordinate Hierarchy
Hard
<p>Table: <code>Employees</code></p> <pre> +---------------+---------+ | Column Name | Type | +---------------+---------+ | employee_id | int | | employee_name | varchar | | manager_id | int | | salary | int | +---------------+---------+ employee_id is the unique identifier for this table. manager_id is the employee_id of the employee&#39;s manager. The CEO has a NULL manager_id. </pre> <p>Write a solution to find subordinates of the CEO (both <strong>direct</strong> and <strong>indirect</strong>), along with their <strong>level in the hierarchy</strong> and their <strong>salary difference</strong> from the CEO.</p> <p>The result should have the following columns:</p> <p>The query result format is in the following example.</p> <ul> <li><code>subordinate_id</code>: The employee_id of the subordinate</li> <li><code>subordinate_name</code>: The name of the subordinate</li> <li><code>hierarchy_level</code>: The level of the subordinate in the hierarchy (<code>1</code> for <strong>direct</strong> reports, <code>2</code> for <strong>their direct</strong> reports, and <strong>so on</strong>)</li> <li><code>salary_difference</code>: The difference between the subordinate&#39;s salary and the CEO&#39;s salary</li> </ul> <p>Return <em>the result table ordered by</em> <code>hierarchy_level</code> <em><strong>ascending</strong></em>, <em>and then by</em> <code>subordinate_id</code> <em><strong>ascending</strong></em>.</p> <p>The query result format is in the following example.</p> <p>&nbsp;</p> <p><strong class="example">Example:</strong></p> <div class="example-block"> <p><strong>Input:</strong></p> <p><code>Employees</code> table:</p> <pre class="example-io"> +-------------+----------------+------------+---------+ | employee_id | employee_name | manager_id | salary | +-------------+----------------+------------+---------+ | 1 | Alice | NULL | 150000 | | 2 | Bob | 1 | 120000 | | 3 | Charlie | 1 | 110000 | | 4 | David | 2 | 105000 | | 5 | Eve | 2 | 100000 | | 6 | Frank | 3 | 95000 | | 7 | Grace | 3 | 98000 | | 8 | Helen | 5 | 90000 | +-------------+----------------+------------+---------+ </pre> <p><strong>Output:</strong></p> <pre class="example-io"> +----------------+------------------+------------------+-------------------+ | subordinate_id | subordinate_name | hierarchy_level | salary_difference | +----------------+------------------+------------------+-------------------+ | 2 | Bob | 1 | -30000 | | 3 | Charlie | 1 | -40000 | | 4 | David | 2 | -45000 | | 5 | Eve | 2 | -50000 | | 6 | Frank | 2 | -55000 | | 7 | Grace | 2 | -52000 | | 8 | Helen | 3 | -60000 | +----------------+------------------+------------------+-------------------+ </pre> <p><strong>Explanation:</strong></p> <ul> <li>Bob and Charlie are direct subordinates of Alice (CEO) and thus have a hierarchy_level of 1.</li> <li>David and Eve report to Bob, while Frank and Grace report to Charlie, making them second-level subordinates (hierarchy_level 2).</li> <li>Helen reports to Eve, making Helen a third-level subordinate (hierarchy_level 3).</li> <li>Salary differences are calculated relative to Alice&#39;s salary of 150000.</li> <li>The result is ordered by hierarchy_level ascending, and then by subordinate_id ascending.</li> </ul> <p><strong>Note:</strong> The output is ordered first by hierarchy_level in ascending order, then by subordinate_id in ascending order.</p> </div>
Database
SQL
# Write your MySQL query statement below WITH RECURSIVE T AS ( SELECT employee_id, employee_name, 0 AS hierarchy_level, manager_id, salary FROM Employees WHERE manager_id IS NULL UNION ALL SELECT e.employee_id, e.employee_name, hierarchy_level + 1 AS hierarchy_level, e.manager_id, e.salary FROM T t JOIN Employees e ON t.employee_id = e.manager_id ), P AS ( SELECT salary FROM Employees WHERE manager_id IS NULL ) SELECT employee_id subordinate_id, employee_name subordinate_name, hierarchy_level, t.salary - p.salary salary_difference FROM T t JOIN P p WHERE hierarchy_level != 0 ORDER BY 3, 1;
3,237
Alt and Tab Simulation
Medium
<p>There are <code>n</code> windows open numbered from <code>1</code> to <code>n</code>, we want to simulate using alt + tab to navigate between the windows.</p> <p>You are given an array <code>windows</code> which contains the initial order of the windows (the first element is at the top and the last one is at the bottom).</p> <p>You are also given an array <code>queries</code> where for each query, the window <code>queries[i]</code> is brought to the top.</p> <p>Return the final state of the array <code>windows</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">windows = [1,2,3], queries = [3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">[2,3,1]</span></p> <p><strong>Explanation:</strong></p> <p>Here is the window array after each query:</p> <ul> <li>Initial order: <code>[1,2,3]</code></li> <li>After the first query: <code>[<u><strong>3</strong></u>,1,2]</code></li> <li>After the second query: <code>[<u><strong>3</strong></u>,1,2]</code></li> <li>After the last query: <code>[<u><strong>2</strong></u>,3,1]</code></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">windows = [1,4,2,3], queries = [4,1,3]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,1,4,2]</span></p> <p><strong>Explanation:</strong></p> <p>Here is the window array after each query:</p> <ul> <li>Initial order: <code>[1,4,2,3]</code></li> <li>After the first query: <code>[<u><strong>4</strong></u>,1,2,3]</code></li> <li>After the second query: <code>[<u><strong>1</strong></u>,4,2,3]</code></li> <li>After the last query: <code>[<u><strong>3</strong></u>,1,4,2]</code></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == windows.length &lt;= 10<sup>5</sup></code></li> <li><code>windows</code> is a permutation of <code>[1, n]</code>.</li> <li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= queries[i] &lt;= n</code></li> </ul>
Array; Hash Table; Simulation
C++
class Solution { public: vector<int> simulationResult(vector<int>& windows, vector<int>& queries) { int n = windows.size(); vector<bool> s(n + 1); vector<int> ans; for (int i = queries.size() - 1; ~i; --i) { int q = queries[i]; if (!s[q]) { s[q] = true; ans.push_back(q); } } for (int w : windows) { if (!s[w]) { ans.push_back(w); } } return ans; } };
3,237
Alt and Tab Simulation
Medium
<p>There are <code>n</code> windows open numbered from <code>1</code> to <code>n</code>, we want to simulate using alt + tab to navigate between the windows.</p> <p>You are given an array <code>windows</code> which contains the initial order of the windows (the first element is at the top and the last one is at the bottom).</p> <p>You are also given an array <code>queries</code> where for each query, the window <code>queries[i]</code> is brought to the top.</p> <p>Return the final state of the array <code>windows</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">windows = [1,2,3], queries = [3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">[2,3,1]</span></p> <p><strong>Explanation:</strong></p> <p>Here is the window array after each query:</p> <ul> <li>Initial order: <code>[1,2,3]</code></li> <li>After the first query: <code>[<u><strong>3</strong></u>,1,2]</code></li> <li>After the second query: <code>[<u><strong>3</strong></u>,1,2]</code></li> <li>After the last query: <code>[<u><strong>2</strong></u>,3,1]</code></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">windows = [1,4,2,3], queries = [4,1,3]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,1,4,2]</span></p> <p><strong>Explanation:</strong></p> <p>Here is the window array after each query:</p> <ul> <li>Initial order: <code>[1,4,2,3]</code></li> <li>After the first query: <code>[<u><strong>4</strong></u>,1,2,3]</code></li> <li>After the second query: <code>[<u><strong>1</strong></u>,4,2,3]</code></li> <li>After the last query: <code>[<u><strong>3</strong></u>,1,4,2]</code></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == windows.length &lt;= 10<sup>5</sup></code></li> <li><code>windows</code> is a permutation of <code>[1, n]</code>.</li> <li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= queries[i] &lt;= n</code></li> </ul>
Array; Hash Table; Simulation
Go
func simulationResult(windows []int, queries []int) (ans []int) { n := len(windows) s := make([]bool, n+1) for i := len(queries) - 1; i >= 0; i-- { q := queries[i] if !s[q] { s[q] = true ans = append(ans, q) } } for _, w := range windows { if !s[w] { ans = append(ans, w) } } return }
3,237
Alt and Tab Simulation
Medium
<p>There are <code>n</code> windows open numbered from <code>1</code> to <code>n</code>, we want to simulate using alt + tab to navigate between the windows.</p> <p>You are given an array <code>windows</code> which contains the initial order of the windows (the first element is at the top and the last one is at the bottom).</p> <p>You are also given an array <code>queries</code> where for each query, the window <code>queries[i]</code> is brought to the top.</p> <p>Return the final state of the array <code>windows</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">windows = [1,2,3], queries = [3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">[2,3,1]</span></p> <p><strong>Explanation:</strong></p> <p>Here is the window array after each query:</p> <ul> <li>Initial order: <code>[1,2,3]</code></li> <li>After the first query: <code>[<u><strong>3</strong></u>,1,2]</code></li> <li>After the second query: <code>[<u><strong>3</strong></u>,1,2]</code></li> <li>After the last query: <code>[<u><strong>2</strong></u>,3,1]</code></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">windows = [1,4,2,3], queries = [4,1,3]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,1,4,2]</span></p> <p><strong>Explanation:</strong></p> <p>Here is the window array after each query:</p> <ul> <li>Initial order: <code>[1,4,2,3]</code></li> <li>After the first query: <code>[<u><strong>4</strong></u>,1,2,3]</code></li> <li>After the second query: <code>[<u><strong>1</strong></u>,4,2,3]</code></li> <li>After the last query: <code>[<u><strong>3</strong></u>,1,4,2]</code></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == windows.length &lt;= 10<sup>5</sup></code></li> <li><code>windows</code> is a permutation of <code>[1, n]</code>.</li> <li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= queries[i] &lt;= n</code></li> </ul>
Array; Hash Table; Simulation
Java
class Solution { public int[] simulationResult(int[] windows, int[] queries) { int n = windows.length; boolean[] s = new boolean[n + 1]; int[] ans = new int[n]; int k = 0; for (int i = queries.length - 1; i >= 0; --i) { int q = queries[i]; if (!s[q]) { ans[k++] = q; s[q] = true; } } for (int w : windows) { if (!s[w]) { ans[k++] = w; } } return ans; } }
3,237
Alt and Tab Simulation
Medium
<p>There are <code>n</code> windows open numbered from <code>1</code> to <code>n</code>, we want to simulate using alt + tab to navigate between the windows.</p> <p>You are given an array <code>windows</code> which contains the initial order of the windows (the first element is at the top and the last one is at the bottom).</p> <p>You are also given an array <code>queries</code> where for each query, the window <code>queries[i]</code> is brought to the top.</p> <p>Return the final state of the array <code>windows</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">windows = [1,2,3], queries = [3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">[2,3,1]</span></p> <p><strong>Explanation:</strong></p> <p>Here is the window array after each query:</p> <ul> <li>Initial order: <code>[1,2,3]</code></li> <li>After the first query: <code>[<u><strong>3</strong></u>,1,2]</code></li> <li>After the second query: <code>[<u><strong>3</strong></u>,1,2]</code></li> <li>After the last query: <code>[<u><strong>2</strong></u>,3,1]</code></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">windows = [1,4,2,3], queries = [4,1,3]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,1,4,2]</span></p> <p><strong>Explanation:</strong></p> <p>Here is the window array after each query:</p> <ul> <li>Initial order: <code>[1,4,2,3]</code></li> <li>After the first query: <code>[<u><strong>4</strong></u>,1,2,3]</code></li> <li>After the second query: <code>[<u><strong>1</strong></u>,4,2,3]</code></li> <li>After the last query: <code>[<u><strong>3</strong></u>,1,4,2]</code></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == windows.length &lt;= 10<sup>5</sup></code></li> <li><code>windows</code> is a permutation of <code>[1, n]</code>.</li> <li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= queries[i] &lt;= n</code></li> </ul>
Array; Hash Table; Simulation
Python
class Solution: def simulationResult(self, windows: List[int], queries: List[int]) -> List[int]: s = set() ans = [] for q in queries[::-1]: if q not in s: ans.append(q) s.add(q) for w in windows: if w not in s: ans.append(w) return ans
3,237
Alt and Tab Simulation
Medium
<p>There are <code>n</code> windows open numbered from <code>1</code> to <code>n</code>, we want to simulate using alt + tab to navigate between the windows.</p> <p>You are given an array <code>windows</code> which contains the initial order of the windows (the first element is at the top and the last one is at the bottom).</p> <p>You are also given an array <code>queries</code> where for each query, the window <code>queries[i]</code> is brought to the top.</p> <p>Return the final state of the array <code>windows</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">windows = [1,2,3], queries = [3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">[2,3,1]</span></p> <p><strong>Explanation:</strong></p> <p>Here is the window array after each query:</p> <ul> <li>Initial order: <code>[1,2,3]</code></li> <li>After the first query: <code>[<u><strong>3</strong></u>,1,2]</code></li> <li>After the second query: <code>[<u><strong>3</strong></u>,1,2]</code></li> <li>After the last query: <code>[<u><strong>2</strong></u>,3,1]</code></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">windows = [1,4,2,3], queries = [4,1,3]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,1,4,2]</span></p> <p><strong>Explanation:</strong></p> <p>Here is the window array after each query:</p> <ul> <li>Initial order: <code>[1,4,2,3]</code></li> <li>After the first query: <code>[<u><strong>4</strong></u>,1,2,3]</code></li> <li>After the second query: <code>[<u><strong>1</strong></u>,4,2,3]</code></li> <li>After the last query: <code>[<u><strong>3</strong></u>,1,4,2]</code></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == windows.length &lt;= 10<sup>5</sup></code></li> <li><code>windows</code> is a permutation of <code>[1, n]</code>.</li> <li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= queries[i] &lt;= n</code></li> </ul>
Array; Hash Table; Simulation
TypeScript
function simulationResult(windows: number[], queries: number[]): number[] { const n = windows.length; const s: boolean[] = Array(n + 1).fill(false); const ans: number[] = []; for (let i = queries.length - 1; i >= 0; i--) { const q = queries[i]; if (!s[q]) { s[q] = true; ans.push(q); } } for (const w of windows) { if (!s[w]) { ans.push(w); } } return ans; }
3,238
Find the Number of Winning Players
Easy
<p>You are given an integer <code>n</code> representing the number of players in a game and a 2D array <code>pick</code> where <code>pick[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents that the player <code>x<sub>i</sub></code> picked a ball of color <code>y<sub>i</sub></code>.</p> <p>Player <code>i</code> <strong>wins</strong> the game if they pick <strong>strictly more</strong> than <code>i</code> balls of the <strong>same</strong> color. In other words,</p> <ul> <li>Player 0 wins if they pick any ball.</li> <li>Player 1 wins if they pick at least two balls of the <em>same</em> color.</li> <li>...</li> <li>Player <code>i</code> wins if they pick at least <code>i + 1</code> balls of the <em>same</em> color.</li> </ul> <p>Return the number of players who <strong>win</strong> the game.</p> <p><strong>Note</strong> that <em>multiple</em> players can win the game.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4, pick = [[0,0],[1,0],[1,0],[2,1],[2,1],[2,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Player 0 and player 1 win the game, while players 2 and 3 do not win.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, pick = [[1,1],[1,2],[1,3],[1,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No player wins the game.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, pick = [[1,1],[2,4],[2,4],[2,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>Player 2 wins the game by picking 3 balls with color 4.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10</code></li> <li><code>1 &lt;= pick.length &lt;= 100</code></li> <li><code>pick[i].length == 2</code></li> <li><code>0 &lt;= x<sub>i</sub> &lt;= n - 1 </code></li> <li><code>0 &lt;= y<sub>i</sub> &lt;= 10</code></li> </ul>
Array; Hash Table; Counting
C++
class Solution { public: int winningPlayerCount(int n, vector<vector<int>>& pick) { int cnt[10][11]{}; unordered_set<int> s; for (const auto& p : pick) { int x = p[0], y = p[1]; if (++cnt[x][y] > x) { s.insert(x); } } return s.size(); } };
3,238
Find the Number of Winning Players
Easy
<p>You are given an integer <code>n</code> representing the number of players in a game and a 2D array <code>pick</code> where <code>pick[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents that the player <code>x<sub>i</sub></code> picked a ball of color <code>y<sub>i</sub></code>.</p> <p>Player <code>i</code> <strong>wins</strong> the game if they pick <strong>strictly more</strong> than <code>i</code> balls of the <strong>same</strong> color. In other words,</p> <ul> <li>Player 0 wins if they pick any ball.</li> <li>Player 1 wins if they pick at least two balls of the <em>same</em> color.</li> <li>...</li> <li>Player <code>i</code> wins if they pick at least <code>i + 1</code> balls of the <em>same</em> color.</li> </ul> <p>Return the number of players who <strong>win</strong> the game.</p> <p><strong>Note</strong> that <em>multiple</em> players can win the game.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4, pick = [[0,0],[1,0],[1,0],[2,1],[2,1],[2,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Player 0 and player 1 win the game, while players 2 and 3 do not win.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, pick = [[1,1],[1,2],[1,3],[1,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No player wins the game.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, pick = [[1,1],[2,4],[2,4],[2,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>Player 2 wins the game by picking 3 balls with color 4.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10</code></li> <li><code>1 &lt;= pick.length &lt;= 100</code></li> <li><code>pick[i].length == 2</code></li> <li><code>0 &lt;= x<sub>i</sub> &lt;= n - 1 </code></li> <li><code>0 &lt;= y<sub>i</sub> &lt;= 10</code></li> </ul>
Array; Hash Table; Counting
Go
func winningPlayerCount(n int, pick [][]int) int { cnt := make([][11]int, n) s := map[int]struct{}{} for _, p := range pick { x, y := p[0], p[1] cnt[x][y]++ if cnt[x][y] > x { s[x] = struct{}{} } } return len(s) }
3,238
Find the Number of Winning Players
Easy
<p>You are given an integer <code>n</code> representing the number of players in a game and a 2D array <code>pick</code> where <code>pick[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents that the player <code>x<sub>i</sub></code> picked a ball of color <code>y<sub>i</sub></code>.</p> <p>Player <code>i</code> <strong>wins</strong> the game if they pick <strong>strictly more</strong> than <code>i</code> balls of the <strong>same</strong> color. In other words,</p> <ul> <li>Player 0 wins if they pick any ball.</li> <li>Player 1 wins if they pick at least two balls of the <em>same</em> color.</li> <li>...</li> <li>Player <code>i</code> wins if they pick at least <code>i + 1</code> balls of the <em>same</em> color.</li> </ul> <p>Return the number of players who <strong>win</strong> the game.</p> <p><strong>Note</strong> that <em>multiple</em> players can win the game.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4, pick = [[0,0],[1,0],[1,0],[2,1],[2,1],[2,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Player 0 and player 1 win the game, while players 2 and 3 do not win.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, pick = [[1,1],[1,2],[1,3],[1,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No player wins the game.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, pick = [[1,1],[2,4],[2,4],[2,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>Player 2 wins the game by picking 3 balls with color 4.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10</code></li> <li><code>1 &lt;= pick.length &lt;= 100</code></li> <li><code>pick[i].length == 2</code></li> <li><code>0 &lt;= x<sub>i</sub> &lt;= n - 1 </code></li> <li><code>0 &lt;= y<sub>i</sub> &lt;= 10</code></li> </ul>
Array; Hash Table; Counting
Java
class Solution { public int winningPlayerCount(int n, int[][] pick) { int[][] cnt = new int[n][11]; Set<Integer> s = new HashSet<>(); for (var p : pick) { int x = p[0], y = p[1]; if (++cnt[x][y] > x) { s.add(x); } } return s.size(); } }
3,238
Find the Number of Winning Players
Easy
<p>You are given an integer <code>n</code> representing the number of players in a game and a 2D array <code>pick</code> where <code>pick[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents that the player <code>x<sub>i</sub></code> picked a ball of color <code>y<sub>i</sub></code>.</p> <p>Player <code>i</code> <strong>wins</strong> the game if they pick <strong>strictly more</strong> than <code>i</code> balls of the <strong>same</strong> color. In other words,</p> <ul> <li>Player 0 wins if they pick any ball.</li> <li>Player 1 wins if they pick at least two balls of the <em>same</em> color.</li> <li>...</li> <li>Player <code>i</code> wins if they pick at least <code>i + 1</code> balls of the <em>same</em> color.</li> </ul> <p>Return the number of players who <strong>win</strong> the game.</p> <p><strong>Note</strong> that <em>multiple</em> players can win the game.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4, pick = [[0,0],[1,0],[1,0],[2,1],[2,1],[2,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Player 0 and player 1 win the game, while players 2 and 3 do not win.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, pick = [[1,1],[1,2],[1,3],[1,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No player wins the game.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, pick = [[1,1],[2,4],[2,4],[2,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>Player 2 wins the game by picking 3 balls with color 4.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10</code></li> <li><code>1 &lt;= pick.length &lt;= 100</code></li> <li><code>pick[i].length == 2</code></li> <li><code>0 &lt;= x<sub>i</sub> &lt;= n - 1 </code></li> <li><code>0 &lt;= y<sub>i</sub> &lt;= 10</code></li> </ul>
Array; Hash Table; Counting
Python
class Solution: def winningPlayerCount(self, n: int, pick: List[List[int]]) -> int: cnt = [[0] * 11 for _ in range(n)] s = set() for x, y in pick: cnt[x][y] += 1 if cnt[x][y] > x: s.add(x) return len(s)
3,238
Find the Number of Winning Players
Easy
<p>You are given an integer <code>n</code> representing the number of players in a game and a 2D array <code>pick</code> where <code>pick[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents that the player <code>x<sub>i</sub></code> picked a ball of color <code>y<sub>i</sub></code>.</p> <p>Player <code>i</code> <strong>wins</strong> the game if they pick <strong>strictly more</strong> than <code>i</code> balls of the <strong>same</strong> color. In other words,</p> <ul> <li>Player 0 wins if they pick any ball.</li> <li>Player 1 wins if they pick at least two balls of the <em>same</em> color.</li> <li>...</li> <li>Player <code>i</code> wins if they pick at least <code>i + 1</code> balls of the <em>same</em> color.</li> </ul> <p>Return the number of players who <strong>win</strong> the game.</p> <p><strong>Note</strong> that <em>multiple</em> players can win the game.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4, pick = [[0,0],[1,0],[1,0],[2,1],[2,1],[2,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Player 0 and player 1 win the game, while players 2 and 3 do not win.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, pick = [[1,1],[1,2],[1,3],[1,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No player wins the game.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, pick = [[1,1],[2,4],[2,4],[2,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>Player 2 wins the game by picking 3 balls with color 4.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10</code></li> <li><code>1 &lt;= pick.length &lt;= 100</code></li> <li><code>pick[i].length == 2</code></li> <li><code>0 &lt;= x<sub>i</sub> &lt;= n - 1 </code></li> <li><code>0 &lt;= y<sub>i</sub> &lt;= 10</code></li> </ul>
Array; Hash Table; Counting
TypeScript
function winningPlayerCount(n: number, pick: number[][]): number { const cnt: number[][] = Array.from({ length: n }, () => Array(11).fill(0)); const s = new Set<number>(); for (const [x, y] of pick) { if (++cnt[x][y] > x) { s.add(x); } } return s.size; }
3,239
Minimum Number of Flips to Make Binary Grid Palindromic I
Medium
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code>1</code> to <code>0</code>.</p> <p>Return the <strong>minimum</strong> number of cells that need to be flipped to make <strong>either</strong> all rows <strong>palindromic</strong> or all columns <strong>palindromic</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,0],[0,0,0],[0,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3239.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20I/images/screenshot-from-2024-07-08-00-20-10.png" style="width: 420px; height: 108px;" /></p> <p>Flipping the highlighted cells makes all the rows palindromic.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = </span>[[0,1],[0,1],[0,0]]</p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3239.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20I/images/screenshot-from-2024-07-08-00-31-23.png" style="width: 300px; height: 100px;" /></p> <p>Flipping the highlighted cell makes all the columns palindromic.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1],[0]]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>All rows are already palindromic.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m * n &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= grid[i][j] &lt;= 1</code></li> </ul>
Array; Two Pointers; Matrix
C++
class Solution { public: int minFlips(vector<vector<int>>& grid) { int m = grid.size(), n = grid[0].size(); int cnt1 = 0, cnt2 = 0; for (const auto& row : grid) { for (int j = 0; j < n / 2; ++j) { if (row[j] != row[n - j - 1]) { ++cnt1; } } } for (int j = 0; j < n; ++j) { for (int i = 0; i < m / 2; ++i) { if (grid[i][j] != grid[m - i - 1][j]) { ++cnt2; } } } return min(cnt1, cnt2); } };
3,239
Minimum Number of Flips to Make Binary Grid Palindromic I
Medium
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code>1</code> to <code>0</code>.</p> <p>Return the <strong>minimum</strong> number of cells that need to be flipped to make <strong>either</strong> all rows <strong>palindromic</strong> or all columns <strong>palindromic</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,0],[0,0,0],[0,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3239.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20I/images/screenshot-from-2024-07-08-00-20-10.png" style="width: 420px; height: 108px;" /></p> <p>Flipping the highlighted cells makes all the rows palindromic.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = </span>[[0,1],[0,1],[0,0]]</p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3239.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20I/images/screenshot-from-2024-07-08-00-31-23.png" style="width: 300px; height: 100px;" /></p> <p>Flipping the highlighted cell makes all the columns palindromic.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1],[0]]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>All rows are already palindromic.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m * n &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= grid[i][j] &lt;= 1</code></li> </ul>
Array; Two Pointers; Matrix
Go
func minFlips(grid [][]int) int { m, n := len(grid), len(grid[0]) cnt1, cnt2 := 0, 0 for _, row := range grid { for j := 0; j < n/2; j++ { if row[j] != row[n-j-1] { cnt1++ } } } for j := 0; j < n; j++ { for i := 0; i < m/2; i++ { if grid[i][j] != grid[m-i-1][j] { cnt2++ } } } return min(cnt1, cnt2) }
3,239
Minimum Number of Flips to Make Binary Grid Palindromic I
Medium
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code>1</code> to <code>0</code>.</p> <p>Return the <strong>minimum</strong> number of cells that need to be flipped to make <strong>either</strong> all rows <strong>palindromic</strong> or all columns <strong>palindromic</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,0],[0,0,0],[0,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3239.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20I/images/screenshot-from-2024-07-08-00-20-10.png" style="width: 420px; height: 108px;" /></p> <p>Flipping the highlighted cells makes all the rows palindromic.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = </span>[[0,1],[0,1],[0,0]]</p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3239.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20I/images/screenshot-from-2024-07-08-00-31-23.png" style="width: 300px; height: 100px;" /></p> <p>Flipping the highlighted cell makes all the columns palindromic.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1],[0]]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>All rows are already palindromic.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m * n &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= grid[i][j] &lt;= 1</code></li> </ul>
Array; Two Pointers; Matrix
Java
class Solution { public int minFlips(int[][] grid) { int m = grid.length, n = grid[0].length; int cnt1 = 0, cnt2 = 0; for (var row : grid) { for (int j = 0; j < n / 2; ++j) { if (row[j] != row[n - j - 1]) { ++cnt1; } } } for (int j = 0; j < n; ++j) { for (int i = 0; i < m / 2; ++i) { if (grid[i][j] != grid[m - i - 1][j]) { ++cnt2; } } } return Math.min(cnt1, cnt2); } }
3,239
Minimum Number of Flips to Make Binary Grid Palindromic I
Medium
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code>1</code> to <code>0</code>.</p> <p>Return the <strong>minimum</strong> number of cells that need to be flipped to make <strong>either</strong> all rows <strong>palindromic</strong> or all columns <strong>palindromic</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,0],[0,0,0],[0,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3239.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20I/images/screenshot-from-2024-07-08-00-20-10.png" style="width: 420px; height: 108px;" /></p> <p>Flipping the highlighted cells makes all the rows palindromic.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = </span>[[0,1],[0,1],[0,0]]</p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3239.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20I/images/screenshot-from-2024-07-08-00-31-23.png" style="width: 300px; height: 100px;" /></p> <p>Flipping the highlighted cell makes all the columns palindromic.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1],[0]]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>All rows are already palindromic.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m * n &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= grid[i][j] &lt;= 1</code></li> </ul>
Array; Two Pointers; Matrix
Python
class Solution: def minFlips(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) cnt1 = cnt2 = 0 for row in grid: for j in range(n // 2): if row[j] != row[n - j - 1]: cnt1 += 1 for j in range(n): for i in range(m // 2): if grid[i][j] != grid[m - i - 1][j]: cnt2 += 1 return min(cnt1, cnt2)
3,239
Minimum Number of Flips to Make Binary Grid Palindromic I
Medium
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code>1</code> to <code>0</code>.</p> <p>Return the <strong>minimum</strong> number of cells that need to be flipped to make <strong>either</strong> all rows <strong>palindromic</strong> or all columns <strong>palindromic</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,0],[0,0,0],[0,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3239.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20I/images/screenshot-from-2024-07-08-00-20-10.png" style="width: 420px; height: 108px;" /></p> <p>Flipping the highlighted cells makes all the rows palindromic.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = </span>[[0,1],[0,1],[0,0]]</p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3239.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20I/images/screenshot-from-2024-07-08-00-31-23.png" style="width: 300px; height: 100px;" /></p> <p>Flipping the highlighted cell makes all the columns palindromic.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1],[0]]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>All rows are already palindromic.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m * n &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= grid[i][j] &lt;= 1</code></li> </ul>
Array; Two Pointers; Matrix
TypeScript
function minFlips(grid: number[][]): number { const [m, n] = [grid.length, grid[0].length]; let [cnt1, cnt2] = [0, 0]; for (const row of grid) { for (let j = 0; j < n / 2; ++j) { if (row[j] !== row[n - 1 - j]) { ++cnt1; } } } for (let j = 0; j < n; ++j) { for (let i = 0; i < m / 2; ++i) { if (grid[i][j] !== grid[m - 1 - i][j]) { ++cnt2; } } } return Math.min(cnt1, cnt2); }
3,240
Minimum Number of Flips to Make Binary Grid Palindromic II
Medium
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code>1</code> to <code>0</code>.</p> <p>Return the <strong>minimum</strong> number of cells that need to be flipped to make <strong>all</strong> rows and columns <strong>palindromic</strong>, and the total number of <code>1</code>&#39;s in <code>grid</code> <strong>divisible</strong> by <code>4</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,0],[0,1,0],[0,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p><img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3240.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20II/images/image.png" style="width: 400px; height: 105px;" /></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1],[0,1],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3240.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20II/images/screenshot-from-2024-07-09-01-37-48.png" style="width: 300px; height: 104px;" /></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1],[1]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3240.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20II/images/screenshot-from-2024-08-01-23-05-26.png" style="width: 200px; height: 70px;" /></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m * n &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= grid[i][j] &lt;= 1</code></li> </ul>
Array; Two Pointers; Matrix
C++
class Solution { public: int minFlips(vector<vector<int>>& grid) { int m = grid.size(), n = grid[0].size(); int ans = 0; for (int i = 0; i < m / 2; ++i) { for (int j = 0; j < n / 2; ++j) { int x = m - i - 1, y = n - j - 1; int cnt1 = grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y]; ans += min(cnt1, 4 - cnt1); } } if (m % 2 == 1 && n % 2 == 1) { ans += grid[m / 2][n / 2]; } int diff = 0, cnt1 = 0; if (m % 2 == 1) { for (int j = 0; j < n / 2; ++j) { if (grid[m / 2][j] == grid[m / 2][n - j - 1]) { cnt1 += grid[m / 2][j] * 2; } else { diff += 1; } } } if (n % 2 == 1) { for (int i = 0; i < m / 2; ++i) { if (grid[i][n / 2] == grid[m - i - 1][n / 2]) { cnt1 += grid[i][n / 2] * 2; } else { diff += 1; } } } ans += cnt1 % 4 == 0 || diff > 0 ? diff : 2; return ans; } };
3,240
Minimum Number of Flips to Make Binary Grid Palindromic II
Medium
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code>1</code> to <code>0</code>.</p> <p>Return the <strong>minimum</strong> number of cells that need to be flipped to make <strong>all</strong> rows and columns <strong>palindromic</strong>, and the total number of <code>1</code>&#39;s in <code>grid</code> <strong>divisible</strong> by <code>4</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,0],[0,1,0],[0,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p><img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3240.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20II/images/image.png" style="width: 400px; height: 105px;" /></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1],[0,1],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3240.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20II/images/screenshot-from-2024-07-09-01-37-48.png" style="width: 300px; height: 104px;" /></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1],[1]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3240.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20II/images/screenshot-from-2024-08-01-23-05-26.png" style="width: 200px; height: 70px;" /></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m * n &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= grid[i][j] &lt;= 1</code></li> </ul>
Array; Two Pointers; Matrix
Go
func minFlips(grid [][]int) int { m, n := len(grid), len(grid[0]) ans := 0 for i := 0; i < m/2; i++ { for j := 0; j < n/2; j++ { x, y := m-i-1, n-j-1 cnt1 := grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y] ans += min(cnt1, 4-cnt1) } } if m%2 == 1 && n%2 == 1 { ans += grid[m/2][n/2] } diff, cnt1 := 0, 0 if m%2 == 1 { for j := 0; j < n/2; j++ { if grid[m/2][j] == grid[m/2][n-j-1] { cnt1 += grid[m/2][j] * 2 } else { diff += 1 } } } if n%2 == 1 { for i := 0; i < m/2; i++ { if grid[i][n/2] == grid[m-i-1][n/2] { cnt1 += grid[i][n/2] * 2 } else { diff += 1 } } } if cnt1%4 == 0 || diff > 0 { ans += diff } else { ans += 2 } return ans }
3,240
Minimum Number of Flips to Make Binary Grid Palindromic II
Medium
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code>1</code> to <code>0</code>.</p> <p>Return the <strong>minimum</strong> number of cells that need to be flipped to make <strong>all</strong> rows and columns <strong>palindromic</strong>, and the total number of <code>1</code>&#39;s in <code>grid</code> <strong>divisible</strong> by <code>4</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,0],[0,1,0],[0,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p><img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3240.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20II/images/image.png" style="width: 400px; height: 105px;" /></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1],[0,1],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3240.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20II/images/screenshot-from-2024-07-09-01-37-48.png" style="width: 300px; height: 104px;" /></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1],[1]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3240.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20II/images/screenshot-from-2024-08-01-23-05-26.png" style="width: 200px; height: 70px;" /></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m * n &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= grid[i][j] &lt;= 1</code></li> </ul>
Array; Two Pointers; Matrix
Java
class Solution { public int minFlips(int[][] grid) { int m = grid.length, n = grid[0].length; int ans = 0; for (int i = 0; i < m / 2; ++i) { for (int j = 0; j < n / 2; ++j) { int x = m - i - 1, y = n - j - 1; int cnt1 = grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y]; ans += Math.min(cnt1, 4 - cnt1); } } if (m % 2 == 1 && n % 2 == 1) { ans += grid[m / 2][n / 2]; } int diff = 0, cnt1 = 0; if (m % 2 == 1) { for (int j = 0; j < n / 2; ++j) { if (grid[m / 2][j] == grid[m / 2][n - j - 1]) { cnt1 += grid[m / 2][j] * 2; } else { diff += 1; } } } if (n % 2 == 1) { for (int i = 0; i < m / 2; ++i) { if (grid[i][n / 2] == grid[m - i - 1][n / 2]) { cnt1 += grid[i][n / 2] * 2; } else { diff += 1; } } } ans += cnt1 % 4 == 0 || diff > 0 ? diff : 2; return ans; } }
3,240
Minimum Number of Flips to Make Binary Grid Palindromic II
Medium
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code>1</code> to <code>0</code>.</p> <p>Return the <strong>minimum</strong> number of cells that need to be flipped to make <strong>all</strong> rows and columns <strong>palindromic</strong>, and the total number of <code>1</code>&#39;s in <code>grid</code> <strong>divisible</strong> by <code>4</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,0],[0,1,0],[0,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p><img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3240.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20II/images/image.png" style="width: 400px; height: 105px;" /></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1],[0,1],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3240.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20II/images/screenshot-from-2024-07-09-01-37-48.png" style="width: 300px; height: 104px;" /></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1],[1]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3240.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20II/images/screenshot-from-2024-08-01-23-05-26.png" style="width: 200px; height: 70px;" /></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m * n &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= grid[i][j] &lt;= 1</code></li> </ul>
Array; Two Pointers; Matrix
Python
class Solution: def minFlips(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) ans = 0 for i in range(m // 2): for j in range(n // 2): x, y = m - i - 1, n - j - 1 cnt1 = grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y] ans += min(cnt1, 4 - cnt1) if m % 2 and n % 2: ans += grid[m // 2][n // 2] diff = cnt1 = 0 if m % 2: for j in range(n // 2): if grid[m // 2][j] == grid[m // 2][n - j - 1]: cnt1 += grid[m // 2][j] * 2 else: diff += 1 if n % 2: for i in range(m // 2): if grid[i][n // 2] == grid[m - i - 1][n // 2]: cnt1 += grid[i][n // 2] * 2 else: diff += 1 ans += diff if cnt1 % 4 == 0 or diff else 2 return ans
3,240
Minimum Number of Flips to Make Binary Grid Palindromic II
Medium
<p>You are given an <code>m x n</code> binary matrix <code>grid</code>.</p> <p>A row or column is considered <strong>palindromic</strong> if its values read the same forward and backward.</p> <p>You can <strong>flip</strong> any number of cells in <code>grid</code> from <code>0</code> to <code>1</code>, or from <code>1</code> to <code>0</code>.</p> <p>Return the <strong>minimum</strong> number of cells that need to be flipped to make <strong>all</strong> rows and columns <strong>palindromic</strong>, and the total number of <code>1</code>&#39;s in <code>grid</code> <strong>divisible</strong> by <code>4</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,0],[0,1,0],[0,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p><img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3240.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20II/images/image.png" style="width: 400px; height: 105px;" /></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1],[0,1],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3240.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20II/images/screenshot-from-2024-07-09-01-37-48.png" style="width: 300px; height: 104px;" /></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1],[1]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3240.Minimum%20Number%20of%20Flips%20to%20Make%20Binary%20Grid%20Palindromic%20II/images/screenshot-from-2024-08-01-23-05-26.png" style="width: 200px; height: 70px;" /></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m * n &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= grid[i][j] &lt;= 1</code></li> </ul>
Array; Two Pointers; Matrix
TypeScript
function minFlips(grid: number[][]): number { const m = grid.length; const n = grid[0].length; let ans = 0; for (let i = 0; i < Math.floor(m / 2); i++) { for (let j = 0; j < Math.floor(n / 2); j++) { const x = m - i - 1; const y = n - j - 1; const cnt1 = grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y]; ans += Math.min(cnt1, 4 - cnt1); } } if (m % 2 === 1 && n % 2 === 1) { ans += grid[Math.floor(m / 2)][Math.floor(n / 2)]; } let diff = 0, cnt1 = 0; if (m % 2 === 1) { for (let j = 0; j < Math.floor(n / 2); j++) { if (grid[Math.floor(m / 2)][j] === grid[Math.floor(m / 2)][n - j - 1]) { cnt1 += grid[Math.floor(m / 2)][j] * 2; } else { diff += 1; } } } if (n % 2 === 1) { for (let i = 0; i < Math.floor(m / 2); i++) { if (grid[i][Math.floor(n / 2)] === grid[m - i - 1][Math.floor(n / 2)]) { cnt1 += grid[i][Math.floor(n / 2)] * 2; } else { diff += 1; } } } ans += cnt1 % 4 === 0 || diff > 0 ? diff : 2; return ans; }
3,242
Design Neighbor Sum Service
Easy
<p>You are given a <code>n x n</code> 2D array <code>grid</code> containing <strong>distinct</strong> elements in the range <code>[0, n<sup>2</sup> - 1]</code>.</p> <p>Implement the <code>NeighborSum</code> class:</p> <ul> <li><code>NeighborSum(int [][]grid)</code> initializes the object.</li> <li><code>int adjacentSum(int value)</code> returns the <strong>sum</strong> of elements which are adjacent neighbors of <code>value</code>, that is either to the top, left, right, or bottom of <code>value</code> in <code>grid</code>.</li> <li><code>int diagonalSum(int value)</code> returns the <strong>sum</strong> of elements which are diagonal neighbors of <code>value</code>, that is either to the top-left, top-right, bottom-left, or bottom-right of <code>value</code> in <code>grid</code>.</li> </ul> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3242.Design%20Neighbor%20Sum%20Service/images/design.png" style="width: 400px; height: 248px;" /></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong></p> <p>[&quot;NeighborSum&quot;, &quot;adjacentSum&quot;, &quot;adjacentSum&quot;, &quot;diagonalSum&quot;, &quot;diagonalSum&quot;]</p> <p>[[[[0, 1, 2], [3, 4, 5], [6, 7, 8]]], [1], [4], [4], [8]]</p> <p><strong>Output:</strong> [null, 6, 16, 16, 4]</p> <p><strong>Explanation:</strong></p> <p><strong class="example"><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3242.Design%20Neighbor%20Sum%20Service/images/designexample0.png" style="width: 250px; height: 249px;" /></strong></p> <ul> <li>The adjacent neighbors of 1 are 0, 2, and 4.</li> <li>The adjacent neighbors of 4 are 1, 3, 5, and 7.</li> <li>The diagonal neighbors of 4 are 0, 2, 6, and 8.</li> <li>The diagonal neighbor of 8 is 4.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong></p> <p>[&quot;NeighborSum&quot;, &quot;adjacentSum&quot;, &quot;diagonalSum&quot;]</p> <p>[[[[1, 2, 0, 3], [4, 7, 15, 6], [8, 9, 10, 11], [12, 13, 14, 5]]], [15], [9]]</p> <p><strong>Output:</strong> [null, 23, 45]</p> <p><strong>Explanation:</strong></p> <p><strong class="example"><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3242.Design%20Neighbor%20Sum%20Service/images/designexample2.png" style="width: 300px; height: 300px;" /></strong></p> <ul> <li>The adjacent neighbors of 15 are 0, 10, 7, and 6.</li> <li>The diagonal neighbors of 9 are 4, 12, 14, and 15.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n == grid.length == grid[0].length &lt;= 10</code></li> <li><code>0 &lt;= grid[i][j] &lt;= n<sup>2</sup> - 1</code></li> <li>All <code>grid[i][j]</code> are distinct.</li> <li><code>value</code> in <code>adjacentSum</code> and <code>diagonalSum</code> will be in the range <code>[0, n<sup>2</sup> - 1]</code>.</li> <li>At most <code>2 * n<sup>2</sup></code> calls will be made to <code>adjacentSum</code> and <code>diagonalSum</code>.</li> </ul>
Design; Array; Hash Table; Matrix; Simulation
C++
class NeighborSum { public: NeighborSum(vector<vector<int>>& grid) { this->grid = grid; int m = grid.size(), n = grid[0].size(); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { d[grid[i][j]] = {i, j}; } } } int adjacentSum(int value) { return cal(value, 0); } int diagonalSum(int value) { return cal(value, 1); } private: vector<vector<int>> grid; unordered_map<int, pair<int, int>> d; int dirs[2][5] = {{-1, 0, 1, 0, -1}, {-1, 1, 1, -1, -1}}; int cal(int value, int k) { auto [i, j] = d[value]; int s = 0; for (int q = 0; q < 4; ++q) { int x = i + dirs[k][q], y = j + dirs[k][q + 1]; if (x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size()) { s += grid[x][y]; } } return s; } }; /** * Your NeighborSum object will be instantiated and called as such: * NeighborSum* obj = new NeighborSum(grid); * int param_1 = obj->adjacentSum(value); * int param_2 = obj->diagonalSum(value); */
3,242
Design Neighbor Sum Service
Easy
<p>You are given a <code>n x n</code> 2D array <code>grid</code> containing <strong>distinct</strong> elements in the range <code>[0, n<sup>2</sup> - 1]</code>.</p> <p>Implement the <code>NeighborSum</code> class:</p> <ul> <li><code>NeighborSum(int [][]grid)</code> initializes the object.</li> <li><code>int adjacentSum(int value)</code> returns the <strong>sum</strong> of elements which are adjacent neighbors of <code>value</code>, that is either to the top, left, right, or bottom of <code>value</code> in <code>grid</code>.</li> <li><code>int diagonalSum(int value)</code> returns the <strong>sum</strong> of elements which are diagonal neighbors of <code>value</code>, that is either to the top-left, top-right, bottom-left, or bottom-right of <code>value</code> in <code>grid</code>.</li> </ul> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3242.Design%20Neighbor%20Sum%20Service/images/design.png" style="width: 400px; height: 248px;" /></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong></p> <p>[&quot;NeighborSum&quot;, &quot;adjacentSum&quot;, &quot;adjacentSum&quot;, &quot;diagonalSum&quot;, &quot;diagonalSum&quot;]</p> <p>[[[[0, 1, 2], [3, 4, 5], [6, 7, 8]]], [1], [4], [4], [8]]</p> <p><strong>Output:</strong> [null, 6, 16, 16, 4]</p> <p><strong>Explanation:</strong></p> <p><strong class="example"><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3242.Design%20Neighbor%20Sum%20Service/images/designexample0.png" style="width: 250px; height: 249px;" /></strong></p> <ul> <li>The adjacent neighbors of 1 are 0, 2, and 4.</li> <li>The adjacent neighbors of 4 are 1, 3, 5, and 7.</li> <li>The diagonal neighbors of 4 are 0, 2, 6, and 8.</li> <li>The diagonal neighbor of 8 is 4.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong></p> <p>[&quot;NeighborSum&quot;, &quot;adjacentSum&quot;, &quot;diagonalSum&quot;]</p> <p>[[[[1, 2, 0, 3], [4, 7, 15, 6], [8, 9, 10, 11], [12, 13, 14, 5]]], [15], [9]]</p> <p><strong>Output:</strong> [null, 23, 45]</p> <p><strong>Explanation:</strong></p> <p><strong class="example"><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3242.Design%20Neighbor%20Sum%20Service/images/designexample2.png" style="width: 300px; height: 300px;" /></strong></p> <ul> <li>The adjacent neighbors of 15 are 0, 10, 7, and 6.</li> <li>The diagonal neighbors of 9 are 4, 12, 14, and 15.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n == grid.length == grid[0].length &lt;= 10</code></li> <li><code>0 &lt;= grid[i][j] &lt;= n<sup>2</sup> - 1</code></li> <li>All <code>grid[i][j]</code> are distinct.</li> <li><code>value</code> in <code>adjacentSum</code> and <code>diagonalSum</code> will be in the range <code>[0, n<sup>2</sup> - 1]</code>.</li> <li>At most <code>2 * n<sup>2</sup></code> calls will be made to <code>adjacentSum</code> and <code>diagonalSum</code>.</li> </ul>
Design; Array; Hash Table; Matrix; Simulation
Go
type NeighborSum struct { grid [][]int d map[int][2]int dirs [2][5]int } func Constructor(grid [][]int) NeighborSum { d := map[int][2]int{} for i, row := range grid { for j, x := range row { d[x] = [2]int{i, j} } } dirs := [2][5]int{{-1, 0, 1, 0, -1}, {-1, 1, 1, -1, -1}} return NeighborSum{grid, d, dirs} } func (this *NeighborSum) AdjacentSum(value int) int { return this.cal(value, 0) } func (this *NeighborSum) DiagonalSum(value int) int { return this.cal(value, 1) } func (this *NeighborSum) cal(value, k int) int { p := this.d[value] s := 0 for q := 0; q < 4; q++ { x, y := p[0]+this.dirs[k][q], p[1]+this.dirs[k][q+1] if x >= 0 && x < len(this.grid) && y >= 0 && y < len(this.grid[0]) { s += this.grid[x][y] } } return s } /** * Your NeighborSum object will be instantiated and called as such: * obj := Constructor(grid); * param_1 := obj.AdjacentSum(value); * param_2 := obj.DiagonalSum(value); */
3,242
Design Neighbor Sum Service
Easy
<p>You are given a <code>n x n</code> 2D array <code>grid</code> containing <strong>distinct</strong> elements in the range <code>[0, n<sup>2</sup> - 1]</code>.</p> <p>Implement the <code>NeighborSum</code> class:</p> <ul> <li><code>NeighborSum(int [][]grid)</code> initializes the object.</li> <li><code>int adjacentSum(int value)</code> returns the <strong>sum</strong> of elements which are adjacent neighbors of <code>value</code>, that is either to the top, left, right, or bottom of <code>value</code> in <code>grid</code>.</li> <li><code>int diagonalSum(int value)</code> returns the <strong>sum</strong> of elements which are diagonal neighbors of <code>value</code>, that is either to the top-left, top-right, bottom-left, or bottom-right of <code>value</code> in <code>grid</code>.</li> </ul> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3242.Design%20Neighbor%20Sum%20Service/images/design.png" style="width: 400px; height: 248px;" /></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong></p> <p>[&quot;NeighborSum&quot;, &quot;adjacentSum&quot;, &quot;adjacentSum&quot;, &quot;diagonalSum&quot;, &quot;diagonalSum&quot;]</p> <p>[[[[0, 1, 2], [3, 4, 5], [6, 7, 8]]], [1], [4], [4], [8]]</p> <p><strong>Output:</strong> [null, 6, 16, 16, 4]</p> <p><strong>Explanation:</strong></p> <p><strong class="example"><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3242.Design%20Neighbor%20Sum%20Service/images/designexample0.png" style="width: 250px; height: 249px;" /></strong></p> <ul> <li>The adjacent neighbors of 1 are 0, 2, and 4.</li> <li>The adjacent neighbors of 4 are 1, 3, 5, and 7.</li> <li>The diagonal neighbors of 4 are 0, 2, 6, and 8.</li> <li>The diagonal neighbor of 8 is 4.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong></p> <p>[&quot;NeighborSum&quot;, &quot;adjacentSum&quot;, &quot;diagonalSum&quot;]</p> <p>[[[[1, 2, 0, 3], [4, 7, 15, 6], [8, 9, 10, 11], [12, 13, 14, 5]]], [15], [9]]</p> <p><strong>Output:</strong> [null, 23, 45]</p> <p><strong>Explanation:</strong></p> <p><strong class="example"><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3242.Design%20Neighbor%20Sum%20Service/images/designexample2.png" style="width: 300px; height: 300px;" /></strong></p> <ul> <li>The adjacent neighbors of 15 are 0, 10, 7, and 6.</li> <li>The diagonal neighbors of 9 are 4, 12, 14, and 15.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n == grid.length == grid[0].length &lt;= 10</code></li> <li><code>0 &lt;= grid[i][j] &lt;= n<sup>2</sup> - 1</code></li> <li>All <code>grid[i][j]</code> are distinct.</li> <li><code>value</code> in <code>adjacentSum</code> and <code>diagonalSum</code> will be in the range <code>[0, n<sup>2</sup> - 1]</code>.</li> <li>At most <code>2 * n<sup>2</sup></code> calls will be made to <code>adjacentSum</code> and <code>diagonalSum</code>.</li> </ul>
Design; Array; Hash Table; Matrix; Simulation
Java
class NeighborSum { private int[][] grid; private final Map<Integer, int[]> d = new HashMap<>(); private final int[][] dirs = {{-1, 0, 1, 0, -1}, {-1, 1, 1, -1, -1}}; public NeighborSum(int[][] grid) { this.grid = grid; int m = grid.length, n = grid[0].length; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { d.put(grid[i][j], new int[] {i, j}); } } } public int adjacentSum(int value) { return cal(value, 0); } public int diagonalSum(int value) { return cal(value, 1); } private int cal(int value, int k) { int[] p = d.get(value); int s = 0; for (int q = 0; q < 4; ++q) { int x = p[0] + dirs[k][q], y = p[1] + dirs[k][q + 1]; if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length) { s += grid[x][y]; } } return s; } } /** * Your NeighborSum object will be instantiated and called as such: * NeighborSum obj = new NeighborSum(grid); * int param_1 = obj.adjacentSum(value); * int param_2 = obj.diagonalSum(value); */
3,242
Design Neighbor Sum Service
Easy
<p>You are given a <code>n x n</code> 2D array <code>grid</code> containing <strong>distinct</strong> elements in the range <code>[0, n<sup>2</sup> - 1]</code>.</p> <p>Implement the <code>NeighborSum</code> class:</p> <ul> <li><code>NeighborSum(int [][]grid)</code> initializes the object.</li> <li><code>int adjacentSum(int value)</code> returns the <strong>sum</strong> of elements which are adjacent neighbors of <code>value</code>, that is either to the top, left, right, or bottom of <code>value</code> in <code>grid</code>.</li> <li><code>int diagonalSum(int value)</code> returns the <strong>sum</strong> of elements which are diagonal neighbors of <code>value</code>, that is either to the top-left, top-right, bottom-left, or bottom-right of <code>value</code> in <code>grid</code>.</li> </ul> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3242.Design%20Neighbor%20Sum%20Service/images/design.png" style="width: 400px; height: 248px;" /></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong></p> <p>[&quot;NeighborSum&quot;, &quot;adjacentSum&quot;, &quot;adjacentSum&quot;, &quot;diagonalSum&quot;, &quot;diagonalSum&quot;]</p> <p>[[[[0, 1, 2], [3, 4, 5], [6, 7, 8]]], [1], [4], [4], [8]]</p> <p><strong>Output:</strong> [null, 6, 16, 16, 4]</p> <p><strong>Explanation:</strong></p> <p><strong class="example"><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3242.Design%20Neighbor%20Sum%20Service/images/designexample0.png" style="width: 250px; height: 249px;" /></strong></p> <ul> <li>The adjacent neighbors of 1 are 0, 2, and 4.</li> <li>The adjacent neighbors of 4 are 1, 3, 5, and 7.</li> <li>The diagonal neighbors of 4 are 0, 2, 6, and 8.</li> <li>The diagonal neighbor of 8 is 4.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong></p> <p>[&quot;NeighborSum&quot;, &quot;adjacentSum&quot;, &quot;diagonalSum&quot;]</p> <p>[[[[1, 2, 0, 3], [4, 7, 15, 6], [8, 9, 10, 11], [12, 13, 14, 5]]], [15], [9]]</p> <p><strong>Output:</strong> [null, 23, 45]</p> <p><strong>Explanation:</strong></p> <p><strong class="example"><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3242.Design%20Neighbor%20Sum%20Service/images/designexample2.png" style="width: 300px; height: 300px;" /></strong></p> <ul> <li>The adjacent neighbors of 15 are 0, 10, 7, and 6.</li> <li>The diagonal neighbors of 9 are 4, 12, 14, and 15.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n == grid.length == grid[0].length &lt;= 10</code></li> <li><code>0 &lt;= grid[i][j] &lt;= n<sup>2</sup> - 1</code></li> <li>All <code>grid[i][j]</code> are distinct.</li> <li><code>value</code> in <code>adjacentSum</code> and <code>diagonalSum</code> will be in the range <code>[0, n<sup>2</sup> - 1]</code>.</li> <li>At most <code>2 * n<sup>2</sup></code> calls will be made to <code>adjacentSum</code> and <code>diagonalSum</code>.</li> </ul>
Design; Array; Hash Table; Matrix; Simulation
Python
class NeighborSum: def __init__(self, grid: List[List[int]]): self.grid = grid self.d = {} self.dirs = ((-1, 0, 1, 0, -1), (-1, 1, 1, -1, -1)) for i, row in enumerate(grid): for j, x in enumerate(row): self.d[x] = (i, j) def adjacentSum(self, value: int) -> int: return self.cal(value, 0) def cal(self, value: int, k: int): i, j = self.d[value] s = 0 for a, b in pairwise(self.dirs[k]): x, y = i + a, j + b if 0 <= x < len(self.grid) and 0 <= y < len(self.grid[0]): s += self.grid[x][y] return s def diagonalSum(self, value: int) -> int: return self.cal(value, 1) # Your NeighborSum object will be instantiated and called as such: # obj = NeighborSum(grid) # param_1 = obj.adjacentSum(value) # param_2 = obj.diagonalSum(value)
3,242
Design Neighbor Sum Service
Easy
<p>You are given a <code>n x n</code> 2D array <code>grid</code> containing <strong>distinct</strong> elements in the range <code>[0, n<sup>2</sup> - 1]</code>.</p> <p>Implement the <code>NeighborSum</code> class:</p> <ul> <li><code>NeighborSum(int [][]grid)</code> initializes the object.</li> <li><code>int adjacentSum(int value)</code> returns the <strong>sum</strong> of elements which are adjacent neighbors of <code>value</code>, that is either to the top, left, right, or bottom of <code>value</code> in <code>grid</code>.</li> <li><code>int diagonalSum(int value)</code> returns the <strong>sum</strong> of elements which are diagonal neighbors of <code>value</code>, that is either to the top-left, top-right, bottom-left, or bottom-right of <code>value</code> in <code>grid</code>.</li> </ul> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3242.Design%20Neighbor%20Sum%20Service/images/design.png" style="width: 400px; height: 248px;" /></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong></p> <p>[&quot;NeighborSum&quot;, &quot;adjacentSum&quot;, &quot;adjacentSum&quot;, &quot;diagonalSum&quot;, &quot;diagonalSum&quot;]</p> <p>[[[[0, 1, 2], [3, 4, 5], [6, 7, 8]]], [1], [4], [4], [8]]</p> <p><strong>Output:</strong> [null, 6, 16, 16, 4]</p> <p><strong>Explanation:</strong></p> <p><strong class="example"><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3242.Design%20Neighbor%20Sum%20Service/images/designexample0.png" style="width: 250px; height: 249px;" /></strong></p> <ul> <li>The adjacent neighbors of 1 are 0, 2, and 4.</li> <li>The adjacent neighbors of 4 are 1, 3, 5, and 7.</li> <li>The diagonal neighbors of 4 are 0, 2, 6, and 8.</li> <li>The diagonal neighbor of 8 is 4.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong></p> <p>[&quot;NeighborSum&quot;, &quot;adjacentSum&quot;, &quot;diagonalSum&quot;]</p> <p>[[[[1, 2, 0, 3], [4, 7, 15, 6], [8, 9, 10, 11], [12, 13, 14, 5]]], [15], [9]]</p> <p><strong>Output:</strong> [null, 23, 45]</p> <p><strong>Explanation:</strong></p> <p><strong class="example"><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3242.Design%20Neighbor%20Sum%20Service/images/designexample2.png" style="width: 300px; height: 300px;" /></strong></p> <ul> <li>The adjacent neighbors of 15 are 0, 10, 7, and 6.</li> <li>The diagonal neighbors of 9 are 4, 12, 14, and 15.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n == grid.length == grid[0].length &lt;= 10</code></li> <li><code>0 &lt;= grid[i][j] &lt;= n<sup>2</sup> - 1</code></li> <li>All <code>grid[i][j]</code> are distinct.</li> <li><code>value</code> in <code>adjacentSum</code> and <code>diagonalSum</code> will be in the range <code>[0, n<sup>2</sup> - 1]</code>.</li> <li>At most <code>2 * n<sup>2</sup></code> calls will be made to <code>adjacentSum</code> and <code>diagonalSum</code>.</li> </ul>
Design; Array; Hash Table; Matrix; Simulation
TypeScript
class NeighborSum { private grid: number[][]; private d: Map<number, [number, number]> = new Map(); private dirs: number[][] = [ [-1, 0, 1, 0, -1], [-1, 1, 1, -1, -1], ]; constructor(grid: number[][]) { for (let i = 0; i < grid.length; ++i) { for (let j = 0; j < grid[0].length; ++j) { this.d.set(grid[i][j], [i, j]); } } this.grid = grid; } adjacentSum(value: number): number { return this.cal(value, 0); } diagonalSum(value: number): number { return this.cal(value, 1); } cal(value: number, k: number): number { const [i, j] = this.d.get(value)!; let s = 0; for (let q = 0; q < 4; ++q) { const [x, y] = [i + this.dirs[k][q], j + this.dirs[k][q + 1]]; if (x >= 0 && x < this.grid.length && y >= 0 && y < this.grid[0].length) { s += this.grid[x][y]; } } return s; } } /** * Your NeighborSum object will be instantiated and called as such: * var obj = new NeighborSum(grid) * var param_1 = obj.adjacentSum(value) * var param_2 = obj.diagonalSum(value) */
3,243
Shortest Distance After Road Addition Queries I
Medium
<p>You are given an integer <code>n</code> and a 2D integer array <code>queries</code>.</p> <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code>. Initially, there is a <strong>unidirectional</strong> road from city <code>i</code> to city <code>i + 1</code> for all <code>0 &lt;= i &lt; n - 1</code>.</p> <p><code>queries[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> represents the addition of a new <strong>unidirectional</strong> road from city <code>u<sub>i</sub></code> to city <code>v<sub>i</sub></code>. After each query, you need to find the <strong>length</strong> of the <strong>shortest path</strong> from city <code>0</code> to city <code>n - 1</code>.</p> <p>Return an array <code>answer</code> where for each <code>i</code> in the range <code>[0, queries.length - 1]</code>, <code>answer[i]</code> is the <em>length of the shortest path</em> from city <code>0</code> to city <code>n - 1</code> after processing the <strong>first </strong><code>i + 1</code> queries.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, queries = [[2,4],[0,2],[0,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p> <p><strong>Explanation: </strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image8.jpg" style="width: 350px; height: 60px;" /></p> <p>After the addition of the road from 2 to 4, the length of the shortest path from 0 to 4 is 3.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image9.jpg" style="width: 350px; height: 60px;" /></p> <p>After the addition of the road from 0 to 2, the length of the shortest path from 0 to 4 is 2.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image10.jpg" style="width: 350px; height: 96px;" /></p> <p>After the addition of the road from 0 to 4, the length of the shortest path from 0 to 4 is 1.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4, queries = [[0,3],[0,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">[1,1]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image11.jpg" style="width: 300px; height: 70px;" /></p> <p>After the addition of the road from 0 to 3, the length of the shortest path from 0 to 3 is 1.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image12.jpg" style="width: 300px; height: 70px;" /></p> <p>After the addition of the road from 0 to 2, the length of the shortest path remains 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 500</code></li> <li><code>1 &lt;= queries.length &lt;= 500</code></li> <li><code>queries[i].length == 2</code></li> <li><code>0 &lt;= queries[i][0] &lt; queries[i][1] &lt; n</code></li> <li><code>1 &lt; queries[i][1] - queries[i][0]</code></li> <li>There are no repeated roads among the queries.</li> </ul>
Breadth-First Search; Graph; Array
C++
class Solution { public: vector<int> shortestDistanceAfterQueries(int n, vector<vector<int>>& queries) { vector<int> g[n]; for (int i = 0; i < n - 1; ++i) { g[i].push_back(i + 1); } auto bfs = [&](int i) -> int { queue<int> q{{i}}; vector<bool> vis(n); vis[i] = true; for (int d = 0;; ++d) { for (int k = q.size(); k; --k) { int u = q.front(); q.pop(); if (u == n - 1) { return d; } for (int v : g[u]) { if (!vis[v]) { vis[v] = true; q.push(v); } } } } }; vector<int> ans; for (const auto& q : queries) { g[q[0]].push_back(q[1]); ans.push_back(bfs(0)); } return ans; } };
3,243
Shortest Distance After Road Addition Queries I
Medium
<p>You are given an integer <code>n</code> and a 2D integer array <code>queries</code>.</p> <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code>. Initially, there is a <strong>unidirectional</strong> road from city <code>i</code> to city <code>i + 1</code> for all <code>0 &lt;= i &lt; n - 1</code>.</p> <p><code>queries[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> represents the addition of a new <strong>unidirectional</strong> road from city <code>u<sub>i</sub></code> to city <code>v<sub>i</sub></code>. After each query, you need to find the <strong>length</strong> of the <strong>shortest path</strong> from city <code>0</code> to city <code>n - 1</code>.</p> <p>Return an array <code>answer</code> where for each <code>i</code> in the range <code>[0, queries.length - 1]</code>, <code>answer[i]</code> is the <em>length of the shortest path</em> from city <code>0</code> to city <code>n - 1</code> after processing the <strong>first </strong><code>i + 1</code> queries.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, queries = [[2,4],[0,2],[0,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p> <p><strong>Explanation: </strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image8.jpg" style="width: 350px; height: 60px;" /></p> <p>After the addition of the road from 2 to 4, the length of the shortest path from 0 to 4 is 3.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image9.jpg" style="width: 350px; height: 60px;" /></p> <p>After the addition of the road from 0 to 2, the length of the shortest path from 0 to 4 is 2.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image10.jpg" style="width: 350px; height: 96px;" /></p> <p>After the addition of the road from 0 to 4, the length of the shortest path from 0 to 4 is 1.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4, queries = [[0,3],[0,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">[1,1]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image11.jpg" style="width: 300px; height: 70px;" /></p> <p>After the addition of the road from 0 to 3, the length of the shortest path from 0 to 3 is 1.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image12.jpg" style="width: 300px; height: 70px;" /></p> <p>After the addition of the road from 0 to 2, the length of the shortest path remains 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 500</code></li> <li><code>1 &lt;= queries.length &lt;= 500</code></li> <li><code>queries[i].length == 2</code></li> <li><code>0 &lt;= queries[i][0] &lt; queries[i][1] &lt; n</code></li> <li><code>1 &lt; queries[i][1] - queries[i][0]</code></li> <li>There are no repeated roads among the queries.</li> </ul>
Breadth-First Search; Graph; Array
Go
func shortestDistanceAfterQueries(n int, queries [][]int) []int { g := make([][]int, n) for i := range g { g[i] = append(g[i], i+1) } bfs := func(i int) int { q := []int{i} vis := make([]bool, n) vis[i] = true for d := 0; ; d++ { for k := len(q); k > 0; k-- { u := q[0] if u == n-1 { return d } q = q[1:] for _, v := range g[u] { if !vis[v] { vis[v] = true q = append(q, v) } } } } } ans := make([]int, len(queries)) for i, q := range queries { g[q[0]] = append(g[q[0]], q[1]) ans[i] = bfs(0) } return ans }
3,243
Shortest Distance After Road Addition Queries I
Medium
<p>You are given an integer <code>n</code> and a 2D integer array <code>queries</code>.</p> <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code>. Initially, there is a <strong>unidirectional</strong> road from city <code>i</code> to city <code>i + 1</code> for all <code>0 &lt;= i &lt; n - 1</code>.</p> <p><code>queries[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> represents the addition of a new <strong>unidirectional</strong> road from city <code>u<sub>i</sub></code> to city <code>v<sub>i</sub></code>. After each query, you need to find the <strong>length</strong> of the <strong>shortest path</strong> from city <code>0</code> to city <code>n - 1</code>.</p> <p>Return an array <code>answer</code> where for each <code>i</code> in the range <code>[0, queries.length - 1]</code>, <code>answer[i]</code> is the <em>length of the shortest path</em> from city <code>0</code> to city <code>n - 1</code> after processing the <strong>first </strong><code>i + 1</code> queries.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, queries = [[2,4],[0,2],[0,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p> <p><strong>Explanation: </strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image8.jpg" style="width: 350px; height: 60px;" /></p> <p>After the addition of the road from 2 to 4, the length of the shortest path from 0 to 4 is 3.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image9.jpg" style="width: 350px; height: 60px;" /></p> <p>After the addition of the road from 0 to 2, the length of the shortest path from 0 to 4 is 2.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image10.jpg" style="width: 350px; height: 96px;" /></p> <p>After the addition of the road from 0 to 4, the length of the shortest path from 0 to 4 is 1.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4, queries = [[0,3],[0,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">[1,1]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image11.jpg" style="width: 300px; height: 70px;" /></p> <p>After the addition of the road from 0 to 3, the length of the shortest path from 0 to 3 is 1.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image12.jpg" style="width: 300px; height: 70px;" /></p> <p>After the addition of the road from 0 to 2, the length of the shortest path remains 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 500</code></li> <li><code>1 &lt;= queries.length &lt;= 500</code></li> <li><code>queries[i].length == 2</code></li> <li><code>0 &lt;= queries[i][0] &lt; queries[i][1] &lt; n</code></li> <li><code>1 &lt; queries[i][1] - queries[i][0]</code></li> <li>There are no repeated roads among the queries.</li> </ul>
Breadth-First Search; Graph; Array
Java
class Solution { private List<Integer>[] g; private int n; public int[] shortestDistanceAfterQueries(int n, int[][] queries) { this.n = n; g = new List[n]; Arrays.setAll(g, i -> new ArrayList<>()); for (int i = 0; i < n - 1; ++i) { g[i].add(i + 1); } int m = queries.length; int[] ans = new int[m]; for (int i = 0; i < m; ++i) { int u = queries[i][0], v = queries[i][1]; g[u].add(v); ans[i] = bfs(0); } return ans; } private int bfs(int i) { Deque<Integer> q = new ArrayDeque<>(); q.offer(i); boolean[] vis = new boolean[n]; vis[i] = true; for (int d = 0;; ++d) { for (int k = q.size(); k > 0; --k) { int u = q.poll(); if (u == n - 1) { return d; } for (int v : g[u]) { if (!vis[v]) { vis[v] = true; q.offer(v); } } } } } }
3,243
Shortest Distance After Road Addition Queries I
Medium
<p>You are given an integer <code>n</code> and a 2D integer array <code>queries</code>.</p> <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code>. Initially, there is a <strong>unidirectional</strong> road from city <code>i</code> to city <code>i + 1</code> for all <code>0 &lt;= i &lt; n - 1</code>.</p> <p><code>queries[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> represents the addition of a new <strong>unidirectional</strong> road from city <code>u<sub>i</sub></code> to city <code>v<sub>i</sub></code>. After each query, you need to find the <strong>length</strong> of the <strong>shortest path</strong> from city <code>0</code> to city <code>n - 1</code>.</p> <p>Return an array <code>answer</code> where for each <code>i</code> in the range <code>[0, queries.length - 1]</code>, <code>answer[i]</code> is the <em>length of the shortest path</em> from city <code>0</code> to city <code>n - 1</code> after processing the <strong>first </strong><code>i + 1</code> queries.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, queries = [[2,4],[0,2],[0,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p> <p><strong>Explanation: </strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image8.jpg" style="width: 350px; height: 60px;" /></p> <p>After the addition of the road from 2 to 4, the length of the shortest path from 0 to 4 is 3.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image9.jpg" style="width: 350px; height: 60px;" /></p> <p>After the addition of the road from 0 to 2, the length of the shortest path from 0 to 4 is 2.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image10.jpg" style="width: 350px; height: 96px;" /></p> <p>After the addition of the road from 0 to 4, the length of the shortest path from 0 to 4 is 1.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4, queries = [[0,3],[0,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">[1,1]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image11.jpg" style="width: 300px; height: 70px;" /></p> <p>After the addition of the road from 0 to 3, the length of the shortest path from 0 to 3 is 1.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image12.jpg" style="width: 300px; height: 70px;" /></p> <p>After the addition of the road from 0 to 2, the length of the shortest path remains 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 500</code></li> <li><code>1 &lt;= queries.length &lt;= 500</code></li> <li><code>queries[i].length == 2</code></li> <li><code>0 &lt;= queries[i][0] &lt; queries[i][1] &lt; n</code></li> <li><code>1 &lt; queries[i][1] - queries[i][0]</code></li> <li>There are no repeated roads among the queries.</li> </ul>
Breadth-First Search; Graph; Array
Python
class Solution: def shortestDistanceAfterQueries( self, n: int, queries: List[List[int]] ) -> List[int]: def bfs(i: int) -> int: q = deque([i]) vis = [False] * n vis[i] = True d = 0 while 1: for _ in range(len(q)): u = q.popleft() if u == n - 1: return d for v in g[u]: if not vis[v]: vis[v] = True q.append(v) d += 1 g = [[i + 1] for i in range(n - 1)] ans = [] for u, v in queries: g[u].append(v) ans.append(bfs(0)) return ans
3,243
Shortest Distance After Road Addition Queries I
Medium
<p>You are given an integer <code>n</code> and a 2D integer array <code>queries</code>.</p> <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code>. Initially, there is a <strong>unidirectional</strong> road from city <code>i</code> to city <code>i + 1</code> for all <code>0 &lt;= i &lt; n - 1</code>.</p> <p><code>queries[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> represents the addition of a new <strong>unidirectional</strong> road from city <code>u<sub>i</sub></code> to city <code>v<sub>i</sub></code>. After each query, you need to find the <strong>length</strong> of the <strong>shortest path</strong> from city <code>0</code> to city <code>n - 1</code>.</p> <p>Return an array <code>answer</code> where for each <code>i</code> in the range <code>[0, queries.length - 1]</code>, <code>answer[i]</code> is the <em>length of the shortest path</em> from city <code>0</code> to city <code>n - 1</code> after processing the <strong>first </strong><code>i + 1</code> queries.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, queries = [[2,4],[0,2],[0,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p> <p><strong>Explanation: </strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image8.jpg" style="width: 350px; height: 60px;" /></p> <p>After the addition of the road from 2 to 4, the length of the shortest path from 0 to 4 is 3.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image9.jpg" style="width: 350px; height: 60px;" /></p> <p>After the addition of the road from 0 to 2, the length of the shortest path from 0 to 4 is 2.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image10.jpg" style="width: 350px; height: 96px;" /></p> <p>After the addition of the road from 0 to 4, the length of the shortest path from 0 to 4 is 1.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4, queries = [[0,3],[0,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">[1,1]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image11.jpg" style="width: 300px; height: 70px;" /></p> <p>After the addition of the road from 0 to 3, the length of the shortest path from 0 to 3 is 1.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3243.Shortest%20Distance%20After%20Road%20Addition%20Queries%20I/images/image12.jpg" style="width: 300px; height: 70px;" /></p> <p>After the addition of the road from 0 to 2, the length of the shortest path remains 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 500</code></li> <li><code>1 &lt;= queries.length &lt;= 500</code></li> <li><code>queries[i].length == 2</code></li> <li><code>0 &lt;= queries[i][0] &lt; queries[i][1] &lt; n</code></li> <li><code>1 &lt; queries[i][1] - queries[i][0]</code></li> <li>There are no repeated roads among the queries.</li> </ul>
Breadth-First Search; Graph; Array
TypeScript
function shortestDistanceAfterQueries(n: number, queries: number[][]): number[] { const g: number[][] = Array.from({ length: n }, () => []); for (let i = 0; i < n - 1; ++i) { g[i].push(i + 1); } const bfs = (i: number): number => { const q: number[] = [i]; const vis: boolean[] = Array(n).fill(false); vis[i] = true; for (let d = 0; ; ++d) { const nq: number[] = []; for (const u of q) { if (u === n - 1) { return d; } for (const v of g[u]) { if (!vis[v]) { vis[v] = true; nq.push(v); } } } q.splice(0, q.length, ...nq); } }; const ans: number[] = []; for (const [u, v] of queries) { g[u].push(v); ans.push(bfs(0)); } return ans; }
3,244
Shortest Distance After Road Addition Queries II
Hard
<p>You are given an integer <code>n</code> and a 2D integer array <code>queries</code>.</p> <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code>. Initially, there is a <strong>unidirectional</strong> road from city <code>i</code> to city <code>i + 1</code> for all <code>0 &lt;= i &lt; n - 1</code>.</p> <p><code>queries[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> represents the addition of a new <strong>unidirectional</strong> road from city <code>u<sub>i</sub></code> to city <code>v<sub>i</sub></code>. After each query, you need to find the <strong>length</strong> of the <strong>shortest path</strong> from city <code>0</code> to city <code>n - 1</code>.</p> <p>There are no two queries such that <code>queries[i][0] &lt; queries[j][0] &lt; queries[i][1] &lt; queries[j][1]</code>.</p> <p>Return an array <code>answer</code> where for each <code>i</code> in the range <code>[0, queries.length - 1]</code>, <code>answer[i]</code> is the <em>length of the shortest path</em> from city <code>0</code> to city <code>n - 1</code> after processing the <strong>first </strong><code>i + 1</code> queries.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, queries = [[2,4],[0,2],[0,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p> <p><strong>Explanation: </strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image8.jpg" style="width: 350px; height: 60px;" /></p> <p>After the addition of the road from 2 to 4, the length of the shortest path from 0 to 4 is 3.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image9.jpg" style="width: 350px; height: 60px;" /></p> <p>After the addition of the road from 0 to 2, the length of the shortest path from 0 to 4 is 2.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image10.jpg" style="width: 350px; height: 96px;" /></p> <p>After the addition of the road from 0 to 4, the length of the shortest path from 0 to 4 is 1.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4, queries = [[0,3],[0,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">[1,1]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image11.jpg" style="width: 300px; height: 70px;" /></p> <p>After the addition of the road from 0 to 3, the length of the shortest path from 0 to 3 is 1.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image12.jpg" style="width: 300px; height: 70px;" /></p> <p>After the addition of the road from 0 to 2, the length of the shortest path remains 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2</code></li> <li><code>0 &lt;= queries[i][0] &lt; queries[i][1] &lt; n</code></li> <li><code>1 &lt; queries[i][1] - queries[i][0]</code></li> <li>There are no repeated roads among the queries.</li> <li>There are no two queries such that <code>i != j</code> and <code>queries[i][0] &lt; queries[j][0] &lt; queries[i][1] &lt; queries[j][1]</code>.</li> </ul>
Greedy; Graph; Array; Ordered Set
C++
class Solution { public: vector<int> shortestDistanceAfterQueries(int n, vector<vector<int>>& queries) { vector<int> nxt(n - 1); iota(nxt.begin(), nxt.end(), 1); int cnt = n - 1; vector<int> ans; for (const auto& q : queries) { int u = q[0], v = q[1]; if (nxt[u] && nxt[u] < v) { int i = nxt[u]; while (i < v) { --cnt; int t = nxt[i]; nxt[i] = 0; i = t; } nxt[u] = v; } ans.push_back(cnt); } return ans; } };
3,244
Shortest Distance After Road Addition Queries II
Hard
<p>You are given an integer <code>n</code> and a 2D integer array <code>queries</code>.</p> <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code>. Initially, there is a <strong>unidirectional</strong> road from city <code>i</code> to city <code>i + 1</code> for all <code>0 &lt;= i &lt; n - 1</code>.</p> <p><code>queries[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> represents the addition of a new <strong>unidirectional</strong> road from city <code>u<sub>i</sub></code> to city <code>v<sub>i</sub></code>. After each query, you need to find the <strong>length</strong> of the <strong>shortest path</strong> from city <code>0</code> to city <code>n - 1</code>.</p> <p>There are no two queries such that <code>queries[i][0] &lt; queries[j][0] &lt; queries[i][1] &lt; queries[j][1]</code>.</p> <p>Return an array <code>answer</code> where for each <code>i</code> in the range <code>[0, queries.length - 1]</code>, <code>answer[i]</code> is the <em>length of the shortest path</em> from city <code>0</code> to city <code>n - 1</code> after processing the <strong>first </strong><code>i + 1</code> queries.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, queries = [[2,4],[0,2],[0,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p> <p><strong>Explanation: </strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image8.jpg" style="width: 350px; height: 60px;" /></p> <p>After the addition of the road from 2 to 4, the length of the shortest path from 0 to 4 is 3.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image9.jpg" style="width: 350px; height: 60px;" /></p> <p>After the addition of the road from 0 to 2, the length of the shortest path from 0 to 4 is 2.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image10.jpg" style="width: 350px; height: 96px;" /></p> <p>After the addition of the road from 0 to 4, the length of the shortest path from 0 to 4 is 1.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4, queries = [[0,3],[0,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">[1,1]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image11.jpg" style="width: 300px; height: 70px;" /></p> <p>After the addition of the road from 0 to 3, the length of the shortest path from 0 to 3 is 1.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image12.jpg" style="width: 300px; height: 70px;" /></p> <p>After the addition of the road from 0 to 2, the length of the shortest path remains 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2</code></li> <li><code>0 &lt;= queries[i][0] &lt; queries[i][1] &lt; n</code></li> <li><code>1 &lt; queries[i][1] - queries[i][0]</code></li> <li>There are no repeated roads among the queries.</li> <li>There are no two queries such that <code>i != j</code> and <code>queries[i][0] &lt; queries[j][0] &lt; queries[i][1] &lt; queries[j][1]</code>.</li> </ul>
Greedy; Graph; Array; Ordered Set
Go
func shortestDistanceAfterQueries(n int, queries [][]int) (ans []int) { nxt := make([]int, n-1) for i := range nxt { nxt[i] = i + 1 } cnt := n - 1 for _, q := range queries { u, v := q[0], q[1] if nxt[u] > 0 && nxt[u] < v { i := nxt[u] for i < v { cnt-- nxt[i], i = 0, nxt[i] } nxt[u] = v } ans = append(ans, cnt) } return }
3,244
Shortest Distance After Road Addition Queries II
Hard
<p>You are given an integer <code>n</code> and a 2D integer array <code>queries</code>.</p> <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code>. Initially, there is a <strong>unidirectional</strong> road from city <code>i</code> to city <code>i + 1</code> for all <code>0 &lt;= i &lt; n - 1</code>.</p> <p><code>queries[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> represents the addition of a new <strong>unidirectional</strong> road from city <code>u<sub>i</sub></code> to city <code>v<sub>i</sub></code>. After each query, you need to find the <strong>length</strong> of the <strong>shortest path</strong> from city <code>0</code> to city <code>n - 1</code>.</p> <p>There are no two queries such that <code>queries[i][0] &lt; queries[j][0] &lt; queries[i][1] &lt; queries[j][1]</code>.</p> <p>Return an array <code>answer</code> where for each <code>i</code> in the range <code>[0, queries.length - 1]</code>, <code>answer[i]</code> is the <em>length of the shortest path</em> from city <code>0</code> to city <code>n - 1</code> after processing the <strong>first </strong><code>i + 1</code> queries.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, queries = [[2,4],[0,2],[0,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p> <p><strong>Explanation: </strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image8.jpg" style="width: 350px; height: 60px;" /></p> <p>After the addition of the road from 2 to 4, the length of the shortest path from 0 to 4 is 3.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image9.jpg" style="width: 350px; height: 60px;" /></p> <p>After the addition of the road from 0 to 2, the length of the shortest path from 0 to 4 is 2.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image10.jpg" style="width: 350px; height: 96px;" /></p> <p>After the addition of the road from 0 to 4, the length of the shortest path from 0 to 4 is 1.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4, queries = [[0,3],[0,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">[1,1]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image11.jpg" style="width: 300px; height: 70px;" /></p> <p>After the addition of the road from 0 to 3, the length of the shortest path from 0 to 3 is 1.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image12.jpg" style="width: 300px; height: 70px;" /></p> <p>After the addition of the road from 0 to 2, the length of the shortest path remains 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2</code></li> <li><code>0 &lt;= queries[i][0] &lt; queries[i][1] &lt; n</code></li> <li><code>1 &lt; queries[i][1] - queries[i][0]</code></li> <li>There are no repeated roads among the queries.</li> <li>There are no two queries such that <code>i != j</code> and <code>queries[i][0] &lt; queries[j][0] &lt; queries[i][1] &lt; queries[j][1]</code>.</li> </ul>
Greedy; Graph; Array; Ordered Set
Java
class Solution { public int[] shortestDistanceAfterQueries(int n, int[][] queries) { int[] nxt = new int[n - 1]; for (int i = 1; i < n; ++i) { nxt[i - 1] = i; } int m = queries.length; int cnt = n - 1; int[] ans = new int[m]; for (int i = 0; i < m; ++i) { int u = queries[i][0], v = queries[i][1]; if (nxt[u] > 0 && nxt[u] < v) { int j = nxt[u]; while (j < v) { --cnt; int t = nxt[j]; nxt[j] = 0; j = t; } nxt[u] = v; } ans[i] = cnt; } return ans; } }
3,244
Shortest Distance After Road Addition Queries II
Hard
<p>You are given an integer <code>n</code> and a 2D integer array <code>queries</code>.</p> <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code>. Initially, there is a <strong>unidirectional</strong> road from city <code>i</code> to city <code>i + 1</code> for all <code>0 &lt;= i &lt; n - 1</code>.</p> <p><code>queries[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> represents the addition of a new <strong>unidirectional</strong> road from city <code>u<sub>i</sub></code> to city <code>v<sub>i</sub></code>. After each query, you need to find the <strong>length</strong> of the <strong>shortest path</strong> from city <code>0</code> to city <code>n - 1</code>.</p> <p>There are no two queries such that <code>queries[i][0] &lt; queries[j][0] &lt; queries[i][1] &lt; queries[j][1]</code>.</p> <p>Return an array <code>answer</code> where for each <code>i</code> in the range <code>[0, queries.length - 1]</code>, <code>answer[i]</code> is the <em>length of the shortest path</em> from city <code>0</code> to city <code>n - 1</code> after processing the <strong>first </strong><code>i + 1</code> queries.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, queries = [[2,4],[0,2],[0,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p> <p><strong>Explanation: </strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image8.jpg" style="width: 350px; height: 60px;" /></p> <p>After the addition of the road from 2 to 4, the length of the shortest path from 0 to 4 is 3.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image9.jpg" style="width: 350px; height: 60px;" /></p> <p>After the addition of the road from 0 to 2, the length of the shortest path from 0 to 4 is 2.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image10.jpg" style="width: 350px; height: 96px;" /></p> <p>After the addition of the road from 0 to 4, the length of the shortest path from 0 to 4 is 1.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4, queries = [[0,3],[0,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">[1,1]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image11.jpg" style="width: 300px; height: 70px;" /></p> <p>After the addition of the road from 0 to 3, the length of the shortest path from 0 to 3 is 1.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image12.jpg" style="width: 300px; height: 70px;" /></p> <p>After the addition of the road from 0 to 2, the length of the shortest path remains 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2</code></li> <li><code>0 &lt;= queries[i][0] &lt; queries[i][1] &lt; n</code></li> <li><code>1 &lt; queries[i][1] - queries[i][0]</code></li> <li>There are no repeated roads among the queries.</li> <li>There are no two queries such that <code>i != j</code> and <code>queries[i][0] &lt; queries[j][0] &lt; queries[i][1] &lt; queries[j][1]</code>.</li> </ul>
Greedy; Graph; Array; Ordered Set
Python
class Solution: def shortestDistanceAfterQueries( self, n: int, queries: List[List[int]] ) -> List[int]: nxt = list(range(1, n)) ans = [] cnt = n - 1 for u, v in queries: if 0 < nxt[u] < v: i = nxt[u] while i < v: cnt -= 1 nxt[i], i = 0, nxt[i] nxt[u] = v ans.append(cnt) return ans
3,244
Shortest Distance After Road Addition Queries II
Hard
<p>You are given an integer <code>n</code> and a 2D integer array <code>queries</code>.</p> <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code>. Initially, there is a <strong>unidirectional</strong> road from city <code>i</code> to city <code>i + 1</code> for all <code>0 &lt;= i &lt; n - 1</code>.</p> <p><code>queries[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> represents the addition of a new <strong>unidirectional</strong> road from city <code>u<sub>i</sub></code> to city <code>v<sub>i</sub></code>. After each query, you need to find the <strong>length</strong> of the <strong>shortest path</strong> from city <code>0</code> to city <code>n - 1</code>.</p> <p>There are no two queries such that <code>queries[i][0] &lt; queries[j][0] &lt; queries[i][1] &lt; queries[j][1]</code>.</p> <p>Return an array <code>answer</code> where for each <code>i</code> in the range <code>[0, queries.length - 1]</code>, <code>answer[i]</code> is the <em>length of the shortest path</em> from city <code>0</code> to city <code>n - 1</code> after processing the <strong>first </strong><code>i + 1</code> queries.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, queries = [[2,4],[0,2],[0,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p> <p><strong>Explanation: </strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image8.jpg" style="width: 350px; height: 60px;" /></p> <p>After the addition of the road from 2 to 4, the length of the shortest path from 0 to 4 is 3.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image9.jpg" style="width: 350px; height: 60px;" /></p> <p>After the addition of the road from 0 to 2, the length of the shortest path from 0 to 4 is 2.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image10.jpg" style="width: 350px; height: 96px;" /></p> <p>After the addition of the road from 0 to 4, the length of the shortest path from 0 to 4 is 1.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4, queries = [[0,3],[0,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">[1,1]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image11.jpg" style="width: 300px; height: 70px;" /></p> <p>After the addition of the road from 0 to 3, the length of the shortest path from 0 to 3 is 1.</p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/images/image12.jpg" style="width: 300px; height: 70px;" /></p> <p>After the addition of the road from 0 to 2, the length of the shortest path remains 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2</code></li> <li><code>0 &lt;= queries[i][0] &lt; queries[i][1] &lt; n</code></li> <li><code>1 &lt; queries[i][1] - queries[i][0]</code></li> <li>There are no repeated roads among the queries.</li> <li>There are no two queries such that <code>i != j</code> and <code>queries[i][0] &lt; queries[j][0] &lt; queries[i][1] &lt; queries[j][1]</code>.</li> </ul>
Greedy; Graph; Array; Ordered Set
TypeScript
function shortestDistanceAfterQueries(n: number, queries: number[][]): number[] { const nxt: number[] = Array.from({ length: n - 1 }, (_, i) => i + 1); const ans: number[] = []; let cnt = n - 1; for (const [u, v] of queries) { if (nxt[u] && nxt[u] < v) { let i = nxt[u]; while (i < v) { --cnt; [nxt[i], i] = [0, nxt[i]]; } nxt[u] = v; } ans.push(cnt); } return ans; }
3,246
Premier League Table Ranking
Easy
<p>Table: <code>TeamStats</code></p> <pre> +------------------+---------+ | Column Name | Type | +------------------+---------+ | team_id | int | | team_name | varchar | | matches_played | int | | wins | int | | draws | int | | losses | int | +------------------+---------+ team_id is the unique key for this table. This table contains team id, team name, matches_played, wins, draws, and losses. </pre> <p>Write a solution to calculate the <strong>points</strong> and <strong>rank</strong> for each team in the league. Points are calculated as follows:</p> <ul> <li><code>3</code> points for a <strong>win</strong></li> <li><code>1</code> point for a <strong>draw</strong></li> <li><code>0</code> points for a <strong>loss</strong></li> </ul> <p><strong>Note:</strong>&nbsp;Teams with the same points must be assigned the same rank.</p> <p>Return <em>the result table ordered by</em> <code>points</code>&nbsp;<em>in&nbsp;<strong>descending</strong>,<strong>&nbsp;</strong>and then by</em> <code>team_name</code> <em>in <strong>ascending </strong>order.</em></p> <p>The query result format is in the following example.</p> <p>&nbsp;</p> <p><strong class="example">Example:</strong></p> <div class="example-block"> <p><strong>Input:</strong></p> <p><code>TeamStats</code> table:</p> <pre class="example-io"> +---------+-----------------+----------------+------+-------+--------+ | team_id | team_name | matches_played | wins | draws | losses | +---------+-----------------+----------------+------+-------+--------+ | 1 | Manchester City | 10 | 6 | 2 | 2 | | 2 | Liverpool | 10 | 6 | 2 | 2 | | 3 | Chelsea | 10 | 5 | 3 | 2 | | 4 | Arsenal | 10 | 4 | 4 | 2 | | 5 | Tottenham | 10 | 3 | 5 | 2 | +---------+-----------------+----------------+------+-------+--------+ </pre> <p><strong>Output:</strong></p> <pre class="example-io"> +---------+-----------------+--------+----------+ | team_id | team_name | points | position | +---------+-----------------+--------+----------+ | 2 | Liverpool | 20 | 1 | | 1 | Manchester City | 20 | 1 | | 3 | Chelsea | 18 | 3 | | 4 | Arsenal | 16 | 4 | | 5 | Tottenham | 14 | 5 | +---------+-----------------+--------+----------+ </pre> <p><strong>Explanation:</strong></p> <ul> <li>Manchester City and Liverpool both have 20 points (6 wins * 3 points + 2 draws * 1 point), so they share position 1.</li> <li>Chelsea has 18 points (5 wins * 3 points + 3 draws * 1 point) and is position 3rd.</li> <li>Arsenal has 16 points (4 wins * 3 points + 4 draws * 1 point) and is position 4th.</li> <li>Tottenham has 14 points (3 wins * 3 points + 5 draws * 1 point) and is position 5th.</li> </ul> <p>The output table is ordered by points in descending order, then by team_name in ascending order.</p> </div>
Database
Python
import pandas as pd def calculate_team_standings(team_stats: pd.DataFrame) -> pd.DataFrame: team_stats["points"] = team_stats["wins"] * 3 + team_stats["draws"] team_stats["position"] = team_stats["points"].rank(method="min", ascending=False) team_stats = team_stats.sort_values( by=["points", "team_name"], ascending=[False, True] ) return team_stats[["team_id", "team_name", "points", "position"]]
3,246
Premier League Table Ranking
Easy
<p>Table: <code>TeamStats</code></p> <pre> +------------------+---------+ | Column Name | Type | +------------------+---------+ | team_id | int | | team_name | varchar | | matches_played | int | | wins | int | | draws | int | | losses | int | +------------------+---------+ team_id is the unique key for this table. This table contains team id, team name, matches_played, wins, draws, and losses. </pre> <p>Write a solution to calculate the <strong>points</strong> and <strong>rank</strong> for each team in the league. Points are calculated as follows:</p> <ul> <li><code>3</code> points for a <strong>win</strong></li> <li><code>1</code> point for a <strong>draw</strong></li> <li><code>0</code> points for a <strong>loss</strong></li> </ul> <p><strong>Note:</strong>&nbsp;Teams with the same points must be assigned the same rank.</p> <p>Return <em>the result table ordered by</em> <code>points</code>&nbsp;<em>in&nbsp;<strong>descending</strong>,<strong>&nbsp;</strong>and then by</em> <code>team_name</code> <em>in <strong>ascending </strong>order.</em></p> <p>The query result format is in the following example.</p> <p>&nbsp;</p> <p><strong class="example">Example:</strong></p> <div class="example-block"> <p><strong>Input:</strong></p> <p><code>TeamStats</code> table:</p> <pre class="example-io"> +---------+-----------------+----------------+------+-------+--------+ | team_id | team_name | matches_played | wins | draws | losses | +---------+-----------------+----------------+------+-------+--------+ | 1 | Manchester City | 10 | 6 | 2 | 2 | | 2 | Liverpool | 10 | 6 | 2 | 2 | | 3 | Chelsea | 10 | 5 | 3 | 2 | | 4 | Arsenal | 10 | 4 | 4 | 2 | | 5 | Tottenham | 10 | 3 | 5 | 2 | +---------+-----------------+----------------+------+-------+--------+ </pre> <p><strong>Output:</strong></p> <pre class="example-io"> +---------+-----------------+--------+----------+ | team_id | team_name | points | position | +---------+-----------------+--------+----------+ | 2 | Liverpool | 20 | 1 | | 1 | Manchester City | 20 | 1 | | 3 | Chelsea | 18 | 3 | | 4 | Arsenal | 16 | 4 | | 5 | Tottenham | 14 | 5 | +---------+-----------------+--------+----------+ </pre> <p><strong>Explanation:</strong></p> <ul> <li>Manchester City and Liverpool both have 20 points (6 wins * 3 points + 2 draws * 1 point), so they share position 1.</li> <li>Chelsea has 18 points (5 wins * 3 points + 3 draws * 1 point) and is position 3rd.</li> <li>Arsenal has 16 points (4 wins * 3 points + 4 draws * 1 point) and is position 4th.</li> <li>Tottenham has 14 points (3 wins * 3 points + 5 draws * 1 point) and is position 5th.</li> </ul> <p>The output table is ordered by points in descending order, then by team_name in ascending order.</p> </div>
Database
SQL
# Write your MySQL query statement below SELECT team_id, team_name, wins * 3 + draws points, RANK() OVER (ORDER BY (wins * 3 + draws) DESC) position FROM TeamStats ORDER BY 3 DESC, 2;
3,247
Number of Subsequences with Odd Sum
Medium
<p>Given an array <code>nums</code>, return the number of <span data-keyword="subsequence-array">subsequences</span> with an odd sum of elements.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The odd-sum subsequences are: <code>[<u><strong>1</strong></u>, 1, 1]</code>, <code>[1, <u><strong>1</strong></u>, 1],</code> <code>[1, 1, <u><strong>1</strong></u>]</code>, <code>[<u><strong>1, 1, 1</strong></u>]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,2]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The odd-sum subsequences are: <code>[<u><strong>1</strong></u>, 2, 2]</code>, <code>[<u><strong>1, 2</strong></u>, 2],</code> <code>[<u><strong>1</strong></u>, 2, <b><u>2</u></b>]</code>, <code>[<u><strong>1, 2, 2</strong></u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Math; Dynamic Programming; Combinatorics
C++
class Solution { public: int subsequenceCount(vector<int>& nums) { const int mod = 1e9 + 7; vector<int> f(2); for (int x : nums) { vector<int> g(2); if (x % 2 == 1) { g[0] = (f[0] + f[1]) % mod; g[1] = (f[0] + f[1] + 1) % mod; } else { g[0] = (f[0] + f[0] + 1) % mod; g[1] = (f[1] + f[1]) % mod; } f = g; } return f[1]; } };
3,247
Number of Subsequences with Odd Sum
Medium
<p>Given an array <code>nums</code>, return the number of <span data-keyword="subsequence-array">subsequences</span> with an odd sum of elements.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The odd-sum subsequences are: <code>[<u><strong>1</strong></u>, 1, 1]</code>, <code>[1, <u><strong>1</strong></u>, 1],</code> <code>[1, 1, <u><strong>1</strong></u>]</code>, <code>[<u><strong>1, 1, 1</strong></u>]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,2]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The odd-sum subsequences are: <code>[<u><strong>1</strong></u>, 2, 2]</code>, <code>[<u><strong>1, 2</strong></u>, 2],</code> <code>[<u><strong>1</strong></u>, 2, <b><u>2</u></b>]</code>, <code>[<u><strong>1, 2, 2</strong></u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Math; Dynamic Programming; Combinatorics
Go
func subsequenceCount(nums []int) int { mod := int(1e9 + 7) f := [2]int{} for _, x := range nums { g := [2]int{} if x%2 == 1 { g[0] = (f[0] + f[1]) % mod g[1] = (f[0] + f[1] + 1) % mod } else { g[0] = (f[0] + f[0] + 1) % mod g[1] = (f[1] + f[1]) % mod } f = g } return f[1] }
3,247
Number of Subsequences with Odd Sum
Medium
<p>Given an array <code>nums</code>, return the number of <span data-keyword="subsequence-array">subsequences</span> with an odd sum of elements.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The odd-sum subsequences are: <code>[<u><strong>1</strong></u>, 1, 1]</code>, <code>[1, <u><strong>1</strong></u>, 1],</code> <code>[1, 1, <u><strong>1</strong></u>]</code>, <code>[<u><strong>1, 1, 1</strong></u>]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,2]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The odd-sum subsequences are: <code>[<u><strong>1</strong></u>, 2, 2]</code>, <code>[<u><strong>1, 2</strong></u>, 2],</code> <code>[<u><strong>1</strong></u>, 2, <b><u>2</u></b>]</code>, <code>[<u><strong>1, 2, 2</strong></u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Math; Dynamic Programming; Combinatorics
Java
class Solution { public int subsequenceCount(int[] nums) { final int mod = (int) 1e9 + 7; int[] f = new int[2]; for (int x : nums) { int[] g = new int[2]; if (x % 2 == 1) { g[0] = (f[0] + f[1]) % mod; g[1] = (f[0] + f[1] + 1) % mod; } else { g[0] = (f[0] + f[0] + 1) % mod; g[1] = (f[1] + f[1]) % mod; } f = g; } return f[1]; } }
3,247
Number of Subsequences with Odd Sum
Medium
<p>Given an array <code>nums</code>, return the number of <span data-keyword="subsequence-array">subsequences</span> with an odd sum of elements.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The odd-sum subsequences are: <code>[<u><strong>1</strong></u>, 1, 1]</code>, <code>[1, <u><strong>1</strong></u>, 1],</code> <code>[1, 1, <u><strong>1</strong></u>]</code>, <code>[<u><strong>1, 1, 1</strong></u>]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,2]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The odd-sum subsequences are: <code>[<u><strong>1</strong></u>, 2, 2]</code>, <code>[<u><strong>1, 2</strong></u>, 2],</code> <code>[<u><strong>1</strong></u>, 2, <b><u>2</u></b>]</code>, <code>[<u><strong>1, 2, 2</strong></u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Math; Dynamic Programming; Combinatorics
Python
class Solution: def subsequenceCount(self, nums: List[int]) -> int: mod = 10**9 + 7 f = [0] * 2 for x in nums: if x % 2: f[0], f[1] = (f[0] + f[1]) % mod, (f[0] + f[1] + 1) % mod else: f[0], f[1] = (f[0] + f[0] + 1) % mod, (f[1] + f[1]) % mod return f[1]
3,247
Number of Subsequences with Odd Sum
Medium
<p>Given an array <code>nums</code>, return the number of <span data-keyword="subsequence-array">subsequences</span> with an odd sum of elements.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The odd-sum subsequences are: <code>[<u><strong>1</strong></u>, 1, 1]</code>, <code>[1, <u><strong>1</strong></u>, 1],</code> <code>[1, 1, <u><strong>1</strong></u>]</code>, <code>[<u><strong>1, 1, 1</strong></u>]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,2]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The odd-sum subsequences are: <code>[<u><strong>1</strong></u>, 2, 2]</code>, <code>[<u><strong>1, 2</strong></u>, 2],</code> <code>[<u><strong>1</strong></u>, 2, <b><u>2</u></b>]</code>, <code>[<u><strong>1, 2, 2</strong></u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Math; Dynamic Programming; Combinatorics
TypeScript
function subsequenceCount(nums: number[]): number { const mod = 1e9 + 7; let f = [0, 0]; for (const x of nums) { const g = [0, 0]; if (x % 2 === 1) { g[0] = (f[0] + f[1]) % mod; g[1] = (f[0] + f[1] + 1) % mod; } else { g[0] = (f[0] + f[0] + 1) % mod; g[1] = (f[1] + f[1]) % mod; } f = g; } return f[1]; }
3,248
Snake in Matrix
Easy
<p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p> <p>The snake starts at cell 0 and follows a sequence of commands.</p> <p>You are given an integer <code>n</code> representing the size of the <code>grid</code> and an array of strings <code>commands</code> where each <code>command[i]</code> is either <code>&quot;UP&quot;</code>, <code>&quot;RIGHT&quot;</code>, <code>&quot;DOWN&quot;</code>, and <code>&quot;LEFT&quot;</code>. It&#39;s guaranteed that the snake will remain within the <code>grid</code> boundaries throughout its movement.</p> <p>Return the position of the final cell where the snake ends up after executing <code>commands</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, commands = [&quot;RIGHT&quot;,&quot;DOWN&quot;]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <div style="display:flex; gap: 12px;"> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">3</td> </tr> </tbody> </table> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">3</td> </tr> </tbody> </table> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">3</td> </tr> </tbody> </table> </div> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, commands = [&quot;DOWN&quot;,&quot;RIGHT&quot;,&quot;UP&quot;]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <div style="display:flex; gap: 12px;"> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">3</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">4</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">5</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">6</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">7</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">8</td> </tr> </tbody> </table> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">3</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">4</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">5</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">6</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">7</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">8</td> </tr> </tbody> </table> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">3</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">4</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">5</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">6</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">7</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">8</td> </tr> </tbody> </table> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">3</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">4</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">5</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">6</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">7</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">8</td> </tr> </tbody> </table> </div> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10</code></li> <li><code>1 &lt;= commands.length &lt;= 100</code></li> <li><code>commands</code> consists only of <code>&quot;UP&quot;</code>, <code>&quot;RIGHT&quot;</code>, <code>&quot;DOWN&quot;</code>, and <code>&quot;LEFT&quot;</code>.</li> <li>The input is generated such the snake will not move outside of the boundaries.</li> </ul>
Array; String; Simulation
C++
class Solution { public: int finalPositionOfSnake(int n, vector<string>& commands) { int x = 0, y = 0; for (const auto& c : commands) { switch (c[0]) { case 'U': x--; break; case 'D': x++; break; case 'L': y--; break; case 'R': y++; break; } } return x * n + y; } };
3,248
Snake in Matrix
Easy
<p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p> <p>The snake starts at cell 0 and follows a sequence of commands.</p> <p>You are given an integer <code>n</code> representing the size of the <code>grid</code> and an array of strings <code>commands</code> where each <code>command[i]</code> is either <code>&quot;UP&quot;</code>, <code>&quot;RIGHT&quot;</code>, <code>&quot;DOWN&quot;</code>, and <code>&quot;LEFT&quot;</code>. It&#39;s guaranteed that the snake will remain within the <code>grid</code> boundaries throughout its movement.</p> <p>Return the position of the final cell where the snake ends up after executing <code>commands</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, commands = [&quot;RIGHT&quot;,&quot;DOWN&quot;]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <div style="display:flex; gap: 12px;"> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">3</td> </tr> </tbody> </table> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">3</td> </tr> </tbody> </table> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">3</td> </tr> </tbody> </table> </div> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, commands = [&quot;DOWN&quot;,&quot;RIGHT&quot;,&quot;UP&quot;]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <div style="display:flex; gap: 12px;"> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">3</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">4</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">5</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">6</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">7</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">8</td> </tr> </tbody> </table> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">3</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">4</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">5</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">6</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">7</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">8</td> </tr> </tbody> </table> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">3</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">4</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">5</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">6</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">7</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">8</td> </tr> </tbody> </table> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">3</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">4</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">5</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">6</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">7</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">8</td> </tr> </tbody> </table> </div> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10</code></li> <li><code>1 &lt;= commands.length &lt;= 100</code></li> <li><code>commands</code> consists only of <code>&quot;UP&quot;</code>, <code>&quot;RIGHT&quot;</code>, <code>&quot;DOWN&quot;</code>, and <code>&quot;LEFT&quot;</code>.</li> <li>The input is generated such the snake will not move outside of the boundaries.</li> </ul>
Array; String; Simulation
Go
func finalPositionOfSnake(n int, commands []string) int { x, y := 0, 0 for _, c := range commands { switch c[0] { case 'U': x-- case 'D': x++ case 'L': y-- case 'R': y++ } } return x*n + y }
3,248
Snake in Matrix
Easy
<p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p> <p>The snake starts at cell 0 and follows a sequence of commands.</p> <p>You are given an integer <code>n</code> representing the size of the <code>grid</code> and an array of strings <code>commands</code> where each <code>command[i]</code> is either <code>&quot;UP&quot;</code>, <code>&quot;RIGHT&quot;</code>, <code>&quot;DOWN&quot;</code>, and <code>&quot;LEFT&quot;</code>. It&#39;s guaranteed that the snake will remain within the <code>grid</code> boundaries throughout its movement.</p> <p>Return the position of the final cell where the snake ends up after executing <code>commands</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, commands = [&quot;RIGHT&quot;,&quot;DOWN&quot;]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <div style="display:flex; gap: 12px;"> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">3</td> </tr> </tbody> </table> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">3</td> </tr> </tbody> </table> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">3</td> </tr> </tbody> </table> </div> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, commands = [&quot;DOWN&quot;,&quot;RIGHT&quot;,&quot;UP&quot;]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <div style="display:flex; gap: 12px;"> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">3</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">4</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">5</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">6</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">7</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">8</td> </tr> </tbody> </table> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">3</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">4</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">5</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">6</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">7</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">8</td> </tr> </tbody> </table> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">3</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">4</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">5</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">6</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">7</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">8</td> </tr> </tbody> </table> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">3</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">4</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">5</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">6</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">7</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">8</td> </tr> </tbody> </table> </div> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10</code></li> <li><code>1 &lt;= commands.length &lt;= 100</code></li> <li><code>commands</code> consists only of <code>&quot;UP&quot;</code>, <code>&quot;RIGHT&quot;</code>, <code>&quot;DOWN&quot;</code>, and <code>&quot;LEFT&quot;</code>.</li> <li>The input is generated such the snake will not move outside of the boundaries.</li> </ul>
Array; String; Simulation
Java
class Solution { public int finalPositionOfSnake(int n, List<String> commands) { int x = 0, y = 0; for (var c : commands) { switch (c.charAt(0)) { case 'U' -> x--; case 'D' -> x++; case 'L' -> y--; case 'R' -> y++; } } return x * n + y; } }
3,248
Snake in Matrix
Easy
<p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p> <p>The snake starts at cell 0 and follows a sequence of commands.</p> <p>You are given an integer <code>n</code> representing the size of the <code>grid</code> and an array of strings <code>commands</code> where each <code>command[i]</code> is either <code>&quot;UP&quot;</code>, <code>&quot;RIGHT&quot;</code>, <code>&quot;DOWN&quot;</code>, and <code>&quot;LEFT&quot;</code>. It&#39;s guaranteed that the snake will remain within the <code>grid</code> boundaries throughout its movement.</p> <p>Return the position of the final cell where the snake ends up after executing <code>commands</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, commands = [&quot;RIGHT&quot;,&quot;DOWN&quot;]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <div style="display:flex; gap: 12px;"> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">3</td> </tr> </tbody> </table> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">3</td> </tr> </tbody> </table> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">3</td> </tr> </tbody> </table> </div> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, commands = [&quot;DOWN&quot;,&quot;RIGHT&quot;,&quot;UP&quot;]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <div style="display:flex; gap: 12px;"> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">3</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">4</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">5</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">6</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">7</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">8</td> </tr> </tbody> </table> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">3</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">4</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">5</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">6</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">7</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">8</td> </tr> </tbody> </table> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">1</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">3</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">4</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">5</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">6</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">7</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">8</td> </tr> </tbody> </table> <table border="1" cellspacing="3" style="border-collapse: separate; text-align: center;"> <tbody> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">0</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid red; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">1</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">2</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">3</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">4</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #b30000; --darkreader-inline-border-right: #b30000; --darkreader-inline-border-bottom: #b30000; --darkreader-inline-border-left: #b30000;">5</td> </tr> <tr> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">6</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">7</td> <td data-darkreader-inline-border-bottom="" data-darkreader-inline-border-left="" data-darkreader-inline-border-right="" data-darkreader-inline-border-top="" style="padding: 5px 10px; border: 1px solid black; --darkreader-inline-border-top: #8c8273; --darkreader-inline-border-right: #8c8273; --darkreader-inline-border-bottom: #8c8273; --darkreader-inline-border-left: #8c8273;">8</td> </tr> </tbody> </table> </div> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10</code></li> <li><code>1 &lt;= commands.length &lt;= 100</code></li> <li><code>commands</code> consists only of <code>&quot;UP&quot;</code>, <code>&quot;RIGHT&quot;</code>, <code>&quot;DOWN&quot;</code>, and <code>&quot;LEFT&quot;</code>.</li> <li>The input is generated such the snake will not move outside of the boundaries.</li> </ul>
Array; String; Simulation
Python
class Solution: def finalPositionOfSnake(self, n: int, commands: List[str]) -> int: x = y = 0 for c in commands: match c[0]: case "U": x -= 1 case "D": x += 1 case "L": y -= 1 case "R": y += 1 return x * n + y