Skip to content

Commit

Permalink
Merge pull request #5 from jyzeng17/witness-of-the-tall-people
Browse files Browse the repository at this point in the history
Add solution for Witness of The Tall People problem
  • Loading branch information
emafazillah authored Oct 31, 2020
2 parents d1ea9bf + d97fab5 commit 8c5b8e6
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.dailyinterviewprojava.google;

import java.util.Scanner;

import com.dailyinterviewprojava.util.InputUtil;

/**
*
* @author ema
Expand All @@ -21,4 +25,31 @@
*/
public class WitnessOfTheTallPeople {

public static void main(String...strings) {
// Input
Scanner scanner = new Scanner(System.in);
String[] inputs = InputUtil.inputArr(scanner.next());
int[] nums = InputUtil.integerArr(inputs);

// Output
System.out.print(countWitnesses(nums));

scanner.close();
}

public static int countWitnesses(int[] nums) {
int counter = 1;
int n = nums.length;
int maxHeight = nums[n - 1];

for (int i = n - 2; i >= 0; i--) {
if (nums[i] > maxHeight) {
counter++;

maxHeight = nums[i];
}
}

return counter;
}
}

0 comments on commit 8c5b8e6

Please sign in to comment.