Download presentation

Presentation is loading. Please wait.

Published byCecilia Reyner Modified over 3 years ago

1
Problem 1: Balls Problem 2: Josephine Problem 3: Alternating List

3
Problem: - Given N balls labeled from 1 to N and M operations, determine the last state after doing M operations. Input The first line contains two integers, N (1<= N <= 1,000) and M (1<= M <= 1,000) The next M lines contain the operations. Sample Input 10 5 A 2 1 A 10 1 A 5 6 B 6 9 R 3

4
Operations: 1. A x y: move the ball labeled with number x to the left of the ball labeled with number y. 2. B x y: move the ball labeled with number x to the right of the ball labeled with number y. 3. R x: Remove the ball labeled with number x from our list. Output: Output the final arrangement of the N balls from left to right. Each number is followed by a whitespace.

5
1. What kind of data structure can we use to solve this problem?

6
1. Hint: use List - Q: What kind of List (Singly-Linked List/ Doubly-Linked List/ Circular-Linked List)? - A: Use Doubly-Linked List Why?

7
2. Simplest implementation: -Traversing all the nodes (to find the correct node) for removing and moving Better implementation: - Without traversing through the nodes (will be discussed later)

8
ListNode stores: 1. Number/Id 2. Previous Ball 3. Next Ball ID int ID ListNode next ListNode prev

9
Remove: 1. Find position of x 2. Update the affected ListNode.

10
Rough steps: Iterate though the list to find the node with label x, and remember which node it is (point to it from a variable) Adjust the pointers

11
Remove node X from position X next prev Next of X next prev Previous of X next prev

12
Remove node X from position X next prev Next of X next prev Previous of X next prev X.next.prev = X.prev X.prev.next = X.next Must handle special case of 1 st and last nodes when X.prev == null or X.next == null

13
Move (Simplest Implementation): 1. Find position of X and Y 2. Update the affected ListNode Example: A x y

14
Rough steps: Iterate though the list to find the node with labels x and y, and store them in two variables. Remove node X Insert node X left/right of node Y (practice using back the same node removed and insert it back, rather than creating a new node)

15
Insert X on the left of Y Y next prev Previous of Y next prev

16
Insert X on the left of Y X next prev Y next prev Previous of Y next prev X.prev = Y.prev X.next = Y Y.prev.next = X Y.prev = X Must handle special case of inserting X before the 1 st node when Y.prev == null Do it in the right order – dont lose the previous of Y node

17
Insert X on the left of Y X next prev Y next prev Previous of Y next prev X.prev = Y.prev X.next = Y Y.prev.next = X Y.prev = X Must handle special case of inserting X before the 1 st node when Y.prev == null Do it in the right order – dont lose the previous of Y node

18
Insert X to the right of Y Next of Y next prev Y next prev

19
Insert X to the right of Y X next prev Next of Y next prev Y next prev X.next = Y.next X.prev = Y Y.next.prev = X Y.next = X Must handle special case of inserting X after the last node when Y.next == null Do it in the right order – dont lose the previous of Y node

20
Insert X to the right of Y X next prev Next of Y next prev Y next prev X.next = Y.next X.prev = Y Y.next.prev = X Y.next = X Must handle special case of inserting X after the last node when Y.next == null Do it in the right order – dont lose the previous of Y node

21
Y PYNY PX NX X

22
X Y PY PX NX

23
An idea to improve performance: 1. We store a reference of all balls (ListNodes) in an array, so that we dont need to traverse the Linked List to find the balls every time. 12435head 12345

25
Problem: - Given N candidates in circle, we want to keep removing the K-th candidate until we find the number of people in the circle equals to 1. Output the removed candidate for each remove operation.

26
Input The first line consists of T, the number of test cases, T <= 100. The following T lines describe T test cases, each line containing two integers, N and K. Output Output the final arrangement of the N balls from left to right. Each number is followed by a whitespace. Sample Input 2 3 1 4 2 Sample Output 1 2 3 2 4 3 1

27
1. What kind of data structure can we use to solve this problem?

28
1. Hint: - Use Circular Linked List Why? Simulates the problem!

29
Keep removing K-th person by iterating K times from current position. Update the State of ListNode. More or less similar to Previous Problem but using different type of LinkedList.

30
Removing a node: Since you dont have the previous pointer in a singly-linked circular linked list, you need to keep track of the previous node when you traverse the list! 12435head

32
Problem: Given a list of integers, and a list of operations, determine whether it is alternating after each operation Definition of Alternating: -Adjacent integers have different signs List of one or zero elements is alternating #3: A LTERNATING LIST

33
Input 1 <= N <= 100 1 <= Q <= 100 N is the size of the original linked list Q the number of operations Output For each operation print YES if the updated linked list is alternating, otherwise print NO. #3: A LTERNATING LIST Sample Input 4 1 -2 3 -4 M 1 3 A 1 1 14 R 2 2 A 2 1 -11 Sample Output YES NO YES

34
1. What kind of data structure can we use to solve this problem?

35
1. Hint: use List - Q: What kind of List (Singly-Linked List/ Doubly-Linked List/ Circular-Linked List)? - A: Use (Singly) Linked List Why?

36
M [index] [size] Move size number of elements starting from index to the end of list Index12345 -43-212 M 2 3 Index15234 -423-21

37
Pseudo-code: function Move (int index, int size) { for i=1 to size { temp remove element at index insert temp to the end of list } } Why removing at the index only works?

38
R [index] [size] Remove size number of elements starting from index. Index12345 -43-212 R 2 3 Index15 -42

39
Pseudo-code: fun Remove (int index, int size) { for i=1 to size { remove element at index } } Can you use this to implement Move?

40
A [index] [size] [value] add the elements between index [index] and [index + size - 1] (inclusive) with [value]. Index12345 -43-212 A 2 3 5 Index12345 -48362 +5

41
Pseudo-code: fun AddValue (int index, int size, int value) { for i=1 to size { temp remove value at index+i-1 insert (temp+value) to index+i-1 } } How do we get the (index+i-1) ? (or we can cheat and just modify the number in listNode…)

42
List of 1 element : YES Loop through list: once two adjacent elements with same sign found, return NO i.e. Keep track of previousSign, and then check every iteration if previousSign == currentSign Return YES if loop completes without returning

43
Any Questions?

Similar presentations

OK

Selection Structures: Switch CSC 1401: Introduction to Programming with Java Week 4 – Lecture 1 Wanda M. Kunkle.

Selection Structures: Switch CSC 1401: Introduction to Programming with Java Week 4 – Lecture 1 Wanda M. Kunkle.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on scramjet engines Ppt on windows 2000 operating system Hrm ppt on recruitment Raster scan and random scan display ppt on tv Download ppt on electric current and circuits Download ppt on bill gates biography Ppt on motivational stories Presentations ppt online templates Ppt on causes of land pollution Ppt on environment safety measures