We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Hexagonal Grid
Hexagonal Grid
+ 0 comments ~10 lines Python, 100%, cool bit things
If you figure out what the numbers mean I'll buy you a virtual coffee :D
@cache def f(a,b): x=((a&3)<<2)|(b&3) if a==b: return True if (a,b)==(0,1) or (a,b)==(1,0) or x in {6,11,14}: return False if x in {2,7,8,13}: return f(a>>1,b>>1) if x in {0,3,5,9,10,12,15}: return f(a>>2,b>>2) return f(a>>1,(b>>1)|1) or f((a>>1)|1,b>>1) def hexagonalGrid(a, b): return "YES" if f(int(a,2),int(b,2)) else "NO"
+ 0 comments Here is Hexagonal Grid problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-hexagonal-grid-problem-solution.html
+ 0 comments My python3 example with O(n) complexity:
def hexagonalGrid(a, b): if ((a.count('1') + b.count('1')) % 2 != 0): return 'NO' white = 0 block = False for i in range(len(a)): if (a[i] == '1'): if block and white % 2 != 0: return 'NO' block = True elif (a[i] == '0'): block = False white += 1 if (b[i] == '1'): if block and white % 2 != 0: return 'NO' block = True elif (b[i] == '0'): block = False white += 1 return 'YES'
+ 0 comments python3,
def hexagonalGrid(a, b): def explore(name, i): if name == 'a': if i < len(a) and a[i] == 0: a[i] = 1 return 1 + explore('b', i) + explore('a', i+1) if name == 'b': if i < len(b) and b[i] == 0: b[i] = 1 return 1 + explore('a', i+1) + explore('b', i+1) return 0 a = [int(i) for i in a] b = [int(i) for i in b] for i in range(len(a)): cnt = 0 if a[i] == 0: cnt = explore('a', i) if b[i] == 0: cnt = explore('b', i) if cnt % 2 != 0: return "NO" return "YES"
+ 0 comments I worked up a solution in C++ but can someone tell if this is DP or not ??
string hexagonalGrid(string& a, string& b) { int n = a.size(); int a0=0, b0=0; for(int i =0;i<n;i++){ if(a[i]=='0') a0++; if(b[i]=='0') b0++; } if((a0+b0)%2==1) return "NO"; else{ int i=0,j=0; while(i<n && j<n){ while(i<n && a[i]=='1') i++; while(j<n && b[j]=='1') j++; if(i==n || j==n) break; if(i==j){ a[i]='1'; b[j]='1'; i++;j++; if(i==n || j==n) break; } else if(i==j+1){ a[i]='1'; b[j]='1'; i++;j++; if(i==n || j==n) break; } else if(i>j+1){ while(i>j+1){ if(b[j+1]=='1') return "NO"; b[j]='1';b[j+1]='1';j++;j++; while(j<n && b[j]=='1') j++; if(j==n) break; } } else if(j>i){ while(j>i){ if(a[i+1]=='1') return "NO"; a[i]='1';a[i+1]='1';i++;i++; while(i<n && a[i]=='1') i++; if(i==n) break; } } } if(i<n){ while(i<n-1){ while(i<n && a[i]=='1') i++; if(i==n) break; if(i==n-1 || a[i+1]=='1') return "NO"; a[i]='1';a[i+1]='1';i++;i++; } } else if(j<n){ while(j<n-1){ while(j<n && b[j]=='1') j++; if(j==n) break; if(j==n-1 || b[j+1]=='1') return "NO"; b[j]='1';b[j+1]='1';j++;j++; } } } return "YES"; }
Load more conversations
Sort 37 Discussions, By:
Please Login in order to post a comment