34S-Section-Solution

34S-Section-Solution - CS106A Handout 34S th May 18 –...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS106A Handout 34S th May 18 – 20th, 2011 Spring 2011 Section Solution Discussion Problem 1 Solution: Heap/Stack Diagram Discussion Problem 2 Solution: Maximizing Game Score private int computeMaxScore(int board) { int prev = Math.max(board[0][0], board[1][0]); if (board[0].length) == 1) return prev; int curr = Math.max(prev, Math.max(board[0][1], board[1][1])); if (board[0].length == 2) return curr; for (int col = 2; col < board[0].length; col++) { int next = Math.max(curr, prev + Math.max(board[0][col], board[1][col])); prev = curr; curr = next; } return curr; } Discussion Problem 3 Solution: Image Processing private GImage flipHorizontal(GImage image) { 2 int array = image.getPixelArray(); int width = array[0].length; int height = array.length; for (int row = 0; row < height; row++) { for (int p1 = 0; p1 < width / 2; p1++) { int p2 = width - p1 - 1; int temp = array[row][p1]; array[row][p1] = array[row][p2]; array[row][p2] = temp; } } return new GImage(array); } ...
View Full Document

This note was uploaded on 05/28/2011 for the course CS 106A taught by Professor Sahami,m during the Spring '08 term at Stanford.

Ask a homework question - tutors are online