496. Next Greater Element I

496. Next Greater Element I

class Solution {
  	public int[] nextGreaterElement(int[] nums1, int[] nums2) {
		int[] out = new int[nums1.length];

		for (int i = 0; i < nums1.length; i++) {
			// search
			int startSearch = 0;
			for (int k = 0; k < nums2.length; k++) {
				if (nums1[i] == nums2[k]) {
					startSearch = k;
					break;
				}
			}
			int outElement = -1;
			for (int j = startSearch + 1; j < nums2.length; j++) {
				if (nums1[i] < nums2[j]) {
					outElement = nums2[j];
					break;
				}
			}
			out[i] = outElement;
		}
		return out;
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s