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.
- Prepare
- Algorithms
- Implementation
- Drawing Book
- Discussions
Drawing Book
Drawing Book
+ 0 comments A simple swift
func pageCount(n: Int, p: Int) -> Int { var p = p var n = n if p % 2 == 0 { p += 1 } if n % 2 == 0 { n += 1 } return n - p < p - 1 ? (n - p) / 2 : (p - 1) / 2 }
+ 0 comments function pageCount(n, p) { // Write your code here const front_page_flip = Math.ceil((p-1)/2); const back_page_flip = n%2 === 0 ? Math.ceil((n-p)/2) : Math.floor((n-p)/2); return front_page_flip < back_page_flip ? front_page_flip : back_page_flip;
}
+ 0 comments int value1=0; int value2=0; if(p==1){ return 0; } for(int i=1;i<=n/2;i++){ value1++; if(i==p/2){ break; } } for(int k=n/2;k<=n;k--){ if(k==p/2){ break; } value2++; } if(value1<=value2){ return value1; } else{ return value2; } } }
+ 0 comments function pageCount(n, p) { // Write your code here let frontTurn = 0; let backTurn = 0; let frontPage = 1; let lastPage; if(n%2 == 0){ lastPage = n; } else if(n%2 != 0){ lastPage = n-1; } if(p == 1 ){ return 0 } else if(p%2 != 0 && (lastPage == p || lastPage+1 == p)){ return 0 } else if(p%2==0 && lastPage== p){ return 0 } for(let i = 1; i<= n; i++){ frontTurn += 1; frontPage += 2; if(frontPage >= p){ break } } for(let i = 1; i<= n; i++){ backTurn += 1; lastPage -= 2; if(lastPage <= p){ break } } if(frontTurn < backTurn){ return frontTurn } else{ return backTurn } }
+ 0 comments Straight Forward Java Solution θ(1) Time Complexity
public static int pageCount(int n, int p) { // Write your code here if(n%2==0) n++; int fromFront = (int)p/2; int fromBack = (int)(n-p)/2; if(fromBack<fromFront) return fromBack; else return fromFront; }
Load more conversations
Sort 1739 Discussions, By:
Please Login in order to post a comment