java - Hackerearth Remove Friends: Runtime Error - NZEC -
i stuck on problem. code passes testcases given in example there error in code. kindly point out error.
problem statement (https://www.hackerearth.com/problem/algorithm/remove-friends-5)
after getting phd, christie has become celebrity @ university, , facebook profile full of friend requests. being nice girl is, christie has accepted requests.
now kuldeep jealous of attention getting other guys, asks delete of guys friend list. avoid 'scene', christie decides remove friends friend list, since knows popularity of each of friend has, uses following algorithm delete friend.
algorithm delete(friend):
deletefriend=false = 1 friend.length-1 if (friend[i].popularity < friend[i+1].popularity) delete th friend deletefriend=true break if(deletefriend == false) delete last friend
input: first line contains t number of test cases. first line of each test case contains n, number of friends christie has , k ,the number of friends christie decides delete. next lines contains popularity of friends separated space.
output: each test case print n-k numbers represent popularity of christie friend's after deleting k friends.
note order of friends after deleting k friends should maintained given in input.
my solution
class testclass { static class node { int data; node next; node(int d) { data = d; next = null; }} static node head = null; public static void main(string args[] ) throws exception { bufferedreader br = new bufferedreader(new inputstreamreader(system.in)); string line = br.readline(); int cases = integer.parseint(line); (int = 0; < cases; i++) { line = br.readline(); int friends = integer.parseint(line); line = br.readline(); int delete = integer.parseint(line); head = null; node p =null; for(int j=0;j < friends;j++){ line = br.readline(); int temp = integer.parseint(line); if(head == null){ head = new node(temp); p = head; } else{ node q = new node(temp); p.next = q; p = q; }} delete_friend(head , delete); print_list(head); }} static void delete_friend(node h, int delete){ node p = head; node q = null; int flag = 0; (int x = 1; x<=delete;x++){ p = head; flag = 0; q = p.next; while(p.next != null){ q = p.next; if(p.data < q.data){ p.data = q.data; p.next = q.next; flag=1; p = head; break; } if (flag == 0 && q.next == null){ if (p.data >= q.data) { p.next = null; break; }} p = p.next; }}} static void print_list(node head){ node tnode = head; while (tnode != null) { system.out.print(tnode.data+" "); tnode = tnode.next; } system.out.println(); }}
the way read input data flawed: implementation assumes 1 integer per line, doesn't match problem description:
first line of each test case contains n, number of friends christie has , k ,the number of friends christie decides delete. next lines contains popularity of friends separated space.
instead of using bufferedreader
, suggest try using scanner
instead, it's simpler, this:
scanner scanner = new scanner(system.in); int t = scanner.nextint(); (int = 0; < t; i++) { int friendsnum = scanner.nextint(); int todeletenum = scanner.nextint(); // ... (int j = 0; j < friendsnum; j++) { int current = scanner.nextint(); // ... } // ... }
after fix input parsing, of tests pass.
but many of them still fail, due different problem, time limit exceeded. that's because algorithm not efficient enough. in worst case, each friend delete, iterate until end of list of friends.
a different algorithm possible:
- for each friend
- while still need delete more friends, , current friend more popular previous, delete previous
- add current friend stack
- while still need delete more friends, remove last stack
here's meat of it:
for (int j = 0; j < friendsnum; j++) { int current = scanner.nextint(); while (deleted < todeletenum && !stack.isempty() && stack.peek() < current) { stack.pop(); deleted++; } stack.push(current); } while (deleted < todeletenum) { stack.pop(); deleted++; }
Comments
Post a Comment