question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
minimize-the-maximum-adjacent-element-difference
[Java/C++/Python] No Binary Search, O(1) Space
javacpython-no-binary-search-o1-space-by-ykbd
Complexity\nTime O(n)\nSpace O(1)\n\n# Intuition 1\nAssume r is the maximum absolute difference between adjacent elements.\nr is short for radius.\nx can cover
lee215
NORMAL
2024-11-17T15:30:49.526769+00:00
2024-11-18T14:52:24.658074+00:00
1,067
false
# **Complexity**\nTime `O(n)`\nSpace `O(1)`\n\n# **Intuition 1**\nAssume `r` is the maximum absolute difference between adjacent elements.\n`r` is short for radius.\n`x` can cover the range of `[x - r, x + r]`\n`y` can cover the range of `[y - r, y + r]`\n\nNotice that these two range could overlap.\n\n# **Intuition 2*...
22
1
['C', 'Python', 'Java']
3
minimize-the-maximum-adjacent-element-difference
C++ | easiest solution with detailed explanation and comment on code
c-easiest-solution-with-detailed-explana-31ms
\n### 1. Intuition\n\n- You are given an array nums that contains integers and -1 values representing missing elements.\n- You are allowed to replace the -1 ele
blue106
NORMAL
2024-11-17T04:16:42.841801+00:00
2024-11-17T04:16:42.841829+00:00
1,627
false
\n### **1. Intuition**\n\n- You are given an array `nums` that contains integers and `-1` values representing missing elements.\n- You are allowed to replace the `-1` elements with two positive integers (`minValue` and `maxValue`), which are chosen once for the entire array.\n- The task is to replace the `-1` values in...
20
6
['Array', 'Two Pointers', 'Binary Search', 'Greedy', 'C++']
7
minimize-the-maximum-adjacent-element-difference
[Python] Binary Search + Interval Stabbing
python-binary-search-interval-stabbing-b-28ke
Binary search for the answer.\n\nLet\'s say we are trying to decide whether bound is an answer. Every wildcard ? next to a positive value a induces an interval
awice
NORMAL
2024-11-17T04:23:41.385222+00:00
2024-11-17T04:23:41.385246+00:00
947
false
Binary search for the answer.\n\nLet\'s say we are trying to decide whether `bound` is an answer. Every wildcard `?` next to a positive value `a` induces an interval `[a-bound, a+bound]`. Wildcards like `a?b` induce the interval `[a-bound,a+bound] INTERSECT [b-bound,b+bound]`.\n\nAt the end, we have some intervals an...
12
0
['Python3']
1
minimize-the-maximum-adjacent-element-difference
C++ - Binary Search - O(NlogM)
c-binary-search-onlogm-by-facug91-nab5
Intuition\nThe main idea is to have a function to check if the problem can be solved with a difference smaller or equals to a given value.\nWith that in mind, w
facug91
NORMAL
2024-11-17T05:22:18.763869+00:00
2024-11-17T06:17:17.390595+00:00
680
false
# Intuition\nThe main idea is to have a function to check if the problem can be solved with a difference smaller or equals to a given value.\nWith that in mind, we can use a binary search over the possible range of answers, to check which difference is the smallest.\n\n# Approach\nFirst I try to solve some special case...
6
0
['Binary Search', 'C++']
1
minimize-the-maximum-adjacent-element-difference
Binary Search (29th place solution)
binary-search-29th-place-solution-by-ton-wd4i
Intuition\nBinary search, greedy\n\n\n# Approach\nBinary search on the possible result.\n\nThen the question is reduced to - given a specific maximum distance t
tonghuikang
NORMAL
2024-11-17T04:03:49.606950+00:00
2024-11-17T18:01:30.980213+00:00
782
false
# Intuition\nBinary search, greedy\n\n\n# Approach\nBinary search on the possible result.\n\nThen the question is reduced to - given a specific maximum distance `target_diff`, can you find a pair of (x,y) that satisifies the constraint?\n\nThe input can be reduced to three types of element pairs (a,b)\n- There are no m...
5
0
['Python3']
0
minimize-the-maximum-adjacent-element-difference
[Python] 🔥 Binary Search | Very Detailed Explanation | Visual Diagrams|
python-binary-search-very-detailed-expla-7env
Intuition\n\n Describe your first thoughts on how to solve this problem. \n\nThanks for the inspiration of @Alex Wi\'s solution. \n\nConsidering it as a convex
Yosemiti
NORMAL
2024-11-18T03:45:04.877971+00:00
2024-11-18T07:03:50.506286+00:00
74
false
# Intuition\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nThanks for the inspiration of [@Alex Wi](https://leetcode.com/problems/minimize-the-maximum-adjacent-element-difference/solutions/6053410/python-binary-search-interval-stabbing/)\'s solution. \n\nConsidering it as a convex optimizatio...
1
0
['Python3']
0
minimize-the-maximum-adjacent-element-difference
Python 3 | Detailed Explanation with Extensive Comments on My Thinking Process
python-3-detailed-explanation-with-exten-kbrx
Observations\n\n## Which algorithm to use?\n\nIt\'s hard to direcly choose x and y to minimize the maximum absolute difference, but it\'s not as hard to check i
ianlo0511
NORMAL
2024-11-17T17:07:12.789193+00:00
2024-11-17T17:07:12.789228+00:00
89
false
# Observations\n\n## Which algorithm to use?\n\nIt\'s hard to direcly choose `x` and `y` to minimize the maximum absolute difference, but it\'s not as hard to check if we can find a pair of `(x, y)` to achieve the maximum absolute difference as some arbitrary number `d` \u2014 binary search can be a good fit.\n\n## How...
1
0
['Python3']
0
minimize-the-maximum-adjacent-element-difference
Binary Search on diff. || maintain range L - R and if R-L>=1... detailed explanation. MUST CHECK!!
binary-search-on-diff-maintain-range-l-r-yf2k
Hard problem made easy. Observation + Binary Search = AC Reconstruct array bcz repeatation of adjacent element does'nt affect solution and consecutive 2 and m
Priyanshu_pandey15
NORMAL
2024-11-17T07:39:20.172115+00:00
2024-12-25T20:01:46.554037+00:00
188
false
![Screenshot 2024-11-17 110930.png](https://assets.leetcode.com/users/images/d946f5a1-ffa8-4844-b369-886d9adc5875_1731829339.968351.png) --- - Hard problem made easy. - Observation + Binary Search = AC - Reconstruct array bcz repeatation of adjacent element does'nt affect solution and consecutive 2 and more -1 can ...
1
0
['Binary Search', 'Java']
1
minimize-the-maximum-adjacent-element-difference
BEATS 100% || Java Solution || Optimum Solution
beats-100-java-solution-optimum-solution-iliy
Intuition\n- The problem requires determining the minimal difference that ensures an array remains sorted after replacing -1 values in the array. The approach i
yallavamsipavan1234
NORMAL
2024-11-17T04:14:51.245919+00:00
2024-11-17T04:14:51.245951+00:00
285
false
# Intuition\n- The problem requires determining the minimal difference that ensures an array remains sorted after replacing -1 values in the array. The approach involves analyzing intervals where -1s appear and ensuring the replacements maintain the sorted property of the array.\n\n# Approach\n- **Identify Segments :**...
1
1
['Java']
1
minimize-the-maximum-adjacent-element-difference
DP + Greedy [EXPLAINED]
dp-greedy-explained-by-r9n-wxo2
IntuitionFind the minimum difference between adjacent elements in an array, where some elements are missing and represented by -1. We can replace these -1 value
r9n
NORMAL
2025-02-12T06:09:31.251597+00:00
2025-02-12T06:09:31.251597+00:00
9
false
# Intuition Find the minimum difference between adjacent elements in an array, where some elements are missing and represented by -1. We can replace these -1 values to minimize the difference between adjacent elements. The challenge is to fill in the missing elements optimally. # Approach First, count how many -1 valu...
0
0
['Array', 'Binary Search', 'Dynamic Programming', 'Greedy', 'Rust']
0
minimize-the-maximum-adjacent-element-difference
Explanation with video - Java
explanation-with-video-java-by-gvp-w6g6
Intuition\nStarting with a basic use case like [2, -1, 16] for example, where the answer to the above simple case is 7 which is quite easy to see because (16 -
gvp
NORMAL
2024-12-05T22:28:27.210141+00:00
2024-12-06T07:43:26.095363+00:00
49
false
# Intuition\nStarting with a basic use case like [2, -1, 16] for example, where the answer to the above simple case is `7` which is quite easy to see because `(16 - 2) / 2 = 7` therefore `d` is 7 and `x = 9`.\n\nLets expand to a bit more complicated use case like `[2, -1, 16, 12, -1, 4, 5, -1, 16, 19, -1, 18, 19, -1, 3...
0
0
['Java']
0
minimize-the-maximum-adjacent-element-difference
C# Solution
c-solution-by-advalas-kvnt
Intuition\n Describe your first thoughts on how to solve this problem. \nI wasnt able to complete this during the contest a few weeks ago but it was bugging me
advalas
NORMAL
2024-12-04T15:40:20.390406+00:00
2024-12-04T15:41:12.743428+00:00
5
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI wasnt able to complete this during the contest a few weeks ago but it was bugging me that i didnt finish it. I decided to come back to it and see if i could finish it in C# since nobody else has done that.\n\n# Approach\n<!-- Describe y...
0
0
['C#']
0
minimize-the-maximum-adjacent-element-difference
C# Solution
c-solution-by-advalas-8egn
Intuition\n Describe your first thoughts on how to solve this problem. \nI wasnt able to complete this during the contest a few weeks ago but it was bugging me
advalas
NORMAL
2024-12-04T15:40:04.363158+00:00
2024-12-04T15:40:04.363182+00:00
3
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI wasnt able to complete this during the contest a few weeks ago but it was bugging me that i didnt finish it. I decided to come back to it and see if i could finish it in C# since nobody else has done that.\n\n# Approach\n<!-- Describe y...
0
0
['C#']
0
minimize-the-maximum-adjacent-element-difference
Simple Code to Avoid Corner Cases O(N)
simple-code-to-avoid-corner-cases-on-by-ntian
Intuition\n Describe your first thoughts on how to solve this problem. \n For data size(10^5) provided and the problem aims to find minimaml difference, the fir
HaixinShi
NORMAL
2024-11-26T07:17:36.017874+00:00
2024-11-26T07:17:36.017906+00:00
27
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n For data size(10^5) provided and the problem aims to find minimaml difference, the first thought came to my mind is binary search. We can simply set min_diff = 0 and max_diff = 10^9 and do binary search. The question became how to check ...
0
0
['Binary Search', 'Python3']
0
minimize-the-maximum-adjacent-element-difference
C++ || Binary Search || Dp
c-binary-search-dp-by-ks404536-dee4
Complexity\n- Time complexity:\n Add your time complexity here, e.g. O(n) \nO(log(1e9) * (n * 2))\n\n- Space complexity:\n Add your space complexity here, e.g.
ks404536
NORMAL
2024-11-24T18:20:41.559721+00:00
2024-11-24T18:20:41.559755+00:00
63
false
# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(log(1e9) * (n * 2))\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n * 2)\n\n# Code\n```cpp []\nclass Solution {\n int find(vector<int> &nums){\n int n = nums.size();\n int ans ...
0
0
['Binary Search', 'Dynamic Programming', 'C++']
0
minimize-the-maximum-adjacent-element-difference
binary search, interval, and special treatment of adjacent -1's
binary-search-interval-and-special-treat-pl3n
Intuition\nWhen first looking at this problem of filling in missing values (-1s) while minimizing the maximum difference between adjacent numbers, the key insig
phi9t
NORMAL
2024-11-24T03:12:11.562460+00:00
2024-11-24T03:12:11.562496+00:00
17
false
# Intuition\nWhen first looking at this problem of filling in missing values (-1s) while minimizing the maximum difference between adjacent numbers, the key insight is that we\'re looking for a minimum maximum difference. Whenever we see a "minimize the maximum" or "maximize the minimum" pattern, binary search often pr...
0
0
['Python3']
0
minimize-the-maximum-adjacent-element-difference
C++ | binary search + check constraints case by case
c-binary-search-check-constraints-case-b-obe3
Intuition\n Describe your first thoughts on how to solve this problem. \nThis approach is based on the hints.\n\nFirstly, some definitions:\n let\'s call the po
baihuaxie
NORMAL
2024-11-20T10:09:54.208525+00:00
2024-11-20T10:26:35.457579+00:00
52
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis approach is based on the hints.\n\nFirstly, some definitions:\n* let\'s call the positions with -1 as the value the wildcards and I will denote them using ? instead of -1 for better clarity\n* let\'s use $$d_{total}$$ to denote the m...
0
0
['C++']
0
minimize-the-maximum-adjacent-element-difference
[Binary Search] Left & Right Neighbours - Easy C++ Solution
binary-search-left-right-neighbours-easy-0mwg
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
pran-2110
NORMAL
2024-11-20T00:00:22.272300+00:00
2024-11-20T00:00:22.272347+00:00
38
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(N\\log{M})$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(N)$$\n<!-- Add your space complex...
0
0
['C++']
0
minimize-the-maximum-adjacent-element-difference
Two pass, O(n) time
two-pass-on-time-by-solid_kalium-r8g4
Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n\n# Intuition\n- When choosing x and y, we only care about the numbers adjacent to them\n- x an
solid_kalium
NORMAL
2024-11-19T01:47:54.564168+00:00
2024-11-19T01:53:36.474209+00:00
61
false
# Complexity\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(1)$$\n\n# Intuition\n- When choosing `x` and `y`, we only care about the numbers adjacent to them\n- `x` and `y` will each have a set of adjacent numbers\n - One set will include the smallest adjacent number: `minAdj`\n - The other will include ...
0
0
['Greedy', 'Union Find', 'Python3']
0
minimize-the-maximum-adjacent-element-difference
Binary Search | All Test Cases Passes
binary-search-all-test-cases-passes-by-f-4jzs
Approach\nSame as @votrubac except the extra checkpoint where nums start with -1\n\n# Complexity\n- Time complexity:\nO(nlog(m)) ---- m is of size 10^9 and n is
mfarqua00
NORMAL
2024-11-18T16:48:04.731416+00:00
2024-11-18T17:29:14.216411+00:00
59
false
# Approach\nSame as [@votrubac](https://leetcode.com/problems/minimize-the-maximum-adjacent-element-difference/solutions/6054022/binary-search/) except the extra checkpoint where nums start with -1\n\n# Complexity\n- Time complexity:\nO(nlog(m)) ---- m is of size 10^9 and n is 10^5\n\n- Space complexity:\nO(1)\n\n# Cod...
0
0
['C++']
0
minimize-the-maximum-adjacent-element-difference
My Solution
my-solution-by-hope_ma-j82r
\n/**\n * Time Complexity: O(n)\n * Space Complexity: O(1)\n * where `n` is the length of the vector `nums`\n */\nclass Solution {\n public:\n int minDifferenc
hope_ma
NORMAL
2024-11-18T10:33:24.555708+00:00
2024-11-18T12:40:44.725240+00:00
8
false
```\n/**\n * Time Complexity: O(n)\n * Space Complexity: O(1)\n * where `n` is the length of the vector `nums`\n */\nclass Solution {\n public:\n int minDifference(const vector<int> &nums) {\n constexpr int missing = -1;\n const int n = static_cast<int>(nums.size());\n int min_left = numeric_limits<int>::max(...
0
0
[]
0
minimize-the-maximum-adjacent-element-difference
Finding x and y via interval intersections and binary search on answer
finding-x-and-y-via-interval-intersectio-zwel
Approach\n\n## Key Idea\n1. It is obvious that if we can obtain the answer as mid, then we can obtain a larger answer by choosing x and y accordingly. \n Thu
NastyWaterEspresso
NORMAL
2024-11-18T09:27:57.475774+00:00
2024-11-18T13:29:56.455920+00:00
55
false
# Approach\n\n## Key Idea\n1. It is obvious that if we can obtain the answer as `mid`, then we can obtain a larger answer by choosing `x` and `y` accordingly. \n Thus, we can apply **binary search** on the answer.\n\n---\n\n## Obtaining `x` and `y` via Interval Intersections\n1. For every `nums[i]` with at least one...
0
0
['C++']
0
minimize-the-maximum-adjacent-element-difference
Binary Search
binary-search-by-tensortrove-h352
Intuition\nThe problem revolves around minimizing the maximum difference between adjacent elements in an array where missing elements (denoted as -1) can be rep
20241219.tensortrove
NORMAL
2024-11-18T07:20:37.427010+00:00
2024-11-18T07:25:54.208436+00:00
48
false
# Intuition\nThe problem revolves around minimizing the maximum difference between adjacent elements in an array where missing elements (denoted as -1) can be replaced by one of two chosen integers, x or y. To achieve the optimal solution:\n\n1. Observe that replacing -1 with two values allows us to control the adjacen...
0
0
['Python3']
0
minimize-the-maximum-adjacent-element-difference
Rust Solution
rust-solution-by-abhineetraj1-f3bm
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem asks us to find the minimum difference between adjacent values in an array
abhineetraj1
NORMAL
2024-11-18T04:29:52.586027+00:00
2024-11-18T04:29:52.586062+00:00
12
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem asks us to find the minimum difference between adjacent values in an array while possibly replacing `-1` entries with values from adjacent elements. The key idea is to minimize the maximum difference between adjacent values, p...
0
0
['Rust']
0
minimize-the-maximum-adjacent-element-difference
Binary Search Solution to Minimize Maximum Adjacent Difference in an Array [c++ with explaination]
binary-search-solution-to-minimize-maxim-fz5f
Intuition\n Describe your first thoughts on how to solve this problem. \nWe aim to find the minimum possible maximum difference between adjacent elements in the
sumanth977
NORMAL
2024-11-17T16:57:32.177376+00:00
2024-11-17T16:57:32.177476+00:00
48
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe aim to find the minimum possible maximum difference between adjacent elements in the array. This involves two key steps:\n1.\tBinary Search: Use binary search on the maximum difference (maxDiff) to minimize it.\n2.\tValidation: For eac...
0
0
['Binary Search', 'C++']
0
minimize-the-maximum-adjacent-element-difference
Explained + well documented code
explained-well-documented-code-by-ivangn-zi51
Ideas\n1. Reduce sequences of missing numbers: length >= 3 to only 2\n2. Use a "meet in middle" strategy to get upper bound\n3. Binary searching for the minimum
ivangnilomedov
NORMAL
2024-11-17T16:56:51.679557+00:00
2024-11-17T16:56:51.679597+00:00
41
false
# Ideas\n1. Reduce sequences of missing numbers: length >= 3 to only 2\n2. Use a "meet in middle" strategy to get upper bound\n3. Binary searching for the minimum possible difference\n\n# Approach\n1. **Preprocessing**\n - Collapse 3+ consecutive -1s to just 2 since with only two values (x,y), longer sequences can al...
0
0
['C++']
0
minimize-the-maximum-adjacent-element-difference
Python3 binary search 42 lines
python3-binary-search-42-lines-by-nguyen-hkhd
Intuition\n Describe your first thoughts on how to solve this problem. \nFind the maximum, minimum of elements in nums that next to -1\nFor the maximum absolute
nguyenquocthao00
NORMAL
2024-11-17T14:08:26.106017+00:00
2024-11-17T14:08:26.106055+00:00
38
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFind the maximum, minimum of elements in nums that next to -1\nFor the maximum absolute distance dif, we can always choose the optimal x,y that: x=lo+dif, y=hi-dif, and check whether the chosen x,y can satisfy the requirement.\n\nIf we ha...
0
0
['Python3']
0
minimize-the-maximum-adjacent-element-difference
Hardest problem I ever solved. (though with minor look at hints) ;)
hardest-problem-i-ever-solved-though-wit-jdni
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
user7634ri
NORMAL
2024-11-17T11:41:14.941679+00:00
2024-11-17T11:41:14.941709+00:00
162
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
0
['C++']
1
minimize-the-maximum-adjacent-element-difference
[C++] Binary Search + Classified discussion
c-binary-search-classified-discussion-by-xr7y
CODE:\n\n\nclass Solution {\npublic:\n int minDifference(vector<int>& a) { int n = a.size();\n // Prework 0\uFF1A The maximum diffe
projectcoder
NORMAL
2024-11-17T10:00:48.930560+00:00
2024-11-17T10:18:50.202844+00:00
31
false
**CODE:**\n\n```\nclass Solution {\npublic:\n int minDifference(vector<int>& a) { int n = a.size();\n // Prework 0\uFF1A The maximum difference between consecutive numbers.\n int low = 0, cnt = 0;\n for (int l = 0, r = 0; l < n; l = r) {\n for (l = r; l < n && a[l]=...
0
0
[]
0
minimize-the-maximum-adjacent-element-difference
Binary Search + DP Solution || Interval Merging || Beats 50%
binary-search-dp-solution-interval-mergi-tmhp
Complexity\n- Time complexity:O(n * log(max(nums)))\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(n)\n Add your space complexity here, e.g
dubeyad2003
NORMAL
2024-11-17T07:18:08.485055+00:00
2024-11-17T07:18:08.485105+00:00
59
false
# Complexity\n- Time complexity:$$O(n * log(max(nums)))$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nint dp[100005][3];\nint val[3];\nclass Solution {\npublic:\n bool possible(int diff, vector<int...
0
0
['C++']
0
minimize-the-maximum-adjacent-element-difference
[c++] Binary Search
c-binary-search-by-lyronly-g1eh
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n1 Fix one smaller ele
lyronly
NORMAL
2024-11-17T05:14:49.949509+00:00
2024-11-17T05:14:49.949544+00:00
73
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n1 Fix one smaller element x, \n2 For bigger element y ,given min gap m, try to figure out range of y to determine if y exists.\n\nPreprocess all pair of \n1 : a, -1...
0
0
['C++']
0
minimize-the-maximum-adjacent-element-difference
python3
python3-by-harshitasree-jeyg
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
harshitasree
NORMAL
2024-11-17T04:14:39.872538+00:00
2024-11-17T04:14:39.872563+00:00
111
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
0
['Python3']
1
stone-game-iv
Easy Explanation, you will definitely gonna understand
easy-explanation-you-will-definitely-gon-9g2i
\n\n\n\nWell to be honest this is really not a hard problem. But, anyways our JOB is to solve it. Let\'s do it\n\n\n\n\n\nFirst understand this problem with an
hi-malik
NORMAL
2022-01-22T05:00:08.739044+00:00
2022-01-22T08:41:42.098321+00:00
5,419
false
<hr>\n<hr>\n\n```\nWell to be honest this is really not a hard problem. But, anyways our JOB is to solve it. Let\'s do it\n```\n\n<hr>\n<hr>\n\n`First understand this problem with an example`. Take **n = 7** where n denotes no. of **stone**\n\n![image](https://assets.leetcode.com/users/images/aaa792e5-6f8a-4f36-b258-b2...
270
106
['C', 'Java']
14
stone-game-iv
[Java/C++/Python] DP
javacpython-dp-by-lee215-fbfj
Explanation\ndp[i] means the result for n = i.\n\nif there is any k that dp[i - k * k] == false,\nit means we can make the other lose the game,\nso we can win t
lee215
NORMAL
2020-07-11T16:05:26.901392+00:00
2020-07-11T16:13:46.031484+00:00
13,760
false
# **Explanation**\n`dp[i]` means the result for `n = i`.\n\nif there is any `k` that `dp[i - k * k] == false`,\nit means we can make the other lose the game,\nso we can win the game an `dp[i] = true`.\n<br>\n\n# **Complexity**\nTime `O(n^1.5)`\nSpace `O(N)`\n<br>\n\n**Java**\n```java\n public boolean winnerSquareGam...
182
6
[]
24
stone-game-iv
Java | Heavily Commented | Subproblems Visualised
java-heavily-commented-subproblems-visua-rm4l
Let\'s enumerate all possible moves for n = 7: \n\n\n\nAs you can see, for Alice, there is no subtree that can make him win.\n\nNow let\'s consider for n = 8. C
khaufnak
NORMAL
2020-07-11T16:00:46.185843+00:00
2020-07-12T05:23:14.805436+00:00
3,403
false
Let\'s enumerate all possible moves for ```n = 7```: \n\n![image](https://assets.leetcode.com/users/images/6a068930-6f08-4529-8e40-4c748d685c36_1594483737.314722.png)\n\nAs you can see, for Alice, there is no subtree that can make him win.\n\nNow let\'s consider for ```n = 8```. Can Alice choose a subtree that can make...
100
2
[]
10
stone-game-iv
[Python] DP solution, using game theory, explained
python-dp-solution-using-game-theory-exp-x1f3
In this problems we have game with two persons, and we need to understand who is winning, if they play with optimal strategies. In this game at each moment of t
dbabichev
NORMAL
2020-10-25T07:32:12.805115+00:00
2020-10-25T07:32:12.805148+00:00
2,446
false
In this problems we have game with two persons, and we need to understand who is winning, if they play with optimal strategies. In this game at each moment of time we have several (say `k`) stones, and we say that it is **position** in our game. At each step, each player can go from one position to another. Let us use ...
49
3
['Dynamic Programming']
4
stone-game-iv
📌[ Java ] A very detailed explanation | From Brute Force to Optimised
java-a-very-detailed-explanation-from-br-vi5y
*\nPlease upvote if the explanation helps, as it keeps up the motivation to provide such posts.\n\n\u2714\uFE0F Given statements:\nAlice starts the game\nEach t
Kaustubh_22
NORMAL
2022-01-22T03:14:35.932050+00:00
2022-01-22T19:10:09.332022+00:00
1,439
false
****\nPlease upvote if the explanation helps, as it keeps up the motivation to provide such posts.\n****\n\u2714\uFE0F **Given statements**:\nAlice starts the game\nEach time, a player takes `any non-zero square number` of stones in the pile.\n\n**\u2714\uFE0F Obervations:**\nWe don\'t know how much stones does Alice/B...
36
14
['Dynamic Programming', 'Recursion', 'Java']
3
stone-game-iv
C++ DP solution | Explained
c-dp-solution-explained-by-asthacs-11td
Both the players play optimally. dp[i] = true represents that for i th number, Alice can win. False means Alice loses.\nLets assume Alice loses for n=j.\nThus,
asthacs
NORMAL
2020-07-11T16:08:32.084833+00:00
2020-07-11T17:37:23.657748+00:00
2,849
false
Both the players play optimally. dp[i] = true represents that for i th number, Alice can win. False means Alice loses.\nLets assume Alice loses for n=j.\nThus, if at any point i Alice can remove a square number such that the remaining number is equal to j, and j is false, then Alice can win at the point i.\n\n*Time com...
31
4
['Dynamic Programming', 'C']
4
stone-game-iv
💎 C++ || ❌ DFS 🆚 ✔ DP || Easy Understanding
c-dfs-vs-dp-easy-understanding-by-intell-qru4
\uD83D\uDC68\u200D\uD83D\uDCBB Friend\'s if you find this solution helpful \uD83D\uDE0A, PLEASE do UPVOTE. By doing that motivates me to create a better post li
intellisense
NORMAL
2022-01-22T03:22:13.721556+00:00
2022-01-22T13:53:21.181364+00:00
1,532
false
\uD83D\uDC68\u200D\uD83D\uDCBB Friend\'s if you find this solution helpful \uD83D\uDE0A, PLEASE do UPVOTE. By doing that motivates me to create a better post like this \u270D\uFE0F\n____________________________________________________________________________________________________________\n____________________________...
30
23
['Dynamic Programming', 'C']
11
stone-game-iv
✔️ [Python3] JUST 4-LINES, (ノ゚0゚)ノ~ OMG!!!, Explained
python3-just-4-lines-no0no-omg-explained-99jg
UPVOTE if you like (\uD83C\uDF38\u25E0\u203F\u25E0), If you have any question, feel free to ask.\n\nIn the beginning, it may seem that we would need to do a hug
artod
NORMAL
2022-01-22T02:31:06.992887+00:00
2022-01-22T18:11:01.571328+00:00
1,649
false
**UPVOTE if you like (\uD83C\uDF38\u25E0\u203F\u25E0), If you have any question, feel free to ask.**\n\nIn the beginning, it may seem that we would need to do a huge amount of scans, recursive calls and take lots of space doing backtracking since the input `n` is in the range `1..10e5`. But the thing is that a player c...
28
17
['Dynamic Programming', 'Python3']
5
stone-game-iv
Beginners Solution with image
beginners-solution-with-image-by-java_pr-9k62
\nclass Solution {\n public boolean winnerSquareGame(int n) {\n HashSet<Integer> losingState = new HashSet<>();\n HashSet<Integer> winingState
Java_Programmer_Ketan
NORMAL
2022-01-22T04:48:46.388078+00:00
2022-01-22T06:18:04.667122+00:00
709
false
```\nclass Solution {\n public boolean winnerSquareGame(int n) {\n HashSet<Integer> losingState = new HashSet<>();\n HashSet<Integer> winingState = new HashSet<>();\n losingState.add(0);\n for(int cur=1;cur<=n;cur++){\n for(int i=1;i*i<=cur;i++){\n if(losingState...
24
10
[]
1
stone-game-iv
C++ || Easy to Understand || Explanation
c-easy-to-understand-explanation-by-__ph-xmcv
See, in this types of game problem, you have to be one of the player & play optimally for this player. Lets say, I become Alice & want to defeat bob. Playing op
__phoenixer__
NORMAL
2022-01-22T07:17:06.745005+00:00
2022-01-22T07:19:11.701107+00:00
1,440
false
See, in this types of game problem, you have to be one of the player & play optimally for this player. Lets say, I become Alice & want to defeat bob. Playing optimally means:\n**at any turn I will look after every possiblity & see that at which possibility I can win. If no such possibility found, then it means that now...
21
4
['Dynamic Programming', 'Recursion', 'Memoization', 'C']
3
stone-game-iv
C++ dp code in just 4 lines with explanation
c-dp-code-in-just-4-lines-with-explanati-k36j
Logic for Code\n\nAlice have to pick the square numbers only e.g. 1,4,9,16..............\nLet\'s say that Alice pick number k*k then remaining number of stones
rahulrajdav699
NORMAL
2022-01-22T12:16:51.477788+00:00
2022-01-23T07:00:43.357528+00:00
896
false
**Logic for Code**\n\nAlice have to pick the square numbers only e.g. 1,4,9,16..............\nLet\'s say that Alice pick number **k*k** then remaining number of stones in piles will be **n-k*k**\nNow its Bob\'s turn and he has **n-k*k** stones in pile.\nIf there is a force win for **n-k*k** stones in piles then Bob wi...
15
3
['Dynamic Programming']
1
stone-game-iv
Java Simple Top Down DP
java-simple-top-down-dp-by-hobiter-ps35
DFS, caching result with a hashmap, aka, top-down DP;\n\nclass Solution {\n Map<Integer, Boolean> map = new HashMap<>();\n public boolean winnerSquareGame
hobiter
NORMAL
2020-07-11T23:14:22.111406+00:00
2020-07-11T23:14:22.111452+00:00
634
false
DFS, caching result with a hashmap, aka, top-down DP;\n```\nclass Solution {\n Map<Integer, Boolean> map = new HashMap<>();\n public boolean winnerSquareGame(int n) {\n if (n == 0) return false;\n if (map.containsKey(n)) return map.get(n);\n boolean res = false;\n for (int i = 1; i * i...
12
2
[]
0
stone-game-iv
🎨 The ART of Dynamic Programming
the-art-of-dynamic-programming-by-clayto-q5dn
Synopsis:\n\n\uD83C\uDFA8 The ART of Dynamic Programming:\n\n1. All possible jth squared amount of stones which can be taken are considered for each ith amount
claytonjwong
NORMAL
2020-07-15T16:33:12.668093+00:00
2023-05-25T01:52:54.938330+00:00
608
false
**Synopsis:**\n\n[\uD83C\uDFA8 The ART of Dynamic Programming:](https://leetcode.com/discuss/general-discussion/712010/the-art-of-dynamic-programming-an-intuitive-approach-from-apprentice-to-master/)\n\n1. **A**ll possible `j`<sup>th</sup> squared amount of stones which can be taken are considered for each `i`<sup>th</...
11
0
[]
1
stone-game-iv
Python Recursion + Memo = DP
python-recursion-memo-dp-by-wsy19970125-65bw
Recursion + Memo:\n\tclass Solution:\n\t\t@lru_cache(None)\n\t\tdef winnerSquareGame(self, n: int) -> bool:\n\t\t\tif n == 0:\n\t\t\t\treturn False\n\t\t\ti = i
wsy19970125
NORMAL
2020-07-11T16:20:01.866237+00:00
2020-07-11T23:52:36.554624+00:00
1,028
false
### Recursion + Memo:\n\tclass Solution:\n\t\t@lru_cache(None)\n\t\tdef winnerSquareGame(self, n: int) -> bool:\n\t\t\tif n == 0:\n\t\t\t\treturn False\n\t\t\ti = int(math.sqrt(n))\n\t\t\twhile i >= 1:\n\t\t\t\tif not self.winnerSquareGame(n - i * i):\n\t\t\t\t\treturn True\n\t\t\t\ti -= 1\n\t\t\treturn False\n### DP:\...
11
0
[]
3
stone-game-iv
JAVA | Stone Game IV | 100% Fast | Explained
java-stone-game-iv-100-fast-explained-by-0yfq
\n\n\nclass Solution {\n public boolean winnerSquareGame(int n) {\n //if dp[i]==false, it means Alice will loose anyway, dp[i]==true, Alice will win non
sanket_64
NORMAL
2020-10-25T08:16:15.366263+00:00
2020-10-25T08:16:15.366297+00:00
264
false
\n\n```\nclass Solution {\n public boolean winnerSquareGame(int n) {\n //if dp[i]==false, it means Alice will loose anyway, dp[i]==true, Alice will win nonetheless\n boolean dp[] = new boolean[n+1];\n \n //for all numbers, \n for(int i=0;i<=n;i++){\n \n // if already true, noth...
9
4
[]
3
stone-game-iv
[Python3] DP
python3-dp-by-patrickoweijane-ep0y
Below is the code, please let me know if you have any questions!\n\nclass Solution:\n def winnerSquareGame(self, n: int) -> bool:\n dp = [False] * (n
PatrickOweijane
NORMAL
2022-01-22T00:56:31.257366+00:00
2022-01-22T17:04:39.432984+00:00
778
false
Below is the code, please let me know if you have any questions!\n```\nclass Solution:\n def winnerSquareGame(self, n: int) -> bool:\n dp = [False] * (n + 1)\n squares = []\n curSquare = 1\n for i in range(1, n + 1):\n if i == curSquare * curSquare:\n squares.app...
8
0
['Dynamic Programming', 'Python', 'Python3']
3
stone-game-iv
Standard DP question Explained BEST - ALL you need to know
standard-dp-question-explained-best-all-meiu1
We are asked to find the solution for an input of n. So,we find solution for all numbers from [0,n] and we do this because the previous answers help us in findi
akhil_ak
NORMAL
2020-07-11T18:21:59.710620+00:00
2020-07-12T09:16:58.011578+00:00
758
false
We are asked to find the solution for an input of n. So,we find solution for all numbers from [0,n] and we do this because the previous answers help us in finding the solution for a potentially larger number. We have n states of dp,each state represents the solution of that specific state. states = [0,n]. let dp[i] be ...
8
0
['Python', 'Python3']
1
stone-game-iv
✅ [Python] ULTIMATE ONE-LINER
python-ultimate-one-liner-by-stanislav-i-yhow
\u2705 IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE.\n*\nThis solution employs recursion to explore all possible games. Time complexity is O(N\sqrtN). Space complex
stanislav-iablokov
NORMAL
2022-11-11T17:06:04.544367+00:00
2022-11-14T09:37:59.333183+00:00
307
false
**\u2705 IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE.**\n****\nThis solution employs recursion to explore all possible games. Time complexity is **O(N\\*sqrtN)**. Space complexity is **O(N)**.\n\n**Comment.** (don\'t have time for that now, but If you want explanation, please leave a comment)\n\n**Python.**\n```\nclass So...
6
0
[]
0
stone-game-iv
Easy || recursion || memoization || easy explaination
easy-recursion-memoization-easy-explaina-1px8
We want a sequence of choices for alice such that bob always loose no mater what choices he make.\nSo for all the choices bob make we\'ll check if in all cases
Sandy_14
NORMAL
2022-09-22T09:00:18.672551+00:00
2022-09-22T09:00:18.672597+00:00
411
false
We want a sequence of choices for alice such that bob always loose no mater what choices he make.\nSo for all the choices bob make we\'ll check if in all cases alice wins (that\'s why the AND operation in bob\'s choices).\nSimilarly, for alice choice we\'\'ll check any choice that lead to his win.\nSee code for more ex...
6
0
['Dynamic Programming', 'Recursion', 'C']
0
stone-game-iv
💚[C++] Fast & Easy Solution Explained | O(N(sqrt (N)) | Philosophy
c-fast-easy-solution-explained-onsqrt-n-tic2k
Welcome to abivilion\'s solution. Kindly upvote for more documentation\n\n## Philosophy\n\n\n1. I don\'t know what can Alice take or Bob take for himself/herse
abivilion
NORMAL
2022-01-22T01:52:54.039412+00:00
2022-01-22T02:42:53.513840+00:00
432
false
**Welcome to abivilion\'s solution. Kindly upvote for more documentation**\n\n## ***Philosophy***\n\n```\n1. I don\'t know what can Alice take or Bob take for himself/herself , But I do know they are playing optimally.\n\n2. And playing optimally means first perferences should be PERFECT SQUARE. Other choices are tot...
6
3
['Dynamic Programming', 'Memoization', 'C']
2
stone-game-iv
Python simple and very short DP solution
python-simple-and-very-short-dp-solution-4n8w
\nclass Solution:\n def winnerSquareGame(self, n: int) -> bool:\n dp = [0]*(n+1)\n for i in range(1, n+1):\n j = 1\n whil
yehudisk
NORMAL
2020-10-25T08:07:16.893465+00:00
2020-10-25T08:07:16.893507+00:00
417
false
```\nclass Solution:\n def winnerSquareGame(self, n: int) -> bool:\n dp = [0]*(n+1)\n for i in range(1, n+1):\n j = 1\n while j*j <= i and not dp[i]:\n dp[i] = dp[i-j*j]^1\n j+=1\n return dp[n]\n```\n**Like it? please upvote...**
6
0
['Python3']
1
stone-game-iv
💥Runtime beats 92.86%, Memory beats 100.00% [EXPLAINED]
runtime-beats-9286-memory-beats-10000-ex-o6fz
Intuition\nThe intuition behind this problem is that each player can only remove a certain number of stones defined by perfect squares, and the goal is to deter
r9n
NORMAL
2024-09-26T18:30:59.875510+00:00
2024-09-26T18:30:59.875549+00:00
28
false
# Intuition\nThe intuition behind this problem is that each player can only remove a certain number of stones defined by perfect squares, and the goal is to determine if the first player (Alice) can always win if both play optimally.\n\n# Approach\nThe approach involves using dynamic programming to build a solution whe...
5
0
['C#']
0
stone-game-iv
[Python3] Game Theory + DP - Simple Solution
python3-game-theory-dp-simple-solution-b-lrks
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
dolong2110
NORMAL
2024-09-10T09:28:32.749730+00:00
2024-09-10T09:28:32.749760+00:00
105
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(N)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(N)$$\n<!-- Add your space complexity here...
5
0
['Math', 'Dynamic Programming', 'Game Theory', 'Python3']
0
stone-game-iv
[Template Solution] DP = DFS + memoization
template-solution-dp-dfs-memoization-by-svvg6
\n/*\nDP = DFS + memoization\nAssume NN is the length of arr.\nTime complexity: O}(N*sqrt(N)) since we spend O(sqrt(N)) at most for each dfs call, and there a
codedayday
NORMAL
2020-10-25T19:17:46.435643+00:00
2022-01-23T00:27:07.875379+00:00
362
false
```\n/*\nDP = DFS + memoization\nAssume NN is the length of arr.\nTime complexity: O}(N*sqrt(N)) since we spend O(sqrt(N)) at most for each dfs call, and there are O(N) dfs calls in total.\nSpace complexity: O(N) since we need spaces of O(N) to store the result of dfs.\n*/\nclass Solution {// DP = DFS + memoization\...
4
0
['C']
1
stone-game-iv
Easy python DP solution with explanation
easy-python-dp-solution-with-explanation-gjov
Approach\n\nAt every state user has the option to choose one of the square numbers less than or equal to the current number. The player should choose a number t
surakshith
NORMAL
2020-10-25T07:57:08.454418+00:00
2020-10-25T07:58:50.647104+00:00
215
false
**Approach**\n\nAt every state user has the option to choose one of the square numbers less than or equal to the current number. The player should choose a number that guarantees the next player to lose. This is a choice based problem and the player has square numbers as choices\n\nExample:\nif n = 1 -> Player1 will c...
4
0
['Dynamic Programming', 'Python', 'Python3']
0
stone-game-iv
C++ | DP
c-dp-by-di_b-b75i
```\nbool winnerSquareGame(int n) {\n if(sqrt(n) - floor(sqrt(n)) == 0)\n return true;\n vector dp(n+1,false);\n for(int i = 1;
di_b
NORMAL
2020-07-11T16:08:52.119583+00:00
2020-07-11T16:08:52.119660+00:00
400
false
```\nbool winnerSquareGame(int n) {\n if(sqrt(n) - floor(sqrt(n)) == 0)\n return true;\n vector<bool> dp(n+1,false);\n for(int i = 1; i <= n; ++i)\n {\n for(int j = 1; j*j <= i; ++j)\n {\n int x = j*j;\n if(!dp[i-x])\n ...
4
0
[]
0
stone-game-iv
DP || Almost 95% Beats || Easy to Understand ||
dp-almost-95-beats-easy-to-understand-by-uag6
IntuitionApproachComplexity Time complexity: Space complexity: Code
kdhakal
NORMAL
2025-03-27T02:10:36.370891+00:00
2025-03-27T02:10:36.370891+00:00
29
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
3
0
['Java']
0
stone-game-iv
Simple C++ best intutition | Dynamic programming
simple-c-best-intutition-dynamic-program-1xbr
Here we simply define our dp state as whether the person performing currently can win or not . Base case is that a person loses is he has 0 left . Then for ever
rachit_17
NORMAL
2023-05-27T06:43:02.811255+00:00
2023-05-27T06:43:45.785876+00:00
450
false
Here we simply define our dp state as whether the person performing currently can win or not . Base case is that a person loses is he has 0 left . Then for every i , I subtract squares less than i and see whether I can make the other person reach a state where he loses . So if dp[i-(j*j)]==false , the current person ca...
3
0
['Dynamic Programming']
0
stone-game-iv
[C++] Easy solution, dynamic programming-bottom up
c-easy-solution-dynamic-programming-bott-62zz
\nclass Solution {\npublic:\n bool winnerSquareGame(int n) {\n if(sqrt(n) == floor(sqrt(n))) return true;\n \n vector<bool> firstPlayerWin(n
harshuljain
NORMAL
2022-01-22T10:53:43.342415+00:00
2022-01-22T10:53:43.342504+00:00
368
false
```\nclass Solution {\npublic:\n bool winnerSquareGame(int n) {\n if(sqrt(n) == floor(sqrt(n))) return true;\n \n vector<bool> firstPlayerWin(n+1);\n firstPlayerWin[1] = true;\n \n for(int i = 2; i <= n; i++) {\n if(sqrt(i) == floor(sqrt(i))) {\n firstP...
3
0
['Dynamic Programming', 'C', 'C++']
1
stone-game-iv
C++ || Explained with examples || O(kN) time,
c-explained-with-examples-okn-time-by-ke-fcok
Intution\nNotice the pattern, I am taking A as Alice here and B as Bob\nn = 1 , A wins\nn = 2 , B wins\nn = 3 , A wins\nn = 4 , A wins\nn = 5 , B wins\nn = 6,
kevinujunior
NORMAL
2022-01-22T07:44:28.738707+00:00
2022-01-23T09:08:34.771950+00:00
134
false
**Intution**\nNotice the pattern, I am taking A as Alice here and B as Bob\nn = 1 , A wins\nn = 2 , B wins\nn = 3 , A wins\nn = 4 , A wins\nn = 5 , B wins\nn = 6, A wins\nn = 7, B wins\n\nnow let us understand what\'s happening let us start with example n=5\nso as Alice is starting first he has two choices either he c...
3
0
['Dynamic Programming']
1
stone-game-iv
Simple DP solution with Explanation (C++)
simple-dp-solution-with-explanation-c-by-4i28
Alice makes a move removing square number of stones and remaining stones are passed to Bob and Bob does the same. So, when there are odd number of moves made,
umsisamess
NORMAL
2022-01-22T06:57:06.234651+00:00
2022-01-22T06:57:06.234683+00:00
95
false
Alice makes a move removing square number of stones and remaining stones are passed to Bob and Bob does the same. So, when there are **odd number of moves** made, **Alice wins** and when there are **even number of moves** made, **Bob wins**, as Alice is the one starting the game. \n\nTherefore, here we use **dynamic p...
3
0
['Dynamic Programming']
0
stone-game-iv
Diagram | Easy DP solution
diagram-easy-dp-solution-by-abhay_kumar_-fu52
The basic idea is very simple: if the count of stone is a square number, Alice can easily win by removing all the stones at once, leaving nothing for Bob to rem
abhay_kumar_singh
NORMAL
2022-01-22T00:41:07.010371+00:00
2022-01-22T01:59:06.878360+00:00
151
false
The basic idea is very simple: if the count of stone is a square number, Alice can easily win by removing all the stones at once, leaving nothing for Bob to remove. Otherwise, by jumping square numbers backwards, we look for a previous solution (at least one) where Alice would actually lose and mark the current solutio...
3
2
['Dynamic Programming', 'C', 'C++']
1
stone-game-iv
not a best solution like others but better understanding using my solution
not-a-best-solution-like-others-but-bett-cg4c
\n\n\nt=0 alice \nt=1 bob \nboth try to win and plays optimally but one thing bob will win when he gets one 0 means false and alice will win when he get one 1
asppanda
NORMAL
2021-11-10T07:53:23.499714+00:00
2021-11-10T07:53:23.499750+00:00
102
false
\n\n\nt=0 alice \nt=1 bob \nboth try to win and plays optimally but one thing bob will win when he gets one 0 means false and alice will win when he get one 1 means true so i hv done for getting one zero u can use and operator and for getting 1 1 you have to do or operator\n\n\n```\nclass Solution\n{\n public stati...
3
0
['Dynamic Programming', 'Java']
0
stone-game-iv
C++ Simple Top Down DP solution
c-simple-top-down-dp-solution-by-deepans-y850
class Solution {\npublic:\n\n int dp[100002] ;\n bool fun(int n ){\n \n if(n<0){\n return false ;\n }\n \n i
deepanshugupta134
NORMAL
2021-05-04T06:58:11.556009+00:00
2021-05-04T06:58:11.556055+00:00
338
false
class Solution {\npublic:\n\n int dp[100002] ;\n bool fun(int n ){\n \n if(n<0){\n return false ;\n }\n \n if(dp[n] != -1){\n return dp[n]==1?true:false ;\n }\n\n bool ans = false ;\n for(int i=1 ; i*i<=n ; i++){\n ans = ...
3
1
['Dynamic Programming', 'Recursion', 'Memoization', 'C']
1
stone-game-iv
C++
c-by-ashuruntimeterror-9jiy
```\nclass Solution {\npublic:\n bool winnerSquareGame(int n) {\n vector dp(n + 1);\n \n for(int i = 1; i <= n; ++i) {\n for(
ashuruntimeterror
NORMAL
2020-10-27T05:44:15.678986+00:00
2020-10-27T05:44:15.679039+00:00
130
false
```\nclass Solution {\npublic:\n bool winnerSquareGame(int n) {\n vector<int> dp(n + 1);\n \n for(int i = 1; i <= n; ++i) {\n for(int j = 1; j * j <= i; ++j) dp[i] |= !dp[i - j * j];\n }\n \n return dp[n];\n}\n};
3
0
[]
0
stone-game-iv
[C++] DP Bottom-Up Solution Explained, ~95% Time, 75% Space
c-dp-bottom-up-solution-explained-95-tim-b3vy
The intuition: the result for number n can be computed as solving subtracting from it all the squares which are < n and checking for the most convenient path, b
ajna
NORMAL
2020-10-26T09:56:10.832022+00:00
2020-10-26T09:56:10.832055+00:00
377
false
The intuition: the result for number `n` can be computed as solving subtracting from it all the squares which are `< n` and checking for the most convenient path, boiling it down to a few base cases.\n\nIn fewer words, it is a bit like solving the classical problem of finding the nth Fibonacci number, in terms of gener...
3
0
['Dynamic Programming', 'Memoization', 'C', 'C++']
0
stone-game-iv
C++ 4 lines using bitset, no nested for loops
c-4-lines-using-bitset-no-nested-for-loo-t4ir
The idea is the same as using a bool array, but you can update the entire data by simply << and |, instead of going through a for loop.\n\ncpp\nbool winnerSquar
alvin-777
NORMAL
2020-10-26T04:43:46.599680+00:00
2020-10-26T04:59:21.152032+00:00
222
false
The idea is the same as using a bool array, but you can update the entire data by simply `<<` and `|`, instead of going through a for loop.\n\n```cpp\nbool winnerSquareGame(int n) {\n\tbitset<100001> dp(0), mask(0);\n\tfor (int i = 1; i*i <= n; ++i) mask.set(i*i); // Marks all square numbers\n\tfor (int i = 0; i < n &&...
3
0
['Dynamic Programming', 'C']
1
stone-game-iv
Stone Game IV | java | c++| kotlin| dp
stone-game-iv-java-c-kotlin-dp-by-cygnus-xyo5
Let\'s say we have a boolean array dp with size n+1. each number dp[i] in the aray means if a player(either A or B) can win when there are i stones. \n\nSo for
cygnus
NORMAL
2020-10-25T09:09:10.072216+00:00
2020-10-25T09:20:02.123864+00:00
234
false
Let\'s say we have a boolean array dp with size n+1. each number dp[i] in the aray means if a player(either A or B) can win when there are i stones. \n\nSo for dp[i], we need to check the value of dp[i - j * j ] for every possible j. If this number is false, this means the other play B will lose with i -j * j stones...
3
1
['Dynamic Programming', 'C', 'Java', 'Kotlin']
1
stone-game-iv
C++ super-simple and short DP solution
c-super-simple-and-short-dp-solution-by-d84yf
\nclass Solution {\npublic:\n bool winnerSquareGame(int n) {\n vector<bool> dp(n+1, false);\n for (int i=1; i<=n; i++) {\n \n
yehudisk
NORMAL
2020-10-25T08:10:47.644149+00:00
2020-10-25T08:10:47.644191+00:00
230
false
```\nclass Solution {\npublic:\n bool winnerSquareGame(int n) {\n vector<bool> dp(n+1, false);\n for (int i=1; i<=n; i++) {\n \n int j = 1;\n while ((j*j <= i) && (!dp[i])) \n dp[i] = !dp[i-j*j++];\n }\n \n return dp[n];\n }\n};\n`...
3
0
['C']
0
stone-game-iv
Easy C++ solution
easy-c-solution-by-nc-26cg
Should be rated medium imo\n\n\nclass Solution {\npublic:\n bool winnerSquareGame(int n) {\n vector<bool> dp(n+1, false);\n \n for (int
nc_
NORMAL
2020-07-21T06:56:47.237117+00:00
2020-07-21T06:56:47.237164+00:00
207
false
Should be rated medium imo\n\n```\nclass Solution {\npublic:\n bool winnerSquareGame(int n) {\n vector<bool> dp(n+1, false);\n \n for (int i = 1; i <= n; ++i) {\n for (int j = 1; j * j <= i; ++j) {\n if (!dp[i - j * j]) {\n dp[i] = true;\n ...
3
0
[]
0
stone-game-iv
Simple DP solution. top-down.
simple-dp-solution-top-down-by-chejianch-mdop
\nclass Solution {\npublic:\n int dp[100010][2];\n bool winnerSquareGame(int n) {\n memset(dp, -1, sizeof(dp));\n return dfs(n, true);\n
chejianchao
NORMAL
2020-07-11T21:44:51.618239+00:00
2020-07-11T21:48:06.930364+00:00
134
false
```\nclass Solution {\npublic:\n int dp[100010][2];\n bool winnerSquareGame(int n) {\n memset(dp, -1, sizeof(dp));\n return dfs(n, true);\n }\n \n bool dfs(int n, bool isAlice) {\n if(n == 0) return isAlice ? false : true;\n if(dp[n][isAlice] >= 0) return dp[n][isAlice];\n ...
3
0
[]
1
stone-game-iv
[Python3] bottom-up dp
python3-bottom-up-dp-by-ye15-4smd
Algo\nDefine ans as indicator of Alice winning the game given k stones. Then, \nans[0] = False (boundary condition)\nans[k] = True if ans[k-i*i] == False for i
ye15
NORMAL
2020-07-11T16:26:19.596064+00:00
2022-01-22T01:43:02.357738+00:00
274
false
Algo\nDefine `ans` as indicator of Alice winning the game given `k` stones. Then, \n`ans[0] = False` (boundary condition)\n`ans[k] = True` if `ans[k-i*i] == False` for `i in range(1, int(sqrt(k))+1)`. \n\n`O(N^(3/2))` time & `O(N)` space \n```\nclass Solution:\n def winnerSquareGame(self, n: int) -> bool:\n d...
3
0
['Python3']
0
stone-game-iv
[Java] Recursive concise solution
java-recursive-concise-solution-by-hydro-24n0
java\nclass Solution {\n Boolean[] memo;\n public boolean winnerSquareGame(int n) {\n memo = new Boolean[n + 1];\n\t\tmemo[0] = false; // 0 mean pl
hydro_0
NORMAL
2020-07-11T16:04:48.233043+00:00
2020-07-12T14:52:45.675418+00:00
344
false
```java\nclass Solution {\n Boolean[] memo;\n public boolean winnerSquareGame(int n) {\n memo = new Boolean[n + 1];\n\t\tmemo[0] = false; // 0 mean player lost\n return f(n); \n }\n \n boolean f(int n) {\n if (memo[n] != null) return memo[n];\n for (int i = (int) Math.sqrt(n);...
3
0
['Java']
1
stone-game-iv
[Javascript] DP Problem
javascript-dp-problem-by-alanchanghsnu-qbpq
\n// DP problem\nvar winnerSquareGame = function(n) {\n\n// //////////////////////////////////\n// 1. Define an array that Alice will win or los
alanchanghsnu
NORMAL
2020-07-11T16:01:12.870576+00:00
2020-07-11T16:01:12.870623+00:00
247
false
```\n// DP problem\nvar winnerSquareGame = function(n) {\n\n// //////////////////////////////////\n// 1. Define an array that Alice will win or loss (T/F)\n// //////////////////////////////////\n\n const res = new Array(n+1).fill(false);\n let k = 1;\n \n for (let i = 1; i <= n; ...
3
0
['JavaScript']
0
stone-game-iv
[Python3] Basic Nims game theory
python3-basic-nims-game-theory-by-ironic-07ku
\n#!/usr/bin/python3.8\n\n# https://en.wikipedia.org/wiki/Nim\n\nimport math\nimport functools\nclass Solution:\n @functools.lru_cache(maxsize=None)\n def
ironic_arrow
NORMAL
2020-07-11T16:01:04.593547+00:00
2020-07-11T16:01:04.593588+00:00
241
false
```\n#!/usr/bin/python3.8\n\n# https://en.wikipedia.org/wiki/Nim\n\nimport math\nimport functools\nclass Solution:\n @functools.lru_cache(maxsize=None)\n def winnerSquareGame(self, n: int) -> bool:\n if n<=0:\n return False\n i = int(math.sqrt(n))+1\n while i:\n if i*i<=...
3
1
[]
1
stone-game-iv
C++ | | DP Memoization Approach | | Explained Steps
c-dp-memoization-approach-explained-step-2kqb
Intuition\n- The problem requires determining if Alice will win a game given an initial number of stones n where players take turns removing a perfect square nu
Satyamjha_369
NORMAL
2024-08-20T06:13:27.969680+00:00
2024-08-20T06:13:27.969714+00:00
234
false
# Intuition\n- The problem requires determining if Alice will win a game given an initial number of stones n where players take turns removing a perfect square number of stones. \n- The game continues until a player cannot make a valid move, causing them to lose. \n- To solve this problem, we need to evaluate all possi...
2
0
['Dynamic Programming', 'Game Theory', 'C++']
1
stone-game-iv
Pictorial Representation || Simple Game Theory || easy to understand
pictorial-representation-simple-game-the-34cj
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem is about determining if the first player (Alice) will win the game if both
tinku_tries
NORMAL
2024-05-31T08:27:11.218262+00:00
2024-05-31T08:27:11.218294+00:00
293
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is about determining if the first player (Alice) will win the game if both players play optimally. The game involves taking turns removing a square number of stones, and the player who cannot make a move loses. This can be app...
2
0
['Math', 'Dynamic Programming', 'Game Theory', 'C++']
0
stone-game-iv
Java | Recursive | Tabulation | Dynamic Programming solution
java-recursive-tabulation-dynamic-progra-fte7
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
PrachiSaid
NORMAL
2024-01-31T11:12:34.218153+00:00
2024-01-31T11:15:20.523993+00:00
120
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\nO(N^2)\n\n- Space complexity:\nO(N)\n\n# Code\n```\nclass Solution {\n public boolean winnerSquareGame(int n) {\n // retu...
2
0
['Java']
0
stone-game-iv
Python short and clean 1-liners. Multiple solutions. Functional programming.
python-short-and-clean-1-liners-multiple-urun
Approach 1: Top-Down DP\n\n# Complexity\n- Time complexity: O(n \cdot \sqrt{n})\n\n- Space complexity: O(n)\n\n# Code\nMultiline Recursive:\npython\nclass Solut
darshan-as
NORMAL
2023-07-28T08:00:28.814512+00:00
2023-07-28T08:01:34.588675+00:00
385
false
# Approach 1: Top-Down DP\n\n# Complexity\n- Time complexity: $$O(n \\cdot \\sqrt{n})$$\n\n- Space complexity: $$O(n)$$\n\n# Code\nMultiline Recursive:\n```python\nclass Solution:\n def winnerSquareGame(self, n: int) -> bool:\n squares = lambda x: (i * i for i in range(isqrt(x), 0, -1))\n \n @ca...
2
0
['Math', 'Dynamic Programming', 'Game Theory', 'Python', 'Python3']
0
stone-game-iv
Simple Game theory
simple-game-theory-by-techtinkerer-mbgg
\n\n# Complexity\n- Time complexity:O(root(n))\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(n)\n Add your space complexity here, e.g. O(n
TechTinkerer
NORMAL
2023-07-10T11:27:43.395955+00:00
2023-07-10T11:46:32.776701+00:00
46
false
\n\n# Complexity\n- Time complexity:O(root(n))\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int dp[100001];\n Solution(){\n memset(dp,-1,sizeof(dp));\n }\n bool...
2
0
['Dynamic Programming', 'Game Theory', 'C++']
0
stone-game-iv
Simple C++ Solution
simple-c-solution-by-lotus18-mk4l
Code\n\nclass Solution \n{\npublic:\n int dp[100011];\n bool f(int n) \n {\n if(n==0) return 0;\n int i=1;\n if(dp[n]!=-1) return
lotus18
NORMAL
2023-02-03T11:00:50.559986+00:00
2023-02-03T11:00:50.560032+00:00
346
false
# Code\n```\nclass Solution \n{\npublic:\n int dp[100011];\n bool f(int n) \n {\n if(n==0) return 0;\n int i=1;\n if(dp[n]!=-1) return dp[n];\n while(i*i<=n)\n {\n if(!f(n-i*i)) return dp[n]=1;\n i++;\n }\n return dp[n]=0;\n }\n bool ...
2
0
['C++']
0
stone-game-iv
c++ | easy | short
c-easy-short-by-venomhighs7-4yly
\n# Code\n\nclass Solution {\npublic:\n bool winnerSquareGame(int n) {\n vector<bool> dp(n + 1, false);\n for (int i = 1; i <= n; ++i) {\n
venomhighs7
NORMAL
2022-10-11T04:02:23.030481+00:00
2022-10-11T04:02:23.030513+00:00
1,376
false
\n# Code\n```\nclass Solution {\npublic:\n bool winnerSquareGame(int n) {\n vector<bool> dp(n + 1, false);\n for (int i = 1; i <= n; ++i) {\n for (int k = 1; k * k <= i; ++k) {\n if (!dp[i - k * k]) {\n dp[i] = true;\n break;\n ...
2
0
['C++']
1
stone-game-iv
[C++]✅ || Easiest Solution || DP / Game Theory
c-easiest-solution-dp-game-theory-by-ros-srua
class Solution {\npublic:\n\n vector dp;\n\t\n bool fun(int n){\n \n if(n <= 0) // Base Case\n return 0;\n\t\t\t\n
rosario1975
NORMAL
2022-07-26T19:37:07.804893+00:00
2022-07-26T19:37:07.804925+00:00
289
false
class Solution {\npublic:\n\n vector<int> dp;\n\t\n bool fun(int n){\n \n if(n <= 0) // Base Case\n return 0;\n\t\t\t\n if(dp[n] != -1)\n return dp[n]; // If already solved, return\n \n \n bool ans = 0;\n for(int i = 1; i*i <= n; i++)...
2
0
['Dynamic Programming', 'C', 'Game Theory', 'C++']
0
stone-game-iv
Recursion + Memoization || C++
recursion-memoization-c-by-user0382o-upjc
\nclass Solution {\npublic:\n int dp[100001];\n bool util(int n){\n if(n == 1) return true;\n if(n == 2) return false;\n if(dp[n] !=
user0382o
NORMAL
2022-06-19T14:26:27.077006+00:00
2022-06-19T14:26:27.077046+00:00
197
false
```\nclass Solution {\npublic:\n int dp[100001];\n bool util(int n){\n if(n == 1) return true;\n if(n == 2) return false;\n if(dp[n] != -1) return dp[n];\n bool temp = false;\n for(int i = 1; i*i <= n; i++){ //checking for each and every square number\n temp = !(util(...
2
0
['Dynamic Programming', 'Recursion', 'Memoization']
0
stone-game-iv
Java || easy explanation|| understandable
java-easy-explanation-understandable-by-4rbum
\nclass Solution {\n Boolean[] dp = new Boolean[100001];// taking constraints under consideration\n public boolean winnerSquareGame(int n) {\n \n
abhay270901
NORMAL
2022-01-23T07:15:21.805449+00:00
2022-01-23T07:15:21.805478+00:00
81
false
```\nclass Solution {\n Boolean[] dp = new Boolean[100001];// taking constraints under consideration\n public boolean winnerSquareGame(int n) {\n \n if(dp[n]!=null)// not empty\n {\n return dp[n];//return \n }\n Boolean win=false;// default win is false..\n for(in...
2
0
[]
1
stone-game-iv
Dp, Game Theory
dp-game-theory-by-dev2104rumal-6xva
\nclass Solution {\npublic:\n bool winnerSquareGame(int n) {\n vector<int> dp(n+1);\n dp[0] = 0; // loosing state\n for(int i=1;i<=n;i++
dev2104rumal
NORMAL
2022-01-23T06:13:12.269952+00:00
2022-01-23T06:13:12.269997+00:00
113
false
```\nclass Solution {\npublic:\n bool winnerSquareGame(int n) {\n vector<int> dp(n+1);\n dp[0] = 0; // loosing state\n for(int i=1;i<=n;i++){\n for(int j=1;j*j<=i;j++){\n if(dp[i-(j*j)] == 0) {dp[i] = 1; break;} // using one step I can make him stand at loosing state\n ...
2
0
[]
0
stone-game-iv
Recursion->memoization || beginner friendly code
recursion-memoization-beginner-friendly-hytxv
\n//Recursive solution\nclass Solution {\npublic: \n bool winnerSquareGame(int n) \n {\n if(n<=0) return false;\n for(int i=1; i*i<=n; i++)
bhagyajeet
NORMAL
2022-01-22T15:15:38.448485+00:00
2022-01-22T15:15:38.448514+00:00
289
false
```\n//Recursive solution\nclass Solution {\npublic: \n bool winnerSquareGame(int n) \n {\n if(n<=0) return false;\n for(int i=1; i*i<=n; i++)\n if(!solve(n-i*i, dp)) return true;\n return false;\n } \n}; \n\n//memoization solution- same code as recursion solution, used dp v...
2
0
['Dynamic Programming', 'Recursion', 'Memoization', 'C', 'C++']
0
stone-game-iv
Simple proof | Easy to understand | C++
simple-proof-easy-to-understand-c-by-gun-on46
Proof\nKey Points\n- Alice plays first\n- Alice wins if there are odd number of moves\n- Bob wins if there are even number of moves \n\nWhen the game is played
gunzaan
NORMAL
2022-01-22T13:27:07.205956+00:00
2022-01-22T13:27:07.205984+00:00
90
false
## Proof\n**Key Points**\n- Alice plays first\n- Alice wins if there are odd number of moves\n- Bob wins if there are even number of moves \n\nWhen the game is played Alice plays the first move and thats in her control. Other moves may depend on Bobs move. So if there is any first move for which we can force even numbe...
2
0
['Dynamic Programming', 'C']
0
stone-game-iv
c++ DP explained with comments
c-dp-explained-with-comments-by-antonkru-pt6j
\nclass Solution {\npublic:\n bool winnerSquareGame(int n) {\n\t\t// Create vector n + 1 because on top of 1..n for the game we will hold the 0 for the loss
antonkrug
NORMAL
2022-01-22T12:53:31.188392+00:00
2022-01-22T22:49:05.029356+00:00
99
false
```\nclass Solution {\npublic:\n bool winnerSquareGame(int n) {\n\t\t// Create vector n + 1 because on top of 1..n for the game we will hold the 0 for the loss state\n vector<bool> dp(n + 1, false); // Populate whole vector with \'false\' (including the dp[0])\n\n // For each number (upto the N we were...
2
0
['Dynamic Programming', 'C']
0
stone-game-iv
JAVA DP Solution
java-dp-solution-by-piyushja1n-621p
\nclass Solution {\n \n public boolean winnerSquareGame(int n) {\n \n boolean dp[] = new boolean[n + 1];\n \n for(int i = 0; i
piyushja1n
NORMAL
2022-01-22T05:39:46.479990+00:00
2022-01-22T05:39:46.480029+00:00
198
false
```\nclass Solution {\n \n public boolean winnerSquareGame(int n) {\n \n boolean dp[] = new boolean[n + 1];\n \n for(int i = 0; i <= n; i++){\n \n if(dp[i]) continue;\n \n for(int j = 1; i + j * j <= n; j++)\n dp[i + j * j] = t...
2
0
['Dynamic Programming', 'Java']
0
stone-game-iv
✅ [Solution] Swift: Stone Game IV (+ test cases)
solution-swift-stone-game-iv-test-cases-wmhrg
swift\nclass Solution {\n func winnerSquareGame(_ n: Int) -> Bool {\n var dp = [Bool](repeating: false, count: n + 1)\n \n for i in 1...
AsahiOcean
NORMAL
2022-01-22T04:30:35.267730+00:00
2022-01-22T04:30:35.267771+00:00
1,174
false
```swift\nclass Solution {\n func winnerSquareGame(_ n: Int) -> Bool {\n var dp = [Bool](repeating: false, count: n + 1)\n \n for i in 1...n {\n var val = 1\n while val * val <= i {\n if !dp[i - val * val] {\n dp[i] = true\n ...
2
0
['Dynamic Programming', 'Swift']
0
stone-game-iv
Simple C solution with DP
simple-c-solution-with-dp-by-linhbk93-uelt
\nbool winnerSquareGame(int n){\n int dp[100001] = {0};\n for(int i = 0; i <= n; i++)\n {\n if(dp[i]) continue;\n for(int j = 1; i + j *
linhbk93
NORMAL
2022-01-22T01:42:06.151485+00:00
2022-01-22T01:42:06.151526+00:00
147
false
```\nbool winnerSquareGame(int n){\n int dp[100001] = {0};\n for(int i = 0; i <= n; i++)\n {\n if(dp[i]) continue;\n for(int j = 1; i + j * j <= n; j++)\n dp[i + j * j] = 1;\n }\n return dp[n];\n}\n```
2
0
['Dynamic Programming', 'C']
0
stone-game-iv
Java Simple and easy DP Memoization solution, clean code with comments
java-simple-and-easy-dp-memoization-solu-gvj7
PLEASE UPVOTE IF YOU LIKE THIS SOLUTION\n\n\n\nclass Solution {\n HashMap<Integer, Boolean> cache;\n public boolean winnerSquareGame(int n) {\n cac
satyaDcoder
NORMAL
2021-06-17T16:06:10.927555+00:00
2021-06-17T16:06:10.927604+00:00
168
false
**PLEASE UPVOTE IF YOU LIKE THIS SOLUTION**\n\n\n```\nclass Solution {\n HashMap<Integer, Boolean> cache;\n public boolean winnerSquareGame(int n) {\n cache = new HashMap();\n return canAliceWin(n);\n }\n \n private boolean canAliceWin(int n){\n if(n <= 0) return false;\n \n ...
2
0
['Dynamic Programming', 'Memoization', 'Java']
0
stone-game-iv
10% :: Python (Sprague-Grundy Function)
10-python-sprague-grundy-function-by-tuh-exhm
\n# A Modified Nim Game\n# Use Sprague Grundy Functions for the same\n\nclass Solution:\n def getMex(self, arr):\n counter = 0\n arr.sort()\n
tuhinnn_py
NORMAL
2021-05-06T12:10:50.639358+00:00
2021-05-06T12:10:50.639398+00:00
203
false
```\n# A Modified Nim Game\n# Use Sprague Grundy Functions for the same\n\nclass Solution:\n def getMex(self, arr):\n counter = 0\n arr.sort()\n for num in arr:\n if num != counter:\n return counter\n counter += 1\n return arr[-1] + 1\n # Altern...
2
0
[]
0
stone-game-iv
C# solution dp+back tracking approach
c-solution-dpback-tracking-approach-by-v-eg0p
\n var dp = new bool[n+1];\n var ps = new List<int>(){1};\n dp[1] = true;\n for (int i = 2; i <= n; i++)\n {\n if
victorsdk
NORMAL
2020-10-25T21:32:51.446878+00:00
2020-10-25T21:34:16.314227+00:00
92
false
```\n var dp = new bool[n+1];\n var ps = new List<int>(){1};\n dp[1] = true;\n for (int i = 2; i <= n; i++)\n {\n if (Math.Sqrt(i) % 1 == 0)\n {\n ps.Add(i);\n dp[i] = true;\n }\n else\n {\n ...
2
0
['Dynamic Programming', 'Backtracking']
0
stone-game-iv
Animated Explanation | Both Recursive + Iterative Solutions | C++ Implementations
animated-explanation-both-recursive-iter-u9sb
Animated explanation:\nhttps://www.youtube.com/watch?v=akGqO-cFodE\n\nIterative implementation:\n\nbool winnerSquareGame(int n) {\n vector<bool> dp(n + 1); //
procedurecall
NORMAL
2020-10-25T11:37:32.929607+00:00
2020-10-25T11:37:32.929649+00:00
132
false
Animated explanation:\nhttps://www.youtube.com/watch?v=akGqO-cFodE\n\nIterative implementation:\n```\nbool winnerSquareGame(int n) {\n vector<bool> dp(n + 1); // same as our memo before, but now a vector\n // dp[0] is already false by default\n // iterate through the remaining values bottom-up\n // note that compar...
2
1
[]
0
stone-game-iv
Stone Game IV (python DP)
stone-game-iv-python-dp-by-qianzechang-vpd6
\nclass Solution:\n def winnerSquareGame(self, n: int) -> bool:\n square=[i**2 for i in range(1,int(n**0.5)+1)]\n dp=[False]*(n+1)\n for
qianzechang
NORMAL
2020-10-25T07:31:23.170215+00:00
2020-10-25T07:31:23.170260+00:00
127
false
```\nclass Solution:\n def winnerSquareGame(self, n: int) -> bool:\n square=[i**2 for i in range(1,int(n**0.5)+1)]\n dp=[False]*(n+1)\n for i in range(1,n+1):\n for s in square:\n if s>i:\n break\n if dp[i-s]==False:\n ...
2
1
[]
0
stone-game-iv
Java DP O(N)time. faster than 100%.
java-dp-ontime-faster-than-100-by-103sty-oj52
Runtime: 2 ms, faster than 100.00%, Memory Usage: 36.6 MB, less than 100.00% of Java online submissions\n\n\n//O(N)time\n//O(N)space\npublic boolean winnerSquar
103style
NORMAL
2020-07-17T05:42:31.240641+00:00
2020-07-17T05:42:31.240677+00:00
239
false
**Runtime: 2 ms, faster than 100.00%, Memory Usage: 36.6 MB, less than 100.00% of Java online submissions**\n\n```\n//O(N)time\n//O(N)space\npublic boolean winnerSquareGame(int n) {\n boolean[] dp = new boolean[n + 1];\n for(int i = 0; i <= n; i++){\n if(dp[i]){\n continue;\n }\n f...
2
1
[]
1